// File: /workspace/github_c_makefile_star2_O3/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 0x432b
xorl %eax, %eax
callq 0x2e49
cmpl $0x7, (%rax)
jl 0x42ed
movq %r14, %rdi
callq 0x35d9
leaq 0x2e61(%rip), %rsi      # 0x7128
leaq 0x3037(%rip), %rdx      # 0x7305
leaq 0x30a2(%rip), %r8       # 0x7377
leaq 0x7f94(%rip), %r9       # 0xc270
movl $0x7, %edi
movl $0x267, %ecx            # imm = 0x267
xorl %eax, %eax
callq 0x2e82
movl 0x80(%r14), %edx
movl %ebx, %edi
movq %r14, %rsi
callq 0x21b0
testl %eax, %eax
je 0x43a6
callq 0x2030
movq %rax, %r12
movl (%rax), %eax
cmpl $0xb, %eax
je 0x431e
cmpl $0x73, %eax
jne 0x441e
movl %ebx, (%r15)
movl $0x1, %ebp
jmp 0x44e7
movzwl (%r14), %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x20a0
cmpl $-0x1, %eax
je 0x43b3
movl %eax, %ebx
movl %eax, %edi
callq 0x33cf
testl %eax, %eax
je 0x448b
xorl %eax, %eax
callq 0x2e49
cmpl $0x3, (%rax)
jl 0x4395
movq %r14, %rdi
callq 0x35d9
leaq 0x2db9(%rip), %rsi      # 0x7128
leaq 0x2f8f(%rip), %rdx      # 0x7305
leaq 0x2fbc(%rip), %r8       # 0x7339
leaq 0x7eec(%rip), %r9       # 0xc270
movl $0x3, %edi
movl $0x259, %ecx            # imm = 0x259
xorl %eax, %eax
callq 0x2e82
movl %ebx, %edi
callq 0x23b0
movl $0x13, %ebp
jmp 0x44e7
movl %ebx, (%r15)
movl $0x2, %ebp
jmp 0x44e7
xorl %eax, %eax
callq 0x2e49
movl $0x12, %ebp
cmpl $0x3, (%rax)
jl 0x44e7
callq 0x2030
movl (%rax), %ebx
movl %ebx, %edi
callq 0x20c0
movq %rax, %r15
movq %r14, %rdi
callq 0x35d9
leaq 0x7e88(%rip), %r10      # 0xc270
leaq 0x2d39(%rip), %rsi      # 0x7128
leaq 0x2f0f(%rip), %rdx      # 0x7305
leaq 0x2f14(%rip), %r8       # 0x7311
movl $0x3, %edi
movl $0x253, %ecx            # imm = 0x253
movl %ebx, %r9d
xorl %eax, %eax
pushq %r10
pushq %r15
callq 0x2e82
addq $0x10, %rsp
jmp 0x44e7
xorl %eax, %eax
callq 0x2e49
cmpl $0x3, (%rax)
jl 0x4478
movl (%r12), %ebp
movl %ebp, %edi
callq 0x20c0
movq %rax, %r12
movq %r14, %rdi
callq 0x35d9
leaq 0x7e29(%rip), %r10      # 0xc270
leaq 0x2cda(%rip), %rsi      # 0x7128
leaq 0x2eb0(%rip), %rdx      # 0x7305
leaq 0x2eca(%rip), %r8       # 0x7326
movl $0x3, %edi
movl $0x26d, %ecx            # imm = 0x26D
movl %ebp, %r9d
xorl %eax, %eax
pushq %r10
pushq %r12
callq 0x2e82
addq $0x10, %rsp
movl $0x15, %ebp
cmpl (%r15), %ebx
je 0x44e7
movl %ebx, %edi
callq 0x23b0
jmp 0x44e7
movl %ebx, %edi
callq 0x34fa
testl %eax, %eax
je 0x42ac
xorl %eax, %eax
callq 0x2e49
cmpl $0x3, (%rax)
jl 0x44db
movq %r14, %rdi
callq 0x35d9
leaq 0x2c73(%rip), %rsi      # 0x7128
leaq 0x2e49(%rip), %rdx      # 0x7305
leaq 0x2e96(%rip), %r8       # 0x7359
leaq 0x7da6(%rip), %r9       # 0xc270
movl $0x3, %edi
movl $0x260, %ecx            # imm = 0x260
xorl %eax, %eax
callq 0x2e82
movl %ebx, %edi
callq 0x23b0
movl $0x14, %ebp
movl %ebp, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq