// File: /workspace/github_c_makefile_star2_O1/wahern[P]cqueues/src/socket.c
// Function: lso_recvfd2
static lso_nargs_t lso_recvfd2(lua_State *L) {
	struct luasocket *S = lso_checkself(L, 1);
	size_t bufsiz = luaL_optunsigned(L, 2, S->ibuf.maxline);
	struct msghdr *msg;
	struct cmsghdr *cmsg;
	struct iovec iov;
	int fd = -1, error;

	if ((error = lso_preprcv(L, S)))
		goto error;

	if ((error = fifo_grow(&S->ibuf.fifo, bufsiz)))
		goto error;

	fifo_wvec(&S->ibuf.fifo, &iov, 1);

	msg = so_fdmsg(iov.iov_base, iov.iov_len, -1);

#if defined MSG_CMSG_CLOEXEC
	if ((error = so_recvmsg(S->socket, msg, MSG_CMSG_CLOEXEC)))
		goto error;
#else
	if ((error = so_recvmsg(S->socket, msg, 0)))
		goto error;
#endif

	for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
		if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS)
			continue;

		cqs_closefd(&fd);
		memcpy(&fd, CMSG_DATA(cmsg), sizeof fd);
	}

	if (msg->msg_flags & (MSG_TRUNC|MSG_CTRUNC))
		goto trunc;

	if (msg->msg_iovlen > 0 && msg->msg_iov[0].iov_len > 0)
		lua_pushlstring(L, msg->msg_iov[0].iov_base, msg->msg_iov[0].iov_len);
	else
		lua_pushliteral(L, "");

	if (fd == -1)
		lua_pushnil(L);
	else if ((error = cqs_socket_fdopen(L, fd, so_opts())))
		goto error;

	return 2;
trunc:
	error = ENOBUFS;
error:
	cqs_closefd(&fd);

	lua_pushnil(L);
	lua_pushnil(L);
	lua_pushinteger(L, error);

	return 3;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rdi, %rbx
callq 0x18609
movq %rax, %r14
movq 0x48(%rax), %rdx
movq %rbx, %rdi
movl $0x2, %esi
callq 0xf400
movq %rax, %r13
movq %rbx, %rdi
movq %r14, %rsi
callq 0x190fa
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
je 0x17928
movl %eax, %r15d
jmp 0x17960
movq %r14, %r12
addq $0x58, %r12
movq 0x70(%r14), %rax
movq 0x80(%r14), %rsi
subq %rsi, %rax
xorl %r15d, %r15d
cmpq %r13, %rax
jae 0x1795b
movl $0x4b, %r15d
addq %r13, %rsi
jb 0x1795b
movq %r12, %rdi
callq 0x163ad
movl %eax, %r15d
testl %r15d, %r15d
je 0x1799e
cmpl $-0x1, %ebp
je 0x1796c
movl %ebp, %edi
callq 0x10a10
movq %rbx, %rdi
callq 0xf980
movq %rbx, %rdi
callq 0xf980
movslq %r15d, %rsi
movq %rbx, %rdi
callq 0xf910
movl $0x3, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x50(%rsp), %r15
movq %r12, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x1989c
leaq 0x8(%rsp), %rsi
movq $0x0, (%rsi)
xorl %eax, %eax
movl %eax, 0x8(%rsi)
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rcx
movq %rcx, 0x10(%rsi)
movq $0x1, 0x18(%rsi)
leaq 0x60(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, 0x20(%rdx)
movaps %xmm0, 0x30(%rdx)
movq %rdx, 0x20(%rsi)
movl %eax, 0x30(%rsi)
movups (%r15), %xmm0
movaps %xmm0, (%rcx)
movq $0x18, 0x28(%rsi)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x8(%rdx)
movq $0x14, (%rdx)
movl $0xffffffff, 0x10(%rdx) # imm = 0xFFFFFFFF
movq 0x128(%r14), %rdi
movl $0x40000000, %edx       # imm = 0x40000000
callq 0x10400
testl %eax, %eax
jne 0x17923
movq 0x28(%rsp), %r14
cmpq $0x10, 0x30(%rsp)
setb %al
testq %r14, %r14
sete %cl
orb %al, %cl
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jne 0x17a8a
leaq 0x8(%rsp), %r15
cmpl $0x1, 0x8(%r14)
jne 0x17a77
cmpl $0x1, 0xc(%r14)
jne 0x17a77
cmpl $-0x1, %ebp
je 0x17a73
movl %ebp, %edi
callq 0x10a10
movl 0x10(%r14), %ebp
movq %r15, %rdi
movq %r14, %rsi
callq 0xf290
movq %rax, %r14
testq %rax, %rax
jne 0x17a59
movl $0x69, %r15d
testb $0x28, 0x38(%rsp)
jne 0x17960
cmpq $0x0, 0x20(%rsp)
je 0x17abe
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rdx
testq %rdx, %rdx
je 0x17abe
movq (%rax), %rsi
movq %rbx, %rdi
callq 0xf4c0
jmp 0x17acd
leaq 0x21586(%rip), %rsi     # 0x3904b
movq %rbx, %rdi
callq 0xfbd0
cmpl $-0x1, %ebp
je 0x17b24
xorps %xmm0, %xmm0
leaq 0xa0(%rsp), %rdx
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, 0x40(%rdx)
movaps %xmm0, 0x30(%rdx)
movaps %xmm0, 0x20(%rdx)
movaps %xmm0, (%rdx)
movb $0x1, %al
movb %al, 0x11(%rdx)
movl $0x0, 0x18(%rdx)
movw $0x101, 0x1c(%rdx)      # imm = 0x101
movb %al, 0x1e(%rdx)
movq $0x1, 0x40(%rdx)
movb %al, 0x48(%rdx)
movq %rbx, %rdi
movl %ebp, %esi
callq 0x10170
testl %eax, %eax
jne 0x17923
jmp 0x17b2c
movq %rbx, %rdi
callq 0xf980
movl $0x2, %eax
jmp 0x1798c