// File: /workspace/github_cpp_makefile_star2_O1/hugosantos[P]dbeacon/dbeacon_posix.cpp
// Function: RecvMsg(int, address&, address&, unsigned char*, int, int&, unsigned long&)
int RecvMsg(int sock, address &from, address &to, uint8_t *buffer, int buflen, int &ttl, uint64_t &ts) {
	int len;
	struct msghdr msg;
	struct iovec iov;
	uint8_t ctlbuf[64];

	from.set_family(beaconUnicastAddr.family());

	msg.msg_name = (char *)from.saddr();
	msg.msg_namelen = from.addrlen();
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;
	msg.msg_control = (char *)ctlbuf;
	msg.msg_controllen = sizeof(ctlbuf);
	msg.msg_flags = 0;

	iov.iov_base = (char *)buffer;
	iov.iov_len = buflen;

	len = recvmsg(sock, &msg, 0);
	if (len < 0)
		return len;

	ts = 0;
	ttl = 127;

	to = beaconUnicastAddr;

	if (msg.msg_controllen > 0) {
		for (cmsghdr *hdr = CMSG_FIRSTHDR(&msg); hdr; hdr = CMSG_NXTHDR(&msg, hdr)) {
			if (hdr->cmsg_level == IPPROTO_IPV6 && hdr->cmsg_type == IPV6_HOPLIMIT) {
				ttl = *(uint8_t *)CMSG_DATA(hdr);
#ifdef IPV6_PKTINFO
			} else if (hdr->cmsg_level == IPPROTO_IPV6 && hdr->cmsg_type == IPV6_PKTINFO) {
				if (hdr->cmsg_len == CMSG_LEN(sizeof(in6_pktinfo))) {
					in6_pktinfo *pktinfo = (in6_pktinfo *)CMSG_DATA(hdr);
					to.set_family(AF_INET6);
					to.v6()->sin6_addr = pktinfo->ipi6_addr;
				}
#endif
#ifdef IP_RECVTTL
			} else if (hdr->cmsg_level == IPPROTO_IP && hdr->cmsg_type == IP_RECVTTL) {
				ttl = *(uint8_t *)CMSG_DATA(hdr);
#endif
			} else if (hdr->cmsg_level == IPPROTO_IP && hdr->cmsg_type == IP_TTL) {
				ttl = *(uint8_t *)CMSG_DATA(hdr);
#ifdef SO_TIMESTAMP
			} else if (hdr->cmsg_level == SOL_SOCKET && hdr->cmsg_type == SO_TIMESTAMP) {
				timeval *tv = (timeval *)CMSG_DATA(hdr);
				ts = tv->tv_sec;
				ts *= 1000;
				ts += tv->tv_usec / 1000;
#endif
			}
		}
	}

	if (!ts) {
		ts = get_time_of_day();
	}

	return len;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r9, %rbx
movq %rdx, %r14
leaq 0x8470(%rip), %rax      # 0x124b8
movzwl (%rax), %eax
movl %eax, %edx
andl $-0x9, %edx
cmpw $0x2, %dx
jne 0xa059
movw %ax, (%rsi)
leaq 0x8(%rsp), %rax
movq %rsi, (%rax)
cmpw $0xa, (%rsi)
movl $0x1c, %edx
movl $0x10, %esi
cmovel %edx, %esi
movl %esi, 0x8(%rax)
leaq 0x40(%rsp), %rdx
movq %rdx, 0x10(%rax)
movq $0x1, 0x18(%rax)
leaq 0x50(%rsp), %rsi
movq %rsi, 0x20(%rax)
movq $0x40, 0x28(%rax)
movl $0x0, 0x30(%rax)
movq %rcx, (%rdx)
movslq %r8d, %rcx
movq %rcx, 0x8(%rdx)
movq %rax, %rsi
xorl %edx, %edx
callq 0x3120
movq %rax, %r15
testl %r15d, %r15d
js 0xa112
movq 0xd0(%rsp), %rbp
movq $0x0, (%rbp)
movl $0x7f, (%rbx)
leaq 0x83e1(%rip), %rsi      # 0x124b8
movl $0x10, %ecx
movq %r14, %rdi
rep  movsq (%rsi), %es:(%rdi)
movq 0x30(%rsp), %rax
testq %rax, %rax
je 0xa102
cmpq $0x10, %rax
setb %al
movq 0x28(%rsp), %rsi
testq %rsi, %rsi
sete %cl
orb %al, %cl
je 0xa127
cmpq $0x0, (%rbp)
jne 0xa112
callq 0xa1f6
movq %rax, (%rbp)
movl %r15d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8(%r14), %rax
movq %rax, (%rsp)
leaq 0x8(%rsp), %r12
movabsq $0x20c49ba5e353f7cf, %r13 # imm = 0x20C49BA5E353F7CF
movl 0x8(%rsi), %eax
cmpl $0x29, %eax
jne 0xa170
cmpl $0x34, 0xc(%rsi)
je 0xa1af
cmpl $0x29, %eax
jne 0xa170
cmpl $0x32, 0xc(%rsi)
jne 0xa170
cmpq $0x24, (%rsi)
jne 0xa1b5
movw $0xa, (%r14)
movups 0x10(%rsi), %xmm0
movq (%rsp), %rax
movups %xmm0, (%rax)
jmp 0xa1b5
testl %eax, %eax
je 0xa1a9
cmpl $0x1, %eax
jne 0xa1b5
cmpl $0x1d, 0xc(%rsi)
jne 0xa1b5
imulq $0x3e8, 0x10(%rsi), %rcx # imm = 0x3E8
movq %rcx, (%rbp)
movq %r13, %rax
imulq 0x18(%rsi)
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x7, %rdx
addq %rax, %rdx
addq %rcx, %rdx
movq %rdx, (%rbp)
jmp 0xa1b5
cmpl $0xc, 0xc(%rsi)
jne 0xa1ce
movzbl 0x10(%rsi), %eax
movl %eax, (%rbx)
movq %r12, %rdi
callq 0x30c0
movq %rax, %rsi
testq %rax, %rax
jne 0xa13e
jmp 0xa102
testl %eax, %eax
jne 0xa174
cmpl $0x2, 0xc(%rsi)
jne 0xa174
jmp 0xa1af