// File: /workspace/github_c_makefile_star2_O3/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 0x1a84a
leaq 0x20(%rsp), %rdi
movq %rbx, %rsi
movl $0x1, %edx
callq 0x19c85
leaq 0x1b161(%rip), %rdx     # 0x35901
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 0x1e5fb(%rip), %rdx     # 0x38dce
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 0x1afb0(%rip), %rdx     # 0x357b9
movq %rbx, %rdi
movl $0x1, %esi
callq 0x1a4b1
testb %al, %al
je 0x1a8ef
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 0x1a943
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 0x1a943
leaq 0x1b012(%rip), %rdx     # 0x35908
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 0x1aec6(%rip), %rdx     # 0x357e7
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 0x15ead
movq %rax, %rbp
movq %rax, 0x48(%rsp)
leaq -0x4999(%rip), %rax     # 0x15fc4
movq %rax, 0x50(%rsp)
testq %r15, %r15
je 0x1a9c3
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
movl $0x6c, %edx
cmovbq %rax, %rdx
movq %r15, %rsi
callq 0xfa60
leaq 0x7a(%rsp), %rdi
leaq 0x20(%rsp), %rdx
leaq 0xc(%rsp), %rcx
movl %r14d, %esi
callq 0x10360
jmp 0x1a9e6
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 0x1aa02
movq %rbp, %rdi
callq 0x163df
movl %eax, 0xc(%rsp)
testl %eax, %eax
je 0x1aa1e
movq %rbx, %rdi
callq 0xf980
movslq 0xc(%rsp), %rsi
movq %rbx, %rdi
callq 0xf910
movl $0x2, %eax
jmp 0x1aa2f
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