// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x4b0, %rsp            # imm = 0x4B0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movl $0x1c200, -0xf4(%rbp)   # imm = 0x1C200
xorl %eax, %eax
movl %eax, %edi
callq 0x21d0
movq %rax, -0x100(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x4610
cmpl $0x1, -0x8(%rbp)
jle 0x27c8
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x5b29(%rip), %rsi      # 0x8004
callq 0x2040
cmpl $0x0, %eax
jne 0x2501
leaq 0x6829(%rip), %rdi      # 0x8d15
leaq 0x5b1a(%rip), %rsi      # 0x800d
movb $0x0, %al
callq 0x20f0
xorl %edi, %edi
callq 0x22c0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x5b03(%rip), %rsi      # 0x8013
callq 0x2040
cmpl $0x0, %eax
jne 0x2529
movl $0x1, 0x8d0c(%rip)      # 0xb230
jmp 0x27ac
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x5ade(%rip), %rsi      # 0x8016
callq 0x2040
cmpl $0x0, %eax
jne 0x2551
movl $0x1, 0x8ce8(%rip)      # 0xb234
jmp 0x27aa
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x5ab9(%rip), %rsi      # 0x8019
callq 0x2040
cmpl $0x0, %eax
jne 0x2579
movl $0x1, 0x8cc4(%rip)      # 0xb238
jmp 0x27a8
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x5a94(%rip), %rsi      # 0x801c
callq 0x2040
cmpl $0x0, %eax
jne 0x25a1
movl $0x1, 0x8ca0(%rip)      # 0xb23c
jmp 0x27a6
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x5a6f(%rip), %rsi      # 0x801f
callq 0x2040
cmpl $0x0, %eax
jne 0x260c
cmpl $0x2, -0x8(%rbp)
jle 0x260c
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x21b0
movq %rax, 0x8c4e(%rip)      # 0xb240
movq 0x8c47(%rip), %rsi      # 0xb240
leaq 0x5a22(%rip), %rdi      # 0x8022
movb $0x0, %al
callq 0x42e0
jmp 0x27a4
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x5a23(%rip), %rsi      # 0x803e
callq 0x2040
cmpl $0x0, %eax
jne 0x2733
cmpl $0x2, -0x8(%rbp)
jle 0x2733
movl -0x8(%rbp), %eax
decl %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x21b0
movl %eax, -0xf4(%rbp)
movl -0xf4(%rbp), %eax
movl %eax, -0x498(%rbp)
subl $0x9600, %eax           # imm = 0x9600
je 0x26c3
jmp 0x2678
movl -0x498(%rbp), %eax
subl $0xe100, %eax           # imm = 0xE100
je 0x26cf
jmp 0x2687
movl -0x498(%rbp), %eax
subl $0x1c200, %eax          # imm = 0x1C200
je 0x26db
jmp 0x2696
movl -0x498(%rbp), %eax
subl $0x38400, %eax          # imm = 0x38400
je 0x26e7
jmp 0x26a5
movl -0x498(%rbp), %eax
subl $0x70800, %eax          # imm = 0x70800
je 0x26f3
jmp 0x26b4
movl -0x498(%rbp), %eax
subl $0xe1000, %eax          # imm = 0xE1000
je 0x26ff
jmp 0x270b
movl $0xf, 0x8b03(%rip)      # 0xb1d0
jmp 0x2717
movl $0x1001, 0x8af7(%rip)   # imm = 0x1001
jmp 0x2717
movl $0x1002, 0x8aeb(%rip)   # imm = 0x1002
jmp 0x2717
movl $0x1003, 0x8adf(%rip)   # imm = 0x1003
jmp 0x2717
movl $0x1004, 0x8ad3(%rip)   # imm = 0x1004
jmp 0x2717
movl $0x1007, 0x8ac7(%rip)   # imm = 0x1007
jmp 0x2717
movl -0xf4(%rbp), %eax
movl %eax, 0x8ab9(%rip)      # 0xb1d0
movl -0xf4(%rbp), %esi
movl 0x8aad(%rip), %edx      # 0xb1d0
leaq 0x5917(%rip), %rdi      # 0x8041
movb $0x0, %al
callq 0x42e0
jmp 0x27a2
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x591a(%rip), %rsi      # 0x805c
callq 0x2040
cmpl $0x0, %eax
jne 0x2758
movl $0x3, %edi
callq 0x4570
jmp 0x27a0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x58f8(%rip), %rsi      # 0x805f
callq 0x2040
cmpl $0x0, %eax
jne 0x277d
movl $0x1, 0x8acd(%rip)      # 0xb248
jmp 0x279e
cmpq $0x0, -0x60(%rbp)
jne 0x2792
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x60(%rbp)
jmp 0x279c
movq $0x0, -0x60(%rbp)
jmp 0x27c8
jmp 0x279e
jmp 0x27a0
jmp 0x27a2
jmp 0x27a4
jmp 0x27a6
jmp 0x27a8
jmp 0x27aa
jmp 0x27ac
jmp 0x27ae
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
jmp 0x24c2
cmpq $0x0, -0x60(%rbp)
jne 0x27fd
movq 0x8822(%rip), %rax      # 0xaff8
movq (%rax), %rdi
movq -0x58(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq -0x58(%rbp), %r8
leaq 0x5876(%rip), %rsi      # 0x8062
movb $0x0, %al
callq 0x21a0
movl $0x1, %edi
callq 0x22c0
movq -0x60(%rbp), %rsi
leaq 0x5dd3(%rip), %rdi      # 0x85db
movb $0x0, %al
callq 0x42e0
movq -0x60(%rbp), %rdi
leaq 0x5dcd(%rip), %rsi      # 0x85e7
movl $0x4, %edx
callq 0x2070
cmpl $0x0, %eax
jne 0x287e
movq -0x60(%rbp), %rdi
callq 0x5780
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %esi
leaq 0x5dad(%rip), %rdi      # 0x85ec
movb $0x0, %al
callq 0x42e0
movb $0x1, 0x89ff(%rip)      # 0xb24c
cmpl $0x0, -0x14(%rbp)
jge 0x2879
movq 0x879e(%rip), %rax      # 0xaff8
movq (%rax), %rdi
movq -0x60(%rbp), %rdx
leaq 0x5d91(%rip), %rsi      # 0x85f9
movb $0x0, %al
callq 0x21a0
movl $0x1, %edi
callq 0x22c0
jmp 0x295f
movl $0x900, -0x104(%rbp)    # imm = 0x900
cmpl $0x0, 0x89a1(%rip)      # 0xb230
je 0x28b1
movl -0x104(%rbp), %eax
orl $0x1, %eax
movl %eax, -0x104(%rbp)
movl -0x104(%rbp), %eax
orl $0x40, %eax
movl %eax, -0x104(%rbp)
jmp 0x28dc
cmpl $0x0, 0x8980(%rip)      # 0xb238
je 0x28cb
movl -0x104(%rbp), %eax
orl $0x0, %eax
movl %eax, -0x104(%rbp)
jmp 0x28da
movl -0x104(%rbp), %eax
orl $0x2, %eax
movl %eax, -0x104(%rbp)
jmp 0x28dc
movq -0x60(%rbp), %rdi
movl -0x104(%rbp), %esi
movl $0x1ff, %edx            # imm = 0x1FF
movb $0x0, %al
callq 0x2280
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %esi
leaq 0x5d1d(%rip), %rdi      # 0x861c
movb $0x0, %al
callq 0x42e0
cmpl $0x0, -0x14(%rbp)
jge 0x291e
movq -0x60(%rbp), %rsi
leaq 0x5d0e(%rip), %rdi      # 0x8625
movb $0x0, %al
callq 0x4440
movl -0x14(%rbp), %edi
leaq -0xf0(%rbp), %rsi
callq 0x2310
cmpl $0x0, %eax
jge 0x2944
movq -0x60(%rbp), %rsi
leaq 0x5d0c(%rip), %rdi      # 0x8649
movb $0x0, %al
callq 0x4440
movl -0xd8(%rbp), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x8000, %eax           # imm = 0x8000
sete %al
andb $0x1, %al
movb %al, 0x88ed(%rip)       # 0xb24c
testb $0x1, 0x88e6(%rip)      # 0xb24c
je 0x29ca
movq -0x60(%rbp), %rax
movq %rax, -0x4a0(%rbp)
movq -0x60(%rbp), %rdi
callq 0x20c0
movq -0x4a0(%rbp), %rdi
addq %rax, %rdi
addq $-0x4, %rdi
leaq 0x5cd7(%rip), %rsi      # 0x8668
movl $0x4, %edx
callq 0x2070
cmpl $0x0, %eax
jne 0x29b7
movb $0x1, 0x88a6(%rip)      # 0xb24d
leaq 0x5cbf(%rip), %rdi      # 0x866d
movb $0x0, %al
callq 0x42e0
jmp 0x29c5
leaq 0x5cc2(%rip), %rdi      # 0x8680
movb $0x0, %al
callq 0x42e0
jmp 0x2a7a
leaq 0x5ce4(%rip), %rdi      # 0x86b5
movb $0x0, %al
callq 0x42e0
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
movl $0x3c, %edx
callq 0x2150
movl 0x87e2(%rip), %esi      # 0xb1d0
leaq -0x50(%rbp), %rdi
callq 0x2160
cmpl $0x0, %eax
jge 0x2a10
movl -0xf4(%rbp), %esi
leaq 0x5cda(%rip), %rdi      # 0x86e3
movb $0x0, %al
callq 0x4440
movl -0x48(%rbp), %eax
orl $0x8b0, %eax            # imm = 0x8B0
movl %eax, -0x48(%rbp)
movl -0x50(%rbp), %eax
orl $0x4, %eax
movl %eax, -0x50(%rbp)
movb $0x1, -0x39(%rbp)
movb $0x0, -0x3a(%rbp)
movl -0x14(%rbp), %edi
xorl %esi, %esi
callq 0x20d0
movl -0x14(%rbp), %edi
xorl %esi, %esi
leaq -0x50(%rbp), %rdx
callq 0x2270
leaq 0x5cb3(%rip), %rdi      # 0x86fe
movb $0x0, %al
callq 0x42e0
movl -0x14(%rbp), %edi
movl $0xa1, %esi
leaq 0x8773(%rip), %rdx      # 0xb1d4
movl $0x3, %ecx
xorl %eax, %eax
movl %eax, %r8d
callq 0x2c40
movl $0x2, %edi
callq 0x2300
testb $0x1, 0x87cb(%rip)      # 0xb24c
jne 0x2ae4
movl $0x0, -0xf8(%rbp)
cmpl $0xa, -0xf8(%rbp)
jge 0x2ae2
movl -0x14(%rbp), %edi
movq 0x87a0(%rip), %rax      # 0xb240
movl %eax, %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %edx, %esi
callq 0x5b50
movl %eax, -0x108(%rbp)
movl -0x108(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jle 0x2ae0
movl -0x14(%rbp), %edi
callq 0x3030
cmpl $0x0, %eax
jg 0x2ad1
jmp 0x2ae2
movl -0xf8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf8(%rbp)
jmp 0x2a8d
jmp 0x2ae4
jmp 0x2ae6
cmpl $0x0, 0x8743(%rip)      # 0xb230
je 0x2afc
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x4a4(%rbp)
jmp 0x2b05
movl -0x14(%rbp), %eax
movl %eax, -0x4a4(%rbp)
movl -0x4a4(%rbp), %edi
movl 0x8727(%rip), %ecx      # 0xb238
xorl %esi, %esi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $0x0, %ecx
cmovnel %eax, %esi
movq 0x871b(%rip), %rax      # 0xb240
movl %eax, %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x5b50
movl %eax, -0x494(%rbp)
movl -0x494(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jle 0x2b59
movl -0x14(%rbp), %edi
callq 0x3030
cmpl $0x0, %eax
jg 0x2b57
jmp 0x2c2a
jmp 0x2b59
movl -0x494(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
jle 0x2b77
callq 0x31e0
testb $0x1, %al
jne 0x2b75
jmp 0x2c2a
jmp 0x2b77
jmp 0x2b79
leaq -0x490(%rbp), %rdi
movl $0x384, %esi            # imm = 0x384
callq 0x3260
testb $0x1, %al
jne 0x2b90
jmp 0x2be1
cmpl $0x0, 0x869d(%rip)      # 0xb234
jne 0x2ba8
movl -0x14(%rbp), %edi
leaq -0x490(%rbp), %rsi
callq 0x3380
cmpl $0x0, 0x8699(%rip)      # 0xb248
je 0x2bdf
movq 0x8420(%rip), %rax      # 0xafd8
movq (%rax), %rdi
leaq -0x490(%rbp), %rdx
leaq 0x62e3(%rip), %rsi      # 0x8eac
movb $0x0, %al
callq 0x21a0
movq 0x8401(%rip), %rax      # 0xafd8
movq (%rax), %rdi
callq 0x2200
jmp 0x2b79
xorl %eax, %eax
movl %eax, %edi
callq 0x21d0
subq -0x100(%rbp), %rax
cmpq $0x14, %rax
jle 0x2c25
movl -0x14(%rbp), %edi
movl $0xa1, %esi
leaq 0x85ce(%rip), %rdx      # 0xb1d4
movl $0x3, %ecx
xorl %eax, %eax
movl %eax, %r8d
callq 0x2c40
xorl %eax, %eax
movl %eax, %edi
callq 0x21d0
movq %rax, -0x100(%rbp)
jmp 0x2ae6
movl -0x14(%rbp), %edi
callq 0x2170
xorl %eax, %eax
addq $0x4b0, %rsp            # imm = 0x4B0
popq %rbp
retq
nopl (%rax)