// File: /workspace/github_c_makefile_star2_O1/jfriesse[P]omping/rsfunc.c
// Function: rs_receive_msg
ssize_t
rs_receive_msg(int sock, struct sockaddr_storage *from_addr, char *msg, size_t msg_len,
    uint8_t *ttl, struct timeval *timestamp)
{
	char cmsg_buf[CMSG_SPACE(1024)];
	struct cmsghdr *cmsg;
	struct iovec msg_iovec;
	struct msghdr msg_hdr;
	ssize_t recv_size;
	int ittl;
	int timestamp_set;

	ittl = 0;
	timestamp_set = 0;

	memset(&msg_iovec, 0, sizeof(msg_iovec));
	msg_iovec.iov_base = msg;
	msg_iovec.iov_len = msg_len;

	memset(&msg_hdr, 0, sizeof(msg_hdr));
	msg_hdr.msg_name = from_addr;
	msg_hdr.msg_namelen = sizeof(struct sockaddr_storage);
	msg_hdr.msg_iov = &msg_iovec;
	msg_hdr.msg_iovlen = 1;
	msg_hdr.msg_control = cmsg_buf;
	msg_hdr.msg_controllen = sizeof(cmsg_buf);

	recv_size = recvmsg(sock, &msg_hdr, 0);

	if (recv_size == -1) {
		if (errno == EINTR) {
			DEBUG2_PRINTF("recvmsg error - EINTR");
			return (-2);
		}

		if (errno == EHOSTUNREACH || errno == EHOSTDOWN || errno == ENETDOWN ||
		    errno == ECONNRESET) {
			DEBUG2_PRINTF("recvmsg error - EHOSTUNREACH || EHOSTDOWN || ENETDOWN ||"
			    " ECONNRESET");
			return (-3);
		}

		DEBUG2_PRINTF("recvmsg error - errno = %d", errno);
		return (-1);
	}

	if (msg_hdr.msg_flags & MSG_TRUNC || msg_hdr.msg_flags & MSG_CTRUNC) {
		DEBUG2_PRINTF("recvmsg error - MSG_TRUNC | MSG_CTRUNC");
		return (-4);
	}

	for (cmsg = CMSG_FIRSTHDR(&msg_hdr); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg_hdr, cmsg)) {
		switch (cmsg->cmsg_level) {
		case SOL_SOCKET:
#ifdef SCM_TIMESTAMP
			if (cmsg->cmsg_type == SCM_TIMESTAMP &&
			    cmsg->cmsg_len >= sizeof(struct timeval) && timestamp != NULL) {
				memcpy(timestamp, CMSG_DATA(cmsg), sizeof(struct timeval));
				timestamp_set = 1;
			}
#endif
		case IPPROTO_IP:
			if (cmsg->cmsg_type == IP_TTL && cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
				memcpy(&ittl, CMSG_DATA(cmsg), sizeof(ittl));
			}
#ifdef IP_RECVTTL
			if (cmsg->cmsg_type == IP_RECVTTL && cmsg->cmsg_len > 1) {
				ittl = *(uint8_t *)CMSG_DATA(cmsg);
			}
#endif
			break;
		case IPPROTO_IPV6:
			if (cmsg->cmsg_type == IPV6_HOPLIMIT && cmsg->cmsg_len ==
			    CMSG_LEN(sizeof(int))) {
				memcpy(&ittl, CMSG_DATA(cmsg), sizeof(ittl));
			}
			break;
		}
	}

	*ttl = (uint8_t)ittl;

	if (!timestamp_set && timestamp != NULL) {
		*timestamp = util_get_time();
	}

	return (recv_size);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x468, %rsp            # imm = 0x468
movq %r9, %rbx
movq %r8, %r15
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %r8
movaps %xmm0, (%r8)
movq %rdx, (%r8)
movq %rcx, 0x8(%r8)
movq %rsp, %rax
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movq $0x0, 0x30(%rax)
movq %rsi, (%rax)
movl $0x80, 0x8(%rax)
movq %r8, 0x10(%rax)
movq $0x1, 0x18(%rax)
leaq 0x50(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq $0x410, 0x28(%rax)      # imm = 0x410
movq %rax, %rsi
xorl %edx, %edx
callq 0x2270
cmpq $-0x1, %rax
je 0x75ce
testb $0x28, 0x30(%rsp)
je 0x7614
leaq 0x4092(%rip), %rdi      # 0xb63c
leaq 0x4176(%rip), %rcx      # 0xb727
movl $0xb7, %esi
movl $0x3, %edx
xorl %eax, %eax
callq 0x4ada
movq $-0x4, %r14
jmp 0x7729
callq 0x2060
movl (%rax), %r8d
leal -0x64(%r8), %eax
cmpl $0xd, %eax
ja 0x7638
movl $0x3011, %ecx           # imm = 0x3011
btl %eax, %ecx
jae 0x7638
leaq 0x404c(%rip), %rdi      # 0xb63c
leaq 0x40d1(%rip), %rcx      # 0xb6c8
movl $0xae, %esi
movl $0x3, %edx
xorl %eax, %eax
callq 0x4ada
movq $-0x3, %r14
jmp 0x7729
movq %rax, %r14
movq 0x20(%rsp), %rax
cmpq $0x10, 0x28(%rsp)
setb %cl
testq %rax, %rax
sete %dl
xorl %ebp, %ebp
orb %cl, %dl
je 0x766d
movb $0x1, %al
jmp 0x76ed
leaq 0x3ffd(%rip), %rdi      # 0xb63c
cmpl $0x4, %r8d
jne 0x770a
leaq 0x4062(%rip), %rcx      # 0xb6b2
movl $0xa7, %esi
movl $0x3, %edx
xorl %eax, %eax
callq 0x4ada
movq $-0x2, %r14
jmp 0x7729
movq %rsp, %r12
xorl %r13d, %r13d
movl 0x8(%rax), %ecx
testl %ecx, %ecx
je 0x76a1
cmpl $0x29, %ecx
je 0x76c5
cmpl $0x1, %ecx
jne 0x76d5
cmpl $0x1d, 0xc(%rax)
jne 0x76a1
testq %rbx, %rbx
je 0x76a1
cmpq $0x10, (%rax)
jb 0x76a1
movups 0x10(%rax), %xmm0
movups %xmm0, (%rbx)
movl $0x1, %ebp
movl 0xc(%rax), %ecx
cmpl $0x2, %ecx
jne 0x76b3
cmpq $0x14, (%rax)
jne 0x76b3
movl 0x10(%rax), %r13d
cmpl $0xc, %ecx
jne 0x76d5
cmpq $0x2, (%rax)
jb 0x76d5
movzbl 0x10(%rax), %r13d
jmp 0x76d5
cmpl $0x34, 0xc(%rax)
jne 0x76d5
cmpq $0x14, (%rax)
jne 0x76d5
movl 0x10(%rax), %r13d
movq %r12, %rdi
movq %rax, %rsi
callq 0x2300
testq %rax, %rax
jne 0x7673
testl %ebp, %ebp
sete %al
movl %r13d, %ebp
movb %bpl, (%r15)
testq %rbx, %rbx
sete %cl
xorb $0x1, %al
orb %cl, %al
jne 0x7729
callq 0x8ff2
movq %rax, (%rbx)
movq %rdx, 0x8(%rbx)
jmp 0x7729
leaq 0x3ffb(%rip), %rcx      # 0xb70c
movl $0xb2, %esi
movl $0x3, %edx
xorl %eax, %eax
callq 0x4ada
movq $-0x1, %r14
movq %r14, %rax
addq $0x468, %rsp            # imm = 0x468
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq