// File: /workspace/github_c_makefile_star2_O2/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, %r13
movl %edi, %ebx
movq (%rsi), %rdi
callq 0x6fad
xorl %eax, %eax
leaq 0x9fa8(%rip), %rcx      # 0xd3a0
cmpq $0x80, %rax
je 0x340b
andq $0x0, (%rax,%rcx)
addq $0x8, %rax
jmp 0x33f8
xorl %eax, %eax
leaq 0xa00c(%rip), %rcx      # 0xd420
cmpq $0x80, %rax
je 0x3427
andq $0x0, (%rax,%rcx)
addq $0x8, %rax
jmp 0x3414
xorl %eax, %eax
leaq 0xa070(%rip), %rcx      # 0xd4a0
cmpq $0x80, %rax
je 0x3443
andq $0x0, (%rax,%rcx)
addq $0x8, %rax
jmp 0x3430
pushq $0x1
popq %rdi
pushq $0x11
popq %rsi
callq 0x31bc
movl %eax, 0x9ef4(%rip)      # 0xd348
movl $0x0, 0xc(%rsp)
leaq 0x4d69(%rip), %r14      # 0x81cc
leaq 0x4d71(%rip), %r15      # 0x81db
cmpl $0x1, %ebx
jle 0x362e
movq 0x8(%r13), %rbp
movq %rbp, %rdi
movq %r14, %rsi
callq 0x2040
testl %eax, %eax
je 0x3890
movq %rbp, %rdi
movq %r15, %rsi
callq 0x2040
testl %eax, %eax
je 0x350f
movq %rbp, %rdi
leaq 0x4d3b(%rip), %rsi      # 0x81de
callq 0x2040
testl %eax, %eax
je 0x3513
movq %rbp, %rdi
leaq 0x4d2b(%rip), %rsi      # 0x81e1
callq 0x2040
testl %eax, %eax
je 0x351d
movq %rbp, %rdi
leaq 0x4d1b(%rip), %rsi      # 0x81e4
callq 0x2040
testl %eax, %eax
je 0x3524
movq %rbp, %rdi
leaq 0x4d0b(%rip), %rsi      # 0x81e7
callq 0x2040
cmpl $0x4, %ebx
jb 0x3546
testl %eax, %eax
jne 0x3546
movq 0x10(%r13), %rdi
movq 0x18(%r13), %rsi
callq 0x337e
testb %al, %al
je 0x38a3
addq $0x10, %r13
movb $0x1, 0xa016(%rip)      # 0xd520
addl $-0x2, %ebx
jmp 0x3535
pushq $0x3
jmp 0x3515
pushq $0x1
popq %rdi
callq 0x6f86
jmp 0x3535
pushq $0x1
popq %rdi
pushq $0x10
jmp 0x3529
pushq $0x1
popq %rdi
pushq $0xf
popq %rsi
callq 0x31bc
movl %eax, 0x9e13(%rip)      # 0xd348
movq %r13, %r12
decl %ebx
addq $0x8, %r12
movq %r12, %r13
jmp 0x346a
leaq 0x8(%r13), %r12
movq %rbp, %rdi
leaq 0x4cb2(%rip), %rsi      # 0x8206
callq 0x2040
cmpl $0x2, %ebx
je 0x3571
testl %eax, %eax
jne 0x3571
movq 0x10(%r13), %rax
movq %rax, 0x9e03(%rip)      # 0xd370
decl %ebx
jmp 0x3538
movq %rbp, %rdi
leaq 0x4c98(%rip), %rsi      # 0x8213
callq 0x2040
testl %eax, %eax
je 0x35c5
movq %rbp, %rdi
leaq 0x4c92(%rip), %rsi      # 0x8220
callq 0x2040
cmpl $0x2, %ebx
je 0x35d1
testl %eax, %eax
jne 0x35d1
movq 0x10(%r13), %rdi
leaq 0x62b4(%rip), %rsi      # 0x985b
leaq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x2240
testl %eax, %eax
je 0x356d
movzwl 0x10(%rsp), %eax
movw %ax, 0x9c3d(%rip)       # 0xd200
jmp 0x356d
movb $0x1, 0x9f55(%rip)      # 0xd521
jmp 0x3535
movq %rbp, %rdi
leaq 0x4c48(%rip), %rsi      # 0x8223
callq 0x2040
testl %eax, %eax
je 0x3612
movq %rbp, %rdi
leaq 0x4c40(%rip), %rsi      # 0x822e
callq 0x2040
cmpl $0x2, %ebx
je 0x38b9
testl %eax, %eax
jne 0x38b9
movq 0x10(%r13), %rdi
callq 0x6fdd
jmp 0x356d
pushq $0x1
popq %rdi
pushq $0x12
popq %rsi
callq 0x31bc
movl %eax, 0x9d25(%rip)      # 0xd348
movb $0x1, %al
movl %eax, 0xc(%rsp)
jmp 0x3535
leaq 0x9efb(%rip), %r12      # 0xd530
leaq 0x9f0c(%rip), %r14      # 0xd548
pushq $0xa
popq %rbp
leaq 0x56bb(%rip), %r15      # 0x8d01
movl 0x9ee4(%rip), %edi      # 0xd530
movq 0x11f0d(%rip), %rax     # 0x15560
leaq (%rax,%r12), %rsi
addq $0x18, %rsi
movl $0x7fff, %edx           # imm = 0x7FFF
callq 0x21b0
testq %rax, %rax
jle 0x368e
addq 0x11eef(%rip), %rax     # 0x15560
movq %rax, 0x11ee8(%rip)     # 0x15560
movb $0x0, (%r14,%rax)
movq %r14, %rdi
movl %ebp, %esi
callq 0x2110
testq %rax, %rax
je 0x3646
jmp 0x369a
movq %r15, %rdi
xorl %eax, %eax
callq 0x6e9a
jmp 0x3646
movq %rax, %rbx
leaq 0x9ea4(%rip), %r14      # 0xd548
leaq 0x55f1(%rip), %rsi      # 0x8c9c
movq %r14, %rdi
callq 0x2380
testq %rax, %rax
je 0x36d0
leaq 0x9e89(%rip), %rdi      # 0xd548
leaq 0x55e1(%rip), %rsi      # 0x8ca7
callq 0x2380
testq %rax, %rax
jne 0x36de
leaq 0x55e8(%rip), %rdi      # 0x8cbf
xorl %eax, %eax
callq 0x6e9a
movq 0x11e7b(%rip), %r15     # 0x15560
addq %r14, %r15
incq %rbx
subq %rbx, %r15
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x2200
movq %r15, 0x11e5d(%rip)     # 0x15560
pushq $0xe
popq %rsi
xorl %edi, %edi
callq 0x31bc
testb $0x1, 0xc(%rsp)
jne 0x3832
movzwl 0x9ae1(%rip), %edi      # 0xd200
pushq $0x9
popq %rsi
callq 0x40a0
movzwl 0x9ad2(%rip), %esi      # 0xd200
leaq 0x55fa(%rip), %rdi      # 0x8d2f
xorl %eax, %eax
callq 0x6c6b
movzwl 0x9abd(%rip), %edi      # 0xd200
incl %edi
movl $0xffff, %ebp           # imm = 0xFFFF
andl %ebp, %edi
pushq $0xa
popq %rsi
callq 0x40a0
movzwl 0x9aa5(%rip), %esi      # 0xd200
incl %esi
leaq 0x55f1(%rip), %rdi      # 0x8d55
xorl %eax, %eax
callq 0x6c6b
movzbl 0x9dae(%rip), %eax      # 0xd520
movzwl 0x9a87(%rip), %edi      # 0xd200
addl $0x2, %edi
leal 0xb(%rax), %esi
leaq 0x55fc(%rip), %rcx      # 0x8d82
leaq 0x5626(%rip), %rbx      # 0x8db3
testl %eax, %eax
cmovneq %rcx, %rbx
andl %ebp, %edi
callq 0x40a0
movzwl 0x9a5f(%rip), %esi      # 0xd200
addl $0x2, %esi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x6c6b
movzwl 0x9a4b(%rip), %edi      # 0xd200
addl $0x3, %edi
andl %ebp, %edi
pushq $0x8
popq %rsi
callq 0x40a0
movzwl 0x9a37(%rip), %esi      # 0xd200
addl $0x3, %esi
leaq 0x560a(%rip), %rdi      # 0x8ddd
xorl %eax, %eax
callq 0x6c6b
movzwl 0x9a1f(%rip), %edi      # 0xd200
addl $0x4, %edi
andl %ebp, %edi
pushq $0x7
popq %rsi
callq 0x40a0
movzwl 0x9a0b(%rip), %esi      # 0xd200
addl $0x4, %esi
leaq 0x560c(%rip), %rdi      # 0x8e0b
xorl %eax, %eax
callq 0x6c6b
movzwl 0x99f3(%rip), %edi      # 0xd200
addl $0x5, %edi
andl %ebp, %edi
pushq $0xd
popq %rsi
callq 0x40a0
movzwl 0x99df(%rip), %esi      # 0xd200
addl $0x5, %esi
leaq 0x5605(%rip), %rdi      # 0x8e30
xorl %eax, %eax
callq 0x6c6b
leaq 0x18(%rsp), %rbx
movl $0x90, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2170
movq $0x1, -0x8(%rbx)
pushq $0xd
popq %rdi
leaq 0x10(%rsp), %rsi
xorl %edx, %edx
callq 0x2090
callq 0x38d9
leaq 0x4ff7(%rip), %rdi      # 0x8860
xorl %eax, %eax
callq 0x6c6b
movq 0x9759(%rip), %rax      # 0xcfd0
movq (%rax), %rdi
callq 0x2230
movq 0x976a(%rip), %rax      # 0xcff0
movq (%rax), %rdi
callq 0x2230
jmp 0x389c
leaq 0x493e(%rip), %rdi      # 0x81d5
callq 0x2080
xorl %edi, %edi
callq 0x2310
leaq 0x4940(%rip), %rdi      # 0x81ea
xorl %eax, %eax
callq 0x6e09
pushq $0x1
popq %rdi
callq 0x2310
movq 0x9730(%rip), %rax      # 0xcff0
movq (%rax), %rcx
leaq 0x496d(%rip), %rdi      # 0x8237
pushq $0x1
popq %rdx
movl $0x628, %esi            # imm = 0x628
callq 0x2330
jmp 0x38b1