// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x1c(%rbp)
cmpl $-0x1, -0x1c(%rbp)
jne 0x46e0
movq -0x10(%rbp), %rax
movzwl (%rax), %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x20b0
movl %eax, -0x1c(%rbp)
cmpl $-0x1, -0x1c(%rbp)
jne 0x4618
jmp 0x45a4
movb $0x0, %al
callq 0x2560
cmpl $0x3, (%rax)
jl 0x460a
callq 0x2030
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
callq 0x2030
movl (%rax), %edi
callq 0x20d0
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
callq 0x41d0
movl -0x2c(%rbp), %r9d
movq -0x28(%rbp), %r10
movl $0x3, %edi
leaq 0x5b41(%rip), %rsi      # 0xa128
leaq 0x5d17(%rip), %rdx      # 0xa305
movl $0x253, %ecx            # imm = 0x253
leaq 0x5d17(%rip), %r8       # 0xa311
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x2640
jmp 0x460c
movl $0x12, -0x4(%rbp)
jmp 0x481b
movl -0x1c(%rbp), %edi
callq 0x2fc0
cmpl $0x0, %eax
je 0x467b
jmp 0x4627
movb $0x0, %al
callq 0x2560
cmpl $0x3, (%rax)
jl 0x4665
movq -0x10(%rbp), %rdi
callq 0x41d0
movq %rax, %r9
movl $0x3, %edi
leaq 0x5add(%rip), %rsi      # 0xa128
leaq 0x5cb3(%rip), %rdx      # 0xa305
movl $0x259, %ecx            # imm = 0x259
leaq 0x5cdb(%rip), %r8       # 0xa339
movb $0x0, %al
callq 0x2640
jmp 0x4667
movl -0x1c(%rbp), %edi
callq 0x23a0
movl $0x13, -0x4(%rbp)
jmp 0x481b
movl -0x1c(%rbp), %edi
callq 0x3170
cmpl $0x0, %eax
je 0x46de
jmp 0x468a
movb $0x0, %al
callq 0x2560
cmpl $0x3, (%rax)
jl 0x46c8
movq -0x10(%rbp), %rdi
callq 0x41d0
movq %rax, %r9
movl $0x3, %edi
leaq 0x5a7a(%rip), %rsi      # 0xa128
leaq 0x5c50(%rip), %rdx      # 0xa305
movl $0x260, %ecx            # imm = 0x260
leaq 0x5c98(%rip), %r8       # 0xa359
movb $0x0, %al
callq 0x2640
jmp 0x46ca
movl -0x1c(%rbp), %edi
callq 0x23a0
movl $0x14, -0x4(%rbp)
jmp 0x481b
jmp 0x46e0
jmp 0x46e2
movb $0x0, %al
callq 0x2560
cmpl $0x7, (%rax)
jl 0x4720
movq -0x10(%rbp), %rdi
callq 0x41d0
movq %rax, %r9
movl $0x7, %edi
leaq 0x5a22(%rip), %rsi      # 0xa128
leaq 0x5bf8(%rip), %rdx      # 0xa305
movl $0x267, %ecx            # imm = 0x267
leaq 0x5c5e(%rip), %r8       # 0xa377
movb $0x0, %al
callq 0x2640
jmp 0x4722
movl -0x1c(%rbp), %edi
movq -0x10(%rbp), %rsi
movq -0x10(%rbp), %rax
movl 0x80(%rax), %edx
callq 0x21c0
cmpl $0x0, %eax
je 0x480b
callq 0x2030
movl (%rax), %eax
movl %eax, -0x20(%rbp)
cmpl $0xb, -0x20(%rbp)
je 0x47f9
cmpl $0x73, -0x20(%rbp)
je 0x47f9
cmpl $0xb, -0x20(%rbp)
je 0x47f9
cmpl $0x73, -0x20(%rbp)
je 0x47f9
jmp 0x4775
movb $0x0, %al
callq 0x2560
cmpl $0x3, (%rax)
jl 0x47db
callq 0x2030
movl (%rax), %eax
movl %eax, -0x3c(%rbp)
callq 0x2030
movl (%rax), %edi
callq 0x20d0
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
callq 0x41d0
movl -0x3c(%rbp), %r9d
movq -0x38(%rbp), %r10
movl $0x3, %edi
leaq 0x5970(%rip), %rsi      # 0xa128
leaq 0x5b46(%rip), %rdx      # 0xa305
movl $0x26d, %ecx            # imm = 0x26D
leaq 0x5b5b(%rip), %r8       # 0xa326
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x2640
jmp 0x47dd
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
je 0x47f0
movl -0x1c(%rbp), %edi
callq 0x23a0
movl $0x15, -0x4(%rbp)
jmp 0x481b
movl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x481b
movl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl $0x2, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)