// File: /workspace/github_c_makefile_star2_O2/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, (%rsp)
movq %rcx, 0x8(%rsp)
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 0x10(%rsp), %r12
movq %r12, %rcx
callq 0x22c0
testl %eax, %eax
je 0x24af
movl %eax, %edi
callq 0x2110
leaq 0x1b65(%rip), %rdi      # 0x4004
movq %r14, %rsi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2d51
pushq $-0x1
popq %r13
movl %r13d, %r15d
movq (%r12), %r12
testq %r12, %r12
je 0x252e
movl 0x4(%r12), %edi
movl 0x8(%r12), %esi
movl 0xc(%r12), %edx
callq 0x2330
movl %eax, %r15d
testl %eax, %eax
js 0x2528
cmpl $0x1, %ebp
je 0x2507
cmpl $0x2, %ebp
jne 0x2553
movq 0x18(%r12), %rsi
movl 0x10(%r12), %edx
movl %r15d, %edi
callq 0x2240
testl %eax, %eax
jne 0x251d
movl %r15d, %edi
pushq $0xa
popq %rsi
callq 0x2220
jmp 0x2519
movq 0x18(%r12), %rsi
movl 0x10(%r12), %edx
movl %r15d, %edi
callq 0x22a0
testl %eax, %eax
je 0x2553
movl %r15d, %edi
callq 0x2170
movl %r13d, %r15d
addq $0x28, %r12
jmp 0x24b6
callq 0x2060
movl (%rax), %edi
callq 0x22e0
leaq 0x1ac3(%rip), %rdi      # 0x4004
movq %r14, %rsi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2d51
jmp 0x256d
movl 0x10(%r12), %edx
movq (%rsp), %rax
movl %edx, (%rax)
movq 0x18(%r12), %rsi
movq 0x8(%rsp), %rdi
callq 0x21d0
movq 0x10(%rsp), %rdi
callq 0x2320
testl %ebp, %ebp
je 0x25c4
testl %r15d, %r15d
js 0x25c4
leaq 0x18(%rsp), %rdx
movq $0x1, (%rdx)
movl $0x5421, %esi           # imm = 0x5421
movl %r15d, %edi
xorl %eax, %eax
callq 0x2150
cmpl $0x2, %ebp
leaq 0x1a98(%rip), %rax      # 0x403d
leaq 0x1a9c(%rip), %rsi      # 0x4048
cmoveq %rax, %rsi
leaq 0x1a75(%rip), %rdi      # 0x402c
movq %r14, %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2bb3
movl %r15d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq