// File: /workspace/github_c_makefile_star2_O1/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, %rbx
movl $0x1, %esi
callq 0xf260
cmpl $0x5, %eax
jne 0x1a987
leaq 0x20(%rsp), %rdi
movq %rbx, %rsi
movl $0x1, %edx
callq 0x19dc5
leaq 0x1b04c(%rip), %rdx     # 0x35929
movq %rbx, %rdi
movl $0x1, %esi
callq 0xf1e0
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
callq 0xf400
movq %rax, %rbp
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x10640
leaq 0x1e4fe(%rip), %rdx     # 0x38e0e
movq %rbx, %rdi
movl $0x1, %esi
callq 0xf1e0
movl $0x1, %edx
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xf400
movq %rax, %r14
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x10640
leaq 0x1ae9b(%rip), %rdx     # 0x357e1
movq %rbx, %rdi
movl $0x1, %esi
callq 0x1a5fc
testb %al, %al
je 0x1aa2c
movl $0x1, %r13d
leaq 0x18(%rsp), %rdx
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xf620
movq %rax, %r15
movq $0x0, 0x10(%rsp)
xorl %r12d, %r12d
jmp 0x1aa80
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movw $0x100, 0x30(%rsp)      # imm = 0x100
xorl %r15d, %r15d
movl %r15d, 0x32(%rsp)
movw $0x0, 0x36(%rsp)
movups %xmm0, 0x3f(%rsp)
movups %xmm0, 0x4f(%rsp)
movb $0x0, 0x5f(%rsp)
movl %r15d, 0x69(%rsp)
movl %r15d, 0x6c(%rsp)
movl %r15d, 0x38(%rsp)
movw $0x101, 0x3c(%rsp)      # imm = 0x101
movb $0x1, %al
movb %al, 0x3e(%rsp)
movq $0x1, 0x60(%rsp)
movb %al, 0x68(%rsp)
movq %rbx, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0xf620
movq %rax, 0x10(%rsp)
movq %rbx, %rdi
movl $0x2, %esi
xorl %edx, %edx
callq 0xf620
movq %rax, %r12
movq %rbx, %rdi
movl $0x3, %esi
xorl %edx, %edx
callq 0xf400
movq %rax, %r13
movl $0x1, %edx
movq %rbx, %rdi
movl $0x4, %esi
callq 0xf400
movq %rax, %r14
jmp 0x1aa80
leaq 0x1aefd(%rip), %rdx     # 0x35930
movq %rbx, %rdi
movl $0x1, %esi
callq 0xf1e0
xorl %r15d, %r15d
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
callq 0xf620
movq %rax, 0x10(%rsp)
leaq 0x1adb1(%rip), %rdx     # 0x3580f
movq %rbx, %rdi
movl $0x1, %esi
callq 0xf1e0
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
callq 0xf620
movq %rax, %r12
movl %ebp, %r13d
movq %rbx, %rdi
movl %r14d, %esi
callq 0x15e40
movq %rax, %rbp
movq %rax, 0x48(%rsp)
leaq -0x4b43(%rip), %rax     # 0x15f57
movq %rax, 0x50(%rsp)
testq %r15, %r15
je 0x1ab01
xorps %xmm0, %xmm0
leaq 0x72(%rsp), %rdi
movaps %xmm0, -0x2(%rdi)
movups %xmm0, 0x5c(%rdi)
movaps %xmm0, 0x4e(%rdi)
movaps %xmm0, 0x3e(%rdi)
movaps %xmm0, 0x2e(%rdi)
movaps %xmm0, 0x1e(%rdi)
movaps %xmm0, 0xe(%rdi)
movw $0x1, -0x2(%rdi)
movq 0x18(%rsp), %rax
cmpq $0x6c, %rax
movl $0x6c, %edx
cmovbq %rax, %rdx
movq %r15, %rsi
callq 0xfa60
leaq 0x70(%rsp), %rdi
leaq 0x20(%rsp), %rdx
leaq 0xc(%rsp), %rcx
movl %r14d, %esi
callq 0x10360
jmp 0x1ab24
leaq 0xc(%rsp), %rax
movq %rax, (%rsp)
leaq 0x20(%rsp), %r9
movq 0x10(%rsp), %rdi
movq %r12, %rsi
xorl %edx, %edx
movl %r13d, %ecx
movl %r14d, %r8d
callq 0xf210
movq %rax, 0x128(%rbp)
testq %rax, %rax
je 0x1ab40
movq %rbp, %rdi
callq 0x1636e
movl %eax, 0xc(%rsp)
testl %eax, %eax
je 0x1ab5c
movq %rbx, %rdi
callq 0xf980
movslq 0xc(%rsp), %rsi
movq %rbx, %rdi
callq 0xf910
movl $0x2, %eax
jmp 0x1ab6d
movq 0x128(%rbp), %rdi
callq 0xf7e0
movl $0x1, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq