// File: /workspace/github_c_makefile_star2_O3/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 0x184cf
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 0x18fc8
testl %eax, %eax
je 0x17830
movl %eax, %r15d
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
movq %r14, %r12
addq $0x58, %r12
movq 0x70(%r14), %rax
movq 0x80(%r14), %rsi
subq %rsi, %rax
cmpq %r13, %rax
jae 0x17861
movl $0x4b, %r15d
addq %r13, %rsi
jb 0x177fe
movq %r12, %rdi
callq 0x16418
testl %eax, %eax
jne 0x177fb
leaq 0x50(%rsp), %r15
movq %r12, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x197b6
leaq 0x8(%rsp), %rsi
movq $0x0, (%rsi)
xorl %eax, %eax
movl %eax, 0x8(%rsi)
leaq 0x40(%rsp), %rcx
movq %rcx, 0x10(%rsi)
movq $0x1, 0x18(%rsi)
xorps %xmm0, %xmm0
leaq 0x60(%rsp), %rdx
movups %xmm0, 0x10(%rdx)
movups %xmm0, 0x20(%rdx)
movups %xmm0, 0x30(%rdx)
movq %rdx, 0x20(%rsi)
movl %eax, 0x30(%rsi)
movups (%r15), %xmm0
movups %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 0x177fb
movq 0x28(%rsp), %r14
cmpq $0x10, 0x30(%rsp)
setb %al
testq %r14, %r14
sete %cl
orb %al, %cl
je 0x17927
movl $0x69, %r15d
testb $0x28, 0x38(%rsp)
jne 0x177fe
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x1797d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
leaq 0x8(%rsp), %r15
cmpl $0x1, 0x8(%r14)
jne 0x1794f
cmpl $0x1, 0xc(%r14)
jne 0x1794f
cmpl $-0x1, %ebp
je 0x1794b
movl %ebp, %edi
callq 0x10a10
movl 0x10(%r14), %ebp
movq %r15, %rdi
movq %r14, %rsi
callq 0xf290
movq %rax, %r14
testq %rax, %rax
jne 0x17931
testb $0x28, 0x38(%rsp)
je 0x1797d
movl $0x69, %r15d
cmpl $-0x1, %ebp
jne 0x179fc
jmp 0x177fe
cmpq $0x0, 0x20(%rsp)
je 0x179a0
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rdx
testq %rdx, %rdx
je 0x179a0
movq (%rax), %rsi
movq %rbx, %rdi
callq 0xf4c0
jmp 0x179af
leaq 0x21664(%rip), %rsi     # 0x3900b
movq %rbx, %rdi
callq 0xfbd0
cmpl $-0x1, %ebp
je 0x17a08
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)
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
movl %eax, %r15d
testl %eax, %eax
je 0x17a10
movl %ebp, %edi
callq 0x10a10
jmp 0x177fe
movq %rbx, %rdi
callq 0xf980
movl $0x2, %eax
jmp 0x1781e