It's fascinating watching someone write code for macOS while only referring to Linux and FreeBSD man pages.
On macOS, 'man ip' gives all the necessary info about raw sockets and IP_HDRINCL:
> Outgoing packets automatically have an IP header prepended to them (based on the destination address and the protocol number the socket is created with), unless the IP_HDRINCL option has been set.
> Unlike previous BSD releases, the program must set all the fields of the IP header
> Note that the ip_off and ip_len fields are in host byte order.