// File: /workspace/github_cpp_makefile_star2_O2/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 0x455e
movl %edx, %r15d
movq %rsi, %r12
movl %ecx, -0x30(%rbp)
xorl %esi, %esi
cmpl $0x1, %r14d
setne %sil
incl %esi
pushq $0x2
popq %rdi
xorl %r13d, %r13d
xorl %edx, %edx
callq 0x2090
pushq $-0x1
popq %rcx
movl %eax, -0x34(%rbp)
testl %eax, %eax
jle 0x460e
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 0x44c4
movq %r12, %rdi
callq 0x22a0
movl %eax, %r13d
movl %r13d, 0x24(%rbx)
movl $0x10, 0x30(%rbx)
leaq -0x38(%rbp), %rcx
movl $0x1, (%rcx)
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
pushq $0x4
popq %r8
movl -0x34(%rbp), %r15d
movl %r15d, %edi
callq 0x2400
testl %eax, %eax
movl -0x30(%rbp), %r12d
js 0x45f7
testl %r12d, %r12d
je 0x4537
leaq 0x20(%rbx), %rsi
pushq $0x10
popq %rdx
movl %r15d, %edi
callq 0x2070
testl %eax, %eax
js 0x45f7
cmpl $0x1, %r14d
jne 0x4537
testl %r12d, %r12d
pushq $0x3
popq %rsi
cmovgl %r12d, %esi
movl %r15d, %edi
callq 0x22d0
testl %eax, %eax
js 0x45f7
movl %r15d, %edi
callq 0x522d
testl %eax, %eax
js 0x45f7
cmpl $0x1, %r14d
jne 0x4573
leaq -0x3c(%rbp), %rcx
movl $0x1, (%rcx)
pushq $0x1
popq %rsi
pushq $0x9
jmp 0x4588
andl $-0x2, %r14d
cmpl $0x2, %r14d
jne 0x460b
movl (%rbx), %ecx
jmp 0x460e
cmpl $0x3, %r14d
jne 0x4599
leaq -0x40(%rbp), %rcx
movl $0x1, (%rcx)
pushq $0x1
popq %rsi
pushq $0x6
popq %rdx
pushq $0x4
popq %r8
movl %r15d, %edi
callq 0x2400
testl %eax, %eax
js 0x45f7
movl %r15d, %edi
movl -0x2c(%rbp), %esi
callq 0x4c90
testl %eax, %eax
js 0x45f7
movq 0x78(%rbx), %rcx
movq 0x80(%rbx), %rax
testq %rax, %rax
je 0x45bc
movq %rcx, 0x50(%rax)
leaq 0x58(%rcx), %rdx
testq %rcx, %rcx
leaq 0xacf6(%rip), %rcx      # 0xf2c0
cmovneq %rdx, %rcx
movq %rax, (%rcx)
xorps %xmm0, %xmm0
movups %xmm0, 0x78(%rbx)
leaq 0x5e4c(%rip), %rdx      # 0xa42b
pushq $0x3
popq %rsi
movq %rbx, %rdi
movq %rbx, %rcx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x3eb5
movl %r15d, %ecx
jmp 0x460e
movl %r15d, %edi
callq 0x2440
leaq 0x5e41(%rip), %rdi      # 0xa447
callq 0x2240
pushq $-0x1
popq %rcx
movl %ecx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq