// File: /workspace/github_c_makefile_star2_O1/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 0x3e97
movl %eax, %ebx
movl %eax, %edi
callq 0x33cf
testl %eax, %eax
je 0x3ef2
xorl %eax, %eax
callq 0x2e59
cmpl $0x3, (%rax)
jl 0x3f2f
leaq 0x32ae(%rip), %rsi      # 0x7128
leaq 0x33bd(%rip), %rdx      # 0x723e
leaq 0x33c1(%rip), %r8       # 0x7249
movl $0x3, %edi
movl $0x1e0, %ecx            # imm = 0x1E0
jmp 0x3f28
xorl %eax, %eax
callq 0x2e59
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x3f3b
callq 0x2030
movl (%rax), %ebp
movl %ebp, %edi
callq 0x20c0
movq %rax, %r10
subq $0x8, %rsp
leaq 0x3260(%rip), %rsi      # 0x7128
leaq 0x336f(%rip), %rdx      # 0x723e
leaq 0x3f97(%rip), %r8       # 0x7e6d
movl $0x3, %edi
movl $0x1db, %ecx            # imm = 0x1DB
movl %ebp, %r9d
xorl %eax, %eax
pushq %r10
callq 0x2e92
addq $0x10, %rsp
jmp 0x3f3b
movl %ebx, %edi
callq 0x3477
testl %eax, %eax
je 0x3f42
xorl %eax, %eax
callq 0x2e59
cmpl $0x3, (%rax)
jl 0x3f2f
leaq 0x3218(%rip), %rsi      # 0x7128
leaq 0x3327(%rip), %rdx      # 0x723e
leaq 0x3346(%rip), %r8       # 0x7264
movl $0x3, %edi
movl $0x1e6, %ecx            # imm = 0x1E6
xorl %eax, %eax
callq 0x2e92
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 0x3fa6
xorl %eax, %eax
callq 0x2e59
cmpl $0x3, (%rax)
jl 0x3f2f
movq %r14, %rdi
callq 0x35d9
callq 0x2030
movl (%rax), %r14d
movl %r14d, %edi
callq 0x20c0
movq %rax, %r10
leaq 0x31a3(%rip), %rsi      # 0x7128
leaq 0x32b2(%rip), %rdx      # 0x723e
leaq 0x32f0(%rip), %r8       # 0x7283
leaq 0x82d6(%rip), %r9       # 0xc270
movl $0x3, %edi
movl $0x1ed, %ecx            # imm = 0x1ED
jmp 0x4007
movl %ebx, %edi
movl $0x80, %esi
callq 0x2270
testl %eax, %eax
je 0x401b
xorl %eax, %eax
callq 0x2e59
cmpl $0x3, (%rax)
jl 0x3f2f
movq %r14, %rdi
callq 0x35d9
callq 0x2030
movl (%rax), %r14d
movl %r14d, %edi
callq 0x20c0
movq %rax, %r10
leaq 0x3140(%rip), %rsi      # 0x7128
leaq 0x324f(%rip), %rdx      # 0x723e
leaq 0x32ac(%rip), %r8       # 0x72a2
leaq 0x8273(%rip), %r9       # 0xc270
movl $0x3, %edi
movl $0x1f4, %ecx            # imm = 0x1F4
xorl %eax, %eax
pushq %r10
pushq %r14
callq 0x2e92
addq $0x10, %rsp
jmp 0x3f2f
movl %ebx, 0x88(%r14)
xorl %ebx, %ebx
jmp 0x3f3b