// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rsi, %r12
movl %edi, %ebx
movq (%rsi), %rdi
callq 0x778a
xorps %xmm0, %xmm0
movups %xmm0, 0xaf4a(%rip)     # 0xe400
movups %xmm0, 0xaf33(%rip)     # 0xe3f0
movups %xmm0, 0xaf1c(%rip)     # 0xe3e0
movups %xmm0, 0xaf05(%rip)     # 0xe3d0
movups %xmm0, 0xaeee(%rip)     # 0xe3c0
movups %xmm0, 0xaed7(%rip)     # 0xe3b0
movups %xmm0, 0xaec0(%rip)     # 0xe3a0
movups %xmm0, 0xaea9(%rip)     # 0xe390
movups %xmm0, 0xaf22(%rip)     # 0xe410
movups %xmm0, 0xaf2b(%rip)     # 0xe420
movups %xmm0, 0xaf34(%rip)     # 0xe430
movups %xmm0, 0xaf3d(%rip)     # 0xe440
movups %xmm0, 0xaf46(%rip)     # 0xe450
movups %xmm0, 0xaf4f(%rip)     # 0xe460
movups %xmm0, 0xaf58(%rip)     # 0xe470
movups %xmm0, 0xaf61(%rip)     # 0xe480
movups %xmm0, 0xaf6a(%rip)     # 0xe490
movups %xmm0, 0xaf73(%rip)     # 0xe4a0
movups %xmm0, 0xaf7c(%rip)     # 0xe4b0
movups %xmm0, 0xaf85(%rip)     # 0xe4c0
movups %xmm0, 0xaf8e(%rip)     # 0xe4d0
movups %xmm0, 0xaf97(%rip)     # 0xe4e0
movups %xmm0, 0xafa0(%rip)     # 0xe4f0
movups %xmm0, 0xafa9(%rip)     # 0xe500
movl $0x1, %edi
movl $0x11, %esi
callq 0x3211
movl %eax, 0xadcc(%rip)      # 0xe338
cmpl $0x2, %ebx
jl 0x376b
movl $0x0, 0xc(%rsp)
leaq 0x5c38(%rip), %r14      # 0x91bc
leaq 0x5c40(%rip), %r15      # 0x91cb
movq 0x8(%r12), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x2040
testl %eax, %eax
je 0x39e0
movq %r13, %rdi
movq %r15, %rsi
callq 0x2040
testl %eax, %eax
je 0x362a
movq %r13, %rdi
leaq 0x5c12(%rip), %rsi      # 0x91ce
callq 0x2040
testl %eax, %eax
je 0x3631
movq %r13, %rdi
leaq 0x5c02(%rip), %rsi      # 0x91d1
callq 0x2040
testl %eax, %eax
je 0x363d
movq %r13, %rdi
leaq 0x5bf2(%rip), %rsi      # 0x91d4
callq 0x2040
testl %eax, %eax
je 0x3649
movq %r13, %rdi
leaq 0x5be2(%rip), %rsi      # 0x91d7
callq 0x2040
cmpl $0x4, %ebx
jb 0x367b
testl %eax, %eax
jne 0x367b
movq 0x10(%r12), %rdi
movq 0x18(%r12), %rsi
callq 0x3421
testb %al, %al
je 0x39f3
addq $0x10, %r12
movb $0x1, 0xaeeb(%rip)      # 0xe510
addl $-0x2, %ebx
jmp 0x365e
movl $0x3, %edi
jmp 0x3636
movl $0x1, %edi
callq 0x7761
jmp 0x365e
movl $0x1, %edi
movl $0x10, %esi
jmp 0x3653
movl $0x1, %edi
movl $0xf, %esi
callq 0x3211
movl %eax, 0xacda(%rip)      # 0xe338
movq %r12, %rbp
leal -0x1(%rbx), %eax
addq $0x8, %rbp
movq %rbp, %r12
cmpl $0x2, %ebx
movl %eax, %ebx
jg 0x358b
jmp 0x3773
leaq 0x8(%r12), %rbp
movq %r13, %rdi
leaq 0x5b6c(%rip), %rsi      # 0x91f6
callq 0x2040
cmpl $0x2, %ebx
je 0x36a8
testl %eax, %eax
jne 0x36a8
movq 0x10(%r12), %rax
movq %rax, 0xacbc(%rip)      # 0xe360
decl %ebx
jmp 0x3661
movq %r13, %rdi
leaq 0x5b51(%rip), %rsi      # 0x9203
callq 0x2040
testl %eax, %eax
je 0x36fd
movq %r13, %rdi
leaq 0x5b4b(%rip), %rsi      # 0x9210
callq 0x2040
cmpl $0x2, %ebx
je 0x3709
testl %eax, %eax
jne 0x3709
movq 0x10(%r12), %rdi
leaq 0x717c(%rip), %rsi      # 0xa85b
leaq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x2240
testl %eax, %eax
je 0x36a4
movzwl 0x10(%rsp), %eax
movw %ax, 0xaaf5(%rip)       # 0xe1f0
jmp 0x36a4
movb $0x1, 0xae0d(%rip)      # 0xe511
jmp 0x365e
movq %r13, %rdi
leaq 0x5b00(%rip), %rsi      # 0x9213
callq 0x2040
testl %eax, %eax
je 0x374b
movq %r13, %rdi
leaq 0x5af8(%rip), %rsi      # 0x921e
callq 0x2040
cmpl $0x2, %ebx
je 0x3a0b
testl %eax, %eax
jne 0x3a0b
movq 0x10(%r12), %rdi
callq 0x77d3
jmp 0x36a4
movl $0x1, %edi
movl $0x12, %esi
callq 0x3211
movl %eax, 0xabd8(%rip)      # 0xe338
movb $0x1, %al
movl %eax, 0xc(%rsp)
jmp 0x365e
movl $0x0, 0xc(%rsp)
leaq 0xada6(%rip), %r12      # 0xe520
leaq 0xadb7(%rip), %r14      # 0xe538
leaq 0x6569(%rip), %r15      # 0x9cf1
movl 0xad92(%rip), %edi      # 0xe520
movq 0x12dbb(%rip), %rax     # 0x16550
leaq (%rax,%r12), %rsi
addq $0x18, %rsi
movl $0x7fff, %edx           # imm = 0x7FFF
callq 0x21b0
testq %rax, %rax
jle 0x37d3
addq 0x12d9d(%rip), %rax     # 0x16550
movq %rax, 0x12d96(%rip)     # 0x16550
movb $0x0, (%r14,%rax)
movq %r14, %rdi
movl $0xa, %esi
callq 0x2110
testq %rax, %rax
je 0x3788
jmp 0x37df
movq %r15, %rdi
xorl %eax, %eax
callq 0x766f
jmp 0x3788
movq %rax, %rbx
leaq 0xad4f(%rip), %r14      # 0xe538
leaq 0x649c(%rip), %rsi      # 0x9c8c
movq %r14, %rdi
callq 0x2370
testq %rax, %rax
je 0x3815
leaq 0xad34(%rip), %rdi      # 0xe538
leaq 0x648c(%rip), %rsi      # 0x9c97
callq 0x2370
testq %rax, %rax
jne 0x3823
leaq 0x6493(%rip), %rdi      # 0x9caf
xorl %eax, %eax
callq 0x766f
movq 0x12d26(%rip), %r15     # 0x16550
addq %r14, %r15
incq %rbx
subq %rbx, %r15
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x2200
movq %r15, 0x12d08(%rip)     # 0x16550
xorl %edi, %edi
movl $0xe, %esi
callq 0x3211
testb $0x1, 0xc(%rsp)
jne 0x3983
movzwl 0xa98a(%rip), %edi      # 0xe1f0
movl $0x9, %esi
callq 0x42cc
movzwl 0xa979(%rip), %esi      # 0xe1f0
leaq 0x64a1(%rip), %rdi      # 0x9d1f
xorl %eax, %eax
callq 0x7438
movzwl 0xa964(%rip), %edi      # 0xe1f0
incl %edi
movl $0xffff, %ebp           # imm = 0xFFFF
andl %ebp, %edi
movl $0xa, %esi
callq 0x42cc
movzwl 0xa94a(%rip), %esi      # 0xe1f0
incl %esi
leaq 0x6496(%rip), %rdi      # 0x9d45
xorl %eax, %eax
callq 0x7438
movzbl 0xac53(%rip), %eax      # 0xe510
movzwl 0xa92c(%rip), %edi      # 0xe1f0
addl $0x2, %edi
leal 0xb(%rax), %esi
leaq 0x64a1(%rip), %rcx      # 0x9d72
leaq 0x64cb(%rip), %rbx      # 0x9da3
testl %eax, %eax
cmovneq %rcx, %rbx
andl %ebp, %edi
callq 0x42cc
movzwl 0xa904(%rip), %esi      # 0xe1f0
addl $0x2, %esi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x7438
movzwl 0xa8f0(%rip), %edi      # 0xe1f0
addl $0x3, %edi
andl %ebp, %edi
movl $0x8, %esi
callq 0x42cc
movzwl 0xa8da(%rip), %esi      # 0xe1f0
addl $0x3, %esi
leaq 0x64ad(%rip), %rdi      # 0x9dcd
xorl %eax, %eax
callq 0x7438
movzwl 0xa8c2(%rip), %edi      # 0xe1f0
addl $0x4, %edi
andl %ebp, %edi
movl $0x7, %esi
callq 0x42cc
movzwl 0xa8ac(%rip), %esi      # 0xe1f0
addl $0x4, %esi
leaq 0x64ad(%rip), %rdi      # 0x9dfb
xorl %eax, %eax
callq 0x7438
movzwl 0xa894(%rip), %edi      # 0xe1f0
addl $0x5, %edi
andl %ebp, %edi
movl $0xd, %esi
callq 0x42cc
movzwl 0xa87e(%rip), %esi      # 0xe1f0
addl $0x5, %esi
leaq 0x64a4(%rip), %rdi      # 0x9e20
xorl %eax, %eax
callq 0x7438
leaq 0x10(%rsp), %rbx
movl $0x98, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2170
movq $0x1, (%rbx)
movl $0xd, %edi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x2090
callq 0x3a2d
leaq 0x5e97(%rip), %rdi      # 0x9850
xorl %eax, %eax
callq 0x7438
movq 0xa609(%rip), %rax      # 0xdfd0
movq (%rax), %rdi
callq 0x2230
movq 0xa61a(%rip), %rax      # 0xdff0
movq (%rax), %rdi
callq 0x2230
jmp 0x39ec
leaq 0x57de(%rip), %rdi      # 0x91c5
callq 0x2080
xorl %edi, %edi
callq 0x2300
leaq 0x57e0(%rip), %rdi      # 0x91da
xorl %eax, %eax
callq 0x75dc
movl $0x1, %edi
callq 0x2300
movq 0xa5de(%rip), %rax      # 0xdff0
movq (%rax), %rcx
leaq 0x580b(%rip), %rdi      # 0x9227
movl $0x628, %esi            # imm = 0x628
movl $0x1, %edx
callq 0x2320
jmp 0x3a01