// File: /workspace/github_c_makefile_star2_O2/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 0x391e
xorl %eax, %eax
callq 0x24d9
cmpl $0x7, (%rax)
jl 0x38e6
movq %r14, %rdi
callq 0x2c66
leaq 0x3886(%rip), %rsi      # 0x7148
leaq 0x3a5c(%rip), %rdx      # 0x7325
leaq 0x3ac7(%rip), %r8       # 0x7397
leaq 0x7999(%rip), %r9       # 0xb270
pushq $0x7
popq %rdi
movl $0x267, %ecx            # imm = 0x267
xorl %eax, %eax
callq 0x2512
movl 0x80(%r14), %edx
movl %ebx, %edi
movq %r14, %rsi
callq 0x21b0
testl %eax, %eax
je 0x398d
callq 0x2030
movq %rax, %r12
movl (%rax), %eax
cmpl $0xb, %eax
je 0x3917
cmpl $0x73, %eax
jne 0x39fc
movl %ebx, (%r15)
pushq $0x1
jmp 0x3992
movzwl (%r14), %edi
pushq $0x1
popq %rsi
pushq $0x6
popq %rdx
callq 0x20a0
cmpl $-0x1, %eax
je 0x399e
movl %eax, %ebx
movl %eax, %edi
callq 0x2a89
testl %eax, %eax
je 0x3a6a
xorl %eax, %eax
callq 0x24d9
cmpl $0x3, (%rax)
jl 0x3982
movq %r14, %rdi
callq 0x2c66
leaq 0x37ea(%rip), %rsi      # 0x7148
leaq 0x39c0(%rip), %rdx      # 0x7325
leaq 0x39ed(%rip), %r8       # 0x7359
leaq 0x78fd(%rip), %r9       # 0xb270
pushq $0x3
popq %rdi
movl $0x259, %ecx            # imm = 0x259
xorl %eax, %eax
callq 0x2512
movl %ebx, %edi
callq 0x23b0
pushq $0x13
jmp 0x3992
movl %ebx, (%r15)
pushq $0x2
popq %rbp
movl %ebp, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
callq 0x24d9
pushq $0x12
popq %rbp
cmpl $0x3, (%rax)
jl 0x3993
callq 0x2030
movl (%rax), %ebx
movl %ebx, %edi
callq 0x20c0
movq %rax, %r15
movq %r14, %rdi
callq 0x2c66
leaq 0x78a3(%rip), %r10      # 0xb270
leaq 0x3774(%rip), %rsi      # 0x7148
leaq 0x394a(%rip), %rdx      # 0x7325
leaq 0x394f(%rip), %r8       # 0x7331
pushq $0x3
popq %rdi
movl $0x253, %ecx            # imm = 0x253
movl %ebx, %r9d
xorl %eax, %eax
pushq %r10
pushq %r15
callq 0x2512
popq %rax
popq %rcx
jmp 0x3993
xorl %eax, %eax
callq 0x24d9
cmpl $0x3, (%rax)
jl 0x3a52
movl (%r12), %ebp
movl %ebp, %edi
callq 0x20c0
movq %rax, %r12
movq %r14, %rdi
callq 0x2c66
leaq 0x784b(%rip), %r10      # 0xb270
leaq 0x371c(%rip), %rsi      # 0x7148
leaq 0x38f2(%rip), %rdx      # 0x7325
leaq 0x390c(%rip), %r8       # 0x7346
pushq $0x3
popq %rdi
movl $0x26d, %ecx            # imm = 0x26D
movl %ebp, %r9d
xorl %eax, %eax
pushq %r10
pushq %r12
callq 0x2512
popq %rax
popq %rcx
pushq $0x15
popq %rbp
cmpl (%r15), %ebx
je 0x3993
movl %ebx, %edi
callq 0x23b0
jmp 0x3993
movl %ebx, %edi
callq 0x2b9c
testl %eax, %eax
je 0x38a7
xorl %eax, %eax
callq 0x24d9
cmpl $0x3, (%rax)
jl 0x3ab8
movq %r14, %rdi
callq 0x2c66
leaq 0x36b4(%rip), %rsi      # 0x7148
leaq 0x388a(%rip), %rdx      # 0x7325
leaq 0x38d7(%rip), %r8       # 0x7379
leaq 0x77c7(%rip), %r9       # 0xb270
pushq $0x3
popq %rdi
movl $0x260, %ecx            # imm = 0x260
xorl %eax, %eax
callq 0x2512
movl %ebx, %edi
callq 0x23b0
pushq $0x14
jmp 0x3992