// File: /workspace/github_c_makefile_star2_O2/wahern[P]cqueues/src/socket.c
// Function: lso_connect2
static lso_nargs_t lso_connect2(lua_State *L) {
	const char *host NOTUSED = NULL, *port NOTUSED = NULL;
	const char *path = NULL;
	struct so_options opts;
	struct luasocket *S;
	size_t plen;
	int family, type, error;

	if (lua_istable(L, 1)) {
		opts = lso_checkopts(L, 1);

		lua_getfield(L, 1, "family");
		family = luaL_optinteger(L, -1, AF_UNSPEC);
		lua_pop(L, 1);

		lua_getfield(L, 1, "type");
		type = luaL_optinteger(L, -1, SOCK_STREAM);
		lua_pop(L, 1);

		if (lso_getfield(L, 1, "path")) {
			path = luaL_checklstring(L, -1, &plen);
			family = AF_UNIX;
		} else {
			lua_getfield(L, 1, "host");
			host = luaL_checkstring(L, -1);
			lua_getfield(L, 1, "port");
			port = luaL_checkstring(L, -1);
		}
	} else {
		opts = *so_opts();
		host = luaL_checkstring(L, 1);
		port = luaL_checkstring(L, 2);
		family = luaL_optinteger(L, 3, AF_UNSPEC);
		type = luaL_optinteger(L, 4, SOCK_STREAM);
	}

	S = lso_newsocket(L, type);

	opts.fd_close.arg = S;
	opts.fd_close.cb = &lso_closefd;

	if (path) {
		struct sockaddr_un sun;

		memset(&sun, 0, sizeof sun);
		sun.sun_family = AF_UNIX;
		memcpy(sun.sun_path, path, MIN(plen, sizeof sun.sun_path));

		if (!(S->socket = so_dial((struct sockaddr *)&sun, type, &opts, &error)))
			goto error;
	} else {
		if (!(S->socket = so_open(host, port, 0, family, type, &opts, &error)))
			goto error;
	}

	if ((error = lso_prepsocket(S)))
		goto error;

	(void)so_connect(S->socket);

	return 1;
error:
	lua_pushnil(L);
	lua_pushinteger(L, error);

	return 2;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rdi, %r14
pushq $0x1
popq %rsi
callq 0xf270
cmpl $0x5, %eax
jne 0x190cc
leaq 0x20(%rsp), %rdi
pushq $0x1
popq %r12
movq %r14, %rsi
movl %r12d, %edx
callq 0x185e2
leaq 0x178c8(%rip), %rdx     # 0x30901
movq %r14, %rdi
movl %r12d, %esi
callq 0xf1f0
pushq $-0x1
popq %rbx
movq %r14, %rdi
movl %ebx, %esi
xorl %edx, %edx
callq 0xf420
movq %rax, %r13
pushq $-0x2
popq %rbp
movq %r14, %rdi
movl %ebp, %esi
callq 0x106f0
leaq 0x1ad54(%rip), %rdx     # 0x33dbe
movq %r14, %rdi
movl %r12d, %esi
callq 0xf1f0
pushq $0x1
popq %rdx
movq %r14, %rdi
movl %ebx, %esi
callq 0xf420
movq %rax, %r15
movq %r14, %rdi
movl %ebp, %esi
callq 0x106f0
leaq 0x17723(%rip), %rdx     # 0x307b9
movq %r14, %rdi
movl %r12d, %esi
callq 0x18db8
testb %al, %al
je 0x19166
pushq $-0x1
popq %rsi
leaq 0x18(%rsp), %rdx
movq %r14, %rdi
callq 0xf660
movq %rax, %r13
movq $0x0, 0x10(%rsp)
xorl %ebp, %ebp
jmp 0x191b4
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movw $0x100, 0x30(%rsp)      # imm = 0x100
andl $0x0, 0x32(%rsp)
andw $0x0, 0x36(%rsp)
movups %xmm0, 0x3f(%rsp)
movups %xmm0, 0x4f(%rsp)
movb $0x0, 0x5f(%rsp)
andl $0x0, 0x69(%rsp)
andl $0x0, 0x6c(%rsp)
andl $0x0, 0x38(%rsp)
movw $0x101, 0x3c(%rsp)      # imm = 0x101
movb $0x1, %al
movb %al, 0x3e(%rsp)
movq $0x1, 0x60(%rsp)
movb %al, 0x68(%rsp)
xorl %r13d, %r13d
pushq $0x1
popq %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0xf660
movq %rax, 0x10(%rsp)
pushq $0x2
popq %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0xf660
movq %rax, %rbp
pushq $0x3
popq %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0xf420
movq %rax, %r12
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
callq 0xf420
movq %rax, %r15
jmp 0x191b4
leaq 0x1779b(%rip), %rdx     # 0x30908
pushq $0x1
popq %rbx
movq %r14, %rdi
movl %ebx, %esi
callq 0xf1f0
movq %r13, %r12
xorl %r13d, %r13d
pushq $-0x1
popq %rbp
movq %r14, %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0xf660
movq %rax, 0x10(%rsp)
leaq 0x1764c(%rip), %rdx     # 0x307e7
movq %r14, %rdi
movl %ebx, %esi
callq 0xf1f0
movq %r14, %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0xf660
movq %rax, %rbp
movq %r14, %rdi
movl %r15d, %esi
callq 0x14c41
movq %rax, %rbx
movq %rax, 0x48(%rsp)
leaq -0x4490(%rip), %rax     # 0x14d3e
movq %rax, 0x50(%rsp)
testq %r13, %r13
je 0x19232
leaq 0x7c(%rsp), %rdi
xorps %xmm0, %xmm0
movups %xmm0, 0x5c(%rdi)
movups %xmm0, 0x50(%rdi)
movups %xmm0, 0x40(%rdi)
movups %xmm0, 0x30(%rdi)
movups %xmm0, 0x20(%rdi)
movups %xmm0, 0x10(%rdi)
movups %xmm0, (%rdi)
movw $0x1, -0x2(%rdi)
movq 0x18(%rsp), %rax
cmpq $0x6c, %rax
pushq $0x6c
popq %rdx
cmovbq %rax, %rdx
movq %r13, %rsi
callq 0xfad0
leaq 0x7a(%rsp), %rdi
leaq 0x20(%rsp), %rdx
leaq 0xc(%rsp), %rcx
movl %r15d, %esi
callq 0x10400
jmp 0x19255
leaq 0xc(%rsp), %rax
movq %rax, (%rsp)
leaq 0x20(%rsp), %r9
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
xorl %edx, %edx
movl %r12d, %ecx
movl %r15d, %r8d
callq 0xf220
movq %rax, 0x128(%rbx)
testq %rax, %rax
je 0x19271
movq %rbx, %rdi
callq 0x1518e
movl %eax, 0xc(%rsp)
testl %eax, %eax
je 0x1928b
pushq $0x2
popq %rbx
movq %r14, %rdi
callq 0xf9d0
movslq 0xc(%rsp), %rsi
movq %r14, %rdi
callq 0xf960
jmp 0x1929a
movq 0x128(%rbx), %rdi
callq 0xf820
pushq $0x1
popq %rbx
movl %ebx, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq