// File: /workspace/github_c_makefile_star2_O0/canboat[P]canboat/n2kd/main.c
// Function: main
int main(int argc, char **argv)
{
  bool noServers = false;

  struct sigaction sa;

  setProgName(argv[0]);

  FD_ZERO(&activeSet);
  FD_ZERO(&readSet);
  FD_ZERO(&writeSet);

  outputIdx = setFdUsed(stdoutfd, DATA_OUTPUT_STREAM);

  while (argc > 1)
  {
    if (strcasecmp(argv[1], "-version") == 0)
    {
      printf("%s\n", VERSION);
      exit(0);
    }
    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], "-o") == 0)
    {
      outputIdx = setFdUsed(stdoutfd, DATA_OUTPUT_COPY);
    }
    else if (strcasecmp(argv[1], "-r") == 0)
    {
      outputIdx = setFdUsed(stdoutfd, DATA_OUTPUT_SINK);
    }
    else if (strcasecmp(argv[1], "-u") == 0 && argc > 3)
    {
      if (!parseUDPAddress(argv[2], argv[3]))
      {
        logError("Invalid UDP address + port\n");
        exit(1);
      }
      udp183 = true;
      argc -= 2;
      argv += 2;
    }
    else if (strcasecmp(argv[1], "--src-filter") == 0 && argc > 2)
    {
      srcFilter = argv[2];
      argc--, argv++;
    }
    else if (strcasecmp(argv[1], "--rate-limit") == 0)
    {
      rateLimit = true;
    }
    else if (strcasecmp(argv[1], "-p") == 0 && argc > 2)
    {
      unsigned int uPort;

      if (sscanf(argv[2], "%u", &uPort))
      {
        port = (uint16_t) uPort;
      }
      argc--, argv++;
    }
    else if (strcasecmp(argv[1], "--nmea0183") == 0)
    {
      outputIdx = setFdUsed(stdoutfd, DATA_OUTPUT_NMEA0183_STREAM);
      noServers = true;
    }
    else if (strcasecmp(argv[1], "-fixtime") == 0 && argc > 2)
    {
      setFixedTimestamp(argv[2]);
      argc--, argv++;
    }
    else
    {
      fprintf(stderr,
              "usage: n2kd [-d] [-q] [-o] [-r] [--src-filter <srclist>] [--rate-limit] [-p <port>] | -version\n\n"
              "  -d                      debug mode\n"
              "  -q                      quiet mode\n"
              "  -o                      output mode, send all TCP client data to stdout (as well as stdin)\n"
              "  -r                      restrict mode, send no data to stdout\n"
              "  --src-filter <srclist>  restrict NMEA0183 stream to particular N2K sources\n"
              "  --rate-limit            restrict NMEA0183 stream to one message per source per second\n"
              "  -p <port>               Start servers at <port> instead of 2597\n"
              "  -u <target-addr> <port> Send UDP datagrams to UDP address indicated, can be wildcard address\n"
              "  --nmea0183              Start no servers and send NMEA0183 data on stdout (this is mainly for debugging)\n"
              "  -fixtime str            Print str as timestamp in logging\n"
              "  -version                Show version number on stdout\n\n" COPYRIGHT);
      exit(1);
    }
    argc--, argv++;
  }

  // Read the first line from stdin, this must contain JSON from analyzer
  verifyStdin();
  setFdUsed(stdinfd, DATA_INPUT_STREAM);

  if (!noServers)
  {
    startTcpServers();
  }

  /*  Ignore SIGPIPE, this will let a write to a socket that's closed   */
  /*  at the other end just fail instead of raising SIGPIPE             */
  memset(&sa, 0, sizeof(sa));
  sa.sa_handler = SIG_IGN;
  sigaction(SIGPIPE, &sa, 0);

  doServerWork();

  logInfo("N2KD stopping\n");
  fflush(stdout);
  fflush(stderr);
  exit(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movb $0x0, -0x11(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x9e10
leaq 0xe2c0(%rip), %rax      # 0x11410
movq %rax, -0xc0(%rbp)
movl $0x0, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
cmpq $0x10, %rax
jae 0x3193
movq -0xc0(%rbp), %rax
movl -0xb4(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x3161
jmp 0x3195
jmp 0x3197
leaq 0xe2f2(%rip), %rax      # 0x11490
movq %rax, -0xd0(%rbp)
movl $0x0, -0xc4(%rbp)
movl -0xc4(%rbp), %eax
cmpq $0x10, %rax
jae 0x31e1
movq -0xd0(%rbp), %rax
movl -0xc4(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0x31af
jmp 0x31e3
jmp 0x31e5
leaq 0xe324(%rip), %rax      # 0x11510
movq %rax, -0xe0(%rbp)
movl $0x0, -0xd4(%rbp)
movl -0xd4(%rbp), %eax
cmpq $0x10, %rax
jae 0x322f
movq -0xe0(%rbp), %rax
movl -0xd4(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0xd4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd4(%rbp)
jmp 0x31fd
jmp 0x3231
movl $0x1, %edi
movl $0x11, %esi
callq 0x2c50
movl %eax, 0xe102(%rip)      # 0x11348
cmpl $0x1, -0x8(%rbp)
jle 0x3548
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x8f1d(%rip), %rsi      # 0xc17c
callq 0x2040
cmpl $0x0, %eax
jne 0x3285
leaq 0xa950(%rip), %rdi      # 0xdbc0
leaq 0x8f0e(%rip), %rsi      # 0xc185
movb $0x0, %al
callq 0x2120
xorl %edi, %edi
callq 0x2320
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x8ef7(%rip), %rsi      # 0xc18b
callq 0x2040
cmpl $0x0, %eax
jne 0x32ad
movl $0x3, %edi
callq 0x9d70
jmp 0x352c
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x8ed2(%rip), %rsi      # 0xc18e
callq 0x2040
cmpl $0x0, %eax
jne 0x32d5
movl $0x1, %edi
callq 0x9d70
jmp 0x352a
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x8ead(%rip), %rsi      # 0xc191
callq 0x2040
cmpl $0x0, %eax
jne 0x3308
movl $0x1, %edi
movl $0x10, %esi
callq 0x2c50
movl %eax, 0xe045(%rip)      # 0x11348
jmp 0x3528
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x8e7d(%rip), %rsi      # 0xc194
callq 0x2040
cmpl $0x0, %eax
jne 0x333b
movl $0x1, %edi
movl $0xf, %esi
callq 0x2c50
movl %eax, 0xe012(%rip)      # 0x11348
jmp 0x3526
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x8e4d(%rip), %rsi      # 0xc197
callq 0x2040
cmpl $0x0, %eax
jne 0x33ac
cmpl $0x3, -0x8(%rbp)
jle 0x33ac
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x3060
testb $0x1, %al
jne 0x338b
leaq 0x8e20(%rip), %rdi      # 0xc19a
movb $0x0, %al
callq 0x9b90
movl $0x1, %edi
callq 0x2320
movb $0x1, 0xe1fe(%rip)      # 0x11590
movl -0x8(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x10(%rbp)
jmp 0x3524
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x8dfb(%rip), %rsi      # 0xc1b6
callq 0x2040
cmpl $0x0, %eax
jne 0x33f4
cmpl $0x2, -0x8(%rbp)
jle 0x33f4
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, 0xe006(%rip)      # 0x113e0
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
jmp 0x3522
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x8dc0(%rip), %rsi      # 0xc1c3
callq 0x2040
cmpl $0x0, %eax
jne 0x3419
movb $0x1, 0xe17d(%rip)      # 0x11591
jmp 0x3520
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x8da8(%rip), %rsi      # 0xc1d0
callq 0x2040
cmpl $0x0, %eax
jne 0x3481
cmpl $0x2, -0x8(%rbp)
jle 0x3481
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
leaq 0xa3e4(%rip), %rsi      # 0xd82b
leaq -0xe4(%rbp), %rdx
movb $0x0, %al
callq 0x2260
cmpl $0x0, %eax
je 0x3467
movl -0xe4(%rbp), %eax
movw %ax, 0xdd99(%rip)       # 0x11200
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
jmp 0x351e
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x8d43(%rip), %rsi      # 0xc1d3
callq 0x2040
cmpl $0x0, %eax
jne 0x34b5
movl $0x1, %edi
movl $0x12, %esi
callq 0x2c50
movl %eax, 0xde99(%rip)      # 0x11348
movb $0x1, -0x11(%rbp)
jmp 0x351c
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x8d1a(%rip), %rsi      # 0xc1de
callq 0x2040
cmpl $0x0, %eax
jne 0x34f8
cmpl $0x2, -0x8(%rbp)
jle 0x34f8
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x9e80
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
jmp 0x351a
movq 0xdaf1(%rip), %rax      # 0x10ff0
movq (%rax), %rdi
leaq 0x8cde(%rip), %rsi      # 0xc1e7
movb $0x0, %al
callq 0x2200
movl $0x1, %edi
callq 0x2320
jmp 0x351c
jmp 0x351e
jmp 0x3520
jmp 0x3522
jmp 0x3524
jmp 0x3526
jmp 0x3528
jmp 0x352a
jmp 0x352c
jmp 0x352e
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
jmp 0x3246
callq 0x35d0
xorl %edi, %edi
movl $0xe, %esi
callq 0x2c50
testb $0x1, -0x11(%rbp)
jne 0x3564
callq 0x37f0
leaq -0xb0(%rbp), %rdi
xorl %esi, %esi
movl $0x98, %edx
callq 0x2190
movl $0x1, %eax
movq %rax, -0xb0(%rbp)
movl $0xd, %edi
leaq -0xb0(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x2080
callq 0x3950
leaq 0x926c(%rip), %rdi      # 0xc810
movb $0x0, %al
callq 0x9990
movq 0xda1e(%rip), %rax      # 0x10fd0
movq (%rax), %rdi
callq 0x2250
movq 0xda2f(%rip), %rax      # 0x10ff0
movq (%rax), %rdi
callq 0x2250
xorl %edi, %edi
callq 0x2320