// File: /workspace/github_cpp_makefile_star2_O2/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 0x8b20(%rip), %rax      # 0x124b8
movzwl (%rax), %edx
movl %edx, %eax
andl $-0x9, %eax
cmpw $0x2, %ax
jne 0x99ab
movw %dx, (%rsi)
jmp 0x99ae
movzwl (%rsi), %edx
leaq 0x8(%rsp), %rax
movq %rsi, (%rax)
cmpw $0xa, %dx
pushq $0x1c
popq %rdx
pushq $0x10
popq %rsi
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)
andl $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 0x9af0
movq 0xd0(%rsp), %r13
andq $0x0, (%r13)
movl $0x7f, (%rbx)
leaq 0x8a92(%rip), %rsi      # 0x124b8
pushq $0x10
popq %rcx
movq %r14, %rdi
rep  movsq (%rsi), %es:(%rdi)
movq 0x30(%rsp), %rax
testq %rax, %rax
je 0x9ae0
cmpq $0x10, %rax
jb 0x9a4a
movq 0x28(%rsp), %rsi
jmp 0x9a4c
xorl %esi, %esi
leaq 0x8(%r14), %rax
movq %rax, (%rsp)
leaq 0x8(%rsp), %r12
movl $0x3e8, %ebp            # imm = 0x3E8
testq %rsi, %rsi
je 0x9ae0
movl 0x8(%rsi), %eax
testl %eax, %eax
je 0x9aa7
cmpl $0x1, %eax
je 0x9a83
cmpl $0x29, %eax
jne 0x9aba
movl 0xc(%rsi), %eax
cmpl $0x32, %eax
je 0x9ac7
cmpl $0x34, %eax
je 0x9ab4
jmp 0x9aba
cmpl $0x1d, 0xc(%rsi)
jne 0x9aba
imulq $0x3e8, 0x10(%rsi), %rcx # imm = 0x3E8
movq %rcx, (%r13)
movq 0x18(%rsi), %rax
cqto
idivq %rbp
addq %rcx, %rax
movq %rax, (%r13)
jmp 0x9aba
movl 0xc(%rsi), %eax
cmpl $0x2, %eax
je 0x9ab4
cmpl $0xc, %eax
jne 0x9aba
movzbl 0x10(%rsi), %eax
movl %eax, (%rbx)
movq %r12, %rdi
callq 0x30c0
movq %rax, %rsi
jmp 0x9a5e
cmpq $0x24, (%rsi)
jne 0x9aba
movw $0xa, (%r14)
movups 0x10(%rsi), %xmm0
movq (%rsp), %rax
movups %xmm0, (%rax)
jmp 0x9aba
cmpq $0x0, (%r13)
jne 0x9af0
callq 0x9b22
movq %rax, (%r13)
movl %r15d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop