// File: /workspace/github_c_makefile_star2_O3/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 $0x838, %rsp            # imm = 0x838
movq %rsi, %r15
movl %edi, %r12d
movq (%rsi), %rax
movq %rax, 0x40(%rsp)
xorl %edi, %edi
callq 0x21d0
movq %rax, 0x30(%rsp)
movq (%r15), %rdi
callq 0x3a2a
cmpl $0x2, %r12d
jl 0x2ccf
movl $0x1c200, %eax          # imm = 0x1C200
movq %rax, 0x28(%rsp)
leaq 0x3b32(%rip), %r13      # 0x603c
leaq 0x3b3a(%rip), %rbp      # 0x604b
movq $0x0, 0x8(%rsp)
movq 0x8(%r15), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x2040
testl %eax, %eax
je 0x2cbc
movq %r14, %rdi
movq %rbp, %rsi
callq 0x2040
testl %eax, %eax
je 0x25c7
movq %r14, %rdi
leaq 0x3b00(%rip), %rsi      # 0x604e
callq 0x2040
testl %eax, %eax
je 0x25d0
movq %r14, %rdi
leaq 0x3af0(%rip), %rsi      # 0x6051
callq 0x2040
testl %eax, %eax
je 0x25d9
movq %r14, %rdi
leaq 0x3ae0(%rip), %rsi      # 0x6054
callq 0x2040
testl %eax, %eax
je 0x25e0
leaq 0x8(%r15), %rbx
movq %r14, %rdi
leaq 0x3acc(%rip), %rsi      # 0x6057
callq 0x2040
cmpl $0x2, %r12d
je 0x2601
testl %eax, %eax
jne 0x2601
decl %r12d
movq 0x10(%r15), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x21b0
movq %rax, 0x6c5c(%rip)      # 0x9210
leaq 0x3a9f(%rip), %rdi      # 0x605a
movq %rax, %rsi
xorl %eax, %eax
callq 0x37ed
jmp 0x25e3
movb $0x1, 0x6c32(%rip)      # 0x9200
jmp 0x25e0
movb $0x1, 0x6c2d(%rip)      # 0x9204
jmp 0x25e0
movb $0x1, 0x6c28(%rip)      # 0x9208
movq %r15, %rbx
leal -0x1(%r12), %eax
addq $0x8, %rbx
movq %rbx, %r15
cmpl $0x2, %r12d
movl %eax, %r12d
jg 0x251a
jmp 0x2713
movq %r14, %rdi
leaq 0x3a6b(%rip), %rsi      # 0x6076
callq 0x2040
cmpl $0x2, %r12d
je 0x265c
testl %eax, %eax
jne 0x265c
movq 0x10(%r15), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x21b0
cmpl $0x383ff, %eax          # imm = 0x383FF
jg 0x269b
cmpl $0x9600, %eax           # imm = 0x9600
je 0x26d2
cmpl $0xe100, %eax           # imm = 0xE100
je 0x26e4
cmpl $0x1c200, %eax          # imm = 0x1C200
jne 0x26e0
movl $0x1002, %edx           # imm = 0x1002
jmp 0x26f0
movq %r14, %rdi
leaq 0x3a2e(%rip), %rsi      # 0x6094
callq 0x2040
testl %eax, %eax
je 0x26b7
movq %r14, %rdi
leaq 0x3a1e(%rip), %rsi      # 0x6097
callq 0x2040
testl %eax, %eax
je 0x26c6
movq %r15, %rbx
cmpq $0x0, 0x8(%rsp)
movq %r14, 0x8(%rsp)
je 0x25e3
jmp 0x2ccf
cmpl $0x38400, %eax          # imm = 0x38400
je 0x26d9
cmpl $0x70800, %eax          # imm = 0x70800
je 0x26eb
cmpl $0xe1000, %eax          # imm = 0xE1000
jne 0x26e0
movl $0x1007, %edx           # imm = 0x1007
jmp 0x26f0
movl $0x3, %edi
callq 0x3a01
jmp 0x25e0
movb $0x1, 0x6b4b(%rip)      # 0x9218
jmp 0x25e0
movl $0xf, %edx
jmp 0x26f0
movl $0x1003, %edx           # imm = 0x1003
jmp 0x26f0
movl %eax, %edx
jmp 0x26f0
movl $0x1001, %edx           # imm = 0x1001
jmp 0x26f0
movl $0x1004, %edx           # imm = 0x1004
decl %r12d
movl %edx, 0x6acf(%rip)      # 0x91c8
leaq 0x3979(%rip), %rdi      # 0x6079
movq %rax, 0x28(%rsp)
movl %eax, %esi
xorl %eax, %eax
callq 0x37ed
jmp 0x25e3
cmpq $0x0, 0x8(%rsp)
je 0x2ccf
leaq 0x3eed(%rip), %rdi      # 0x6613
movq 0x8(%rsp), %rbx
movq %rbx, %rsi
xorl %eax, %eax
callq 0x37ed
leaq 0x3ee3(%rip), %rsi      # 0x661f
movl $0x4, %edx
movq %rbx, %rdi
callq 0x2070
testl %eax, %eax
je 0x288f
movzbl 0x6ab0(%rip), %eax      # 0x9208
xorl $0x1, %eax
cmpb $0x0, 0x6a9e(%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 0x3ecb(%rip), %rdi      # 0x6654
movl %eax, %esi
xorl %eax, %eax
callq 0x37ed
testl %ebp, %ebp
jns 0x27a7
leaq 0x3ec0(%rip), %rdi      # 0x665d
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3913
leaq 0x90(%rsp), %rsi
movl %ebp, %edi
callq 0x2310
testl %eax, %eax
jns 0x27cb
leaq 0x3ec0(%rip), %rdi      # 0x6681
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3913
movl $0xf000, %eax           # imm = 0xF000
andl 0xa8(%rsp), %eax
cmpl $0x8000, %eax           # imm = 0x8000
sete 0x6a36(%rip)            # 0x9219
je 0x28b8
leaq 0x3efd(%rip), %rdi      # 0x66ed
xorl %eax, %eax
callq 0x37ed
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %rdi
movaps %xmm0, (%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, 0x20(%rdi)
movups %xmm0, 0x2c(%rdi)
movl 0x69b4(%rip), %esi      # 0x91c8
callq 0x2160
testl %eax, %eax
jns 0x2830
leaq 0x3ef7(%rip), %rdi      # 0x671b
movq 0x28(%rsp), %rsi
xorl %eax, %eax
callq 0x3913
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 0x3ed5(%rip), %rdi      # 0x6736
xorl %eax, %eax
callq 0x37ed
leaq 0x695d(%rip), %rdx      # 0x91cc
movl $0x3, %ecx
movl %ebp, %edi
movl $0xa1, %esi
xorl %r8d, %r8d
callq 0x2ce1
movl $0x2, %edi
callq 0x2300
jmp 0x28fb
movq %rbx, %rdi
callq 0x4424
movl %eax, %ebp
leaq 0x3d84(%rip), %rdi      # 0x6624
movl %eax, %esi
xorl %eax, %eax
callq 0x37ed
movb $0x1, 0x6969(%rip)      # 0x9219
testl %ebp, %ebp
js 0x2cd9
movq %rbx, %rdi
callq 0x20d0
leaq (%rbx,%rax), %rdi
addq $-0x4, %rdi
leaq 0x3dd1(%rip), %rsi      # 0x66a0
movl $0x4, %edx
callq 0x2070
testl %eax, %eax
je 0x28e6
leaq 0x3dd4(%rip), %rdi      # 0x66b8
jmp 0x28f4
movb $0x1, 0x692d(%rip)      # 0x921a
leaq 0x3db1(%rip), %rdi      # 0x66a5
xorl %eax, %eax
callq 0x37ed
cmpb $0x0, 0x6917(%rip)      # 0x9219
jne 0x2933
xorl %ebx, %ebx
movl 0x6904(%rip), %ecx      # 0x9210
movl %ebp, %edi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x4641
testb $0x1, %al
je 0x292e
movl %ebp, %edi
callq 0x2e81
testl %eax, %eax
jle 0x2933
incl %ebx
cmpl $0xa, %ebx
jl 0x2906
leaq 0x3cd8(%rip), %r13      # 0x6612
leaq 0x4b0(%rsp), %r15
leaq 0x68d7(%rip), %r12      # 0x9220
leaq 0x120(%rsp), %r14
movl %ebp, 0x8(%rsp)
cmpb $0x0, 0x68a4(%rip)      # 0x9200
movl %ebp, %edi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmovnel %eax, %edi
movzbl 0x689b(%rip), %esi      # 0x9208
andl $0x1, %esi
negl %esi
movl 0x6898(%rip), %ecx      # 0x9210
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x4641
movl %eax, %ebx
testb $0x1, %bl
je 0x2998
movl %ebp, %edi
callq 0x2e81
testl %eax, %eax
jle 0x2ca1
testb $0x2, %bl
je 0x29dd
movl $0x384, %edx            # imm = 0x384
xorl %edi, %edi
movq %r14, %rsi
callq 0x2190
movq %rax, %rbx
testq %rax, %rax
jg 0x29cf
cmpb $0x0, 0x685e(%rip)      # 0x9219
jne 0x2cc8
leaq 0x3e9d(%rip), %rdi      # 0x6865
xorl %eax, %eax
callq 0x3913
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x3b54
movq 0x683c(%rip), %r14      # 0x9220
testq %r14, %r14
cmoveq %r13, %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x20f0
testq %rax, %rax
je 0x2c57
movq %rax, %rbx
subq %r14, %rbx
leaq 0x1(%rbx), %rbp
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x21c0
movb $0x0, 0x4b1(%rsp,%rbx)
movq %r12, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x3af9
leaq 0x3e46(%rip), %rdi      # 0x687b
movq %r15, %rsi
xorl %eax, %eax
callq 0x37ed
cmpb $0x0, 0x67be(%rip)      # 0x9204
jne 0x2bf5
movq $0x0, 0x38(%rsp)
movzbl 0x4b0(%rsp), %eax
testl %eax, %eax
je 0x2bf5
cmpl $0xa, %eax
je 0x2bf5
movq %r15, %rdi
leaq 0x38(%rsp), %rsi
callq 0x58ca
movq %r15, %rdi
movl $0x2c, %esi
callq 0x20f0
testq %rax, %rax
je 0x2bf5
movq %rax, %r14
movq %rax, %rdi
leaq 0x3ccd(%rip), %rsi      # 0x676b
leaq 0x24(%rsp), %rdx
leaq 0x20(%rsp), %rcx
leaq 0x4c(%rsp), %r8
leaq 0x1c(%rsp), %r9
xorl %eax, %eax
leaq 0x10(%rsp), %r10
pushq %r10
leaq 0x1c(%rsp), %r10
pushq %r10
callq 0x2210
addq $0x10, %rsp
movl %eax, %ebx
leaq 0x3caa(%rip), %rdi      # 0x677e
movq %r14, %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x37ed
cmpl $0x5, %ebx
jne 0x2bac
movl 0x20(%rsp), %ecx
cmpl $0x3ffff, %ecx          # imm = 0x3FFFF
ja 0x2bf5
movslq 0x10(%rsp), %rax
movb 0x24(%rsp), %dl
movb %dl, 0x120(%rsp)
movb %cl, 0x121(%rsp)
movb %ch, 0x122(%rsp)
shrl $0x10, %ecx
movb %cl, 0x123(%rsp)
movb 0x1c(%rsp), %cl
movb %cl, 0x124(%rsp)
movl 0x14(%rsp), %ecx
movb %cl, 0x125(%rsp)
testl %ecx, %ecx
je 0x2bc1
leaq (%r14,%rax), %rbp
decq %rbp
movl $0x6, %r14d
movq %rbp, %rdi
leaq 0x3c44(%rip), %rsi      # 0x679a
leaq 0x18(%rsp), %rdx
leaq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0x2210
cmpl $0x1, %eax
jne 0x2c41
movl 0x18(%rsp), %eax
cmpl $0xff, %eax
ja 0x2c41
leaq 0x1(%r14), %rbx
movb %al, 0x120(%rsp,%r14)
cmpq $0x1f2, %r14            # imm = 0x1F2
ja 0x2bc6
movslq 0x10(%rsp), %rax
addq %rax, %rbp
addl $-0x5, %r14d
cmpl 0x14(%rsp), %r14d
movq %rbx, %r14
jb 0x2b4c
jmp 0x2bc6
leaq 0x3c21(%rip), %rdi      # 0x67d4
movq %r15, %rsi
movl %ebx, %edx
xorl %eax, %eax
callq 0x3880
jmp 0x2bf5
movl $0x6, %ebx
leaq 0x3c36(%rip), %rdi      # 0x6803
movq %r15, %rsi
xorl %eax, %eax
callq 0x37ed
movq 0x38(%rsp), %r8
movl 0x8(%rsp), %edi
movl $0x94, %esi
leaq 0x120(%rsp), %rdx
movq %rbx, %rcx
callq 0x2ce1
cmpb $0x1, 0x661c(%rip)      # 0x9218
jne 0x2c18
movq 0x63d3(%rip), %rbx      # 0x8fd8
movq (%rbx), %rsi
movq %r15, %rdi
callq 0x2150
movq (%rbx), %rdi
callq 0x2200
movq 0x6601(%rip), %r14      # 0x9220
testq %r14, %r14
cmoveq %r13, %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x20f0
movq %rax, %rbx
testq %rax, %rax
jne 0x2a04
jmp 0x2c57
addl $-0x6, %r14d
leaq 0x3b54(%rip), %rdi      # 0x67a0
movq %r15, %rsi
movl %r14d, %edx
jmp 0x2bb8
xorl %edi, %edi
callq 0x21d0
subq 0x30(%rsp), %rax
cmpq $0x15, %rax
movl 0x8(%rsp), %ebp
jl 0x2c94
movl $0x3, %ecx
movl %ebp, %edi
movl $0xa1, %esi
leaq 0x654c(%rip), %rdx      # 0x91cc
xorl %r8d, %r8d
callq 0x2ce1
xorl %edi, %edi
callq 0x21d0
movq %rax, 0x30(%rsp)
leaq 0x120(%rsp), %r14
jmp 0x2955
movl %ebp, %edi
callq 0x2170
xorl %eax, %eax
addq $0x838, %rsp            # imm = 0x838
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3382(%rip), %rdi      # 0x6045
callq 0x20a0
xorl %edi, %edi
callq 0x22c0
movq 0x40(%rsp), %rdi
callq 0x23a4
movq %rbx, %rdi
callq 0x2380