// File: /workspace/github_c_makefile_star2_O2/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, %rbx
movl %edi, %ebp
movq (%rsi), %rax
movq %rax, 0x40(%rsp)
xorl %edi, %edi
callq 0x21d0
movq %rax, 0x30(%rsp)
movq (%rbx), %rdi
callq 0x3909
movl $0x1c200, %eax          # imm = 0x1C200
movq %rax, 0x28(%rsp)
leaq 0x3b8d(%rip), %r12      # 0x603c
leaq 0x3b95(%rip), %r13      # 0x604b
movq $0x0, 0x8(%rsp)
cmpl $0x2, %ebp
jl 0x2691
movq 0x8(%rbx), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x2040
testl %eax, %eax
je 0x2c08
movq %r15, %rdi
movq %r13, %rsi
callq 0x2040
testl %eax, %eax
je 0x256d
movq %r15, %rdi
leaq 0x3b56(%rip), %rsi      # 0x604e
callq 0x2040
testl %eax, %eax
je 0x2576
movq %r15, %rdi
leaq 0x3b46(%rip), %rsi      # 0x6051
callq 0x2040
testl %eax, %eax
je 0x257f
movq %r15, %rdi
leaq 0x3b36(%rip), %rsi      # 0x6054
callq 0x2040
testl %eax, %eax
je 0x2586
leaq 0x8(%rbx), %r14
movq %r15, %rdi
leaq 0x3b22(%rip), %rsi      # 0x6057
callq 0x2040
cmpl $0x2, %ebp
je 0x2597
testl %eax, %eax
jne 0x2597
decl %ebp
movq 0x10(%rbx), %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x21b0
movq %rax, 0x7cb6(%rip)      # 0xa210
leaq 0x3af9(%rip), %rdi      # 0x605a
movq %rax, %rsi
xorl %eax, %eax
callq 0x36d4
jmp 0x2589
movb $0x1, 0x7c8c(%rip)      # 0xa200
jmp 0x2586
movb $0x1, 0x7c87(%rip)      # 0xa204
jmp 0x2586
movb $0x1, 0x7c82(%rip)      # 0xa208
movq %rbx, %r14
decl %ebp
addq $0x8, %r14
movq %r14, %rbx
jmp 0x24bf
movq %r15, %rdi
leaq 0x3ad5(%rip), %rsi      # 0x6076
callq 0x2040
cmpl $0x2, %ebp
je 0x25f9
testl %eax, %eax
jne 0x25f9
movq 0x10(%rbx), %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x21b0
pushq $0xf
popq %rdx
cmpl $0x9600, %eax           # imm = 0x9600
je 0x266f
cmpl $0xe1000, %eax          # imm = 0xE1000
je 0x266a
cmpl $0x1c200, %eax          # imm = 0x1C200
je 0x2651
cmpl $0x38400, %eax          # imm = 0x38400
je 0x265f
cmpl $0x70800, %eax          # imm = 0x70800
je 0x2658
cmpl $0xe100, %eax           # imm = 0xE100
jne 0x2666
movl $0x1001, %edx           # imm = 0x1001
jmp 0x266f
movq %r15, %rdi
leaq 0x3a91(%rip), %rsi      # 0x6094
callq 0x2040
testl %eax, %eax
je 0x2638
movq %r15, %rdi
leaq 0x3a81(%rip), %rsi      # 0x6097
callq 0x2040
testl %eax, %eax
je 0x2645
movq %rbx, %r14
cmpq $0x0, 0x8(%rsp)
movq %r15, 0x8(%rsp)
je 0x2589
jmp 0x2c1b
pushq $0x3
popq %rdi
callq 0x38e2
jmp 0x2586
movb $0x1, 0x7bcc(%rip)      # 0xa218
jmp 0x2586
movl $0x1002, %edx           # imm = 0x1002
jmp 0x266f
movl $0x1004, %edx           # imm = 0x1004
jmp 0x266f
movl $0x1003, %edx           # imm = 0x1003
jmp 0x266f
movl %eax, %edx
jmp 0x266f
movl $0x1007, %edx           # imm = 0x1007
decl %ebp
movl %edx, 0x7b51(%rip)      # 0xa1c8
leaq 0x39fb(%rip), %rdi      # 0x6079
movq %rax, 0x28(%rsp)
movl %eax, %esi
xorl %eax, %eax
callq 0x36d4
jmp 0x2589
cmpq $0x0, 0x8(%rsp)
je 0x2c1b
leaq 0x3f6f(%rip), %rdi      # 0x6613
movq 0x8(%rsp), %rbx
movq %rbx, %rsi
xorl %eax, %eax
callq 0x36d4
leaq 0x3f65(%rip), %rsi      # 0x661f
pushq $0x4
popq %rdx
movq %rbx, %rdi
callq 0x2070
testl %eax, %eax
je 0x280d
movzbl 0x7b34(%rip), %eax      # 0xa208
xorl $0x1, %eax
cmpb $0x0, 0x7b22(%rip)      # 0xa200
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, %r14d
leaq 0x3f4e(%rip), %rdi      # 0x6654
movl %eax, %esi
xorl %eax, %eax
callq 0x36d4
testl %r14d, %r14d
jns 0x2725
leaq 0x3f42(%rip), %rdi      # 0x665d
movq %rbx, %rsi
xorl %eax, %eax
callq 0x37f6
leaq 0x90(%rsp), %rsi
movl %r14d, %edi
callq 0x2310
testl %eax, %eax
jns 0x274a
leaq 0x3f41(%rip), %rdi      # 0x6681
movq %rbx, %rsi
xorl %eax, %eax
callq 0x37f6
movl $0xf000, %eax           # imm = 0xF000
andl 0xa8(%rsp), %eax
cmpl $0x8000, %eax           # imm = 0x8000
sete 0x7ab7(%rip)            # 0xa219
je 0x2838
leaq 0x3f7e(%rip), %rdi      # 0x66ed
xorl %eax, %eax
callq 0x36d4
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %rdi
movaps %xmm0, (%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, 0x20(%rdi)
movups %xmm0, 0x2c(%rdi)
movl 0x7a35(%rip), %esi      # 0xa1c8
callq 0x2160
testl %eax, %eax
jns 0x27af
leaq 0x3f78(%rip), %rdi      # 0x671b
movq 0x28(%rsp), %rsi
xorl %eax, %eax
callq 0x37f6
leaq 0x50(%rsp), %rbx
orl $0x8b0, 0x8(%rbx)       # imm = 0x8B0
orb $0x4, (%rbx)
movw $0x100, 0x16(%rbx)      # imm = 0x100
movl %r14d, %edi
xorl %esi, %esi
callq 0x20e0
movl %r14d, %edi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x2270
leaq 0x3f54(%rip), %rdi      # 0x6736
xorl %eax, %eax
callq 0x36d4
leaq 0x79dc(%rip), %rdx      # 0xa1cc
pushq $0x3
popq %rcx
movl %r14d, %edi
movl $0xa1, %esi
xorl %r8d, %r8d
callq 0x2c63
pushq $0x2
popq %rdi
callq 0x2300
jmp 0x2879
movq %rbx, %rdi
callq 0x425d
movl %eax, %r14d
leaq 0x3e05(%rip), %rdi      # 0x6624
movl %eax, %esi
xorl %eax, %eax
callq 0x36d4
movb $0x1, 0x79ea(%rip)      # 0xa219
testl %r14d, %r14d
js 0x2c40
movq %rbx, %rdi
callq 0x20d0
leaq (%rbx,%rax), %rdi
addq $-0x4, %rdi
leaq 0x3e51(%rip), %rsi      # 0x66a0
pushq $0x4
popq %rdx
callq 0x2070
testl %eax, %eax
je 0x2864
leaq 0x3e56(%rip), %rdi      # 0x66b8
jmp 0x2872
movb $0x1, 0x79af(%rip)      # 0xa21a
leaq 0x3e33(%rip), %rdi      # 0x66a5
xorl %eax, %eax
callq 0x36d4
cmpb $0x0, 0x7999(%rip)      # 0xa219
je 0x2bd1
pushq $0xa
popq %r15
leaq 0x4b0(%rsp), %r12
leaq 0x7987(%rip), %r13      # 0xa220
movl %r14d, 0x8(%rsp)
cmpb $0x0, 0x795b(%rip)      # 0xa200
movl %r14d, %edi
pushq $-0x1
popq %rdx
cmovnel %edx, %edi
movzbl 0x7953(%rip), %esi      # 0xa208
andl $0x1, %esi
negl %esi
movl 0x7950(%rip), %ecx      # 0xa210
callq 0x446f
movl %eax, %ebp
testb $0x1, %bpl
je 0x28dd
movl %r14d, %edi
callq 0x2df6
testl %eax, %eax
jle 0x2bb5
testb $0x2, %bpl
je 0x292b
movl $0x384, %edx            # imm = 0x384
xorl %edi, %edi
leaq 0x120(%rsp), %rbx
movq %rbx, %rsi
callq 0x2190
movq %rax, %r14
testq %rax, %rax
jg 0x291d
cmpb $0x0, 0x7910(%rip)      # 0xa219
jne 0x2c14
leaq 0x3f4f(%rip), %rdi      # 0x6865
xorl %eax, %eax
callq 0x37f6
movq %r13, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x3a2b
movq 0x78ee(%rip), %r14      # 0xa220
testq %r14, %r14
leaq 0x3cd6(%rip), %rax      # 0x6612
cmoveq %rax, %r14
movq %r14, %rdi
movl %r15d, %esi
callq 0x20f0
testq %rax, %rax
je 0x2b6f
movq %rax, %rbp
subq %r14, %rbp
leaq 0x1(%rbp), %rbx
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x21c0
movb $0x0, 0x4b1(%rsp,%rbp)
movq %r13, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x39d0
leaq 0x3ef3(%rip), %rdi      # 0x687b
movq %r12, %rsi
xorl %eax, %eax
callq 0x36d4
cmpb $0x0, 0x786b(%rip)      # 0xa204
jne 0x2b01
andq $0x0, 0x38(%rsp)
movzbl 0x4b0(%rsp), %eax
testl %eax, %eax
je 0x2b01
cmpl $0xa, %eax
je 0x2b01
movq %r12, %rdi
leaq 0x38(%rsp), %rsi
callq 0x55f4
movq %r12, %rdi
pushq $0x2c
popq %rsi
callq 0x20f0
testq %rax, %rax
je 0x2b01
movq %rax, %r14
movq %rax, %rdi
leaq 0x3d7f(%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
popq %rcx
popq %rdx
movl %eax, %ebp
leaq 0x3d5e(%rip), %rdi      # 0x677e
movq %r14, %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x36d4
cmpl $0x5, %ebp
jne 0x2aee
movl 0x20(%rsp), %eax
cmpl $0x3ffff, %eax          # imm = 0x3FFFF
ja 0x2b01
movslq 0x10(%rsp), %rcx
addq %rcx, %r14
decq %r14
movb 0x24(%rsp), %cl
movb %cl, 0x120(%rsp)
movb %al, 0x121(%rsp)
movb %ah, 0x122(%rsp)
shrl $0x10, %eax
movb %al, 0x123(%rsp)
movb 0x1c(%rsp), %al
movb %al, 0x124(%rsp)
movl 0x14(%rsp), %eax
movb %al, 0x125(%rsp)
pushq $0x6
popq %rbp
cmpq $0x1f3, %rbp            # imm = 0x1F3
ja 0x2b2d
leal -0x6(%rbp), %ecx
cmpl %eax, %ecx
jae 0x2b2d
movq %r14, %rdi
leaq 0x3ced(%rip), %rsi      # 0x679a
leaq 0x18(%rsp), %rdx
leaq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0x2210
cmpl $0x1, %eax
jne 0x2b5e
movl 0x18(%rsp), %eax
cmpl $0xff, %eax
ja 0x2b5e
movb %al, 0x120(%rsp,%rbp)
incq %rbp
movslq 0x10(%rsp), %rax
addq %rax, %r14
movl 0x14(%rsp), %eax
jmp 0x2a8b
leaq 0x3cdf(%rip), %rdi      # 0x67d4
movq %r12, %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x3765
cmpb $0x1, 0x7710(%rip)      # 0xa218
jne 0x292b
movq 0x74c3(%rip), %rbx      # 0x9fd8
movq (%rbx), %rsi
movq %r12, %rdi
callq 0x2150
movq (%rbx), %rdi
callq 0x2200
jmp 0x292b
leaq 0x3ccf(%rip), %rdi      # 0x6803
movq %r12, %rsi
xorl %eax, %eax
callq 0x36d4
movq 0x38(%rsp), %r8
movl 0x8(%rsp), %edi
movl $0x94, %esi
leaq 0x120(%rsp), %rdx
movq %rbp, %rcx
callq 0x2c63
jmp 0x2b01
addl $-0x6, %ebp
leaq 0x3c38(%rip), %rdi      # 0x67a0
movq %r12, %rsi
movl %ebp, %edx
jmp 0x2afa
xorl %edi, %edi
callq 0x21d0
subq 0x30(%rsp), %rax
cmpq $0x15, %rax
movl 0x8(%rsp), %r14d
jl 0x289e
movl %r14d, %edi
movl $0xa1, %esi
leaq 0x7633(%rip), %rdx      # 0xa1cc
pushq $0x3
popq %rcx
xorl %r8d, %r8d
callq 0x2c63
xorl %edi, %edi
callq 0x21d0
movq %rax, 0x30(%rsp)
jmp 0x289e
movl %r14d, %edi
callq 0x2170
xorl %eax, %eax
addq $0x838, %rsp            # imm = 0x838
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebp, %ebp
pushq $-0x1
popq %rbx
cmpl $0x9, %ebp
ja 0x2886
movl 0x762b(%rip), %ecx      # 0xa210
movl %r14d, %edi
movl %ebx, %esi
movl %ebx, %edx
callq 0x446f
testb $0x1, %al
je 0x2bd6
movl %r14d, %edi
callq 0x2df6
incl %ebp
testl %eax, %eax
jg 0x2bd6
jmp 0x2886
leaq 0x3436(%rip), %rdi      # 0x6045
callq 0x20a0
xorl %edi, %edi
callq 0x22c0
movq 0x73d6(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
leaq 0x346e(%rip), %rsi      # 0x609a
movq 0x40(%rsp), %rcx
movq %rcx, %rdx
movq %rcx, %r8
xorl %eax, %eax
callq 0x21a0
jmp 0x2c5b
movq 0x73b1(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
leaq 0x39e0(%rip), %rsi      # 0x6631
movq %rbx, %rdx
xorl %eax, %eax
callq 0x21a0
pushq $0x1
popq %rdi
callq 0x22c0