// File: /workspace/github_cpp_makefile_star2_O1/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), %r13d
cmpl $0x1, 0x10(%rdi)
jg 0x4983
movl %edx, %r15d
movq %rsi, %r12
movl %ecx, -0x2c(%rbp)
xorl %esi, %esi
cmpl $0x1, %r13d
setne %sil
incl %esi
xorl %r14d, %r14d
movl $0x2, %edi
xorl %edx, %edx
callq 0x20a0
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %eax, -0x34(%rbp)
testl %eax, %eax
jle 0x49af
movl 0x74(%rbx), %eax
testl %eax, %eax
movl $0x10000, %ecx          # imm = 0x10000
cmovgl %eax, %ecx
movl %ecx, -0x30(%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 0x482d
movq %r12, %rdi
callq 0x22a0
movl %eax, %r14d
movl %r14d, 0x24(%rbx)
movl $0x10, 0x30(%rbx)
leaq -0x38(%rbp), %rcx
movl $0x1, (%rcx)
movl -0x34(%rbp), %r14d
movl %r14d, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x2420
testl %eax, %eax
movl -0x2c(%rbp), %edx
js 0x4996
testl %edx, %edx
je 0x4889
leaq 0x20(%rbx), %rsi
movl %r14d, %edi
movl $0x10, %edx
callq 0x2080
movl -0x2c(%rbp), %edx
testl %eax, %eax
js 0x4996
testl %edx, %edx
sete %al
cmpl $0x1, %r13d
setne %cl
orb %al, %cl
jne 0x48b3
testl %edx, %edx
movl $0x3, %esi
cmovgl %edx, %esi
movl %r14d, %edi
callq 0x22d0
testl %eax, %eax
js 0x4996
movl %r14d, %edi
callq 0x5649
testl %eax, %eax
js 0x4996
cmpl $0x1, %r13d
jne 0x48f3
leaq -0x3c(%rbp), %rcx
movl $0x1, (%rcx)
movl %r14d, %edi
movl $0x1, %esi
movl $0x9, %edx
movl $0x4, %r8d
callq 0x2420
testl %eax, %eax
js 0x4996
cmpl $0x3, %r13d
jne 0x491f
leaq -0x40(%rbp), %rcx
movl $0x1, (%rcx)
movl %r14d, %edi
movl $0x1, %esi
movl $0x6, %edx
movl $0x4, %r8d
callq 0x2420
testl %eax, %eax
js 0x4996
movl %r14d, %edi
movl -0x30(%rbp), %esi
callq 0x5071
testl %eax, %eax
js 0x4996
movq 0x80(%rbx), %rax
testq %rax, %rax
je 0x4942
movq 0x78(%rbx), %rcx
movq %rcx, 0x50(%rax)
movq 0x78(%rbx), %rcx
leaq 0x58(%rcx), %rdx
testq %rcx, %rcx
leaq 0xb97c(%rip), %rcx      # 0x102d0
cmovneq %rdx, %rcx
movq %rax, (%rcx)
xorps %xmm0, %xmm0
movups %xmm0, 0x78(%rbx)
leaq 0x6ac2(%rip), %rdx      # 0xb42b
movq %rbx, %rdi
movl $0x3, %esi
movq %rbx, %rcx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x420f
movl %r14d, %ecx
jmp 0x49af
andl $-0x2, %r13d
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl $0x2, %r13d
jne 0x49af
movl (%rbx), %ecx
jmp 0x49af
movl %r14d, %edi
callq 0x2460
leaq 0x6aa2(%rip), %rdi      # 0xb447
callq 0x2240
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %ecx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq