// File: /workspace/github_cpp_makefile_star2_O3/xocoder[P]m_kcptun/vendor/m_net/src/mnet_core.c
// Function: chann_open_socket(s_mchann*, char const*, int, int)
static int
_chann_open_socket(chann_t *n, const char *host, int port, int backlog) {
   if (n->state <= CHANN_STATE_DISCONNECT) {
      int istcp = n->type == CHANN_TYPE_STREAM;
      int isbc = n->type == CHANN_TYPE_BROADCAST;
      int fd = socket(AF_INET, istcp ? SOCK_STREAM : SOCK_DGRAM, 0);
      if (fd > 0) {
         int buf_size = n->buf_size>0 ? n->buf_size : MNET_BUF_SIZE;
         n->state = CHANN_STATE_DISCONNECT;

         _chann_fill_addr(n, host, port);

         if (_set_reuseaddr(fd) < 0) goto fail;
         if (backlog && _bind(fd, &n->addr) < 0) goto fail;
         if (backlog && istcp && _listen(fd,backlog) < 0) goto fail;
         if (_set_nonblocking(fd) < 0) goto fail;
         if (istcp && _set_keepalive(fd)<0) goto fail;
         if (isbc && _set_broadcast(fd)<0) goto fail;
         if (_set_bufsize(fd, buf_size) < 0) goto fail;
#if (MNET_OS_MACOX || MNET_OS_LINUX || MNET_OS_FreeBSD)
         {
            mnet_t *ss = _gmnet();
            if (n->del_next) { n->del_next->prev = n->del_prev; }
            if (n->del_prev) { n->del_prev->next = n->del_next; }
            else { ss->del_channs = n->del_next; }
            n->del_next = n->del_prev = NULL;
         }
#endif
         mm_log(n, MNET_LOG_VERBOSE, "open socket chann:%p fd:%d\n", n, fd);
         return fd;

        fail:
         close(fd);
         perror("chann open socket: ");
      }
   }
   else if (n->type==CHANN_TYPE_DGRAM || n->type==CHANN_TYPE_BROADCAST) {
      return n->fd;
   }
   return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movl 0x14(%rdi), %r14d
cmpl $0x1, 0x10(%rdi)
jg 0x4860
movl %edx, %r15d
movq %rsi, %r12
movl %ecx, -0x30(%rbp)
xorl %esi, %esi
cmpl $0x1, %r14d
setne %sil
incl %esi
xorl %r13d, %r13d
movl $0x2, %edi
xorl %edx, %edx
callq 0x20a0
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %eax, -0x34(%rbp)
testl %eax, %eax
jle 0x495e
movl 0x74(%rbx), %eax
testl %eax, %eax
movl $0x10000, %ecx          # imm = 0x10000
cmovgl %eax, %ecx
movl %ecx, -0x2c(%rbp)
movl $0x1, 0x10(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rbx)
movw $0x2, 0x20(%rbx)
rolw $0x8, %r15w
movw %r15w, 0x22(%rbx)
testq %r12, %r12
je 0x47d2
movq %r12, %rdi
callq 0x22b0
movl %eax, %r13d
movl %r13d, 0x24(%rbx)
movl $0x10, 0x30(%rbx)
leaq -0x38(%rbp), %rcx
movl $0x1, (%rcx)
movl -0x34(%rbp), %r15d
movl %r15d, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x2430
testl %eax, %eax
movl -0x30(%rbp), %r12d
js 0x4945
testl %r12d, %r12d
je 0x487a
leaq 0x20(%rbx), %rsi
movl %r15d, %edi
movl $0x10, %edx
callq 0x2080
testl %eax, %eax
js 0x4945
cmpl $0x1, %r14d
jne 0x48a9
testl %r12d, %r12d
movl $0x3, %esi
cmovgl %r12d, %esi
movl %r15d, %edi
callq 0x22e0
testl %eax, %eax
js 0x4945
movl %r15d, %edi
callq 0x55bc
testl %eax, %eax
jns 0x4890
jmp 0x4945
andl $-0x2, %r14d
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl $0x2, %r14d
jne 0x495e
movl (%rbx), %ecx
jmp 0x495e
movl %r15d, %edi
callq 0x55bc
testl %eax, %eax
js 0x4945
cmpl $0x1, %r14d
jne 0x48b9
leaq -0x3c(%rbp), %rcx
movl $0x1, (%rcx)
movl %r15d, %edi
movl $0x1, %esi
movl $0x9, %edx
jmp 0x48d6
movl %r15d, %edi
callq 0x55bc
testl %eax, %eax
js 0x4945
cmpl $0x3, %r14d
jne 0x48e5
leaq -0x40(%rbp), %rcx
movl $0x1, (%rcx)
movl %r15d, %edi
movl $0x1, %esi
movl $0x6, %edx
movl $0x4, %r8d
callq 0x2430
testl %eax, %eax
js 0x4945
movl %r15d, %edi
movl -0x2c(%rbp), %esi
callq 0x500c
testl %eax, %eax
js 0x4945
movq 0x78(%rbx), %rcx
movq 0x80(%rbx), %rax
testq %rax, %rax
je 0x4908
movq %rcx, 0x50(%rax)
leaq 0x58(%rcx), %rdx
testq %rcx, %rcx
leaq 0xb9ba(%rip), %rcx      # 0x102d0
cmovneq %rdx, %rcx
movq %rax, (%rcx)
xorps %xmm0, %xmm0
movups %xmm0, 0x78(%rbx)
leaq 0x6b00(%rip), %rdx      # 0xb42b
movq %rbx, %rdi
movl $0x3, %esi
movq %rbx, %rcx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x41bd
movl %r15d, %ecx
jmp 0x495e
movl %r15d, %edi
callq 0x2470
leaq 0x6af3(%rip), %rdi      # 0xb447
callq 0x2250
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %ecx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq