// File: /workspace/github_c_makefile_star2_O3/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 0x7746
xorps %xmm0, %xmm0
movups %xmm0, 0xaf45(%rip)     # 0xe400
movups %xmm0, 0xaf2e(%rip)     # 0xe3f0
movups %xmm0, 0xaf17(%rip)     # 0xe3e0
movups %xmm0, 0xaf00(%rip)     # 0xe3d0
movups %xmm0, 0xaee9(%rip)     # 0xe3c0
movups %xmm0, 0xaed2(%rip)     # 0xe3b0
movups %xmm0, 0xaebb(%rip)     # 0xe3a0
movups %xmm0, 0xaea4(%rip)     # 0xe390
movups %xmm0, 0xaf1d(%rip)     # 0xe410
movups %xmm0, 0xaf26(%rip)     # 0xe420
movups %xmm0, 0xaf2f(%rip)     # 0xe430
movups %xmm0, 0xaf38(%rip)     # 0xe440
movups %xmm0, 0xaf41(%rip)     # 0xe450
movups %xmm0, 0xaf4a(%rip)     # 0xe460
movups %xmm0, 0xaf53(%rip)     # 0xe470
movups %xmm0, 0xaf5c(%rip)     # 0xe480
movups %xmm0, 0xaf65(%rip)     # 0xe490
movups %xmm0, 0xaf6e(%rip)     # 0xe4a0
movups %xmm0, 0xaf77(%rip)     # 0xe4b0
movups %xmm0, 0xaf80(%rip)     # 0xe4c0
movups %xmm0, 0xaf89(%rip)     # 0xe4d0
movups %xmm0, 0xaf92(%rip)     # 0xe4e0
movups %xmm0, 0xaf9b(%rip)     # 0xe4f0
movups %xmm0, 0xafa4(%rip)     # 0xe500
movl $0x1, %edi
movl $0x11, %esi
callq 0x3222
movl %eax, 0xadc7(%rip)      # 0xe338
cmpl $0x2, %ebx
jl 0x3770
movl $0x0, 0xc(%rsp)
leaq 0x5c43(%rip), %r14      # 0x91cc
leaq 0x5c4b(%rip), %r15      # 0x91db
movq 0x8(%r12), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x2040
testl %eax, %eax
je 0x39e8
movq %r13, %rdi
movq %r15, %rsi
callq 0x2040
testl %eax, %eax
je 0x362f
movq %r13, %rdi
leaq 0x5c1d(%rip), %rsi      # 0x91de
callq 0x2040
testl %eax, %eax
je 0x3636
movq %r13, %rdi
leaq 0x5c0d(%rip), %rsi      # 0x91e1
callq 0x2040
testl %eax, %eax
je 0x3642
movq %r13, %rdi
leaq 0x5bfd(%rip), %rsi      # 0x91e4
callq 0x2040
testl %eax, %eax
je 0x364e
movq %r13, %rdi
leaq 0x5bed(%rip), %rsi      # 0x91e7
callq 0x2040
cmpl $0x4, %ebx
jb 0x3680
testl %eax, %eax
jne 0x3680
movq 0x10(%r12), %rdi
movq 0x18(%r12), %rsi
callq 0x3437
testb %al, %al
je 0x39fb
addq $0x10, %r12
movb $0x1, 0xaee6(%rip)      # 0xe510
addl $-0x2, %ebx
jmp 0x3663
movl $0x3, %edi
jmp 0x363b
movl $0x1, %edi
callq 0x771d
jmp 0x3663
movl $0x1, %edi
movl $0x10, %esi
jmp 0x3658
movl $0x1, %edi
movl $0xf, %esi
callq 0x3222
movl %eax, 0xacd5(%rip)      # 0xe338
movq %r12, %rbp
leal -0x1(%rbx), %eax
addq $0x8, %rbp
movq %rbp, %r12
cmpl $0x2, %ebx
movl %eax, %ebx
jg 0x3590
jmp 0x3778
leaq 0x8(%r12), %rbp
movq %r13, %rdi
leaq 0x5b77(%rip), %rsi      # 0x9206
callq 0x2040
cmpl $0x2, %ebx
je 0x36ad
testl %eax, %eax
jne 0x36ad
movq 0x10(%r12), %rax
movq %rax, 0xacb7(%rip)      # 0xe360
decl %ebx
jmp 0x3666
movq %r13, %rdi
leaq 0x5b5c(%rip), %rsi      # 0x9213
callq 0x2040
testl %eax, %eax
je 0x3702
movq %r13, %rdi
leaq 0x5b56(%rip), %rsi      # 0x9220
callq 0x2040
cmpl $0x2, %ebx
je 0x370e
testl %eax, %eax
jne 0x370e
movq 0x10(%r12), %rdi
leaq 0x7177(%rip), %rsi      # 0xa85b
leaq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x2240
testl %eax, %eax
je 0x36a9
movzwl 0x10(%rsp), %eax
movw %ax, 0xaaf0(%rip)       # 0xe1f0
jmp 0x36a9
movb $0x1, 0xae08(%rip)      # 0xe511
jmp 0x3663
movq %r13, %rdi
leaq 0x5b0b(%rip), %rsi      # 0x9223
callq 0x2040
testl %eax, %eax
je 0x3750
movq %r13, %rdi
leaq 0x5b03(%rip), %rsi      # 0x922e
callq 0x2040
cmpl $0x2, %ebx
je 0x3a13
testl %eax, %eax
jne 0x3a13
movq 0x10(%r12), %rdi
callq 0x777a
jmp 0x36a9
movl $0x1, %edi
movl $0x12, %esi
callq 0x3222
movl %eax, 0xabd3(%rip)      # 0xe338
movb $0x1, %al
movl %eax, 0xc(%rsp)
jmp 0x3663
movl $0x0, 0xc(%rsp)
leaq 0xada1(%rip), %r12      # 0xe520
leaq 0xadb2(%rip), %r14      # 0xe538
leaq 0x6574(%rip), %r15      # 0x9d01
movl 0xad8d(%rip), %edi      # 0xe520
movq 0x12db6(%rip), %rax     # 0x16550
leaq (%rax,%r12), %rsi
addq $0x18, %rsi
movl $0x7fff, %edx           # imm = 0x7FFF
callq 0x21b0
testq %rax, %rax
jle 0x37d8
addq 0x12d98(%rip), %rax     # 0x16550
movq %rax, 0x12d91(%rip)     # 0x16550
movb $0x0, (%r14,%rax)
movq %r14, %rdi
movl $0xa, %esi
callq 0x2110
testq %rax, %rax
je 0x378d
jmp 0x37e4
movq %r15, %rdi
xorl %eax, %eax
callq 0x762f
jmp 0x378d
movq %rax, %rbx
leaq 0xad4a(%rip), %r14      # 0xe538
leaq 0x64a7(%rip), %rsi      # 0x9c9c
movq %r14, %rdi
callq 0x2370
testq %rax, %rax
je 0x381a
leaq 0xad2f(%rip), %rdi      # 0xe538
leaq 0x6497(%rip), %rsi      # 0x9ca7
callq 0x2370
testq %rax, %rax
jne 0x3828
leaq 0x649e(%rip), %rdi      # 0x9cbf
xorl %eax, %eax
callq 0x762f
movq 0x12d21(%rip), %r15     # 0x16550
addq %r14, %r15
incq %rbx
subq %rbx, %r15
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x2200
movq %r15, 0x12d03(%rip)     # 0x16550
xorl %edi, %edi
movl $0xe, %esi
callq 0x3222
testb $0x1, 0xc(%rsp)
jne 0x3988
movzwl 0xa985(%rip), %edi      # 0xe1f0
movl $0x9, %esi
callq 0x4253
movzwl 0xa974(%rip), %esi      # 0xe1f0
leaq 0x64ac(%rip), %rdi      # 0x9d2f
xorl %eax, %eax
callq 0x73f8
movzwl 0xa95f(%rip), %edi      # 0xe1f0
incl %edi
movl $0xffff, %ebp           # imm = 0xFFFF
andl %ebp, %edi
movl $0xa, %esi
callq 0x4253
movzwl 0xa945(%rip), %esi      # 0xe1f0
incl %esi
leaq 0x64a1(%rip), %rdi      # 0x9d55
xorl %eax, %eax
callq 0x73f8
movzbl 0xac4e(%rip), %eax      # 0xe510
movzwl 0xa927(%rip), %edi      # 0xe1f0
addl $0x2, %edi
leal 0xb(%rax), %esi
leaq 0x64ac(%rip), %rcx      # 0x9d82
leaq 0x64d6(%rip), %rbx      # 0x9db3
testl %eax, %eax
cmovneq %rcx, %rbx
andl %ebp, %edi
callq 0x4253
movzwl 0xa8ff(%rip), %esi      # 0xe1f0
addl $0x2, %esi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x73f8
movzwl 0xa8eb(%rip), %edi      # 0xe1f0
addl $0x3, %edi
andl %ebp, %edi
movl $0x8, %esi
callq 0x4253
movzwl 0xa8d5(%rip), %esi      # 0xe1f0
addl $0x3, %esi
leaq 0x64b8(%rip), %rdi      # 0x9ddd
xorl %eax, %eax
callq 0x73f8
movzwl 0xa8bd(%rip), %edi      # 0xe1f0
addl $0x4, %edi
andl %ebp, %edi
movl $0x7, %esi
callq 0x4253
movzwl 0xa8a7(%rip), %esi      # 0xe1f0
addl $0x4, %esi
leaq 0x64b8(%rip), %rdi      # 0x9e0b
xorl %eax, %eax
callq 0x73f8
movzwl 0xa88f(%rip), %edi      # 0xe1f0
addl $0x5, %edi
andl %ebp, %edi
movl $0xd, %esi
callq 0x4253
movzwl 0xa879(%rip), %esi      # 0xe1f0
addl $0x5, %esi
leaq 0x64af(%rip), %rdi      # 0x9e30
xorl %eax, %eax
callq 0x73f8
leaq 0x18(%rsp), %rbx
movl $0x90, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2170
movq $0x1, -0x8(%rbx)
leaq 0x10(%rsp), %rsi
movl $0xd, %edi
xorl %edx, %edx
callq 0x2090
callq 0x3a35
leaq 0x5e9f(%rip), %rdi      # 0x9860
xorl %eax, %eax
callq 0x73f8
movq 0xa601(%rip), %rax      # 0xdfd0
movq (%rax), %rdi
callq 0x2230
movq 0xa612(%rip), %rax      # 0xdff0
movq (%rax), %rdi
callq 0x2230
jmp 0x39f4
leaq 0x57e6(%rip), %rdi      # 0x91d5
callq 0x2080
xorl %edi, %edi
callq 0x2300
leaq 0x57e8(%rip), %rdi      # 0x91ea
xorl %eax, %eax
callq 0x759c
movl $0x1, %edi
callq 0x2300
movq 0xa5d6(%rip), %rax      # 0xdff0
movq (%rax), %rcx
leaq 0x5813(%rip), %rdi      # 0x9237
movl $0x628, %esi            # imm = 0x628
movl $0x1, %edx
callq 0x2320
jmp 0x3a09