// File: /workspace/github_c_makefile_star2_O2/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 0x16f98
movq %rax, %r14
movq 0x48(%rax), %rdx
pushq $0x2
popq %rbp
movq %rbx, %rdi
movl %ebp, %esi
callq 0xf420
movq %rax, %r12
orl $-0x1, 0x4(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x17a46
testl %eax, %eax
jne 0x1649d
leaq 0x58(%r14), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x18027
testl %eax, %eax
jne 0x1649d
leaq 0x50(%rsp), %r15
pushq $0x1
popq %rdx
movq %r13, %rdi
movq %r15, %rsi
callq 0x18049
leaq 0x8(%rsp), %rsi
andq $0x0, (%rsi)
andl $0x0, 0x8(%rsi)
leaq 0x40(%rsp), %rax
movq %rax, 0x10(%rsi)
movq $0x1, 0x18(%rsi)
xorps %xmm0, %xmm0
leaq 0x60(%rsp), %rcx
movups %xmm0, 0x10(%rcx)
movq %rcx, 0x20(%rsi)
andl $0x0, 0x30(%rsi)
movups %xmm0, 0x20(%rcx)
movups %xmm0, 0x30(%rcx)
movups (%r15), %xmm0
movups %xmm0, (%rax)
movq $0x18, 0x28(%rsi)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x8(%rcx)
movq $0x14, (%rcx)
orl $-0x1, 0x10(%rcx)
movq 0x128(%r14), %rdi
movl $0x40000000, %edx       # imm = 0x40000000
callq 0x104b0
testl %eax, %eax
je 0x164dc
movl %eax, %r15d
leaq 0x4(%rsp), %rdi
callq 0x1531b
movq %rbx, %rdi
callq 0xf9d0
movq %rbx, %rdi
callq 0xf9d0
movslq %r15d, %rsi
movq %rbx, %rdi
callq 0xf960
pushq $0x3
popq %rbp
movl %ebp, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x10, 0x30(%rsp)
jb 0x164eb
movq 0x28(%rsp), %r15
jmp 0x164ee
xorl %r15d, %r15d
pushq $-0x1
popq %r14
leaq 0x4(%rsp), %r12
leaq 0x8(%rsp), %r13
testq %r15, %r15
je 0x16530
cmpl $0x1, 0x8(%r15)
jne 0x16520
cmpl $0x1, 0xc(%r15)
jne 0x16520
movq %r12, %rdi
callq 0x1531b
movl 0x10(%r15), %r14d
movl %r14d, 0x4(%rsp)
movq %r13, %rdi
movq %r15, %rsi
callq 0xf2a0
movq %rax, %r15
jmp 0x164fc
testb $0x28, 0x38(%rsp)
je 0x16540
pushq $0x69
popq %r15
jmp 0x164a0
cmpq $0x0, 0x20(%rsp)
je 0x16563
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rdx
testq %rdx, %rdx
je 0x16563
movq (%rax), %rsi
movq %rbx, %rdi
callq 0xf4e0
jmp 0x16572
leaq 0x1da91(%rip), %rsi     # 0x33ffb
movq %rbx, %rdi
callq 0xfc50
cmpl $-0x1, %r14d
je 0x165c7
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 %r14d, %esi
callq 0x10200
testl %eax, %eax
jne 0x1649d
jmp 0x164c8
movq %rbx, %rdi
callq 0xf9d0
jmp 0x164c8