// File: /workspace/github_c_makefile_star2_O1/canboat[P]canboat/ikonvert-serial/ikonvert-serial.c
// Function: main
int main(int argc, char **argv)
{
  int            handle;
  struct termios attr;
  char          *name   = argv[0];
  char          *device = 0;
  struct stat    statbuf;
  int            ac = argc;
  char         **av = argv;

  setProgName(av[0]);
  while (ac > 1)
  {
    if (strcasecmp(av[1], "-version") == 0)
    {
      printf("%s\n", VERSION);
      exit(0);
    }
    else if (strcasecmp(av[1], "-w") == 0)
    {
      writeonly = true;
    }
    else if (strcasecmp(av[1], "-p") == 0)
    {
      passthru = true;
    }
    else if (strcasecmp(av[1], "-r") == 0)
    {
      readonly = true;
    }
    else if (strcasecmp(av[1], "-v") == 0)
    {
      verbose = true;
    }
    else if (strcasecmp(av[1], "-x") == 0)
    {
      hexMode = true;
    }
    else if (strcasecmp(av[1], "--rate-limit-off") == 0 || strcasecmp(argv[1], "-l") == 0)
    {
      rate_limit_off = true;
    }
    else if (strcasecmp(av[1], "-rx") == 0 && ac > 2)
    {
      ac--;
      av++;
      if (sbGetLength(&rxList) > 0)
      {
        sbAppendString(&rxList, ",");
      }
      sbAppendFormat(&rxList, "%s", av[1]);
    }
    else if (strcasecmp(av[1], "-tx") == 0 && ac > 2)
    {
      ac--;
      av++;
      if (sbGetLength(&txList) > 0)
      {
        sbAppendString(&txList, ",");
      }
      sbAppendFormat(&txList, "%s", av[1]);
    }
    else if (strcasecmp(av[1], "-t") == 0 && ac > 2)
    {
      ac--;
      av++;
      timeout = strtol(av[1], 0, 10);
      logDebug("timeout set to %ld seconds\n", timeout);
    }
    else if (strcasecmp(av[1], "-reset") == 0 && ac > 2)
    {
      ac--;
      av++;
      resetTimeout = strtol(av[1], 0, 10);
      logDebug("reset timeout set to %ld seconds\n", resetTimeout);
    }
    else if (strcasecmp(av[1], "-s") == 0 && ac > 2)
    {
      ac--;
      av++;
      speed = strtol(av[1], 0, 10);
      switch (speed)
      {
        case 38400:
          baudRate = B38400;
          break;
        case 57600:
          baudRate = B57600;
          break;
        case 115200:
          baudRate = B115200;
          break;
        case 230400:
          baudRate = B230400;
          break;
#ifdef B460800
        case 460800:
          baudRate = B460800;
          break;
#endif
#ifdef B921600
        case 921600:
          baudRate = B921600;
          break;
#endif
        default:
          device = 0;
          break;
      }
      logDebug("speed set to %d (%d) baud\n", speed, baudRate);
    }
    else if (strcasecmp(av[1], "-d") == 0)
    {
      setLogLevel(LOGLEVEL_DEBUG);
    }
    else if (!device)
    {
      device = av[1];
    }
    else
    {
      device = 0;
      break;
    }
    ac--;
    av++;
  }

  if (!device)
  {
    fprintf(stderr,
            "Usage: %s [-w] -[-p] [-r] [-v] [-d] [-s <n>] [-t <n>] device\n"
            "\n"
            "Options:\n"
            "  -w                    writeonly mode, data from device is not sent to stdout\n"
            "  -r                    readonly mode, data from stdin is not sent to device\n"
            "  -p                    passthru mode, data from stdin is also sent to stdout\n"
            "  -v                    verbose\n"
            "  -d                    debug\n"
            "  -rx <list>            Set PGN receive list\n"
            "  -tx <list>            Set PGN transmit list\n"
            "  -l | --rate-limit-off Disable TX rate limits (use at own risk)\n"
            "  -s <n>                set baudrate to 38400, 57600, 115200, 230400"
#ifdef B460800
            ", 460800"
#endif
#ifdef B921600
            ", 921600"
#endif
            " (default 230400)\n"
            "  -t <n>                timeout, if no message is received after <n> seconds the program quits\n"
            "  -x                    hex instead of base64 mode"
            "  <device> can be a serial device, a normal file containing a raw log,\n"
            "  or the address of a TCP server in the format tcp://<host>[:<port>]\n"
            "\n"
            "  Examples: %s /dev/ttyUSB0\n"
            "            %s tcp://192.168.1.1:10001\n"
            "\n" COPYRIGHT,
            name,
            name,
            name);
    exit(1);
  }

  logDebug("Opening %s\n", device);
  if (strncmp(device, "tcp:", STRSIZE("tcp:")) == 0)
  {
    handle = open_socket_stream(device);
    logDebug("socket = %d\n", handle);
    isFile = true;
    if (handle < 0)
    {
      logAbort("Cannot open TCP stream %s\n", device);
    }
  }
  else
  {
    handle = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK);
    logDebug("fd = %d\n", handle);
    if (handle < 0)
    {
      logAbort("Cannot open device %s\n", device);
    }
    if (fstat(handle, &statbuf) < 0)
    {
      logAbort("Cannot determine device %s\n", device);
    }
    isFile = S_ISREG(statbuf.st_mode);
  }

  if (isFile)
  {
    logInfo("Device is a normal file, do not set the attributes.\n");
  }
  else
  {
    logDebug("Device is a serial port, set the attributes for %d baud.\n", speed);

    memset(&attr, 0, sizeof(attr));
    if (cfsetspeed(&attr, baudRate) < 0)
    {
      logAbort("Cannot set baudrate %d\n", speed);
    }

    attr.c_cflag |= CS8 | CLOCAL | CREAD;

    attr.c_iflag |= IGNPAR;
    attr.c_cc[VMIN]  = 1;
    attr.c_cc[VTIME] = 0;
    tcflush(handle, TCIFLUSH);
    tcsetattr(handle, TCSANOW, &attr);

    isSerialDevice = true;
    initializeDevice();
  }

  for (;;)
  {
    uint8_t data[1024];
    ssize_t r;
    bool    receivedSomething = false;

    int     writeHandle = (sbGetLength(&writeBuffer) > 0) ? handle : INVALID_SOCKET;
    int     inHandle    = (sendInitState == 0 && writeHandle == INVALID_SOCKET) ? STDIN : INVALID_SOCKET;

    int rd = isReady(handle, inHandle, writeHandle, timeout);

    logDebug("isReady(%d, %d, %d, %d) = %d\n", handle, inHandle, writeHandle, timeout, rd);

    if ((rd & FD1_ReadReady) > 0)
    {
      r = read(handle, data, sizeof data);
      if (r > 0)
      {
        sbAppendData(&readBuffer, data, r);
      }
      if (r < 0)
      {
        logAbort("Error reading device: %s\n", strerror(errno));
      }
      if (r == 0)
      {
        logAbort("EOF on device\n");
      }
    }

    if ((rd & FD2_ReadReady) > 0)
    {
      r = read(STDIN, data, sizeof data);
      if (r > 0)
      {
        sbAppendData(&inBuffer, data, r);
        processInBuffer(&inBuffer, &writeBuffer);
      }
      if (r < 0)
      {
        logAbort("Error reading stdin: %s\n", strerror(errno));
      }
      if (r == 0)
      {
        logAbort("EOF on stdin\n");
      }
    }

    if ((rd & FD3_WriteReady) > 0 && sbGetLength(&writeBuffer) > 0)
    {
      r = write(handle, sbGet(&writeBuffer), sbGetLength(&writeBuffer));
      if (r > 0)
      {
        if (verbose)
        {
          logInfo("Sent [%-1.*s]\n", r, sbGet(&writeBuffer));
        }
        sbDelete(&writeBuffer, 0, (size_t) r); // Eliminate written bytes from buffer
      }
      if (r == 0)
      {
        logAbort("EOF on stdout\n");
      }
    }

    if (sbGetLength(&readBuffer) > 0)
    {
      logDebug("readBuffer len=%zu\n", sbGetLength(&readBuffer));
      receivedSomething = processReadBuffer(&readBuffer, STDOUT);
    }

    // The isReady() function already aborted the program
    // where nothing at all was received from the iKonvert, for instance
    // when there is no N2K bus power.
    // However, we may also want reinitialize when there was no actual data
    // received from the iKonvert, e.g. no PGN was received. A reset of the
    // iKonvert is enough for that, so initializeDevices() suffices.
    if (sendInitState == 0 && resetTimeout > 0)
    {
      uint64_t now = getNow();

      if (debugReset > 0)
      {
        debugReset--;
      }

      if (lastNow == 0 || receivedSomething)
      {
        lastNow = now;
      }
      if (lastNow < now - 1000 * resetTimeout || debugReset == 0)
      {
        close(handle);
        logError("Last received N2K data %"PRIu64" ms ago.\n", now - lastNow);
        logError("Restart process to reset device\n");
        execvp(argv[0], argv);
      }
    }
  }

  close(handle);
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x648, %rsp            # imm = 0x648
movq %rsi, %r14
movl %edi, %r15d
movq (%rsi), %rbx
movq %rbx, %rdi
callq 0x3cf2
cmpl $0x2, %r15d
movq %r14, 0x40(%rsp)
movq %rbx, 0x28(%rsp)
jl 0x27ff
leaq 0x8d37(%rip), %rbx      # 0xb1f0
leaq 0x8d48(%rip), %r13      # 0xb208
movq $0x0, (%rsp)
movq %rbx, %r12
movq 0x8(%r14), %rbx
movq %rbx, %rdi
leaq 0x4b5f(%rip), %rsi      # 0x7038
callq 0x2040
testl %eax, %eax
je 0x363e
movq %rbx, %rdi
leaq 0x4b57(%rip), %rsi      # 0x7047
callq 0x2040
testl %eax, %eax
je 0x25d9
movq %rbx, %rdi
leaq 0x4b43(%rip), %rsi      # 0x704a
callq 0x2040
testl %eax, %eax
je 0x25e2
movq %rbx, %rdi
leaq 0x4b2f(%rip), %rsi      # 0x704d
callq 0x2040
testl %eax, %eax
je 0x25eb
movq %rbx, %rdi
leaq 0x4b1b(%rip), %rsi      # 0x7050
callq 0x2040
testl %eax, %eax
je 0x25f4
movq %rbx, %rdi
leaq 0x4b07(%rip), %rsi      # 0x7053
callq 0x2040
testl %eax, %eax
je 0x2606
movq %rbx, %rdi
leaq 0x4af3(%rip), %rsi      # 0x7056
callq 0x2040
testl %eax, %eax
je 0x25fd
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rdi
leaq 0x4ae7(%rip), %rsi      # 0x7067
callq 0x2040
testl %eax, %eax
je 0x25fd
leaq 0x8(%r14), %rbp
movq %rbx, %rdi
leaq 0x4ad3(%rip), %rsi      # 0x706a
callq 0x2040
cmpl $0x2, %r15d
je 0x2630
testl %eax, %eax
jne 0x2630
cmpq $0x0, 0x8c42(%rip)      # 0xb1f8
movq %r12, %rbx
je 0x25ca
movq %rbx, %rdi
leaq 0x5667(%rip), %rsi      # 0x7c2c
callq 0x40b0
decl %r15d
movq 0x10(%r14), %rdx
movq %rbx, %rdi
jmp 0x266f
movb $0x1, 0x8c08(%rip)      # 0xb1e8
jmp 0x260d
movb $0x1, 0x8c00(%rip)      # 0xb1e9
jmp 0x260d
movb $0x1, 0x8bf8(%rip)      # 0xb1ea
jmp 0x260d
movb $0x1, 0x8bf0(%rip)      # 0xb1eb
jmp 0x260d
movb $0x1, 0x8be9(%rip)      # 0xb1ed
jmp 0x260d
movb $0x1, 0x8bdf(%rip)      # 0xb1ec
movq %r14, %rbp
movq %r12, %rbx
leal -0x1(%r15), %eax
addq $0x8, %rbp
movq %rbp, %r14
cmpl $0x2, %r15d
movl %eax, %r15d
jg 0x24c8
jmp 0x2807
movq %rbx, %rdi
leaq 0x4a34(%rip), %rsi      # 0x706e
callq 0x2040
cmpl $0x2, %r15d
je 0x267f
testl %eax, %eax
jne 0x267f
cmpq $0x0, 0x8bbf(%rip)      # 0xb210
movq %r12, %rbx
je 0x2665
movq %r13, %rdi
leaq 0x55cc(%rip), %rsi      # 0x7c2c
callq 0x40b0
decl %r15d
movq 0x10(%r14), %rdx
movq %r13, %rdi
leaq 0x5ab6(%rip), %rsi      # 0x812c
xorl %eax, %eax
callq 0x3f7f
jmp 0x2613
movq %rbx, %rdi
leaq 0x49e9(%rip), %rsi      # 0x7072
callq 0x2040
cmpl $0x2, %r15d
je 0x26bb
testl %eax, %eax
jne 0x26bb
decl %r15d
movq 0x10(%r14), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x21d0
movq %rax, 0x8b6e(%rip)      # 0xb220
leaq 0x49c9(%rip), %rdi      # 0x7082
jmp 0x26f5
movq %rbx, %rdi
leaq 0x49b0(%rip), %rsi      # 0x7075
callq 0x2040
cmpl $0x2, %r15d
je 0x2704
testl %eax, %eax
jne 0x2704
decl %r15d
movq 0x10(%r14), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x21d0
movq %rax, 0x8b3a(%rip)      # 0xb228
leaq 0x4987(%rip), %rdi      # 0x707c
movq %rax, %rsi
xorl %eax, %eax
callq 0x3ab1
jmp 0x2610
movq %rbx, %rdi
leaq 0x4990(%rip), %rsi      # 0x709e
callq 0x2040
cmpl $0x2, %r15d
je 0x275f
testl %eax, %eax
jne 0x275f
movq 0x10(%r14), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x21d0
movl %eax, 0x8aa1(%rip)      # 0xb1d4
cmpl $0x383ff, %eax          # imm = 0x383FF
jg 0x2789
cmpl $0x9600, %eax           # imm = 0x9600
je 0x27b9
cmpl $0xe100, %eax           # imm = 0xE100
je 0x27c7
movl $0x0, %ebx
cmpl $0x1c200, %eax          # imm = 0x1C200
jne 0x27dd
movl $0x1002, %ecx           # imm = 0x1002
jmp 0x27d3
movq %rbx, %rdi
leaq 0x4953(%rip), %rsi      # 0x70bc
callq 0x2040
testl %eax, %eax
je 0x27aa
movq %r14, %rbp
cmpq $0x0, (%rsp)
movq %rbx, (%rsp)
movq %r12, %rbx
je 0x2613
jmp 0x27ff
cmpl $0x38400, %eax          # imm = 0x38400
je 0x27c0
cmpl $0x70800, %eax          # imm = 0x70800
je 0x27ce
movl $0x0, %ebx
cmpl $0xe1000, %eax          # imm = 0xE1000
jne 0x27dd
movl $0x1007, %ecx           # imm = 0x1007
jmp 0x27d3
movl $0x3, %edi
callq 0x3cc9
jmp 0x260d
movl $0xf, %ecx
jmp 0x27d3
movl $0x1003, %ecx           # imm = 0x1003
jmp 0x27d3
movl $0x1001, %ecx           # imm = 0x1001
jmp 0x27d3
movl $0x1004, %ecx           # imm = 0x1004
movl %ecx, 0x89f7(%rip)      # 0xb1d0
movq (%rsp), %rbx
decl %r15d
movl 0x89ea(%rip), %edx      # 0xb1d0
leaq 0x48b4(%rip), %rdi      # 0x70a1
movl %eax, %esi
xorl %eax, %eax
callq 0x3ab1
movq %rbx, (%rsp)
jmp 0x2610
movq $0x0, (%rsp)
movq (%rsp), %rbx
testq %rbx, %rbx
je 0x3651
leaq 0x4f33(%rip), %rdi      # 0x774e
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3ab1
leaq 0x4f2e(%rip), %rsi      # 0x775a
movl $0x4, %edx
movq %rbx, %rdi
callq 0x2070
movq %rbx, %rdi
testl %eax, %eax
je 0x28b4
movl $0x902, %esi            # imm = 0x902
xorl %eax, %eax
callq 0x2290
movl %eax, %r15d
leaq 0x4f31(%rip), %rdi      # 0x7787
movl %eax, %esi
xorl %eax, %eax
callq 0x3ab1
testl %r15d, %r15d
jns 0x2875
leaq 0x4f25(%rip), %rdi      # 0x7790
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3bd7
leaq 0x1b0(%rsp), %rsi
movl %r15d, %edi
callq 0x2320
testl %eax, %eax
jns 0x289a
leaq 0x4f17(%rip), %rdi      # 0x77a7
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3bd7
movl $0xf000, %eax           # imm = 0xF000
andl 0x1c8(%rsp), %eax
cmpl $0x8000, %eax           # imm = 0x8000
sete 0x897e(%rip)            # 0xb230
jmp 0x28e9
callq 0x46ed
movl %eax, %r15d
leaq 0x4e9c(%rip), %rdi      # 0x775f
movl %eax, %esi
xorl %eax, %eax
callq 0x3ab1
movb $0x1, 0x895d(%rip)      # 0xb230
testl %r15d, %r15d
jns 0x28e9
leaq 0x4e8d(%rip), %rdi      # 0x776c
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3bd7
cmpb $0x1, 0x8940(%rip)      # 0xb230
jne 0x2905
leaq 0x4eca(%rip), %rdi      # 0x77c3
xorl %eax, %eax
callq 0x39a0
jmp 0x299b
movl 0x88c9(%rip), %esi      # 0xb1d4
leaq 0x4ee6(%rip), %rdi      # 0x77f8
xorl %eax, %eax
callq 0x3ab1
xorps %xmm0, %xmm0
leaq 0x170(%rsp), %rdi
movaps %xmm0, (%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, 0x20(%rdi)
movups %xmm0, 0x2c(%rdi)
movl 0x8897(%rip), %esi      # 0xb1d0
callq 0x2170
testl %eax, %eax
jns 0x2956
movl 0x888c(%rip), %esi      # 0xb1d4
leaq 0x4ee3(%rip), %rdi      # 0x7832
xorl %eax, %eax
callq 0x3bd7
leaq 0x170(%rsp), %rbx
orl $0x8b0, 0x8(%rbx)       # imm = 0x8B0
orb $0x4, (%rbx)
movw $0x100, 0x16(%rbx)      # imm = 0x100
movl %r15d, %edi
xorl %esi, %esi
callq 0x20e0
movl %r15d, %edi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x2280
movb $0x1, 0x88a5(%rip)      # 0xb231
movl $0xe, 0x88ba(%rip)      # 0xb250
callq 0x367e
leaq 0x4dab(%rip), %r13      # 0x774d
leaq 0x88af(%rip), %r12      # 0xb258
movl %r15d, 0x34(%rsp)
cmpq $0x1, 0x888a(%rip)      # 0xb240
movl $0x0, %ebx
sbbl %ebx, %ebx
orl %r15d, %ebx
movl %ebx, %eax
notl %eax
xorl %r14d, %r14d
orl 0x8883(%rip), %eax      # 0xb250
setne %r14b
negl %r14d
movl 0x8846(%rip), %ecx      # 0xb220
movl %r15d, %edi
movl %r14d, %esi
movl %ebx, %edx
callq 0x4923
movl %eax, %ebp
movq 0x8830(%rip), %r8       # 0xb220
leaq 0x4e53(%rip), %rdi      # 0x784a
movl %r15d, %esi
movl %r14d, %edx
movl %ebx, %ecx
movl %eax, %r9d
xorl %eax, %eax
callq 0x3ab1
testb $0x1, %bpl
je 0x2a72
movl $0x400, %edx            # imm = 0x400
movl %r15d, %edi
leaq 0x240(%rsp), %r14
movq %r14, %rsi
callq 0x21a0
movq %rax, %rbx
testq %rax, %rax
jle 0x2a3d
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x3e3a
testq %rbx, %rbx
jns 0x2a5f
callq 0x2050
movl (%rax), %edi
callq 0x2310
leaq 0x4e13(%rip), %rdi      # 0x7868
movq %rax, %rsi
xorl %eax, %eax
callq 0x3bd7
testq %rbx, %rbx
jne 0x2a72
leaq 0x4e17(%rip), %rdi      # 0x7882
xorl %eax, %eax
callq 0x3bd7
testb $0x2, %bpl
leaq 0x8(%rsp), %r14
je 0x2cda
movl $0x400, %edx            # imm = 0x400
xorl %edi, %edi
leaq 0x240(%rsp), %rsi
callq 0x21a0
movq %rax, %rbx
testq %rax, %rax
jle 0x2ca5
leaq 0x87c8(%rip), %rdi      # 0xb270
leaq 0x240(%rsp), %rsi
movq %rbx, (%rsp)
movq %rbx, %rdx
callq 0x3e3a
movq 0x87ad(%rip), %r15      # 0xb270
testq %r15, %r15
sete %bl
movq %r15, %rdi
cmoveq %r13, %rdi
movl $0xa, %esi
callq 0x20f0
testq %rax, %rax
je 0x2c77
movq %rax, %r14
cmpb $0x0, 0x86fd(%rip)      # 0xb1ea
jne 0x2b5c
leaq 0x877a(%rip), %rdi      # 0xb270
leaq 0x48(%rsp), %rsi
callq 0x57ce
movl 0x88(%rsp), %edx
cmpl $0x40000, %edx          # imm = 0x40000
setb %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x2b5c
movzbl 0x8c(%rsp), %ecx
leaq 0x8712(%rip), %rbx      # 0xb238
movq %rbx, %rdi
leaq 0x4dff(%rip), %rsi      # 0x792f
xorl %eax, %eax
callq 0x3f7f
movzbl 0x8e(%rsp), %edx
cmpb $0x1, 0x86a6(%rip)      # 0xb1ec
jne 0x2bac
movq %rbx, %rdi
leaq 0x8f(%rsp), %rsi
xorl %ecx, %ecx
callq 0x3e91
jmp 0x2bbe
movb 0x8e(%rsp), %al
cmpb $0x7, %al
setb %cl
orb 0x867c(%rip), %cl       # 0xb1ea
testb $0x1, %cl
leaq 0x86c0(%rip), %rbx      # 0xb238
jne 0x2beb
movl 0x8f(%rsp), %ecx
movl $0x47445024, %edx       # imm = 0x47445024
xorl %edx, %ecx
movzwl 0x93(%rsp), %edx
xorl $0x59, %edx
orl %ecx, %edx
jne 0x2beb
movzbl %al, %edx
movq %rbx, %rdi
leaq 0x8f(%rsp), %rsi
callq 0x3e3a
jmp 0x2bbe
movq %rbx, %rdi
leaq 0x8f(%rsp), %rsi
xorl %ecx, %ecx
callq 0x5da8
movq %rbx, %rdi
leaq 0x4e8c(%rip), %rsi      # 0x7a54
xorl %eax, %eax
callq 0x3f7f
movq 0x8662(%rip), %rsi      # 0xb238
testq %rsi, %rsi
cmoveq %r13, %rsi
leaq 0x4d58(%rip), %rdi      # 0x793c
xorl %eax, %eax
callq 0x3ab1
cmpb $0x1, 0x85f7(%rip)      # 0xb1e9
jne 0x2c28
movq 0x8675(%rip), %rsi      # 0xb270
testq %rsi, %rsi
cmoveq %r13, %rsi
movq %r14, %rdx
subq %rsi, %rdx
incq %rdx
movl $0x1, %edi
callq 0x20c0
testq %rax, %rax
jg 0x2c28
leaq 0x4d32(%rip), %rdi      # 0x7953
xorl %eax, %eax
callq 0x3bd7
movq 0x8641(%rip), %rax      # 0xb270
testq %rax, %rax
cmoveq %r13, %rax
subq %rax, %r14
incq %r14
leaq 0x862d(%rip), %rdi      # 0xb270
xorl %esi, %esi
movq %r14, %rdx
callq 0x3dd6
movq 0x861c(%rip), %r15      # 0xb270
testq %r15, %r15
sete %bl
movq %r15, %rdi
cmoveq %r13, %rdi
movl $0xa, %esi
callq 0x20f0
movq %rax, %r14
testq %rax, %rax
jne 0x2ae6
cmpq $0x2c1, 0x85f6(%rip) # imm = 0x2C1
jb 0x2c97
movq $0x0, 0x85e9(%rip) # 0xb278
testb %bl, %bl
jne 0x2c97
movb $0x0, (%r15)
movl 0x34(%rsp), %r15d
leaq 0x8(%rsp), %r14
movq (%rsp), %rbx
testq %rbx, %rbx
jns 0x2cc7
callq 0x2050
movl (%rax), %edi
callq 0x2310
leaq 0x4bd4(%rip), %rdi      # 0x7891
movq %rax, %rsi
xorl %eax, %eax
callq 0x3bd7
testq %rbx, %rbx
jne 0x2cda
leaq 0x4bd7(%rip), %rdi      # 0x78aa
xorl %eax, %eax
callq 0x3bd7
shrb $0x2, %bpl
movq 0x855b(%rip), %rdx      # 0xb240
testq %rdx, %rdx
setne %al
andb %bpl, %al
cmpb $0x1, %al
jne 0x2d5c
movq 0x853f(%rip), %rsi      # 0xb238
testq %rsi, %rsi
cmoveq %r13, %rsi
movl %r15d, %edi
callq 0x20c0
movq %rax, %rbx
testq %rax, %rax
jle 0x2d49
cmpb $0x1, 0x84d4(%rip)      # 0xb1eb
jne 0x2d38
movq 0x8518(%rip), %rdx      # 0xb238
testq %rdx, %rdx
cmoveq %r13, %rdx
leaq 0x4b8a(%rip), %rdi      # 0x78b8
movq %rbx, %rsi
xorl %eax, %eax
callq 0x39a0
leaq 0x84f9(%rip), %rdi      # 0xb238
xorl %esi, %esi
movq %rbx, %rdx
callq 0x3dd6
testq %rbx, %rbx
jne 0x2d5c
leaq 0x4b72(%rip), %rdi      # 0x78c7
xorl %eax, %eax
callq 0x3bd7
movq 0x84fd(%rip), %rsi      # 0xb260
testq %rsi, %rsi
je 0x35a2
movl $0x0, (%rsp)
leaq 0x4b5c(%rip), %rdi      # 0x78d6
xorl %eax, %eax
callq 0x3ab1
movq 0x84d8(%rip), %rsi      # 0xb260
leaq 0x4d00(%rip), %rdi      # 0x7a8f
xorl %eax, %eax
callq 0x3ab1
movq %r12, %rdi
movl $0xa, %esi
callq 0x4621
testq %rax, %rax
je 0x3562
movq %rax, %rbp
movb $0x1, %al
movl %eax, 0x28(%rsp)
movl $0x0, (%rsp)
movq 0x8495(%rip), %rbx      # 0xb258
testq %rbx, %rbx
cmoveq %r13, %rbx
movq %rbp, %rsi
subq %rbx, %rsi
cmpq $0x8, %rsi
jb 0x2ebf
movzbl (%rbx), %eax
cmpl $0x24, %eax
je 0x2deb
cmpl $0x21, %eax
jne 0x2ebf
leaq 0x4cb8(%rip), %rdi      # 0x7aaa
xorl %eax, %eax
callq 0x3ab1
movl $0x128, %edx            # imm = 0x128
leaq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x2160
movb $0x0, (%rbp)
leaq 0x1(%rbx), %rax
cmpq %rax, %rbp
jbe 0x2e21
cmpb $0xd, -0x1(%rbp)
jne 0x2e21
movb $0x0, -0x1(%rbp)
leaq 0x4caa(%rip), %rdi      # 0x7ad2
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3ab1
cmpb $0x0, 0x83af(%rip)      # 0xb1e8
jne 0x3479
movq 0x8412(%rip), %rax      # 0xb258
testq %rax, %rax
cmoveq %r13, %rax
movq %rax, 0x8(%rsp)
movq %r14, %rdi
leaq 0x4cf3(%rip), %rsi      # 0x7b4f
callq 0x4bd2
testb %al, %al
je 0x3311
movq %r14, %rdi
leaq 0x4ce3(%rip), %rsi      # 0x7b56
callq 0x4bd2
testb %al, %al
je 0x2f0f
movq 0x8(%rsp), %rsi
leaq 0x4cd0(%rip), %rdi      # 0x7b5c
xorl %eax, %eax
callq 0x39a0
cmpl $0xd, 0x83b6(%rip)      # 0xb250
jne 0x2f22
movl $0xc, 0x83a6(%rip)      # 0xb250
leaq 0x4cbc(%rip), %rdi      # 0x7b6d
movl $0xc, %esi
xorl %eax, %eax
callq 0x3ab1
jmp 0x2f22
incq %rsi
leaq 0x4c71(%rip), %rdi      # 0x7b3a
xorl %eax, %eax
callq 0x3ab1
movq 0x8381(%rip), %rax      # 0xb258
testq %rax, %rax
cmoveq %r13, %rax
subq %rax, %rbp
incq %rbp
movq %r12, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x3dd6
movq %r12, %rdi
movl $0xa, %esi
callq 0x4621
movq %rax, %rbp
testq %rax, %rax
jne 0x2dbc
jmp 0x3562
cmpl $0xd, 0x833a(%rip)      # 0xb250
jne 0x2f5b
movl $0xe, 0x832e(%rip)      # 0xb250
movq 0x832f(%rip), %rsi      # 0xb258
testq %rsi, %rsi
cmoveq %r13, %rsi
leaq 0x4baa(%rip), %rdi      # 0x7ae1
xorl %eax, %eax
callq 0x3ab1
testb $0x1, 0x28(%rsp)
je 0x3479
callq 0x367e
movl $0x0, 0x28(%rsp)
jmp 0x3479
movq %r14, %rdi
leaq 0x4c2f(%rip), %rsi      # 0x7b94
callq 0x4bd2
testb %al, %al
je 0x2f8c
cmpb $0x1, 0x8276(%rip)      # 0xb1eb
jne 0x2f22
movq 0x8(%rsp), %rsi
leaq 0x4c1e(%rip), %rdi      # 0x7ba1
xorl %eax, %eax
callq 0x39a0
jmp 0x2f22
movq %r14, %rdi
leaq 0x4c2a(%rip), %rsi      # 0x7bc0
callq 0x4bd2
testb %al, %al
je 0x2fe3
cmpb $0x1, 0x8245(%rip)      # 0xb1eb
jne 0x2fbb
movq 0x8(%rsp), %rsi
leaq 0x4c19(%rip), %rdi      # 0x7bcd
xorl %eax, %eax
callq 0x39a0
cmpl $0x5, 0x828e(%rip)      # 0xb250
jne 0x2f22
movl $0x4, 0x827e(%rip)      # 0xb250
leaq 0x4b94(%rip), %rdi      # 0x7b6d
movl $0x4, %esi
jmp 0x2eb6
movq %r14, %rdi
leaq 0x4c00(%rip), %rsi      # 0x7bed
callq 0x4bd2
testb %al, %al
je 0x3040
cmpb $0x1, 0x81ee(%rip)      # 0xb1eb
jne 0x3012
movq 0x8(%rsp), %rsi
leaq 0x4be7(%rip), %rdi      # 0x7bf2
xorl %eax, %eax
callq 0x39a0
movl 0x8238(%rip), %esi      # 0xb250
testl %esi, %esi
setle %al
testb $0x1, %sil
sete %cl
orb %al, %cl
jne 0x2f22
decl %esi
movl %esi, 0x821c(%rip)      # 0xb250
leaq 0x4b32(%rip), %rdi      # 0x7b6d
jmp 0x2eb6
movq %r14, %rdi
leaq 0x4bc4(%rip), %rsi      # 0x7c0e
callq 0x4bd2
movq %r14, %rdi
testb %al, %al
je 0x3096
leaq 0x1c(%rsp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x4b7b
testb %al, %al
je 0x2f22
cmpb $0x1, 0x8177(%rip)      # 0xb1eb
jne 0x2f22
movl 0x1c(%rsp), %esi
movq 0x8(%rsp), %rdx
leaq 0x4b89(%rip), %rdi      # 0x7c13
xorl %eax, %eax
callq 0x39a0
jmp 0x2f22
leaq 0x18(%rsp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x4b7b
movq 0x8(%rsp), %rsi
testb %al, %al
je 0x328f
cmpl $0x0, 0x18(%rsp)
jne 0x328f
movq %rsi, %rdi
leaq 0x4b61(%rip), %rsi      # 0x7c28
callq 0x21b0
testl %eax, %eax
je 0x32c0
movl $0x40100, 0x88(%rsp) # imm = 0x40100
movb $0x7, 0x84(%rsp)
movw $0xff, 0x8c(%rsp)
leaq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x38eb
movb $0xf, 0x8e(%rsp)
leaq 0x8f(%rsp), %rax
movq $-0x1, %rcx
movq %rcx, 0x7(%rax)
movq %rcx, (%rax)
movq %r14, %rdi
leaq 0x14(%rsp), %rsi
movl $0xff, %edx
callq 0x4b7b
testb %al, %al
je 0x3154
movl 0x14(%rsp), %esi
movb %sil, 0x8f(%rsp)
cmpb $0x1, 0x80a7(%rip)      # 0xb1eb
jne 0x3154
leaq 0x4afb(%rip), %rdi      # 0x7c48
xorl %eax, %eax
callq 0x39a0
movq %r14, %rdi
leaq 0x24(%rsp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x4b7b
testb %al, %al
je 0x318c
movl 0x24(%rsp), %esi
movl %esi, 0x90(%rsp)
cmpb $0x1, 0x806f(%rip)      # 0xb1eb
jne 0x318c
leaq 0x4ad6(%rip), %rdi      # 0x7c5b
xorl %eax, %eax
callq 0x39a0
movq %r14, %rdi
leaq 0x20(%rsp), %rsi
xorl %edx, %edx
callq 0x4b7b
movl 0x20(%rsp), %esi
testl %esi, %esi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x31ca
movb %sil, 0x94(%rsp)
cmpb $0x1, 0x8031(%rip)      # 0xb1eb
jne 0x31ca
leaq 0x4aab(%rip), %rdi      # 0x7c6e
xorl %eax, %eax
callq 0x39a0
movq %r14, %rdi
leaq 0x10(%rsp), %rsi
xorl %edx, %edx
callq 0x4b7b
movl 0x10(%rsp), %esi
testl %esi, %esi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x3207
movl %esi, 0x95(%rsp)
cmpb $0x1, 0x7ff4(%rip)      # 0xb1eb
jne 0x3207
leaq 0x4a83(%rip), %rdi      # 0x7c83
xorl %eax, %eax
callq 0x39a0
movq %r14, %rdi
leaq 0x3c(%rsp), %rsi
xorl %edx, %edx
callq 0x4b7b
movl 0x3c(%rsp), %esi
testl %esi, %esi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x3245
movb %sil, 0x99(%rsp)
cmpb $0x1, 0x7fb6(%rip)      # 0xb1eb
jne 0x3245
leaq 0x4a5a(%rip), %rdi      # 0x7c98
xorl %eax, %eax
callq 0x39a0
movq %r14, %rdi
leaq 0x38(%rsp), %rsi
xorl %edx, %edx
callq 0x4b7b
movl 0x38(%rsp), %esi
testl %esi, %esi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x2f22
movl %esi, 0x9a(%rsp)
cmpb $0x1, 0x7f75(%rip)      # 0xb1eb
jne 0x2f22
leaq 0x4a2a(%rip), %rdi      # 0x7cad
xorl %eax, %eax
callq 0x39a0
jmp 0x2f22
leaq 0x4a41(%rip), %rdi      # 0x7cd7
xorl %eax, %eax
callq 0x3b44
cmpl $0x0, 0x7fac(%rip)      # 0xb250
jle 0x3311
cmpb $0x1, 0x7f84(%rip)      # 0xb231
jne 0x3307
movl $0xe, 0x7f97(%rip)      # 0xb250
callq 0x367e
jmp 0x3311
leaq 0x4967(%rip), %rdi      # 0x7c2e
xorl %eax, %eax
callq 0x3ab1
movl 0x7fd4(%rip), %eax      # 0xb2a8
leal 0x1(%rax), %ecx
movl %ecx, 0x7fcb(%rip)      # 0xb2a8
cmpl $0xa, %eax
jl 0x2f22
cmpb $0x1, 0x7f44(%rip)      # 0xb231
jne 0x3553
movl $0xe, 0x7f53(%rip)      # 0xb250
callq 0x367e
jmp 0x2f22
movl $0x0, 0x7f3f(%rip)      # 0xb250
movq 0x7f40(%rip), %rbx      # 0xb258
testq %rbx, %rbx
cmoveq %r13, %rbx
movq %rbx, %rdi
callq 0x20d0
movq %r14, %rdx
movq %rax, %r14
subq $0x8, %rsp
movq %rbx, %rdi
leaq 0x49ba(%rip), %rsi      # 0x7cf5
leaq 0x24(%rsp), %rcx
leaq 0x20(%rsp), %r8
leaq 0x1c(%rsp), %r9
xorl %eax, %eax
leaq 0x18(%rsp), %r10
pushq %r10
leaq 0x30(%rsp), %r10
pushq %r10
leaq 0x3c(%rsp), %r10
pushq %r10
callq 0x2220
addq $0x20, %rsp
cmpl $0x6, %eax
jne 0x33c3
movb 0x1c(%rsp), %al
movb %al, 0x84(%rsp)
movl 0x8(%rsp), %eax
movl %eax, 0x88(%rsp)
movb 0x18(%rsp), %al
movb %al, 0x8d(%rsp)
movb 0x14(%rsp), %al
movb %al, 0x8c(%rsp)
movslq 0x10(%rsp), %rax
addq %rax, %rbx
subq %rax, %r14
cmpb $0x1, 0x7e3f(%rip)      # 0xb1ec
jne 0x33e9
leaq 0x7eda(%rip), %rdi      # 0xb290
movq %rbx, %rsi
movq %r14, %rdx
callq 0x4003
jmp 0x33fd
movq 0x7e8e(%rip), %rsi      # 0xb258
testq %rsi, %rsi
cmoveq %r13, %rsi
leaq 0x4725(%rip), %rdi      # 0x7afd
xorl %eax, %eax
callq 0x3b44
leaq 0x8(%rsp), %r14
jmp 0x3479
leaq 0x7ea0(%rip), %rdi      # 0xb290
movq %rbx, %rsi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x5f45
movq 0x7e94(%rip), %rdx      # 0xb298
movl $0xdf, %eax
cmpq %rax, %rdx
cmovaeq %rax, %rdx
movb %dl, 0x8e(%rsp)
movq 0x7e72(%rip), %rbx      # 0xb290
testq %rbx, %rbx
movq %rbx, %rsi
cmoveq %r13, %rsi
leaq 0x8f(%rsp), %rdi
callq 0x21e0
movq $0x0, 0x7e58(%rip) # 0xb298
testq %rbx, %rbx
leaq 0x8(%rsp), %r14
je 0x344d
movb $0x0, (%rbx)
leaq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x38eb
movl $0x0, 0x7e45(%rip)      # 0xb2a8
movb $0x1, %al
movl %eax, (%rsp)
cmpl $0x0, 0x7de1(%rip)      # 0xb250
jle 0x3479
movb $0x0, 0x8e(%rsp)
movzbl 0x8e(%rsp), %r10d
testl %r10d, %r10d
je 0x2ed0
movzbl 0x84(%rsp), %ecx
movl 0x88(%rsp), %r8d
movzbl 0x8d(%rsp), %r9d
movzbl 0x8c(%rsp), %r11d
leaq 0x7ddc(%rip), %rbx      # 0xb290
movq %rbx, %rdi
leaq 0x4669(%rip), %rsi      # 0x7b27
leaq 0x48(%rsp), %rdx
xorl %eax, %eax
pushq %r10
pushq %r11
callq 0x3f7f
addq $0x10, %rsp
movzbl 0x8e(%rsp), %edx
movq %rbx, %rdi
leaq 0x8f(%rsp), %rsi
movl $0x2c, %ecx
callq 0x3e91
movq %rbx, %rdi
leaq 0x4253(%rip), %rsi      # 0x774c
callq 0x40b0
movq 0x7d8b(%rip), %rsi      # 0xb290
testq %rsi, %rsi
cmoveq %r13, %rsi
movq 0x7d85(%rip), %rdx      # 0xb298
movl $0x1, %edi
callq 0x20c0
testq %rax, %rax
jg 0x3530
leaq 0x442a(%rip), %rdi      # 0x7953
xorl %eax, %eax
callq 0x3bd7
movq $0x0, 0x7d5d(%rip) # 0xb298
movq 0x7d4e(%rip), %rax      # 0xb290
testq %rax, %rax
je 0x2ed0
movb $0x0, (%rax)
jmp 0x2ed0
movl $0x0, 0x7cf3(%rip)      # 0xb250
jmp 0x2f22
cmpq $0x0, 0x7cf6(%rip)      # 0xb260
je 0x359d
movq 0x7ce5(%rip), %rax      # 0xb258
testq %rax, %rax
movq %rax, %rcx
cmoveq %r13, %rcx
movzbl (%rcx), %ecx
cmpl $0x21, %ecx
je 0x359d
cmpl $0x24, %ecx
je 0x359d
movq $0x0, 0x7ccb(%rip) # 0xb260
testq %rax, %rax
je 0x359d
movb $0x0, (%rax)
movl (%rsp), %ebp
jmp 0x35a4
xorl %ebp, %ebp
cmpl $0x0, 0x7ca5(%rip)      # 0xb250
jne 0x29ae
cmpq $0x0, 0x7c6f(%rip)      # 0xb228
jle 0x29ae
callq 0x3890
cmpq $0x0, 0x7cbc(%rip)      # 0xb288
movq %rax, %rbx
sete %al
orb %bpl, %al
testb $0x1, %al
je 0x35e0
movq %rbx, 0x7ca8(%rip)      # 0xb288
imulq $-0x3e8, 0x7c3d(%rip), %rax # imm = 0xFC18
addq %rbx, %rax
cmpq %rax, 0x7c93(%rip)      # 0xb288
jae 0x29ae
movl %r15d, %edi
callq 0x2180
movq 0x7c7e(%rip), %rax      # 0xb288
subq %rax, %rbx
leaq 0x42d6(%rip), %rdi      # 0x78ea
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3b44
leaq 0x42e9(%rip), %rdi      # 0x790e
xorl %eax, %eax
callq 0x3b44
movq 0x40(%rsp), %rsi
movq (%rsi), %rdi
callq 0x22c0
jmp 0x29ae
leaq 0x39fc(%rip), %rdi      # 0x7041
callq 0x20a0
xorl %edi, %edi
callq 0x22e0
movq 0x79a0(%rip), %rax      # 0xaff8
movq (%rax), %rdi
leaq 0x3a5d(%rip), %rsi      # 0x70bf
movq 0x28(%rsp), %rcx
movq %rcx, %rdx
movq %rcx, %r8
xorl %eax, %eax
callq 0x21c0
movl $0x1, %edi
callq 0x22e0