// File: /workspace/github_c_makefile_star2_O1/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, 0x8(%rsp)
movq %rcx, 0x10(%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)
movq %rsp, %rcx
callq 0x22c0
testl %eax, %eax
je 0x24db
movl %eax, %edi
callq 0x2110
leaq 0x1b39(%rip), %rdi      # 0x4004
movq %r14, %rsi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2e20
movq (%rsp), %r15
testq %r15, %r15
sete %r13b
je 0x255e
movl 0x4(%r15), %edi
movl 0x8(%r15), %esi
movl 0xc(%r15), %edx
callq 0x2330
movl %eax, %r12d
testl %eax, %eax
js 0x254f
cmpl $0x1, %ebp
je 0x252d
cmpl $0x2, %ebp
jne 0x2564
movq 0x18(%r15), %rsi
movl 0x10(%r15), %edx
movl %r12d, %edi
callq 0x2240
testl %eax, %eax
jne 0x2541
movl %r12d, %edi
movl $0xa, %esi
callq 0x2220
jmp 0x253d
movq 0x18(%r15), %rsi
movl 0x10(%r15), %edx
movl %r12d, %edi
callq 0x22a0
testl %eax, %eax
je 0x2564
movl %r12d, %edi
callq 0x2170
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq 0x28(%r15), %r15
testq %r15, %r15
sete %r13b
jne 0x24e8
jmp 0x2564
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testb $0x1, %r13b
je 0x258f
callq 0x2060
movl (%rax), %edi
callq 0x22e0
leaq 0x1a87(%rip), %rdi      # 0x4004
movq %r14, %rsi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2e20
jmp 0x25ac
movl 0x10(%r15), %eax
movq 0x8(%rsp), %rcx
movl %eax, (%rcx)
movq 0x18(%r15), %rsi
movl 0x10(%r15), %edx
movq 0x10(%rsp), %rdi
callq 0x21d0
movq (%rsp), %rdi
callq 0x2320
testl %ebp, %ebp
je 0x2602
testl %r12d, %r12d
js 0x2602
leaq 0x18(%rsp), %rdx
movq $0x1, (%rdx)
movl $0x5421, %esi           # imm = 0x5421
movl %r12d, %edi
xorl %eax, %eax
callq 0x2150
cmpl $0x2, %ebp
leaq 0x1a5a(%rip), %rax      # 0x403d
leaq 0x1a5e(%rip), %rsi      # 0x4048
cmoveq %rax, %rsi
leaq 0x1a37(%rip), %rdi      # 0x402c
movq %r14, %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2c7c
movl %r12d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq