// File: /workspace/github_c_makefile_star2_O1/GangZhuo[P]dohclient/src/netutils.c
// Function: tcp_connect
conn_status tcp_connect(const sockaddr_t* addr, sock_t* psock)
{
	sock_t sock = *psock;

	if (sock == -1) {
		sock = socket(addr->addr.ss_family, SOCK_STREAM, IPPROTO_TCP);

		if (sock == -1) {
			loge("create socket error. errno=%d, %s - %s\n",
				errno, strerror(errno), get_sockaddrname(addr));
			return cs_err_create_sock;
		}

		if (setnonblock(sock) != 0) {
			loge("set sock non-block failed - %s\n",
				get_sockaddrname(addr));
			close(sock);
			return cs_err_set_nonblock;
		}

		if (setnodelay(sock) != 0) {
			loge("set sock nodelay failed - %s\n",
				get_sockaddrname(addr));
			close(sock);
			return cs_err_set_nodelay;
		}

	}

	logd("connect %s\n", get_sockaddrname(addr));

	if (connect(sock, (const struct sockaddr*)(&addr->addr), addr->addrlen) != 0) {
		int err = errno;
		if (!is_eagain(err)) {
			loge("errno=%d, %s - %s\n",
				errno, strerror(errno), get_sockaddrname(addr));
			if (sock != *psock) {
				close(sock);
			}
			return cs_err_connect;
		}
		else {
			*psock = sock;
			return cs_connecting;
		}
	}
	else {
		*psock = sock;
		return cs_connected;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rsi, %r15
movq %rdi, %r14
movl (%rsi), %ebx
cmpl $-0x1, %ebx
je 0x433b
xorl %eax, %eax
callq 0x2e59
cmpl $0x7, (%rax)
jl 0x42fd
movq %r14, %rdi
callq 0x35d9
leaq 0x2e51(%rip), %rsi      # 0x7128
leaq 0x3027(%rip), %rdx      # 0x7305
leaq 0x3092(%rip), %r8       # 0x7377
leaq 0x7f84(%rip), %r9       # 0xc270
movl $0x7, %edi
movl $0x267, %ecx            # imm = 0x267
xorl %eax, %eax
callq 0x2e92
movl 0x80(%r14), %edx
movl %ebx, %edi
movq %r14, %rsi
callq 0x21b0
testl %eax, %eax
je 0x43b6
callq 0x2030
movq %rax, %r12
movl (%rax), %eax
cmpl $0xb, %eax
je 0x432e
cmpl $0x73, %eax
jne 0x442e
movl %ebx, (%r15)
movl $0x1, %ebp
jmp 0x44f7
movzwl (%r14), %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x20a0
cmpl $-0x1, %eax
je 0x43c3
movl %eax, %ebx
movl %eax, %edi
callq 0x33cf
testl %eax, %eax
je 0x449b
xorl %eax, %eax
callq 0x2e59
cmpl $0x3, (%rax)
jl 0x43a5
movq %r14, %rdi
callq 0x35d9
leaq 0x2da9(%rip), %rsi      # 0x7128
leaq 0x2f7f(%rip), %rdx      # 0x7305
leaq 0x2fac(%rip), %r8       # 0x7339
leaq 0x7edc(%rip), %r9       # 0xc270
movl $0x3, %edi
movl $0x259, %ecx            # imm = 0x259
xorl %eax, %eax
callq 0x2e92
movl %ebx, %edi
callq 0x23b0
movl $0x13, %ebp
jmp 0x44f7
movl %ebx, (%r15)
movl $0x2, %ebp
jmp 0x44f7
xorl %eax, %eax
callq 0x2e59
movl $0x12, %ebp
cmpl $0x3, (%rax)
jl 0x44f7
callq 0x2030
movl (%rax), %ebx
movl %ebx, %edi
callq 0x20c0
movq %rax, %r15
movq %r14, %rdi
callq 0x35d9
leaq 0x7e78(%rip), %r10      # 0xc270
leaq 0x2d29(%rip), %rsi      # 0x7128
leaq 0x2eff(%rip), %rdx      # 0x7305
leaq 0x2f04(%rip), %r8       # 0x7311
movl $0x3, %edi
movl $0x253, %ecx            # imm = 0x253
movl %ebx, %r9d
xorl %eax, %eax
pushq %r10
pushq %r15
callq 0x2e92
addq $0x10, %rsp
jmp 0x44f7
xorl %eax, %eax
callq 0x2e59
cmpl $0x3, (%rax)
jl 0x4488
movl (%r12), %ebp
movl %ebp, %edi
callq 0x20c0
movq %rax, %r12
movq %r14, %rdi
callq 0x35d9
leaq 0x7e19(%rip), %r10      # 0xc270
leaq 0x2cca(%rip), %rsi      # 0x7128
leaq 0x2ea0(%rip), %rdx      # 0x7305
leaq 0x2eba(%rip), %r8       # 0x7326
movl $0x3, %edi
movl $0x26d, %ecx            # imm = 0x26D
movl %ebp, %r9d
xorl %eax, %eax
pushq %r10
pushq %r12
callq 0x2e92
addq $0x10, %rsp
movl $0x15, %ebp
cmpl (%r15), %ebx
je 0x44f7
movl %ebx, %edi
callq 0x23b0
jmp 0x44f7
movl %ebx, %edi
callq 0x34fa
testl %eax, %eax
je 0x42bc
xorl %eax, %eax
callq 0x2e59
cmpl $0x3, (%rax)
jl 0x44eb
movq %r14, %rdi
callq 0x35d9
leaq 0x2c63(%rip), %rsi      # 0x7128
leaq 0x2e39(%rip), %rdx      # 0x7305
leaq 0x2e86(%rip), %r8       # 0x7359
leaq 0x7d96(%rip), %r9       # 0xc270
movl $0x3, %edi
movl $0x260, %ecx            # imm = 0x260
xorl %eax, %eax
callq 0x2e92
movl %ebx, %edi
callq 0x23b0
movl $0x14, %ebp
movl %ebp, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq