// File: /workspace/github_c_makefile_star2_O3/GangZhuo[P]dohclient/src/netutils.c
// Function: tcp_listen
int tcp_listen(listen_t* ctx)
{
	sockaddr_t* addr;
	struct sockaddr* sockaddr;
	sock_t sock;

	addr = &ctx->addr;
	sockaddr = (struct sockaddr*)(&addr->addr);

	sock = socket(sockaddr->sa_family, SOCK_STREAM, IPPROTO_TCP);

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

	if (setnonblock(sock) != 0) {
		loge("set sock non-block failed\n");
		close(sock);
		return -1;
	}

	if (setreuseaddr(sock) != 0) {
		loge("set sock reuse address failed\n");
		close(sock);
		return -1;
	}

	if (bind(sock, sockaddr, addr->addrlen) != 0) {
		loge("bind() error: %s errno=%d, %s\n",
			get_sockaddrname(addr), errno, strerror(errno));
		close(sock);
		return -1;
	}

	if (listen(sock, LISTEN_BACKLOG) != 0) {
		loge("listen() error: %s errno=%d, %s\n",
			get_sockaddrname(addr), errno, strerror(errno));
		close(sock);
		return -1;
	}

	ctx->sock = sock;

	return 0;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
movq %rdi, %r14
movzwl (%rdi), %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x20a0
cmpl $-0x1, %eax
je 0x3e87
movl %eax, %ebx
movl %eax, %edi
callq 0x33cf
testl %eax, %eax
je 0x3ee2
xorl %eax, %eax
callq 0x2e49
cmpl $0x3, (%rax)
jl 0x3f1f
leaq 0x32be(%rip), %rsi      # 0x7128
leaq 0x33cd(%rip), %rdx      # 0x723e
leaq 0x33d1(%rip), %r8       # 0x7249
movl $0x3, %edi
movl $0x1e0, %ecx            # imm = 0x1E0
jmp 0x3f18
xorl %eax, %eax
callq 0x2e49
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x3f2b
callq 0x2030
movl (%rax), %ebp
movl %ebp, %edi
callq 0x20c0
movq %rax, %r10
subq $0x8, %rsp
leaq 0x3270(%rip), %rsi      # 0x7128
leaq 0x337f(%rip), %rdx      # 0x723e
leaq 0x3fa7(%rip), %r8       # 0x7e6d
movl $0x3, %edi
movl $0x1db, %ecx            # imm = 0x1DB
movl %ebp, %r9d
xorl %eax, %eax
pushq %r10
callq 0x2e82
addq $0x10, %rsp
jmp 0x3f2b
movl %ebx, %edi
callq 0x3477
testl %eax, %eax
je 0x3f32
xorl %eax, %eax
callq 0x2e49
cmpl $0x3, (%rax)
jl 0x3f1f
leaq 0x3228(%rip), %rsi      # 0x7128
leaq 0x3337(%rip), %rdx      # 0x723e
leaq 0x3356(%rip), %r8       # 0x7264
movl $0x3, %edi
movl $0x1e6, %ecx            # imm = 0x1E6
xorl %eax, %eax
callq 0x2e82
movl %ebx, %edi
callq 0x23b0
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
popq %rbx
popq %r14
popq %rbp
retq
movl 0x80(%r14), %edx
movl %ebx, %edi
movq %r14, %rsi
callq 0x2070
testl %eax, %eax
je 0x3f96
xorl %eax, %eax
callq 0x2e49
cmpl $0x3, (%rax)
jl 0x3f1f
movq %r14, %rdi
callq 0x35d9
callq 0x2030
movl (%rax), %r14d
movl %r14d, %edi
callq 0x20c0
movq %rax, %r10
leaq 0x31b3(%rip), %rsi      # 0x7128
leaq 0x32c2(%rip), %rdx      # 0x723e
leaq 0x3300(%rip), %r8       # 0x7283
leaq 0x82e6(%rip), %r9       # 0xc270
movl $0x3, %edi
movl $0x1ed, %ecx            # imm = 0x1ED
jmp 0x3ff7
movl %ebx, %edi
movl $0x80, %esi
callq 0x2270
testl %eax, %eax
je 0x400b
xorl %eax, %eax
callq 0x2e49
cmpl $0x3, (%rax)
jl 0x3f1f
movq %r14, %rdi
callq 0x35d9
callq 0x2030
movl (%rax), %r14d
movl %r14d, %edi
callq 0x20c0
movq %rax, %r10
leaq 0x3150(%rip), %rsi      # 0x7128
leaq 0x325f(%rip), %rdx      # 0x723e
leaq 0x32bc(%rip), %r8       # 0x72a2
leaq 0x8283(%rip), %r9       # 0xc270
movl $0x3, %edi
movl $0x1f4, %ecx            # imm = 0x1F4
xorl %eax, %eax
pushq %r10
pushq %r14
callq 0x2e82
addq $0x10, %rsp
jmp 0x3f1f
movl %ebx, 0x88(%r14)
xorl %ebx, %ebx
jmp 0x3f2b