// File: /workspace/github_c_makefile_star2_O2/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 0x2a71
addq $0x8, %rbx
xorl %r13d, %r13d
leaq 0x1c28(%rip), %r15      # 0x4020
leaq 0x1c30(%rip), %r12      # 0x402f
cmpl $0x2, %ebp
jl 0x2478
movq (%rbx), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x2040
testl %eax, %eax
je 0x25f3
movq %r14, %rdi
movq %r12, %rsi
callq 0x2040
testl %eax, %eax
je 0x2459
movq %r14, %rdi
leaq 0x1bff(%rip), %rsi      # 0x4032
callq 0x2040
testl %eax, %eax
je 0x2462
movq %r14, %rdi
leaq 0x1bef(%rip), %rsi      # 0x4035
callq 0x2040
testl %eax, %eax
je 0x2478
testq %r13, %r13
je 0x246d
jmp 0x2606
movb $0x1, 0x4d24(%rip)      # 0x7184
jmp 0x246a
pushq $0x3
popq %rdi
callq 0x2a4a
movq %r13, %r14
decl %ebp
addq $0x8, %rbx
movq %r14, %r13
jmp 0x23ff
testq %r13, %r13
je 0x2606
leaq 0x1f06(%rip), %rdi      # 0x438e
movq %r13, %rsi
xorl %eax, %eax
callq 0x283c
movq %r13, %rdi
movl $0x102, %esi            # imm = 0x102
xorl %eax, %eax
callq 0x2200
movl %eax, %ebx
leaq 0x1ef0(%rip), %rdi      # 0x439a
movl %eax, %esi
xorl %eax, %eax
callq 0x283c
testl %ebx, %ebx
js 0x262c
leaq 0x40(%rsp), %rsi
movl %ebx, %edi
callq 0x2270
testl %eax, %eax
js 0x2635
movl $0xf000, %eax           # imm = 0xF000
andl 0x58(%rsp), %eax
cmpl $0x8000, %eax           # imm = 0x8000
je 0x2527
leaq 0x1f12(%rip), %rdi      # 0x43f8
xorl %eax, %eax
callq 0x283c
xorps %xmm0, %xmm0
movq %rsp, %r14
movaps %xmm0, 0x10(%r14)
movaps %xmm0, 0x20(%r14)
movups %xmm0, 0x2c(%r14)
movaps 0x1b07(%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
pushq $-0x1
popq %rbp
leaq 0xd0(%rsp), %r14
pushq $0x1
popq %r15
movzbl 0x4c47(%rip), %esi      # 0x7184
andl $0x1, %esi
negl %esi
movl %ebx, %edi
movl %ebp, %edx
xorl %ecx, %ecx
callq 0x35d7
movl %eax, %r12d
testb $0x1, %r12b
je 0x2589
movl $0x384, %edx            # imm = 0x384
movl %ebx, %edi
movq %r14, %rsi
callq 0x2140
movq %rax, %r13
testl %r13d, %r13d
js 0x25d8
je 0x2589
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
movl %r15d, %edi
movq %r14, %rsi
movq %r13, %rdx
callq 0x20a0
cmpq %r13, %rax
jl 0x25d8
testb $0x2, %r12b
je 0x2536
movl $0x384, %edx            # imm = 0x384
xorl %edi, %edi
movq %r14, %rsi
callq 0x2140
movq %rax, %r12
testl %r12d, %r12d
js 0x25d8
je 0x2536
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movl %r15d, %edi
movq %r14, %rsi
movq %r12, %rdx
callq 0x20a0
cmpq %r12, %rax
jl 0x25d8
movl %ebx, %edi
movq %r14, %rsi
movq %r12, %rdx
callq 0x20a0
cmpq %r12, %rax
jge 0x2536
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 0x1a2f(%rip), %rdi      # 0x4029
callq 0x2080
xorl %edi, %edi
callq 0x2220
movq 0x49eb(%rip), %rax      # 0x6ff8
movq (%rax), %rcx
leaq 0x1a21(%rip), %rdi      # 0x4038
pushq $0x1
popq %rdx
movl $0x355, %esi            # imm = 0x355
callq 0x2240
pushq $0x1
popq %rdi
callq 0x2220
leaq 0x1d70(%rip), %rdi      # 0x43a3
jmp 0x263c
leaq 0x1d94(%rip), %rdi      # 0x43d0
movq %r13, %rsi
xorl %eax, %eax
callq 0x295e
jmp 0x2624