// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x1
popq %rsi
pushq $0x6
popq %rdx
callq 0x20a0
cmpl $-0x1, %eax
je 0x34ad
movl %eax, %ebx
movl %eax, %edi
callq 0x2a89
testl %eax, %eax
je 0x3502
xorl %eax, %eax
callq 0x24d9
cmpl $0x3, (%rax)
jl 0x353d
leaq 0x3cb6(%rip), %rsi      # 0x7148
leaq 0x3dc5(%rip), %rdx      # 0x725e
leaq 0x3dc9(%rip), %r8       # 0x7269
pushq $0x3
popq %rdi
movl $0x1e0, %ecx            # imm = 0x1E0
jmp 0x3536
xorl %eax, %eax
callq 0x24d9
pushq $-0x1
popq %rbx
cmpl $0x3, (%rax)
jl 0x3547
callq 0x2030
movl (%rax), %ebp
movl %ebp, %edi
callq 0x20c0
movq %rax, %r10
subq $0x8, %rsp
leaq 0x3c6c(%rip), %rsi      # 0x7148
leaq 0x3d7b(%rip), %rdx      # 0x725e
leaq 0x49a3(%rip), %r8       # 0x7e8d
pushq $0x3
popq %rdi
movl $0x1db, %ecx            # imm = 0x1DB
movl %ebp, %r9d
xorl %eax, %eax
pushq %r10
callq 0x2512
popq %rax
popq %rcx
jmp 0x3547
movl %ebx, %edi
callq 0x2b25
testl %eax, %eax
je 0x354e
xorl %eax, %eax
callq 0x24d9
cmpl $0x3, (%rax)
jl 0x353d
leaq 0x3c28(%rip), %rsi      # 0x7148
leaq 0x3d37(%rip), %rdx      # 0x725e
leaq 0x3d56(%rip), %r8       # 0x7284
pushq $0x3
popq %rdi
movl $0x1e6, %ecx            # imm = 0x1E6
xorl %eax, %eax
callq 0x2512
movl %ebx, %edi
callq 0x23b0
pushq $-0x1
popq %rbx
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 0x35b0
xorl %eax, %eax
callq 0x24d9
cmpl $0x3, (%rax)
jl 0x353d
movq %r14, %rdi
callq 0x2c66
callq 0x2030
movl (%rax), %r14d
movl %r14d, %edi
callq 0x20c0
movq %rax, %r10
leaq 0x3bb7(%rip), %rsi      # 0x7148
leaq 0x3cc6(%rip), %rdx      # 0x725e
leaq 0x3d04(%rip), %r8       # 0x72a3
leaq 0x7cca(%rip), %r9       # 0xb270
pushq $0x3
popq %rdi
movl $0x1ed, %ecx            # imm = 0x1ED
jmp 0x360f
movl %ebx, %edi
movl $0x80, %esi
callq 0x2270
testl %eax, %eax
je 0x3621
xorl %eax, %eax
callq 0x24d9
cmpl $0x3, (%rax)
jl 0x353d
movq %r14, %rdi
callq 0x2c66
callq 0x2030
movl (%rax), %r14d
movl %r14d, %edi
callq 0x20c0
movq %rax, %r10
leaq 0x3b56(%rip), %rsi      # 0x7148
leaq 0x3c65(%rip), %rdx      # 0x725e
leaq 0x3cc2(%rip), %r8       # 0x72c2
leaq 0x7c69(%rip), %r9       # 0xb270
pushq $0x3
popq %rdi
movl $0x1f4, %ecx            # imm = 0x1F4
xorl %eax, %eax
pushq %r10
pushq %r14
callq 0x2512
popq %rax
popq %rcx
jmp 0x353d
movl %ebx, 0x88(%r14)
xorl %ebx, %ebx
jmp 0x3547