// File: /workspace/github_cpp_makefile_star2_O3/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 0x8518(%rip), %rax      # 0x124b8
movzwl (%rax), %eax
movl %eax, %edx
andl $-0x9, %edx
cmpw $0x2, %dx
jne 0x9fb3
movw %ax, (%rsi)
jmp 0x9fb6
movzwl (%rsi), %eax
cmpw $0xa, %ax
movl $0x1c, %eax
movl $0x10, %edx
cmovel %eax, %edx
leaq 0x8(%rsp), %rax
movq %rsi, (%rax)
movl %edx, 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 0xa06f
movq 0xd0(%rsp), %rbp
movq $0x0, (%rbp)
movl $0x7f, (%rbx)
leaq 0x8484(%rip), %rsi      # 0x124b8
movl $0x10, %ecx
movq %r14, %rdi
rep  movsq (%rsi), %es:(%rdi)
movq 0x30(%rsp), %rax
testq %rax, %rax
je 0xa05f
cmpq $0x10, %rax
setb %al
movq 0x28(%rsp), %rsi
testq %rsi, %rsi
sete %cl
orb %al, %cl
je 0xa084
cmpq $0x0, (%rbp)
jne 0xa06f
callq 0xa148
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
testl %eax, %eax
je 0xa0eb
cmpl $0x1, %eax
je 0xa0bb
cmpl $0x29, %eax
jne 0xa0fe
movl 0xc(%rsi), %eax
cmpl $0x32, %eax
je 0xa113
cmpl $0x34, %eax
je 0xa0f8
jmp 0xa0fe
cmpl $0x1d, 0xc(%rsi)
jne 0xa0fe
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 0xa0fe
movl 0xc(%rsi), %eax
cmpl $0x2, %eax
je 0xa0f8
cmpl $0xc, %eax
jne 0xa0fe
movzbl 0x10(%rsi), %eax
movl %eax, (%rbx)
movq %r12, %rdi
callq 0x30c0
movq %rax, %rsi
testq %rax, %rax
jne 0xa09b
jmp 0xa05f
cmpq $0x24, (%rsi)
jne 0xa0fe
movw $0xa, (%r14)
movups 0x10(%rsi), %xmm0
movq (%rsp), %rax
movups %xmm0, (%rax)
jmp 0xa0fe