// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x480, %rsp            # imm = 0x480
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x2d40
cmpl $0x1, -0x8(%rbp)
jle 0x24e0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x2bee(%rip), %rsi      # 0x5004
callq 0x2040
cmpl $0x0, %eax
jne 0x243c
leaq 0x328a(%rip), %rdi      # 0x56b1
leaq 0x2bdf(%rip), %rsi      # 0x500d
movb $0x0, %al
callq 0x20d0
xorl %edi, %edi
callq 0x2230
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x2bc8(%rip), %rsi      # 0x5013
callq 0x2040
cmpl $0x0, %eax
jne 0x2461
movl $0x1, 0x4d55(%rip)      # 0x71b4
jmp 0x24c6
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x2ba6(%rip), %rsi      # 0x5016
callq 0x2040
cmpl $0x0, %eax
jne 0x2486
movl $0x3, %edi
callq 0x2ca0
jmp 0x24c4
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x2b84(%rip), %rsi      # 0x5019
callq 0x2040
cmpl $0x0, %eax
jne 0x24a1
jmp 0x24e0
cmpq $0x0, -0x58(%rbp)
jne 0x24b6
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
jmp 0x24c0
movq $0x0, -0x58(%rbp)
jmp 0x24e0
jmp 0x24c2
jmp 0x24c4
jmp 0x24c6
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
jmp 0x23fd
cmpq $0x0, -0x58(%rbp)
jne 0x2509
movq 0x4b0a(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x2b24(%rip), %rsi      # 0x501c
movb $0x0, %al
callq 0x2160
movl $0x1, %edi
callq 0x2230
movq -0x58(%rbp), %rsi
leaq 0x2e5e(%rip), %rdi      # 0x5372
movb $0x0, %al
callq 0x2a10
movq -0x58(%rbp), %rdi
movl $0x102, %esi            # imm = 0x102
movb $0x0, %al
callq 0x2210
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %esi
leaq 0x2e46(%rip), %rdi      # 0x537e
movb $0x0, %al
callq 0x2a10
cmpl $0x0, -0x14(%rbp)
jge 0x2561
movq -0x58(%rbp), %rsi
leaq 0x2e37(%rip), %rdi      # 0x5387
movb $0x0, %al
callq 0x2b70
movl $0x1, %edi
callq 0x2230
movl -0x14(%rbp), %edi
leaq -0xe8(%rbp), %rsi
callq 0x2270
cmpl $0x0, %eax
jge 0x2591
movq -0x58(%rbp), %rsi
leaq 0x2e34(%rip), %rdi      # 0x53b4
movb $0x0, %al
callq 0x2b70
movl $0x1, %edi
callq 0x2230
movl -0xd0(%rbp), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x8000, %eax           # imm = 0x8000
sete %al
andb $0x1, %al
movb %al, 0x4c0c(%rip)       # 0x71b8
testb $0x1, 0x4c05(%rip)      # 0x71b8
jne 0x260f
leaq 0x2e20(%rip), %rdi      # 0x53dc
movb $0x0, %al
callq 0x2a10
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
movl $0x3c, %edx
callq 0x2130
movl $0x8bf, -0x48(%rbp)     # imm = 0x8BF
movl $0x4, -0x50(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x44(%rbp)
movb $0x0, -0x39(%rbp)
movb $0x1, -0x3a(%rbp)
movl -0x14(%rbp), %edi
xorl %esi, %esi
callq 0x20b0
movl -0x14(%rbp), %edi
xorl %esi, %esi
leaq -0x50(%rbp), %rdx
callq 0x2200
jmp 0x2611
movl -0x14(%rbp), %edi
movl 0x4b9a(%rip), %ecx      # 0x71b4
xorl %esi, %esi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $0x0, %ecx
cmovnel %eax, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
callq 0x4280
movl %eax, -0x474(%rbp)
movl -0x474(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jbe 0x26a7
movl -0x14(%rbp), %edi
leaq -0x470(%rbp), %rsi
movl $0x384, %edx            # imm = 0x384
callq 0x2150
movl %eax, -0x478(%rbp)
cmpl $0x0, -0x478(%rbp)
jge 0x266f
jmp 0x2737
cmpl $0x0, -0x478(%rbp)
jle 0x26a3
leaq -0x470(%rbp), %rsi
movslq -0x478(%rbp), %rdx
movl $0x1, %edi
callq 0x2090
movslq -0x478(%rbp), %rcx
cmpq %rcx, %rax
jge 0x26a1
jmp 0x2737
jmp 0x26a3
jmp 0x26a5
jmp 0x26a7
movl -0x474(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
jbe 0x2732
leaq -0x470(%rbp), %rsi
xorl %edi, %edi
movl $0x384, %edx            # imm = 0x384
callq 0x2150
movl %eax, -0x478(%rbp)
cmpl $0x0, -0x478(%rbp)
jge 0x26d9
jmp 0x2737
cmpl $0x0, -0x478(%rbp)
jle 0x272e
leaq -0x470(%rbp), %rsi
movslq -0x478(%rbp), %rdx
movl $0x1, %edi
callq 0x2090
movslq -0x478(%rbp), %rcx
cmpq %rcx, %rax
jge 0x2708
jmp 0x2737
movl -0x14(%rbp), %edi
leaq -0x470(%rbp), %rsi
movslq -0x478(%rbp), %rdx
callq 0x2090
movslq -0x478(%rbp), %rcx
cmpq %rcx, %rax
jge 0x272c
jmp 0x2737
jmp 0x272e
jmp 0x2730
jmp 0x2732
jmp 0x2611
movl -0x14(%rbp), %edi
callq 0x2140
xorl %eax, %eax
addq $0x480, %rsp            # imm = 0x480
popq %rbp
retq
nopw (%rax,%rax)