// File: /workspace/github_c_makefile_star2_O3/canboat[P]canboat/ip/iptee.c
// Function: main
int main(int argc, char **argv)
{
  char          *host = 0;
  char          *port = 0;
  ConnectionType ct   = ClientUDP; // Default client type is UDP
  char           msg[LINESIZE];
  int            r;
  int            i;
  size_t         len;

  setProgName(argv[0]);

  signal(SIGPIPE, SIG_IGN);

  for (i = 0; i < sizeof(client) / sizeof(client[0]); i++)
  {
    client[i].fd = -1;
  }

  while (argc > 1)
  {
    if (strcasecmp(argv[1], "-version") == 0)
    {
      printf("%s\n", VERSION);
      exit(0);
    }
    else if (strcasecmp(argv[1], "-w") == 0)
    {
      writeonly = true;
    }
    else if (strcasecmp(argv[1], "-d") == 0)
    {
      setLogLevel(LOGLEVEL_DEBUG);
    }
    else if (strcasecmp(argv[1], "-q") == 0)
    {
      setLogLevel(LOGLEVEL_ERROR);
    }
    else if (strcasecmp(argv[1], "-u") == 0)
    {
      ct = ClientUDP;
    }
    else if (strcasecmp(argv[1], "-t") == 0)
    {
      ct = ClientTCP;
    }
    else if (strcasecmp(argv[1], "-s") == 0)
    {
      ct = ServerTCP;
    }
    else if (!host)
    {
      host = argv[1];
    }
    else
    {
      port = argv[1];

      client[clients].fd        = -1;
      client[clients].host      = host;
      client[clients].port      = port;
      client[clients].ct        = ct;
      client[clients].reconnect = true;
      clients++;
      if (clients > sizeof(client) / sizeof(client[0]))
      {
        logAbort("Too many connections requested\n");
      }
      host = 0;
      port = 0;
    }
    argc--;
    argv++;
  }

  if (!clients)
  {
    fprintf(stderr,
            "Usage: iptee [-w] [-d] [-q] [-s|-t|-u] host port [host port ...] | -version\n\n"
            "This program forwards stdin to the given TCP and UDP ports.\n"
            "Stdin is also forwarded to stdout unless -w is used.\n"
            "\n"
            "Options:\n"
            "-w - writeonly - only write to network clients/servers, not stdout\n"
            "-d - debug     - log debug information\n"
            "-q - quiet     - do not log status information\n"
            "-s - server    - host and port are a TCP server\n"
            "-u - udp       - host and port are a UDP address that data is sent to\n"
            "-t - tcp       - host and port are a TCP server that data is sent to\n" COPYRIGHT);
    exit(1);
  }
  logInfo("Sending lines to %d servers\n", clients);

  while (fgets(msg, sizeof(msg), stdin))
  {
    len = strlen(msg);

    for (i = 0; i < clients; i++)
    {
      if ((client[i].fd < 0) && (client[i].reconnect))
      {
        client[i].fd = ipConnect(client[i].host, client[i].port, client[i].ct, &client[i].sockaddr.any, &client[i].socklen);
      }
      if (client[i].fd >= 0)
      {
        if (client[i].ct == ServerTCP)
        {
          int sockfd = accept(client[i].fd, 0, 0);

          if (sockfd >= 0)
          {
            int j;

            for (j = 0; j < sizeof(client) / sizeof(client[0]); j++)
            {
              if (client[j].fd == -1 && client[j].reconnect == false)
              {
                storeNewClient(j, sockfd);
                break;
              }
            }
            if (j == sizeof(client) / sizeof(client[0]))
            {
              logError("no room for new client\n");
              close(sockfd);
            }
          }
        }
        else if (client[i].ct == ClientTCP)
        {
          r = send(client[i].fd, msg, len, 0);
          if (r == 0)
          {
            logError("EOF on %s:%s\n", client[i].host, client[i].port);
            close(client[i].fd);
            client[i].fd = -1;
          }
          if (r < 0)
          {
            logError("error on %s:%s: %s\n", client[i].host, client[i].port, strerror(errno));
            close(client[i].fd);
            client[i].fd = -1;
          }
        }
        else
        {
          r = sendto(client[i].fd, msg, len, 0, &client[i].sockaddr.any, client[i].socklen);
          if (r < 0)
          {
            logError("error on %s:%s: %s\n", client[i].host, client[i].port, strerror(errno));
            close(client[i].fd);
            client[i].fd = -1;
          }
        }
      }
    }
    if (!writeonly)
    {
      logDebug("Writing %s\n", msg);
      if (write(1, msg, len) == -1)
      {
        logError("Cannot write to stdout\n");
        return 1;
      };
    }
  }
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x418, %rsp            # imm = 0x418
movq %rsi, %rbx
movl %edi, %ebp
movq (%rsi), %rdi
callq 0x2fda
movl $0x1, %esi
movl $0xd, %edi
callq 0x21a0
xorl %eax, %eax
leaq 0x4a37(%rip), %r12      # 0x71e0
movl $0xffffffff, (%rax,%r12) # imm = 0xFFFFFFFF
addq $0x40, %rax
cmpq $0x4000, %rax           # imm = 0x4000
jne 0x27a9
cmpl $0x2, %ebp
jl 0x2909
incl %ebp
addq $0x8, %rbx
leaq 0x18eb(%rip), %r15      # 0x40be
movl $0x0, 0xc(%rsp)
xorl %r14d, %r14d
movq (%rbx), %r13
movq %r13, %rdi
leaq 0x18c4(%rip), %rsi      # 0x40af
callq 0x2050
testl %eax, %eax
je 0x2b62
movq %r13, %rdi
movq %r15, %rsi
callq 0x2050
testl %eax, %eax
je 0x2870
movq %r13, %rdi
leaq 0x18b0(%rip), %rsi      # 0x40c1
callq 0x2050
testl %eax, %eax
je 0x2879
movq %r13, %rdi
leaq 0x18a0(%rip), %rsi      # 0x40c4
callq 0x2050
testl %eax, %eax
je 0x2880
movq %r13, %rdi
leaq 0x1890(%rip), %rsi      # 0x40c7
callq 0x2050
testl %eax, %eax
je 0x289b
movq %r13, %rdi
leaq 0x1880(%rip), %rsi      # 0x40ca
callq 0x2050
testl %eax, %eax
je 0x28a5
movq %r13, %rdi
leaq 0x1870(%rip), %rsi      # 0x40cd
callq 0x2050
testl %eax, %eax
je 0x28af
testq %r14, %r14
jne 0x28b9
movq %r13, %r14
jmp 0x288a
movb $0x1, 0x4959(%rip)      # 0x71d0
jmp 0x288a
movl $0x3, %edi
jmp 0x2885
movl $0x1, %edi
callq 0x2fb1
decl %ebp
addq $0x8, %rbx
cmpl $0x2, %ebp
jg 0x27de
jmp 0x2909
movl $0x0, 0xc(%rsp)
jmp 0x288a
movl $0x1, 0xc(%rsp)
jmp 0x288a
movl $0x2, 0xc(%rsp)
jmp 0x288a
movslq 0x8920(%rip), %rax      # 0xb1e0
leal 0x1(%rax), %ecx
shlq $0x6, %rax
movl $0xffffffff, (%rax,%r12) # imm = 0xFFFFFFFF
movq %r14, 0x10(%rax,%r12)
movq %r13, 0x18(%rax,%r12)
movl 0xc(%rsp), %edx
movl %edx, 0x4(%rax,%r12)
movb $0x1, 0x8(%rax,%r12)
movl %ecx, 0x88f2(%rip)      # 0xb1e0
cmpl $0x101, %ecx            # imm = 0x101
jb 0x2904
leaq 0x17d3(%rip), %rdi      # 0x40d0
xorl %eax, %eax
callq 0x2ec3
xorl %r14d, %r14d
jmp 0x288a
movl 0x88d1(%rip), %esi      # 0xb1e0
testl %esi, %esi
je 0x2b75
leaq 0x1caa(%rip), %rdi      # 0x45c8
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2c8c
movq 0x46aa(%rip), %rax      # 0x6fd8
movq (%rax), %rdx
leaq 0x10(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x2180
testq %rax, %rax
je 0x2b4e
leaq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x20d0
movq %rax, %rbp
cmpl $0x0, 0x8880(%rip)      # 0xb1e0
jle 0x2ae7
xorl %r15d, %r15d
movq %r15, %rax
shlq $0x6, %rax
leaq (%r12,%rax), %rbx
movl (%rax,%r12), %edi
testl %edi, %edi
js 0x29e8
movl 0x4(%rbx), %eax
cmpl $0x1, %eax
je 0x2a1b
cmpl $0x2, %eax
jne 0x2a5a
xorl %esi, %esi
xorl %edx, %edx
callq 0x2280
testl %eax, %eax
js 0x2ac8
movl %eax, %r13d
leaq 0x483c(%rip), %rax      # 0x71e8
xorl %edi, %edi
cmpl $-0x1, -0x8(%rax)
jne 0x29bd
cmpb $0x0, (%rax)
je 0x2add
incq %rdi
addq $0x40, %rax
cmpq $0x100, %rdi            # imm = 0x100
jne 0x29ae
leaq 0x1c11(%rip), %rdi      # 0x45e5
xorl %eax, %eax
callq 0x2e30
movl %r13d, %edi
callq 0x2170
jmp 0x2ac8
cmpb $0x1, 0x8(%rbx)
jne 0x2ac8
movq 0x10(%rbx), %rdi
movq 0x18(%rbx), %rsi
movl 0x4(%rbx), %edx
leaq 0x20(%rbx), %rcx
leaq 0x3c(%rbx), %r8
callq 0x246c
movl %eax, %edi
movl %eax, (%rbx)
testl %eax, %eax
jns 0x297c
jmp 0x2ac8
movq %r14, %rsi
movq %rbp, %rdx
xorl %ecx, %ecx
callq 0x20e0
testl %eax, %eax
je 0x2aa5
jns 0x2ac8
movq 0x10(%rbx), %r13
movq 0x18(%rbx), %r14
callq 0x2060
movl (%rax), %edi
callq 0x22e0
leaq 0x1bbe(%rip), %rdi      # 0x460b
movq %r13, %rsi
movq %r14, %rdx
leaq 0x10(%rsp), %r14
jmp 0x2a99
leaq 0x20(%rbx), %r8
movl 0x3c(%rbx), %r9d
movq %r14, %rsi
movq %rbp, %rdx
xorl %ecx, %ecx
callq 0x2160
testl %eax, %eax
jns 0x2ac8
movq 0x10(%rbx), %r14
movq 0x18(%rbx), %r13
callq 0x2060
movl (%rax), %edi
callq 0x22e0
leaq 0x1b7d(%rip), %rdi      # 0x460b
movq %r14, %rsi
leaq 0x10(%rsp), %r14
movq %r13, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2e30
jmp 0x2abb
movq 0x10(%rbx), %rsi
movq 0x18(%rbx), %rdx
leaq 0x1b49(%rip), %rdi      # 0x45fd
xorl %eax, %eax
callq 0x2e30
movl (%rbx), %edi
callq 0x2170
movl $0xffffffff, (%rbx)     # imm = 0xFFFFFFFF
incq %r15
movslq 0x870e(%rip), %rax      # 0xb1e0
cmpq %rax, %r15
jl 0x2969
jmp 0x2ae7
movl %r13d, %esi
callq 0x2617
jmp 0x2ac8
cmpb $0x0, 0x46e2(%rip)      # 0x71d0
jne 0x2b17
leaq 0x1b28(%rip), %rdi      # 0x461f
movq %r14, %rsi
xorl %eax, %eax
callq 0x2d9d
movl $0x1, %edi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x20b0
cmpq $-0x1, %rax
je 0x2b3b
movq 0x44ba(%rip), %rax      # 0x6fd8
movq (%rax), %rdx
movq %r14, %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x2180
testq %rax, %rax
jne 0x294e
xorl %ebx, %ebx
jmp 0x2b4e
leaq 0x1ae9(%rip), %rdi      # 0x462b
xorl %eax, %eax
callq 0x2e30
movl $0x1, %ebx
movl %ebx, %eax
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x154f(%rip), %rdi      # 0x40b8
callq 0x2090
xorl %edi, %edi
callq 0x2290
callq 0x2350
nop