// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movzwl (%rax), %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x20b0
movl %eax, -0x24(%rbp)
cmpl $-0x1, -0x24(%rbp)
jne 0x3fdb
jmp 0x3f7d
movb $0x0, %al
callq 0x2560
cmpl $0x3, (%rax)
jl 0x3fcd
callq 0x2030
movl (%rax), %eax
movl %eax, -0x28(%rbp)
callq 0x2030
movl (%rax), %edi
callq 0x20d0
movl -0x28(%rbp), %r9d
movl $0x3, %edi
leaq 0x6179(%rip), %rsi      # 0xa128
leaq 0x6288(%rip), %rdx      # 0xa23e
movl $0x1db, %ecx            # imm = 0x1DB
leaq 0x6f8b(%rip), %r8       # 0xaf4d
movq %rax, (%rsp)
movb $0x0, %al
callq 0x2640
jmp 0x3fcf
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41c3
movl -0x24(%rbp), %edi
callq 0x2fc0
cmpl $0x0, %eax
je 0x4032
jmp 0x3fea
movb $0x0, %al
callq 0x2560
cmpl $0x3, (%rax)
jl 0x401c
movl $0x3, %edi
leaq 0x6126(%rip), %rsi      # 0xa128
leaq 0x6235(%rip), %rdx      # 0xa23e
movl $0x1e0, %ecx            # imm = 0x1E0
leaq 0x6234(%rip), %r8       # 0xa249
movb $0x0, %al
callq 0x2640
jmp 0x401e
movl -0x24(%rbp), %edi
callq 0x23a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41c3
movl -0x24(%rbp), %edi
callq 0x30d0
cmpl $0x0, %eax
je 0x4089
jmp 0x4041
movb $0x0, %al
callq 0x2560
cmpl $0x3, (%rax)
jl 0x4073
movl $0x3, %edi
leaq 0x60cf(%rip), %rsi      # 0xa128
leaq 0x61de(%rip), %rdx      # 0xa23e
movl $0x1e6, %ecx            # imm = 0x1E6
leaq 0x61f8(%rip), %r8       # 0xa264
movb $0x0, %al
callq 0x2640
jmp 0x4075
movl -0x24(%rbp), %edi
callq 0x23a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41c3
movl -0x24(%rbp), %edi
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rax
movl 0x80(%rax), %edx
callq 0x2080
cmpl $0x0, %eax
je 0x4122
jmp 0x40a6
movb $0x0, %al
callq 0x2560
cmpl $0x3, (%rax)
jl 0x410c
movq -0x18(%rbp), %rdi
callq 0x41d0
movq %rax, -0x38(%rbp)
callq 0x2030
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
callq 0x2030
movl (%rax), %edi
callq 0x20d0
movq -0x38(%rbp), %r9
movl -0x2c(%rbp), %r10d
movl $0x3, %edi
leaq 0x603f(%rip), %rsi      # 0xa128
leaq 0x614e(%rip), %rdx      # 0xa23e
movl $0x1ed, %ecx            # imm = 0x1ED
leaq 0x6187(%rip), %r8       # 0xa283
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x2640
jmp 0x410e
movl -0x24(%rbp), %edi
callq 0x23a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41c3
movl -0x24(%rbp), %edi
movl $0x80, %esi
callq 0x2270
cmpl $0x0, %eax
je 0x41af
jmp 0x4136
movb $0x0, %al
callq 0x2560
cmpl $0x3, (%rax)
jl 0x419c
movq -0x18(%rbp), %rdi
callq 0x41d0
movq %rax, -0x48(%rbp)
callq 0x2030
movl (%rax), %eax
movl %eax, -0x3c(%rbp)
callq 0x2030
movl (%rax), %edi
callq 0x20d0
movq -0x48(%rbp), %r9
movl -0x3c(%rbp), %r10d
movl $0x3, %edi
leaq 0x5faf(%rip), %rsi      # 0xa128
leaq 0x60be(%rip), %rdx      # 0xa23e
movl $0x1f4, %ecx            # imm = 0x1F4
leaq 0x6116(%rip), %r8       # 0xa2a2
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x2640
jmp 0x419e
movl -0x24(%rbp), %edi
callq 0x23a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41c3
movl -0x24(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x88(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)