// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x470, %rsp            # imm = 0x470
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x38a0
movl $0xd, %edi
movl $0x1, %esi
callq 0x21b0
movl $0x0, -0x438(%rbp)
movslq -0x438(%rbp), %rax
cmpq $0x100, %rax            # imm = 0x100
jae 0x2ad0
movslq -0x438(%rbp), %rcx
leaq 0x675e(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl -0x438(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x438(%rbp)
jmp 0x2a95
jmp 0x2ad2
cmpl $0x1, -0x8(%rbp)
jle 0x2d06
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x35c4(%rip), %rsi      # 0x60af
callq 0x2050
cmpl $0x0, %eax
jne 0x2b11
leaq 0x3dc5(%rip), %rdi      # 0x68c1
leaq 0x35b5(%rip), %rsi      # 0x60b8
movb $0x0, %al
callq 0x20f0
xorl %edi, %edi
callq 0x22b0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x359e(%rip), %rsi      # 0x60be
callq 0x2050
cmpl $0x0, %eax
jne 0x2b36
movb $0x1, 0x66cf(%rip)      # 0x9200
jmp 0x2cea
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x357c(%rip), %rsi      # 0x60c1
callq 0x2050
cmpl $0x0, %eax
jne 0x2b5e
movl $0x3, %edi
callq 0x3800
jmp 0x2ce8
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x3557(%rip), %rsi      # 0x60c4
callq 0x2050
cmpl $0x0, %eax
jne 0x2b86
movl $0x1, %edi
callq 0x3800
jmp 0x2ce6
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x3532(%rip), %rsi      # 0x60c7
callq 0x2050
cmpl $0x0, %eax
jne 0x2bab
movl $0x0, -0x24(%rbp)
jmp 0x2ce4
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x3510(%rip), %rsi      # 0x60ca
callq 0x2050
cmpl $0x0, %eax
jne 0x2bd0
movl $0x1, -0x24(%rbp)
jmp 0x2ce2
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x34ee(%rip), %rsi      # 0x60cd
callq 0x2050
cmpl $0x0, %eax
jne 0x2bf5
movl $0x2, -0x24(%rbp)
jmp 0x2ce0
cmpq $0x0, -0x18(%rbp)
jne 0x2c0d
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
jmp 0x2cde
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
movslq 0xa5f0(%rip), %rcx      # 0xd210
leaq 0x65e9(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq -0x18(%rbp), %rcx
movslq 0xa5d1(%rip), %rdx      # 0xd210
leaq 0x65ca(%rip), %rax      # 0x9210
shlq $0x6, %rdx
addq %rdx, %rax
movq %rcx, 0x10(%rax)
movq -0x20(%rbp), %rcx
movslq 0xa5b4(%rip), %rdx      # 0xd210
leaq 0x65ad(%rip), %rax      # 0x9210
shlq $0x6, %rdx
addq %rdx, %rax
movq %rcx, 0x18(%rax)
movl -0x24(%rbp), %ecx
movslq 0xa598(%rip), %rdx      # 0xd210
leaq 0x6591(%rip), %rax      # 0x9210
shlq $0x6, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movslq 0xa580(%rip), %rcx      # 0xd210
leaq 0x6579(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
movb $0x1, 0x8(%rax)
movl 0xa568(%rip), %eax      # 0xd210
addl $0x1, %eax
movl %eax, 0xa55f(%rip)      # 0xd210
movslq 0xa558(%rip), %rax      # 0xd210
cmpq $0x100, %rax            # imm = 0x100
jbe 0x2cce
leaq 0x3409(%rip), %rdi      # 0x60d0
movb $0x0, %al
callq 0x36d0
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
jmp 0x2ce0
jmp 0x2ce2
jmp 0x2ce4
jmp 0x2ce6
jmp 0x2ce8
jmp 0x2cea
jmp 0x2cec
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
jmp 0x2ad2
cmpl $0x0, 0xa503(%rip)      # 0xd210
jne 0x2d31
movq 0x62e2(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x33d0(%rip), %rsi      # 0x60f0
movb $0x0, %al
callq 0x21c0
movl $0x1, %edi
callq 0x22b0
movl 0xa4d9(%rip), %esi      # 0xd210
leaq 0x388a(%rip), %rdi      # 0x65c8
movb $0x0, %al
callq 0x3420
leaq -0x430(%rbp), %rdi
movq 0x6285(%rip), %rax      # 0x8fd8
movq (%rax), %rdx
movl $0x400, %esi            # imm = 0x400
callq 0x2190
cmpq $0x0, %rax
je 0x329b
leaq -0x430(%rbp), %rdi
callq 0x20c0
movq %rax, -0x440(%rbp)
movl $0x0, -0x438(%rbp)
movl -0x438(%rbp), %eax
cmpl 0xa47d(%rip), %eax      # 0xd210
jge 0x3241
movslq -0x438(%rbp), %rcx
leaq 0x6469(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
cmpl $0x0, (%rax)
jge 0x2e70
movslq -0x438(%rbp), %rcx
leaq 0x644b(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
testb $0x1, 0x8(%rax)
je 0x2e70
movslq -0x438(%rbp), %rcx
leaq 0x642c(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdi
movslq -0x438(%rbp), %rcx
leaq 0x6413(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rsi
movslq -0x438(%rbp), %rcx
leaq 0x63fa(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
movl 0x4(%rax), %edx
movslq -0x438(%rbp), %rax
leaq 0x63e2(%rip), %rcx      # 0x9210
shlq $0x6, %rax
addq %rax, %rcx
addq $0x20, %rcx
movslq -0x438(%rbp), %rax
leaq 0x63c9(%rip), %r8       # 0x9210
shlq $0x6, %rax
addq %rax, %r8
addq $0x3c, %r8
callq 0x2450
movl %eax, %ecx
movslq -0x438(%rbp), %rdx
leaq 0x63a9(%rip), %rax      # 0x9210
shlq $0x6, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movslq -0x438(%rbp), %rcx
leaq 0x6392(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
cmpl $0x0, (%rax)
jl 0x322b
movslq -0x438(%rbp), %rcx
leaq 0x6374(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
cmpl $0x2, 0x4(%rax)
jne 0x2f8d
movslq -0x438(%rbp), %rcx
leaq 0x6355(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
movl (%rax), %edi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x22a0
movl %eax, -0x444(%rbp)
cmpl $0x0, -0x444(%rbp)
jl 0x2f88
movl $0x0, -0x448(%rbp)
movslq -0x448(%rbp), %rax
cmpq $0x100, %rax            # imm = 0x100
jae 0x2f5e
movslq -0x448(%rbp), %rcx
leaq 0x6306(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
cmpl $-0x1, (%rax)
jne 0x2f4b
movslq -0x448(%rbp), %rcx
leaq 0x62ec(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
movb 0x8(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x2f4b
movl -0x448(%rbp), %edi
movl -0x444(%rbp), %esi
callq 0x2690
jmp 0x2f5e
jmp 0x2f4d
movl -0x448(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x448(%rbp)
jmp 0x2eed
movslq -0x448(%rbp), %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x2f86
leaq 0x3671(%rip), %rdi      # 0x65e5
movb $0x0, %al
callq 0x3620
movl -0x444(%rbp), %edi
callq 0x2180
jmp 0x2f88
jmp 0x3229
movslq -0x438(%rbp), %rcx
leaq 0x6275(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
cmpl $0x1, 0x4(%rax)
jne 0x3112
movslq -0x438(%rbp), %rcx
leaq 0x6256(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
movl (%rax), %edi
leaq -0x430(%rbp), %rsi
movq -0x440(%rbp), %rdx
xorl %ecx, %ecx
callq 0x20d0
movl %eax, -0x434(%rbp)
cmpl $0x0, -0x434(%rbp)
jne 0x305e
movslq -0x438(%rbp), %rcx
leaq 0x621b(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rsi
movslq -0x438(%rbp), %rcx
leaq 0x6202(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rdx
leaq 0x35dd(%rip), %rdi      # 0x65fd
movb $0x0, %al
callq 0x3620
movslq -0x438(%rbp), %rcx
leaq 0x61db(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
movl (%rax), %edi
callq 0x2180
movslq -0x438(%rbp), %rcx
leaq 0x61bf(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
cmpl $0x0, -0x434(%rbp)
jge 0x310d
movslq -0x438(%rbp), %rcx
leaq 0x6197(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0x458(%rbp)
movslq -0x438(%rbp), %rcx
leaq 0x6177(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movq %rax, -0x450(%rbp)
callq 0x2060
movl (%rax), %edi
callq 0x22f0
movq -0x458(%rbp), %rsi
movq -0x450(%rbp), %rdx
movq %rax, %rcx
leaq 0x353c(%rip), %rdi      # 0x660b
movb $0x0, %al
callq 0x3620
movslq -0x438(%rbp), %rcx
leaq 0x612c(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
movl (%rax), %edi
callq 0x2180
movslq -0x438(%rbp), %rcx
leaq 0x6110(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
jmp 0x3227
movslq -0x438(%rbp), %rcx
leaq 0x60f0(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
movl (%rax), %edi
leaq -0x430(%rbp), %rsi
movq -0x440(%rbp), %rdx
movslq -0x438(%rbp), %rax
leaq 0x60cb(%rip), %r8       # 0x9210
shlq $0x6, %rax
addq %rax, %r8
addq $0x20, %r8
movslq -0x438(%rbp), %rcx
leaq 0x60b2(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
movl 0x3c(%rax), %r9d
xorl %ecx, %ecx
callq 0x2170
movl %eax, -0x434(%rbp)
cmpl $0x0, -0x434(%rbp)
jge 0x3225
movslq -0x438(%rbp), %rcx
leaq 0x607f(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0x468(%rbp)
movslq -0x438(%rbp), %rcx
leaq 0x605f(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movq %rax, -0x460(%rbp)
callq 0x2060
movl (%rax), %edi
callq 0x22f0
movq -0x468(%rbp), %rsi
movq -0x460(%rbp), %rdx
movq %rax, %rcx
leaq 0x3424(%rip), %rdi      # 0x660b
movb $0x0, %al
callq 0x3620
movslq -0x438(%rbp), %rcx
leaq 0x6014(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
movl (%rax), %edi
callq 0x2180
movslq -0x438(%rbp), %rcx
leaq 0x5ff8(%rip), %rax      # 0x9210
shlq $0x6, %rcx
addq %rcx, %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
jmp 0x3227
jmp 0x3229
jmp 0x322b
jmp 0x322d
movl -0x438(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x438(%rbp)
jmp 0x2d87
testb $0x1, 0x5fb8(%rip)      # 0x9200
jne 0x3296
leaq -0x430(%rbp), %rsi
leaq 0x33c7(%rip), %rdi      # 0x661f
movb $0x0, %al
callq 0x3570
leaq -0x430(%rbp), %rsi
movq -0x440(%rbp), %rdx
movl $0x1, %edi
callq 0x20a0
cmpq $-0x1, %rax
jne 0x3294
leaq 0x33a7(%rip), %rdi      # 0x662b
movb $0x0, %al
callq 0x3620
movl $0x1, -0x4(%rbp)
jmp 0x32a2
jmp 0x3296
jmp 0x2d45
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x470, %rsp            # imm = 0x470
popq %rbp
retq
nop