// File: /workspace/github_c_makefile_star2_O1/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 0x2fce
movl $0x1, %esi
movl $0xd, %edi
callq 0x21a0
xorl %eax, %eax
leaq 0x4a3a(%rip), %r12      # 0x71e0
movl $0xffffffff, (%rax,%r12) # imm = 0xFFFFFFFF
addq $0x40, %rax
cmpq $0x4000, %rax           # imm = 0x4000
jne 0x27a6
cmpl $0x2, %ebp
jl 0x2906
incl %ebp
addq $0x8, %rbx
leaq 0x18ee(%rip), %r15      # 0x40be
movl $0x0, 0x8(%rsp)
xorl %r14d, %r14d
movq (%rbx), %r13
movq %r13, %rdi
leaq 0x18c7(%rip), %rsi      # 0x40af
callq 0x2050
testl %eax, %eax
je 0x2b53
movq %r13, %rdi
movq %r15, %rsi
callq 0x2050
testl %eax, %eax
je 0x286d
movq %r13, %rdi
leaq 0x18b3(%rip), %rsi      # 0x40c1
callq 0x2050
testl %eax, %eax
je 0x2876
movq %r13, %rdi
leaq 0x18a3(%rip), %rsi      # 0x40c4
callq 0x2050
testl %eax, %eax
je 0x287d
movq %r13, %rdi
leaq 0x1893(%rip), %rsi      # 0x40c7
callq 0x2050
testl %eax, %eax
je 0x2898
movq %r13, %rdi
leaq 0x1883(%rip), %rsi      # 0x40ca
callq 0x2050
testl %eax, %eax
je 0x28a2
movq %r13, %rdi
leaq 0x1873(%rip), %rsi      # 0x40cd
callq 0x2050
testl %eax, %eax
je 0x28ac
testq %r14, %r14
jne 0x28b6
movq %r13, %r14
jmp 0x2887
movb $0x1, 0x495c(%rip)      # 0x71d0
jmp 0x2887
movl $0x3, %edi
jmp 0x2882
movl $0x1, %edi
callq 0x2fa5
decl %ebp
addq $0x8, %rbx
cmpl $0x2, %ebp
jg 0x27db
jmp 0x2906
movl $0x0, 0x8(%rsp)
jmp 0x2887
movl $0x1, 0x8(%rsp)
jmp 0x2887
movl $0x2, 0x8(%rsp)
jmp 0x2887
movslq 0x8923(%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 0x8(%rsp), %edx
movl %edx, 0x4(%rax,%r12)
movb $0x1, 0x8(%rax,%r12)
movl %ecx, 0x88f5(%rip)      # 0xb1e0
cmpl $0x101, %ecx            # imm = 0x101
jb 0x2901
leaq 0x17d6(%rip), %rdi      # 0x40d0
xorl %eax, %eax
callq 0x2eb3
xorl %r14d, %r14d
jmp 0x2887
movl 0x88d4(%rip), %esi      # 0xb1e0
testl %esi, %esi
je 0x2b66
leaq 0x1cad(%rip), %rdi      # 0x45c8
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2c7c
movq 0x46ad(%rip), %rax      # 0x6fd8
movq (%rax), %rdx
leaq 0x10(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x2180
testq %rax, %rax
je 0x2b3f
leaq 0x10(%rsp), %rbx
movq %rbx, %rbp
movq %rbx, %rdi
callq 0x20d0
movq %rax, 0x8(%rsp)
cmpl $0x0, 0x887e(%rip)      # 0xb1e0
jle 0x2ad3
xorl %r15d, %r15d
movq %r15, %rax
shlq $0x6, %rax
leaq (%r12,%rax), %rbx
cmpl $0x0, (%rax,%r12)
jns 0x299d
cmpb $0x1, 0x8(%rbx)
jne 0x299d
movq 0x10(%rbx), %rdi
movq 0x18(%rbx), %rsi
movl 0x4(%rbx), %edx
leaq 0x20(%rbx), %rcx
leaq 0x3c(%rbx), %r8
callq 0x246c
movl %eax, (%rbx)
movl (%rbx), %edi
testl %edi, %edi
js 0x2aa4
movl 0x4(%rbx), %eax
cmpl $0x1, %eax
je 0x2a10
cmpl $0x2, %eax
jne 0x2a51
xorl %esi, %esi
xorl %edx, %edx
callq 0x2280
testl %eax, %eax
js 0x2aa4
movl %eax, %r13d
leaq 0x4815(%rip), %rax      # 0x71e8
xorl %r14d, %r14d
cmpl $-0x1, -0x8(%rax)
jne 0x29e5
cmpb $0x0, (%rax)
je 0x2ab9
incq %r14
addq $0x40, %rax
cmpq $0x100, %r14            # imm = 0x100
jne 0x29d6
leaq 0x1be9(%rip), %rdi      # 0x45e5
xorl %eax, %eax
callq 0x2e20
movl %r13d, %edi
callq 0x2170
jmp 0x2aa4
movq %rbp, %rsi
movq 0x8(%rsp), %rdx
xorl %ecx, %ecx
callq 0x20e0
movq %rax, %r14
testl %r14d, %r14d
jne 0x2a4a
movq 0x10(%rbx), %rsi
movq 0x18(%rbx), %rdx
leaq 0x1bc7(%rip), %rdi      # 0x45fd
xorl %eax, %eax
callq 0x2e20
movl (%rbx), %edi
callq 0x2170
movl $0xffffffff, (%rbx)     # imm = 0xFFFFFFFF
testl %r14d, %r14d
jns 0x2aa4
jmp 0x2a6c
leaq 0x20(%rbx), %r8
movl 0x3c(%rbx), %r9d
movq %rbp, %rsi
movq 0x8(%rsp), %rdx
xorl %ecx, %ecx
callq 0x2160
testl %eax, %eax
jns 0x2aa4
movq 0x10(%rbx), %r14
movq 0x18(%rbx), %r13
callq 0x2060
movl (%rax), %edi
callq 0x22e0
leaq 0x1b84(%rip), %rdi      # 0x460b
movq %r14, %rsi
movq %r13, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2e20
movl (%rbx), %edi
callq 0x2170
movl $0xffffffff, (%rbx)     # imm = 0xFFFFFFFF
incq %r15
movslq 0x8732(%rip), %rax      # 0xb1e0
cmpq %rax, %r15
jl 0x296b
jmp 0x2ad3
movl %r14d, %edi
movl %r13d, %esi
callq 0x2614
cmpq $0x100, %r14            # imm = 0x100
je 0x29f5
jmp 0x2aa4
cmpb $0x0, 0x46f6(%rip)      # 0x71d0
movq %rbp, %rbx
jne 0x2b08
leaq 0x1b39(%rip), %rdi      # 0x461f
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2d8d
movl $0x1, %edi
movq %rbx, %rsi
movq 0x8(%rsp), %rdx
callq 0x20b0
cmpq $-0x1, %rax
je 0x2b2c
movq 0x44c9(%rip), %rax      # 0x6fd8
movq (%rax), %rdx
movq %rbx, %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x2180
testq %rax, %rax
jne 0x294b
xorl %ebx, %ebx
jmp 0x2b3f
leaq 0x1af8(%rip), %rdi      # 0x462b
xorl %eax, %eax
callq 0x2e20
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 0x155e(%rip), %rdi      # 0x40b8
callq 0x2090
xorl %edi, %edi
callq 0x2290
callq 0x2350
nop