// File: /workspace/github_c_makefile_star2_O1/canboat[P]canboat/actisense-serial/actisense-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            speed = 115200;
  int            i;
  time_t         lastPing = time(0);

  setProgName(argv[0]);
  while (argc > 1)
  {
    if (strcasecmp(argv[1], "-version") == 0)
    {
      printf("%s\n", VERSION);
      exit(0);
    }
    else if (strcasecmp(argv[1], "-w") == 0)
    {
      writeonly = 1;
    }
    else if (strcasecmp(argv[1], "-p") == 0)
    {
      passthru = 1;
    }
    else if (strcasecmp(argv[1], "-r") == 0)
    {
      readonly = 1;
    }
    else if (strcasecmp(argv[1], "-v") == 0)
    {
      verbose = 1;
    }
    else if (strcasecmp(argv[1], "-t") == 0 && argc > 2)
    {
      argc--;
      argv++;
      timeout = strtol(argv[1], 0, 10);
      logDebug("timeout set to %ld seconds\n", timeout);
    }
    else if (strcasecmp(argv[1], "-s") == 0 && argc > 2)
    {
      argc--;
      argv++;
      speed = strtol(argv[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:
          baudRate = speed;
          break;
      }
      logDebug("speed set to %d (%d) baud\n", speed, baudRate);
    }
    else if (strcasecmp(argv[1], "-d") == 0)
    {
      setLogLevel(LOGLEVEL_DEBUG);
    }
    else if (strcasecmp(argv[1], "-o") == 0)
    {
      outputCommands = 1;
    }
    else if (!device)
    {
      device = argv[1];
    }
    else
    {
      device = 0;
      break;
    }
    argc--;
    argv++;
  }

  if (!device)
  {
    fprintf(stderr,
            "Usage: %s [-w] -[-p] [-r] [-v] [-d] [-s <n>] [-t <n>] device\n"
            "\n"
            "Options:\n"
            "  -w      writeonly mode, no data is read from device\n"
            "  -r      readonly mode, no data is sent to device\n"
            "  -p      passthru mode, data on stdin is sent to stdout but not to device\n"
            "  -v      verbose\n"
            "  -d      debug\n"
            "  -s <n>  set baudrate to 38400, 57600, 115200, 230400"
#ifdef B460800
            ", 460800"
#endif
#ifdef B921600
            ", 921600"
#endif
            "\n"
            "  -t <n>  timeout, if no message is received after <n> seconds the program quits\n"
            "  -o      output commands sent to stdin to the stdout \n"
            "  <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)
    {
      fprintf(stderr, "Cannot open NGT-1-A TCP stream %s\n", device);
      exit(1);
    }
  }
  else
  {
    int oflag = O_NOCTTY | O_NONBLOCK;

    if (writeonly)
    {
      oflag |= O_WRONLY;
      oflag |= O_CREAT;
    }
    else if (readonly)
    {
      oflag |= O_RDONLY;
    }
    else
    {
      oflag |= O_RDWR;
    }
    handle = open(device, oflag, 0777);

    logDebug("fd = %d\n", handle);
    if (handle < 0)
    {
      logAbort("Cannot open NGT-1-A device/file %s\n", device);
    }
    if (fstat(handle, &statbuf) < 0)
    {
      logAbort("Cannot determine status of %s\n", device);
    }
    isFile = S_ISREG(statbuf.st_mode);
  }

  if (isFile)
  {
    if (strncmp(device + strlen(device) - STRSIZE(".ebl"), ".ebl", STRSIZE(".ebl")) == 0)
    {
      isEBL = true;
      logDebug("EBL mode selected\n");
    }
    else
    {
      logDebug("Device is a normal file, do not set the attributes.\n");
    }
  }
  else
  {
    logDebug("Device is a serial port, set the attributes.\n");

    memset(&attr, 0, sizeof(attr));
    if (cfsetspeed(&attr, baudRate) < 0)
    {
      logAbort("Could not 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);

    logDebug("Device is a serial port, send the startup sequence.\n");

    writeMessage(handle, NGT_MSG_SEND, NGT_STARTUP_SEQ, sizeof(NGT_STARTUP_SEQ), UINT64_C(0));
    sleep(2);
  }

  if (!isFile)
  {
    // Do not read anything until we have seen 10 messages on bus
    for (i = 0; i < 10;)
    {
      int r = isReady(handle, INVALID_SOCKET, INVALID_SOCKET, timeout);

      if ((r & FD1_ReadReady) > 0)
      {
        if (readNGT1(handle) <= 0)
        {
          break;
        }
        i++;
      }
    }
  }

  for (;;)
  {
    char msg[BUFFER_SIZE];
    int  r = isReady(writeonly ? INVALID_SOCKET : handle, readonly ? INVALID_SOCKET : STDIN_FILENO, INVALID_SOCKET, timeout);

    if ((r & FD1_ReadReady) > 0)
    {
      if (readNGT1(handle) <= 0)
      {
        break;
      }
    }
    if ((r & FD2_ReadReady) > 0)
    {
      if (!readIn())
      {
        break;
      }
    }

    while (getInMsg(msg, sizeof(msg)))
    {
      if (!passthru)
      {
        parseAndWriteIn(handle, msg);
      }
      if (outputCommands)
      {
        fprintf(stdout, "%s", msg);
        fflush(stdout);
      }
    }
    if (time(0) - lastPing > 20)
    {
      writeMessage(handle, NGT_MSG_SEND, NGT_STARTUP_SEQ, sizeof(NGT_STARTUP_SEQ), UINT64_C(0));
      lastPing = time(0);
    }
  }

  close(handle);
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x6a8, %rsp            # imm = 0x6A8
movq %rsi, %r14
movl %edi, %r12d
movq (%rsi), %rax
movq %rax, 0x10(%rsp)
movq $0x0, 0x8(%rsp)
xorl %edi, %edi
callq 0x21d0
movq %rax, 0x38(%rsp)
movq (%r14), %rdi
callq 0x3a8e
movl $0x1c200, %eax          # imm = 0x1C200
movq %rax, 0x20(%rsp)
cmpl $0x2, %r12d
jl 0x272f
movl $0x1c200, %eax          # imm = 0x1C200
movq %rax, 0x20(%rsp)
leaq 0x3b3b(%rip), %r13      # 0x6058
leaq 0x3b43(%rip), %rbp      # 0x6067
movq $0x0, 0x8(%rsp)
movq 0x8(%r14), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x2040
testl %eax, %eax
je 0x2ce9
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2040
testl %eax, %eax
je 0x25da
movq %r15, %rdi
leaq 0x3b09(%rip), %rsi      # 0x606a
callq 0x2040
testl %eax, %eax
je 0x25e3
movq %r15, %rdi
leaq 0x3af9(%rip), %rsi      # 0x606d
callq 0x2040
testl %eax, %eax
je 0x25ec
movq %r15, %rdi
leaq 0x3ae9(%rip), %rsi      # 0x6070
callq 0x2040
testl %eax, %eax
je 0x25f3
leaq 0x8(%r14), %rbx
movq %r15, %rdi
leaq 0x3ad5(%rip), %rsi      # 0x6073
callq 0x2040
cmpl $0x2, %r12d
je 0x2614
testl %eax, %eax
jne 0x2614
decl %r12d
movq 0x10(%r14), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x21b0
movq %rax, 0x6c49(%rip)      # 0x9210
leaq 0x3aa8(%rip), %rdi      # 0x6076
movq %rax, %rsi
xorl %eax, %eax
callq 0x384d
jmp 0x25f6
movb $0x1, 0x6c1f(%rip)      # 0x9200
jmp 0x25f3
movb $0x1, 0x6c1a(%rip)      # 0x9204
jmp 0x25f3
movb $0x1, 0x6c15(%rip)      # 0x9208
movq %r14, %rbx
leal -0x1(%r12), %eax
addq $0x8, %rbx
movq %rbx, %r14
cmpl $0x2, %r12d
movl %eax, %r12d
jg 0x252d
jmp 0x272f
movq %r15, %rdi
leaq 0x3a74(%rip), %rsi      # 0x6092
callq 0x2040
cmpl $0x2, %r12d
je 0x266f
testl %eax, %eax
jne 0x266f
movq 0x10(%r14), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x21b0
cmpl $0x383ff, %eax          # imm = 0x383FF
jg 0x26ae
cmpl $0x9600, %eax           # imm = 0x9600
je 0x26e5
cmpl $0xe100, %eax           # imm = 0xE100
je 0x26f7
cmpl $0x1c200, %eax          # imm = 0x1C200
jne 0x26f3
movl $0x1002, %edx           # imm = 0x1002
jmp 0x2703
movq %r15, %rdi
leaq 0x3a37(%rip), %rsi      # 0x60b0
callq 0x2040
testl %eax, %eax
je 0x26ca
movq %r15, %rdi
leaq 0x3a27(%rip), %rsi      # 0x60b3
callq 0x2040
testl %eax, %eax
je 0x26d9
cmpq $0x0, 0x8(%rsp)
jne 0x2726
movq %r14, %rbx
movq %r15, 0x8(%rsp)
jmp 0x25f6
cmpl $0x38400, %eax          # imm = 0x38400
je 0x26ec
cmpl $0x70800, %eax          # imm = 0x70800
je 0x26fe
cmpl $0xe1000, %eax          # imm = 0xE1000
jne 0x26f3
movl $0x1007, %edx           # imm = 0x1007
jmp 0x2703
movl $0x3, %edi
callq 0x3a65
jmp 0x25f3
movb $0x1, 0x6b38(%rip)      # 0x9218
jmp 0x25f3
movl $0xf, %edx
jmp 0x2703
movl $0x1003, %edx           # imm = 0x1003
jmp 0x2703
movl %eax, %edx
jmp 0x2703
movl $0x1001, %edx           # imm = 0x1001
jmp 0x2703
movl $0x1004, %edx           # imm = 0x1004
decl %r12d
movl %edx, 0x6abc(%rip)      # 0x91c8
leaq 0x3982(%rip), %rdi      # 0x6095
movq %rax, 0x20(%rsp)
movl %eax, %esi
xorl %eax, %eax
callq 0x384d
jmp 0x25f6
movq $0x0, 0x8(%rsp)
movq 0x8(%rsp), %rbx
testq %rbx, %rbx
je 0x2cfc
leaq 0x3eeb(%rip), %rdi      # 0x662f
movq %rbx, %rsi
xorl %eax, %eax
callq 0x384d
leaq 0x3ee6(%rip), %rsi      # 0x663b
movl $0x4, %edx
movq %rbx, %rdi
callq 0x2070
testl %eax, %eax
je 0x27fe
movzbl 0x6a97(%rip), %eax      # 0x9208
xorl $0x1, %eax
cmpb $0x0, 0x6a85(%rip)      # 0x9200
leal 0x900(%rax,%rax), %eax
movl $0x941, %esi            # imm = 0x941
cmovel %eax, %esi
movq %rbx, %rdi
movl $0x1ff, %edx            # imm = 0x1FF
xorl %eax, %eax
callq 0x2280
movl %eax, %ebp
leaq 0x3ece(%rip), %rdi      # 0x6670
movl %eax, %esi
xorl %eax, %eax
callq 0x384d
testl %ebp, %ebp
jns 0x27c0
leaq 0x3ec3(%rip), %rdi      # 0x6679
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3973
leaq 0x290(%rsp), %rsi
movl %ebp, %edi
callq 0x2310
testl %eax, %eax
jns 0x27e4
leaq 0x3ec3(%rip), %rdi      # 0x669d
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3973
movl $0xf000, %eax           # imm = 0xF000
andl 0x2a8(%rsp), %eax
cmpl $0x8000, %eax           # imm = 0x8000
sete 0x6a1d(%rip)            # 0x9219
jmp 0x2827
movq %rbx, %rdi
callq 0x4489
movl %eax, %ebp
leaq 0x3e31(%rip), %rdi      # 0x6640
movl %eax, %esi
xorl %eax, %eax
callq 0x384d
movb $0x1, 0x69fa(%rip)      # 0x9219
testl %ebp, %ebp
js 0x2d06
cmpb $0x1, 0x69eb(%rip)      # 0x9219
jne 0x2865
movq %rbx, %rdi
callq 0x20d0
leaq (%rbx,%rax), %rdi
addq $-0x4, %rdi
leaq 0x3e75(%rip), %rsi      # 0x66bc
movl $0x4, %edx
callq 0x2070
testl %eax, %eax
je 0x290b
leaq 0x3e74(%rip), %rdi      # 0x66d4
jmp 0x2919
leaq 0x3e9d(%rip), %rdi      # 0x6709
xorl %eax, %eax
callq 0x384d
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %rdi
movaps %xmm0, (%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, 0x20(%rdi)
movups %xmm0, 0x2c(%rdi)
movl 0x6938(%rip), %esi      # 0x91c8
callq 0x2160
testl %eax, %eax
jns 0x28ac
leaq 0x3e97(%rip), %rdi      # 0x6737
movq 0x20(%rsp), %rsi
xorl %eax, %eax
callq 0x3973
leaq 0x50(%rsp), %rbx
orl $0x8b0, 0x8(%rbx)       # imm = 0x8B0
orb $0x4, (%rbx)
movw $0x100, 0x16(%rbx)      # imm = 0x100
movl %ebp, %edi
xorl %esi, %esi
callq 0x20e0
movl %ebp, %edi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x2270
leaq 0x3e75(%rip), %rdi      # 0x6752
xorl %eax, %eax
callq 0x384d
leaq 0x68e1(%rip), %rdx      # 0x91cc
movl $0x3, %ecx
movl %ebp, %edi
movl $0xa1, %esi
xorl %r8d, %r8d
callq 0x2d0e
movl $0x2, %edi
callq 0x2300
jmp 0x2920
movb $0x1, 0x6908(%rip)      # 0x921a
leaq 0x3da8(%rip), %rdi      # 0x66c1
xorl %eax, %eax
callq 0x384d
cmpb $0x0, 0x68f2(%rip)      # 0x9219
jne 0x2961
xorl %ebx, %ebx
cmpl $0x9, %ebx
jg 0x2961
movl 0x68da(%rip), %ecx      # 0x9210
movl %ebp, %edi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x46bf
movb $0x1, %cl
testb $0x1, %al
je 0x295d
movl %ebp, %edi
callq 0x2eb1
xorl %ecx, %ecx
testl %eax, %eax
setg %cl
addl %ecx, %ebx
testb %cl, %cl
jne 0x292b
leaq 0x3cc6(%rip), %r13      # 0x662e
leaq 0x320(%rsp), %r15
leaq 0x68a9(%rip), %r12      # 0x9220
movl %ebp, 0x10(%rsp)
cmpb $0x0, 0x687e(%rip)      # 0x9200
movl %ebp, %edi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmovnel %eax, %edi
movzbl 0x6875(%rip), %esi      # 0x9208
andl $0x1, %esi
negl %esi
movl 0x6872(%rip), %ecx      # 0x9210
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x46bf
movl %eax, %ebx
testb $0x1, %bl
je 0x29be
movl %ebp, %edi
callq 0x2eb1
testl %eax, %eax
jle 0x2cc4
testb $0x2, %bl
je 0x2a03
movl $0x384, %edx            # imm = 0x384
xorl %edi, %edi
movq %r15, %rsi
callq 0x2190
movq %rax, %rbx
testq %rax, %rax
jg 0x29f5
cmpb $0x0, 0x6838(%rip)      # 0x9219
jne 0x2cf5
leaq 0x3e93(%rip), %rdi      # 0x6881
xorl %eax, %eax
callq 0x3973
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x3bd6
movq 0x6816(%rip), %r14      # 0x9220
testq %r14, %r14
cmoveq %r13, %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x20f0
testq %rax, %rax
je 0x2c7d
movq %rax, %rbx
subq %r14, %rbx
leaq 0x1(%rbx), %rbp
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x21c0
movb $0x0, 0x321(%rsp,%rbx)
movq %r12, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x3b72
leaq 0x3e3c(%rip), %rdi      # 0x6897
movq %r15, %rsi
xorl %eax, %eax
callq 0x384d
cmpb $0x0, 0x6798(%rip)      # 0x9204
jne 0x2c1b
movq $0x0, 0x40(%rsp)
movzbl 0x320(%rsp), %eax
testl %eax, %eax
je 0x2c1b
cmpl $0xa, %eax
je 0x2c1b
movq %r15, %rdi
leaq 0x40(%rsp), %rsi
callq 0x596e
movq %r15, %rdi
movl $0x2c, %esi
callq 0x20f0
testq %rax, %rax
je 0x2c1b
movq %rax, %r14
movq %rax, %rdi
leaq 0x3cc3(%rip), %rsi      # 0x6787
leaq 0x34(%rsp), %rdx
leaq 0x30(%rsp), %rcx
leaq 0x4c(%rsp), %r8
leaq 0x2c(%rsp), %r9
xorl %eax, %eax
leaq 0x4(%rsp), %r10
pushq %r10
leaq 0x24(%rsp), %r10
pushq %r10
callq 0x2210
addq $0x10, %rsp
movl %eax, %ebx
leaq 0x3ca0(%rip), %rdi      # 0x679a
movq %r14, %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x384d
cmpl $0x5, %ebx
jne 0x2bd2
movl 0x30(%rsp), %ecx
cmpl $0x3ffff, %ecx          # imm = 0x3FFFF
ja 0x2c1b
movslq 0x4(%rsp), %rax
movb 0x34(%rsp), %dl
movb %dl, 0x90(%rsp)
movb %cl, 0x91(%rsp)
movb %ch, 0x92(%rsp)
shrl $0x10, %ecx
movb %cl, 0x93(%rsp)
movb 0x2c(%rsp), %cl
movb %cl, 0x94(%rsp)
movl 0x1c(%rsp), %ecx
movb %cl, 0x95(%rsp)
testl %ecx, %ecx
je 0x2be7
leaq (%r14,%rax), %rbp
decq %rbp
movl $0x6, %r14d
movq %rbp, %rdi
leaq 0x3c3a(%rip), %rsi      # 0x67b6
leaq 0x28(%rsp), %rdx
leaq 0x4(%rsp), %rcx
xorl %eax, %eax
callq 0x2210
cmpl $0x1, %eax
jne 0x2c67
movl 0x28(%rsp), %eax
cmpl $0xff, %eax
ja 0x2c67
leaq 0x1(%r14), %rbx
movb %al, 0x90(%rsp,%r14)
cmpq $0x1f2, %r14            # imm = 0x1F2
ja 0x2bec
movslq 0x4(%rsp), %rax
addq %rax, %rbp
addl $-0x5, %r14d
cmpl 0x1c(%rsp), %r14d
movq %rbx, %r14
jb 0x2b72
jmp 0x2bec
leaq 0x3c17(%rip), %rdi      # 0x67f0
movq %r15, %rsi
movl %ebx, %edx
xorl %eax, %eax
callq 0x38e0
jmp 0x2c1b
movl $0x6, %ebx
leaq 0x3c2c(%rip), %rdi      # 0x681f
movq %r15, %rsi
xorl %eax, %eax
callq 0x384d
movq 0x40(%rsp), %r8
movl 0x10(%rsp), %edi
movl $0x94, %esi
leaq 0x90(%rsp), %rdx
movq %rbx, %rcx
callq 0x2d0e
cmpb $0x1, 0x65f6(%rip)      # 0x9218
jne 0x2c3e
movq 0x63ad(%rip), %rbx      # 0x8fd8
movq (%rbx), %rsi
movq %r15, %rdi
callq 0x2150
movq (%rbx), %rdi
callq 0x2200
movq 0x65db(%rip), %r14      # 0x9220
testq %r14, %r14
cmoveq %r13, %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x20f0
movq %rax, %rbx
testq %rax, %rax
jne 0x2a2a
jmp 0x2c7d
addl $-0x6, %r14d
leaq 0x3b4a(%rip), %rdi      # 0x67bc
movq %r15, %rsi
movl %r14d, %edx
jmp 0x2bde
xorl %edi, %edi
callq 0x21d0
subq 0x38(%rsp), %rax
movb $0x1, %bl
cmpq $0x15, %rax
jl 0x2cbe
movl $0x3, %ecx
movl 0x10(%rsp), %ebp
movl %ebp, %edi
movl $0xa1, %esi
leaq 0x6524(%rip), %rdx      # 0x91cc
xorl %r8d, %r8d
callq 0x2d0e
xorl %edi, %edi
callq 0x21d0
movq %rax, 0x38(%rsp)
jmp 0x2cc6
movl 0x10(%rsp), %ebp
jmp 0x2cc6
xorl %ebx, %ebx
testb %bl, %bl
jne 0x297b
movl %ebp, %edi
callq 0x2170
xorl %eax, %eax
addq $0x6a8, %rsp            # imm = 0x6A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3371(%rip), %rdi      # 0x6061
callq 0x20a0
xorl %edi, %edi
callq 0x22c0
movq 0x10(%rsp), %rdi
callq 0x23a4
movq %rbx, %rdi
callq 0x2380