// File: /workspace/github_c_makefile_star2_O3/canboat[P]canboat/ip/iptee.c
// Function: ipConnect
int ipConnect(const char *host, const char *service, ConnectionType ct, struct sockaddr *sockaddr, socklen_t *socklen)
{
  int             sockfd = -1;
  int             n;
  struct addrinfo hints, *res, *addr;

  memset(&hints, 0, sizeof(hints));
  hints.ai_family   = AF_INET;
  hints.ai_socktype = (ct == ClientUDP) ? SOCK_DGRAM : SOCK_STREAM;

  n = getaddrinfo(host, service, &hints, &res);
  if (n != 0)
  {
    logError("Unable to open connection to %s:%s: %s\n", host, service, gai_strerror(n));
  }

  for (addr = res; addr; addr = addr->ai_next)
  {
    sockfd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
    if (sockfd < 0)
    {
      continue;
    }

    if (ct == ServerTCP)
    {
      if (!bind(sockfd, addr->ai_addr, addr->ai_addrlen) && !listen(sockfd, 10))
      {
        break;
      }
    }
    else if (ct == ClientTCP)
    {
      if (!connect(sockfd, addr->ai_addr, addr->ai_addrlen))
      {
        break;
      }
    }
    else /* ClientUDP */
    {
      break;
    }
    close(sockfd);
    sockfd = -1;
  }
  if (!addr)
  {
    logError("Unable to open connection to %s:%s: %s\n", host, service, strerror(errno));
  }
  else
  {
    *socklen = addr->ai_addrlen;
    memcpy(sockaddr, addr->ai_addr, addr->ai_addrlen);
  }

  freeaddrinfo(res);

  if (sockfd >= 0 && ct != ClientUDP)
  {
    unsigned long nonblock = 1;
    ioctl(sockfd, FIONBIO, &nonblock); /* Set to non-blocking */
    logInfo("Opened %s %s:%s\n", ((ct == ServerTCP) ? "server for" : "connection to"), host, service);
  }

  return sockfd;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r8, %r15
movq %rcx, %r13
movl %edx, %ebp
movq %rsi, %rbx
movq %rdi, %r14
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, 0x20(%rdx)
movl $0x2, 0x4(%rdx)
xorl %eax, %eax
cmpl $0x1, %ebp
adcl $0x1, %eax
movl %eax, 0x8(%rdx)
leaq 0x8(%rsp), %rcx
callq 0x22c0
testl %eax, %eax
je 0x24d9
movl %eax, %edi
callq 0x2110
leaq 0x1b3b(%rip), %rdi      # 0x4004
movq %r14, %rsi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2e30
movq 0x8(%rsp), %r12
testq %r12, %r12
je 0x256a
movq %r15, 0x10(%rsp)
movl 0x4(%r12), %edi
movl 0x8(%r12), %esi
movl 0xc(%r12), %edx
callq 0x2330
movl %eax, %r15d
testl %eax, %eax
js 0x255e
cmpl $0x1, %ebp
je 0x253a
cmpl $0x2, %ebp
jne 0x2595
movq 0x18(%r12), %rsi
movl 0x10(%r12), %edx
movl %r15d, %edi
callq 0x2240
testl %eax, %eax
jne 0x2550
movl %r15d, %edi
movl $0xa, %esi
callq 0x2220
jmp 0x254c
movq 0x18(%r12), %rsi
movl 0x10(%r12), %edx
movl %r15d, %edi
callq 0x22a0
testl %eax, %eax
je 0x2595
movl %r15d, %edi
callq 0x2170
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movq 0x28(%r12), %r12
testq %r12, %r12
jne 0x24ec
jmp 0x2570
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
callq 0x2060
movl (%rax), %edi
callq 0x22e0
leaq 0x1a81(%rip), %rdi      # 0x4004
movq %r14, %rsi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2e30
jmp 0x25ae
movl 0x10(%r12), %edx
movq 0x10(%rsp), %rax
movl %edx, (%rax)
movq 0x18(%r12), %rsi
movq %r13, %rdi
callq 0x21d0
movq 0x8(%rsp), %rdi
callq 0x2320
testl %ebp, %ebp
je 0x2605
testl %r15d, %r15d
js 0x2605
leaq 0x18(%rsp), %rdx
movq $0x1, (%rdx)
movl $0x5421, %esi           # imm = 0x5421
movl %r15d, %edi
xorl %eax, %eax
callq 0x2150
cmpl $0x2, %ebp
leaq 0x1a57(%rip), %rax      # 0x403d
leaq 0x1a5b(%rip), %rsi      # 0x4048
cmoveq %rax, %rsi
leaq 0x1a34(%rip), %rdi      # 0x402c
movq %r14, %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2c8c
movl %r15d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq