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

  setProgName(argv[0]);

  while (argc > 1)
  {
    if (strcasecmp(argv[1], "-version") == 0)
    {
      printf("%s\n", VERSION);
      exit(0);
    }
    if (strcasecmp(argv[1], "-r") == 0)
    {
      readonly = 1;
    }
    else if (strcasecmp(argv[1], "-d") == 0)
    {
      setLogLevel(LOGLEVEL_DEBUG);
    }
    else if (strcasecmp(argv[1], "-?") == 0)
    {
      break;
    }
    else if (!device)
    {
      device = argv[1];
    }
    else
    {
      device = 0;
      break;
    }
    argc--;
    argv++;
  }

  if (!device)
  {
    fprintf(stderr,
            "Usage: nmea0183-serial [-r] [-d] <device> | -version\n\n"
            "-r : read-only, do not pass stdin to stdout\n"
            "-d : debug mode\n\n"
            "Example: nmea0183-serial /dev/ttyUSB0\n\n" COPYRIGHT);
    exit(1);
  }

  logDebug("Opening %s\n", device);
  handle = open(device, O_RDWR | O_NOCTTY);
  logDebug("fd = %d\n", handle);
  if (handle < 0)
  {
    logAbort("NMEA-00001: Cannot open NMEA-0183 device %s\n", device);
    exit(1);
  }
  if (fstat(handle, &statbuf) < 0)
  {
    logAbort("NMEA-00002: Cannot determine device %s\n", device);
    exit(1);
  }
  isFile = S_ISREG(statbuf.st_mode);

  if (!isFile)
  {
    logDebug("Device is a serial port, set the attributes.\n");

    memset(&attr, 0, sizeof(attr));
    attr.c_cflag     = B38400 | CS8 | CLOCAL | CREAD;
    attr.c_iflag     = IGNPAR;
    attr.c_oflag     = 0;
    attr.c_lflag     = 0;
    attr.c_cc[VMIN]  = 0;
    attr.c_cc[VTIME] = 1;
    tcflush(handle, TCIFLUSH);
    tcsetattr(handle, TCSANOW, &attr);
  }

  for (;;)
  {
    char                 msg[BUFFER_SIZE];
    enum ReadyDescriptor r;
    int                  b;

    r = isReady(handle, readonly ? INVALID_SOCKET : STDIN, INVALID_SOCKET, 0);

    if ((r & FD1_ReadReady) > 0)
    {
      b = read(handle, msg, sizeof(msg));
      if (b < 0)
      {
        break;
      }
      else if (b > 0)
      {
        if (write(STDOUT, msg, b) < b)
        {
          break;
        }
      }
    }
    if ((r & FD2_ReadReady) > 0)
    {
      b = read(0, msg, sizeof(msg));
      if (b < 0)
      {
        break;
      }
      else if (b > 0)
      {
        if (write(STDOUT, msg, b) < b)
        {
          break;
        }
        if (write(handle, msg, b) < b)
        {
          break;
        }
      }
    }
  }

  close(handle);
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x458, %rsp            # imm = 0x458
movq %rsi, %rbx
movl %edi, %ebp
movq (%rsi), %rdi
callq 0x2ae6
cmpl $0x2, %ebp
jl 0x24b6
incl %ebp
addq $0x8, %rbx
leaq 0x1bd4(%rip), %r15      # 0x4004
leaq 0x1bdc(%rip), %r12      # 0x4013
xorl %r13d, %r13d
movq (%rbx), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x2040
testl %eax, %eax
je 0x2645
movq %r14, %rdi
movq %r12, %rsi
callq 0x2040
testl %eax, %eax
je 0x2490
movq %r14, %rdi
leaq 0x1bad(%rip), %rsi      # 0x4016
callq 0x2040
testl %eax, %eax
je 0x2499
movq %r14, %rdi
leaq 0x1b9d(%rip), %rsi      # 0x4019
callq 0x2040
testl %eax, %eax
je 0x263d
testq %r13, %r13
je 0x24a6
jmp 0x24b6
movb $0x1, 0x4ced(%rip)      # 0x7184
jmp 0x24a3
movl $0x3, %edi
callq 0x2abd
movq %r13, %r14
decl %ebp
addq $0x8, %rbx
movq %r14, %r13
cmpl $0x2, %ebp
jg 0x243a
jmp 0x24b9
xorl %r14d, %r14d
testq %r14, %r14
je 0x2658
leaq 0x1ea9(%rip), %rdi      # 0x4372
movq %r14, %rsi
xorl %eax, %eax
callq 0x28a5
movq %r14, %rdi
movl $0x102, %esi            # imm = 0x102
xorl %eax, %eax
callq 0x2200
movl %eax, %ebx
leaq 0x1e93(%rip), %rdi      # 0x437e
movl %eax, %esi
xorl %eax, %eax
callq 0x28a5
testl %ebx, %ebx
js 0x265d
leaq 0x40(%rsp), %rsi
movl %ebx, %edi
callq 0x2270
testl %eax, %eax
js 0x2666
movl $0xf000, %eax           # imm = 0xF000
andl 0x58(%rsp), %eax
cmpl $0x8000, %eax           # imm = 0x8000
je 0x2572
leaq 0x1eb5(%rip), %rdi      # 0x43dc
xorl %eax, %eax
callq 0x28a5
xorps %xmm0, %xmm0
movq %rsp, %r14
movaps %xmm0, (%r14)
movaps %xmm0, 0x10(%r14)
movaps %xmm0, 0x20(%r14)
movups %xmm0, 0x2c(%r14)
movq $0x8bf, 0x8(%r14)       # imm = 0x8BF
movq $0x4, (%r14)
movw $0x1, 0x16(%r14)
movl %ebx, %edi
xorl %esi, %esi
callq 0x20c0
movl %ebx, %edi
xorl %esi, %esi
movq %r14, %rdx
callq 0x21f0
leaq 0xd0(%rsp), %r14
movzbl 0x4c03(%rip), %esi      # 0x7184
andl $0x1, %esi
negl %esi
movl %ebx, %edi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
callq 0x3717
movl %eax, %ebp
testb $0x1, %bpl
je 0x25d1
movl $0x384, %edx            # imm = 0x384
movl %ebx, %edi
movq %r14, %rsi
callq 0x2140
movq %rax, %r15
testl %r15d, %r15d
js 0x2622
je 0x25d1
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movl $0x1, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x20a0
cmpq %r15, %rax
jl 0x2622
testb $0x2, %bpl
je 0x257a
movl $0x384, %edx            # imm = 0x384
xorl %edi, %edi
movq %r14, %rsi
callq 0x2140
movq %rax, %r15
testl %r15d, %r15d
js 0x2622
je 0x257a
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movl $0x1, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x20a0
cmpq %r15, %rax
jl 0x2622
movl %ebx, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x20a0
cmpq %r15, %rax
jge 0x257a
movl %ebx, %edi
callq 0x2130
xorl %eax, %eax
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, %r14
jmp 0x24b9
leaq 0x19c1(%rip), %rdi      # 0x400d
callq 0x2080
xorl %edi, %edi
callq 0x2220
callq 0x22e0
leaq 0x1d23(%rip), %rdi      # 0x4387
jmp 0x266d
leaq 0x1d47(%rip), %rdi      # 0x43b4
movq %r14, %rsi
xorl %eax, %eax
callq 0x29cb
movl $0x1, %edi
callq 0x2220
nopl (%rax)