// File: /workspace/github_c_makefile_star2_O2/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 0x2ef5
pushq $0xd
popq %rdi
pushq $0x1
popq %rsi
callq 0x21a0
xorl %eax, %eax
leaq 0x4a91(%rip), %r12      # 0x71e0
cmpq $0x4000, %rax           # imm = 0x4000
je 0x2762
orl $-0x1, (%rax,%r12)
addq $0x40, %rax
jmp 0x274f
addq $0x8, %rbx
leaq 0x1951(%rip), %r15      # 0x40be
xorl %r14d, %r14d
movl $0x0, 0xc(%rsp)
cmpl $0x2, %ebp
jl 0x2898
movq (%rbx), %r13
movq %r13, %rdi
leaq 0x1921(%rip), %rsi      # 0x40af
callq 0x2050
testl %eax, %eax
je 0x2a92
movq %r13, %rdi
movq %r15, %rsi
callq 0x2050
testl %eax, %eax
je 0x2813
movq %r13, %rdi
leaq 0x190d(%rip), %rsi      # 0x40c1
callq 0x2050
testl %eax, %eax
je 0x281c
movq %r13, %rdi
leaq 0x18fd(%rip), %rsi      # 0x40c4
callq 0x2050
testl %eax, %eax
je 0x2820
movq %r13, %rdi
leaq 0x18ed(%rip), %rsi      # 0x40c7
callq 0x2050
testl %eax, %eax
je 0x2833
movq %r13, %rdi
leaq 0x18dd(%rip), %rsi      # 0x40ca
callq 0x2050
testl %eax, %eax
je 0x283d
movq %r13, %rdi
leaq 0x18cd(%rip), %rsi      # 0x40cd
callq 0x2050
testl %eax, %eax
je 0x2841
testq %r14, %r14
jne 0x284a
movq %r13, %r14
jmp 0x2828
movb $0x1, 0x49b6(%rip)      # 0x71d0
jmp 0x2828
pushq $0x3
jmp 0x2822
pushq $0x1
popq %rdi
callq 0x2ece
decl %ebp
addq $0x8, %rbx
jmp 0x2778
movl $0x0, 0xc(%rsp)
jmp 0x2828
pushq $0x1
jmp 0x2843
pushq $0x2
popq %rax
movl %eax, 0xc(%rsp)
jmp 0x2828
movslq 0x898f(%rip), %rax      # 0xb1e0
movq %rax, %rcx
shlq $0x6, %rcx
orl $-0x1, (%rcx,%r12)
incl %eax
movq %r14, 0x10(%rcx,%r12)
movq %r13, 0x18(%rcx,%r12)
movl 0xc(%rsp), %edx
movl %edx, 0x4(%rcx,%r12)
movb $0x1, 0x8(%rcx,%r12)
movl %eax, 0x8962(%rip)      # 0xb1e0
cmpl $0x101, %eax            # imm = 0x101
jb 0x2893
leaq 0x1844(%rip), %rdi      # 0x40d0
xorl %eax, %eax
callq 0x2de2
xorl %r14d, %r14d
jmp 0x2828
movl 0x8942(%rip), %esi      # 0xb1e0
testl %esi, %esi
je 0x2aa5
leaq 0x1d1b(%rip), %rdi      # 0x45c8
xorl %eax, %eax
callq 0x2bb3
leaq 0x10(%rsp), %r15
movq 0x4718(%rip), %rax      # 0x6fd8
movq (%rax), %rdx
movq %r15, %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x2180
testq %rax, %rax
je 0x2a7e
movq %r15, %rdi
callq 0x20d0
movq %rax, %rbp
xorl %r13d, %r13d
movslq 0x88f2(%rip), %rax      # 0xb1e0
cmpq %rax, %r13
jge 0x2a35
movq %r13, %rax
shlq $0x6, %rax
leaq (%r12,%rax), %r14
movl (%rax,%r12), %edi
testl %edi, %edi
js 0x295d
movl 0x4(%r14), %eax
cmpl $0x1, %eax
je 0x298f
cmpl $0x2, %eax
jne 0x29b8
xorl %esi, %esi
xorl %edx, %edx
callq 0x2280
testl %eax, %eax
js 0x2a2d
movl %eax, %ebx
leaq 0x48b2(%rip), %rax      # 0x71e8
xorl %edi, %edi
cmpq $0x100, %rdi            # imm = 0x100
je 0x2a0f
cmpl $-0x1, -0x8(%rax)
jne 0x2954
cmpb $0x0, (%rax)
je 0x2a26
incq %rdi
addq $0x40, %rax
jmp 0x2938
cmpb $0x1, 0x8(%r14)
jne 0x2a2d
movq 0x10(%r14), %rdi
movq 0x18(%r14), %rsi
movl 0x4(%r14), %edx
leaq 0x20(%r14), %rcx
leaq 0x3c(%r14), %r8
callq 0x243c
movl %eax, %edi
movl %eax, (%r14)
testl %eax, %eax
jns 0x290a
jmp 0x2a2d
movq %r15, %rsi
movq %rbp, %rdx
xorl %ecx, %ecx
callq 0x20e0
testl %eax, %eax
jne 0x29cf
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
leaq 0x1c4e(%rip), %rdi      # 0x45fd
xorl %eax, %eax
callq 0x2d51
jmp 0x2a01
leaq 0x20(%r14), %r8
movl 0x3c(%r14), %r9d
movq %r15, %rsi
movq %rbp, %rdx
xorl %ecx, %ecx
callq 0x2160
testl %eax, %eax
jns 0x2a2d
movq 0x10(%r14), %rbx
movq 0x18(%r14), %r15
callq 0x2060
movl (%rax), %edi
callq 0x22e0
leaq 0x1c1f(%rip), %rdi      # 0x460b
movq %rbx, %rsi
movq %r15, %rdx
leaq 0x10(%rsp), %r15
movq %rax, %rcx
xorl %eax, %eax
callq 0x2d51
movl (%r14), %edi
callq 0x2170
orl $-0x1, (%r14)
jmp 0x2a2d
leaq 0x1bcf(%rip), %rdi      # 0x45e5
xorl %eax, %eax
callq 0x2d51
movl %ebx, %edi
callq 0x2170
jmp 0x2a2d
movl %ebx, %esi
callq 0x25d6
incq %r13
jmp 0x28e7
cmpb $0x0, 0x4794(%rip)      # 0x71d0
jne 0x28b9
leaq 0x1bd6(%rip), %rdi      # 0x461f
movq %r15, %rsi
xorl %eax, %eax
callq 0x2cc0
pushq $0x1
popq %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x20b0
cmpq $-0x1, %rax
jne 0x28b9
leaq 0x1bb9(%rip), %rdi      # 0x462b
xorl %eax, %eax
callq 0x2d51
pushq $0x1
popq %rax
jmp 0x2a80
xorl %eax, %eax
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x161f(%rip), %rdi      # 0x40b8
callq 0x2090
xorl %edi, %edi
callq 0x2290
movq 0x454c(%rip), %rax      # 0x6ff8
movq (%rax), %rcx
leaq 0x163a(%rip), %rdi      # 0x40f0
pushq $0x1
popq %rdx
movl $0x4d7, %esi            # imm = 0x4D7
callq 0x22b0
pushq $0x1
popq %rdi
callq 0x2290
nop