// File: 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
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x1, 0x10(%rax)
jg 0x769e
movq -0x10(%rbp), %rax
cmpl $0x1, 0x14(%rax)
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x3, 0x14(%rax)
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x28(%rbp)
movl -0x24(%rbp), %ecx
movl $0x2, %esi
movl $0x1, %eax
cmpl $0x0, %ecx
cmovnel %eax, %esi
movl $0x2, %edi
xorl %edx, %edx
callq 0x30b0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jle 0x769c
movq -0x10(%rbp), %rax
cmpl $0x0, 0x74(%rax)
jle 0x750c
movq -0x10(%rbp), %rax
movl 0x74(%rax), %eax
movl %eax, -0x3c(%rbp)
jmp 0x7516
movl $0x10000, %eax          # imm = 0x10000
movl %eax, -0x3c(%rbp)
jmp 0x7516
movl -0x3c(%rbp), %eax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movl $0x1, 0x10(%rax)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0x8a70
movl -0x2c(%rbp), %edi
callq 0x8b00
cmpl $0x0, %eax
jge 0x7549
jmp 0x7688
cmpl $0x0, -0x20(%rbp)
je 0x7569
movl -0x2c(%rbp), %edi
movq -0x10(%rbp), %rsi
addq $0x20, %rsi
callq 0x8b40
cmpl $0x0, %eax
jge 0x7569
jmp 0x7688
cmpl $0x0, -0x20(%rbp)
je 0x758a
cmpl $0x0, -0x24(%rbp)
je 0x758a
movl -0x2c(%rbp), %edi
movl -0x20(%rbp), %esi
callq 0x8b70
cmpl $0x0, %eax
jge 0x758a
jmp 0x7688
movl -0x2c(%rbp), %edi
callq 0x8bb0
cmpl $0x0, %eax
jge 0x759c
jmp 0x7688
cmpl $0x0, -0x24(%rbp)
je 0x75b4
movl -0x2c(%rbp), %edi
callq 0x8c10
cmpl $0x0, %eax
jge 0x75b4
jmp 0x7688
cmpl $0x0, -0x28(%rbp)
je 0x75cc
movl -0x2c(%rbp), %edi
callq 0x8c50
cmpl $0x0, %eax
jge 0x75cc
jmp 0x7688
movl -0x2c(%rbp), %edi
movl -0x30(%rbp), %esi
callq 0x8170
cmpl $0x0, %eax
jge 0x75e1
jmp 0x7688
callq 0x67b0
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x80(%rax)
je 0x760f
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rcx
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rax
movq %rcx, 0x50(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x78(%rax)
je 0x7633
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rcx
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movq %rcx, 0x58(%rax)
jmp 0x7646
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x78(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x80(%rax)
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rcx
movl -0x2c(%rbp), %r8d
movl $0x3, %esi
leaq 0xbd7e(%rip), %rdx      # 0x133f7
movb $0x0, %al
callq 0x6c30
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x76c6
movl -0x2c(%rbp), %edi
callq 0x3570
leaq 0xbd7c(%rip), %rdi      # 0x13413
callq 0x32b0
jmp 0x76bf
movq -0x10(%rbp), %rax
cmpl $0x2, 0x14(%rax)
je 0x76b2
movq -0x10(%rbp), %rax
cmpl $0x3, 0x14(%rax)
jne 0x76bd
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0x76c6
jmp 0x76bf
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nop