// File: /workspace/github_c_makefile_star2_O3/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 0x2ace
cmpl $0x2, %ebp
jl 0x2645
incl %ebp
addq $0x8, %rbx
xorl %r13d, %r13d
leaq 0x1bed(%rip), %r15      # 0x4020
leaq 0x1bf5(%rip), %r12      # 0x402f
movq (%rbx), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x2040
testl %eax, %eax
je 0x2632
movq %r14, %rdi
movq %r12, %rsi
callq 0x2040
testl %eax, %eax
je 0x248f
movq %r14, %rdi
leaq 0x1bc9(%rip), %rsi      # 0x4032
callq 0x2040
testl %eax, %eax
je 0x2498
movq %r14, %rdi
leaq 0x1bb9(%rip), %rsi      # 0x4035
callq 0x2040
testl %eax, %eax
je 0x24b5
testq %r13, %r13
je 0x24a5
jmp 0x2645
movb $0x1, 0x4cee(%rip)      # 0x7184
jmp 0x24a2
movl $0x3, %edi
callq 0x2aa5
movq %r13, %r14
decl %ebp
addq $0x8, %rbx
movq %r14, %r13
cmpl $0x2, %ebp
jg 0x243a
jmp 0x24b8
movq %r13, %r14
testq %r14, %r14
je 0x2645
leaq 0x1ec6(%rip), %rdi      # 0x438e
movq %r14, %rsi
xorl %eax, %eax
callq 0x2891
movq %r14, %rdi
movl $0x102, %esi            # imm = 0x102
xorl %eax, %eax
callq 0x2200
movl %eax, %ebx
leaq 0x1eb0(%rip), %rdi      # 0x439a
movl %eax, %esi
xorl %eax, %eax
callq 0x2891
testl %ebx, %ebx
js 0x264a
leaq 0x40(%rsp), %rsi
movl %ebx, %edi
callq 0x2270
testl %eax, %eax
js 0x2653
movl $0xf000, %eax           # imm = 0xF000
andl 0x58(%rsp), %eax
cmpl $0x8000, %eax           # imm = 0x8000
je 0x2567
leaq 0x1ed2(%rip), %rdi      # 0x43f8
xorl %eax, %eax
callq 0x2891
xorps %xmm0, %xmm0
movq %rsp, %r14
movaps %xmm0, 0x10(%r14)
movaps %xmm0, 0x20(%r14)
movups %xmm0, 0x2c(%r14)
movaps 0x1ac7(%rip), %xmm0     # 0x4010
movaps %xmm0, (%r14)
movb $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 0x4c0e(%rip), %esi      # 0x7184
andl $0x1, %esi
negl %esi
movl %ebx, %edi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
callq 0x36e5
movl %eax, %ebp
testb $0x1, %bpl
je 0x25c6
movl $0x384, %edx            # imm = 0x384
movl %ebx, %edi
movq %r14, %rsi
callq 0x2140
movq %rax, %r15
testl %r15d, %r15d
js 0x2617
je 0x25c6
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movl $0x1, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x20a0
cmpq %r15, %rax
jl 0x2617
testb $0x2, %bpl
je 0x256f
movl $0x384, %edx            # imm = 0x384
xorl %edi, %edi
movq %r14, %rsi
callq 0x2140
movq %rax, %r15
testl %r15d, %r15d
js 0x2617
je 0x256f
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movl $0x1, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x20a0
cmpq %r15, %rax
jl 0x2617
movl %ebx, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x20a0
cmpq %r15, %rax
jge 0x256f
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
leaq 0x19f0(%rip), %rdi      # 0x4029
callq 0x2080
xorl %edi, %edi
callq 0x2220
callq 0x22e0
leaq 0x1d52(%rip), %rdi      # 0x43a3
jmp 0x265a
leaq 0x1d76(%rip), %rdi      # 0x43d0
movq %r14, %rsi
xorl %eax, %eax
callq 0x29b7
movl $0x1, %edi
callq 0x2220
nop