// File: /workspace/github_c_makefile_star2_O2/canboat[P]canboat/candump2analyzer/candump2analyzer.c
// Function: main
int main(int argc, char **argv)
{
  char  msg[MSG_BUF_SIZE];
  FILE *infile  = stdin;
  FILE *outfile = stdout;

  if (argc > 1)
  {
    if (strcasecmp(argv[1], "-version") == 0)
    {
      printf("%s\n", VERSION);
      exit(0);
    }
    infile = fopen(argv[1], "r");
    if (!infile)
    {
      fprintf(stderr, "Could not open input file '%s' (%s)\n", argv[1], strerror(errno));
      return 1;
    }
  }

  // For every line in the candump file...
  //
  int          format           = FMT_TBD;
  unsigned int candump_data_inc = CANDUMP_DATA_INC_3;

  msg[sizeof(msg) - 1] = '\0'; // Make sure algorithm ends
  while (fgets(msg, sizeof(msg) - 1, infile))
  {
    char *p = msg;

    // Ignore empty and comment lines within the candump input.
    //
    while (isspace(*p))
    {
      p++;
    }
    if (*p == 0 || *p == '\r' || *p == '\n' || *p == '#')
    {
      continue;
    }

    // Remove whitespace (including CR, LF) at end
    char *e = p + strlen(msg) - 1;
    while (e >= p && isspace(*e))
    {
      e--;
    }
    e[1] = '\0';

    // Process the CAN ID
    //
    uint32_t canid          = 0;
    int      size           = 0;
    double   currentTime    = 0.;
    uint32_t currentTimeInt = 0;
    uint8_t *u              = (uint8_t *) &canid;

    // Determine which candump format is being used.
    //
    if (format == FMT_TBD)
    {
      // Format not yet detected.
      // See if we can match one.
      //
      if (sscanf(p, "<%x> [%d] ", &canid, &size) == 2)
      {
        format = FMT_1;
      }
      else if (sscanf(p, " %*s %x [%d] ", &canid, &size) == 2)
      {
        format = FMT_2;
      }
      else if (sscanf(p, "(%lf) %*s %8x#", &currentTime, &canid) == 2)
      {
        format           = FMT_3;
        candump_data_inc = CANDUMP_DATA_INC_2;
        size             = (strlen(strchr(p, '#')) - 1) / 2;
      }
      else if (strstr(p, "CAN 16 XTD:") != NULL)
      {
        format = FMT_4;
      }
      else if (sscanf(p, "%07x %02hhx %02hhx %02hhx %02hhx", &currentTimeInt, u, u + 1, u + 2, u + 3) == 5)
      {
        format = FMT_5;
      }
      else
      {
        continue;
      }
    }
    else if (format == FMT_1)
    {
      if (sscanf(p, "<%x> [%d] ", &canid, &size) != 2)
      {
        continue;
      }
    }
    else if (format == FMT_2)
    {
      if (sscanf(p, " %*s %x [%d] ", &canid, &size) != 2)
      {
        continue;
      }
    }
    else if (format == FMT_3)
    {
      if (sscanf(p, "(%lf) %*s %8x#", &currentTime, &canid) != 2)
      {
        continue;
      }
      size = (strlen(strchr(p, '#')) - 1) / 2;
    }
    else if (format == FMT_4)
    {
      if (sscanf(p, "%*d %lf %*s CAN %d XTD: 0x%8x   ", &currentTime, &size, &canid) != 3)
      {
        continue;
      }
      size = size - 8;
    }
    else if (format == FMT_5)
    {
      if (sscanf(p, "%07x %02hhx %02hhx %02hhx %02hhx %02x", &currentTimeInt, u, u + 1, u + 2, u + 3, &size) != 6)
      {
        continue;
      }
    }

    unsigned int pri = 0;
    unsigned int src = 0;
    unsigned int dst = 255;
    unsigned int pgn = 0;

    getISO11783BitsFromCanId(canid, &pri, &pgn, &src, &dst);

    int            msec;
    char           timestamp[20];
    struct timeval tv;
    struct tm     *utc;

    // If the candump format includes a usec timestamp, convert
    // that to a timeval, otherwise use gettimeofday.
    //
    if (format >= FMT_3)
    {
      gettimeval(&tv, currentTime);
    }
    else
    {
      gettimeofday(&tv, NULL);
    }

    // strftime doesn't support fractional seconds, so use another
    // variable.
    //
    msec = lrint(tv.tv_usec / 1000.0);
    if (msec >= 1000)
    {
      msec -= 1000;
      tv.tv_sec++;
    }

    utc = gmtime(&tv.tv_sec);

    // %F = YYYY-MM-DD
    // %T = HH:MM:SS
    //
    strftime(timestamp, 20, "%F-%T", utc);

    // Output all but the data bytes.
    //
    fprintf(outfile, "%s.%03d,%d,%d,%d,%d,%d", timestamp, msec, pri, pgn, src, dst, size);

    // Now process the data bytes.
    //
    int          i;
    char         separator;
    unsigned int data;

    if (format == FMT_5)
    {
      p = strstr(p, " ");
      p += sizeof("0e 1d ff 9d 08 00 00 00");
      separator = ' ';
    }
    else if (format == FMT_4)
    {
      p         = strstr(p, "XTD: ") + sizeof("XTD: ");
      separator = ' ';
      for (; p < msg + sizeof(msg) && *p != 0 && *p != separator; ++p)
        ;
    }
    else
    {
      separator = (format == FMT_3) ? '#' : ']';
      for (p = msg; p < msg + sizeof(msg) && *p != 0 && *p != separator; ++p)
        ;
    }
    if (*p == separator)
    {
      if (format == FMT_3)
      {
        p++;
      }
      else
      {
        while (*(++p) == ' ')
          ;
      }
      for (i = 0; i < size; i++, p += candump_data_inc)
      {
        sscanf(p, "%2x", &data);
        fprintf(outfile, ",%02x", data);
      }
    }
    fprintf(outfile, "\n");
    fflush(outfile);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x858, %rsp            # imm = 0x858
movq 0x4bd9(%rip), %rax      # 0x6fd0
movq (%rax), %rax
movq %rax, 0x38(%rsp)
cmpl $0x2, %edi
jl 0x2472
movq %rsi, %r15
movq 0x8(%rsi), %r14
leaq 0x1c1a(%rip), %rsi      # 0x402c
movq %r14, %rdi
callq 0x2040
testl %eax, %eax
je 0x2a2c
leaq 0x1c12(%rip), %rsi      # 0x403b
movq %r14, %rdi
callq 0x2200
movq %rax, %r14
testq %rax, %rax
jne 0x247c
movq 0x4bb8(%rip), %rax      # 0x6ff8
movq (%rax), %rbx
movq 0x8(%r15), %r14
callq 0x2050
movl (%rax), %edi
callq 0x2260
leaq 0x1be3(%rip), %rsi      # 0x403d
movq %rbx, %rdi
movq %r14, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2160
pushq $0x1
popq %rax
jmp 0x2a1a
movq 0x4b5f(%rip), %rax      # 0x6fd8
movq (%rax), %r14
leaq 0x850(%rsp), %rax
movb $0x0, -0x1(%rax)
pushq $0x3
popq %rax
leaq 0x80(%rsp), %r13
movabsq $0x800002401, %rbx      # imm = 0x800002401
xorl %ecx, %ecx
movl %eax, 0x18(%rsp)
movl %ecx, 0x14(%rsp)
movq %r13, %rdi
movl $0x7cf, %esi            # imm = 0x7CF
movq %r14, %rdx
callq 0x2150
testq %rax, %rax
je 0x2a18
callq 0x2280
movq (%rax), %r15
leaq 0x7f(%rsp), %rbp
movq %rbp, %r12
incq %rbp
movsbq 0x1(%r12), %rax
testb $0x20, 0x1(%r15,%rax,2)
jne 0x24cd
movzbl %al, %eax
cmpl $0x23, %eax
ja 0x24f1
movl %eax, %eax
btq %rax, %rbx
jb 0x24a7
movq %r13, %rdi
callq 0x20b0
addq %rax, %r12
movq %r12, %rax
cmpq %rbp, %r12
jb 0x2514
movsbq (%rax), %rcx
leaq -0x1(%rax), %r12
testb $0x20, 0x1(%r15,%rcx,2)
jne 0x24fc
andl $0x0, 0x8(%rsp)
andl $0x0, 0x10(%rsp)
movb $0x0, 0x1(%rax)
andq $0x0, 0x30(%rsp)
andl $0x0, 0x2c(%rsp)
movl 0x14(%rsp), %eax
cmpl $0x4, %eax
leaq 0x10(%rsp), %r10
leaq 0x8(%rsp), %r8
ja 0x2691
movl %eax, %eax
leaq 0x1acb(%rip), %rcx      # 0x4018
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rbp, %rdi
leaq 0x1b02(%rip), %rsi      # 0x4062
movq %r8, %rdx
movq %r10, %rcx
xorl %eax, %eax
movq %r10, %r15
movq %r8, %r12
callq 0x21c0
movb $0x5d, %dl
cmpl $0x2, %eax
jne 0x26ea
xorl %r8d, %r8d
xorl %r15d, %r15d
xorl %edi, %edi
pushq $0x1
popq %rsi
jmp 0x2717
movq %rbp, %rdi
leaq 0x1b1f(%rip), %rsi      # 0x40b7
leaq 0x30(%rsp), %rdx
movq %r10, %rcx
xorl %eax, %eax
callq 0x21c0
cmpl $0x3, %eax
jne 0x26da
addl $-0x8, 0x10(%rsp)
movb $0x5d, %dl
movb $0x1, %r12b
xorl %r8d, %r8d
movb $0x1, %r15b
xorl %edi, %edi
pushq $0x4
jmp 0x26d7
xorl %r12d, %r12d
movq %rbp, %rdi
leaq 0x1a97(%rip), %rsi      # 0x406d
movq %r8, %rdx
movq %r10, %rcx
xorl %eax, %eax
callq 0x21c0
movb $0x5d, %dl
movl $0x0, %r8d
movl $0x0, %r15d
movl $0x0, %edi
pushq $0x2
jmp 0x2680
movq %rbp, %rdi
leaq 0x1a74(%rip), %rsi      # 0x407b
leaq 0x30(%rsp), %rdx
movq %r8, %rcx
xorl %eax, %eax
callq 0x21c0
cmpl $0x2, %eax
jne 0x26e2
movq %rbp, %rdi
pushq $0x23
popq %rsi
callq 0x20c0
movq %rax, %rdi
callq 0x20b0
decq %rax
shrq %rax
movl %eax, 0x10(%rsp)
movb $0x23, %dl
movb $0x1, %r12b
xorl %r8d, %r8d
xorl %r15d, %r15d
movb $0x1, %dil
pushq $0x3
jmp 0x26d7
xorl %r12d, %r12d
movq %rbp, %rdi
leaq 0x1a04(%rip), %rsi      # 0x4062
movq %r8, %rdx
movq %r10, %rcx
xorl %eax, %eax
callq 0x21c0
movb $0x5d, %dl
movl $0x0, %r8d
movl $0x0, %r15d
movl $0x0, %edi
pushq $0x1
popq %rcx
movl %ecx, %esi
cmpl $0x2, %eax
jne 0x24a3
jmp 0x271a
xorl %r15d, %r15d
movq %rbp, %rdi
leaq 0x1a3a(%rip), %rsi      # 0x40d8
leaq 0x2c(%rsp), %rdx
movq %r8, %rcx
leaq 0x9(%rsp), %r8
leaq 0xa(%rsp), %r9
xorl %eax, %eax
pushq %r10
leaq 0x13(%rsp), %r10
pushq %r10
callq 0x21c0
popq %rcx
popq %rdx
cmpl $0x6, %eax
jne 0x28b4
movb $0x5d, %dl
movb $0x1, %r12b
movb $0x1, %r8b
xorl %edi, %edi
pushq $0x5
popq %rsi
jmp 0x271a
pushq $0x4
popq %rcx
jmp 0x24a3
pushq $0x3
popq %rcx
jmp 0x24a3
movq %rbp, %rdi
leaq 0x1979(%rip), %rsi      # 0x406d
movq %r12, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x21c0
cmpl $0x2, %eax
jne 0x2942
xorl %r8d, %r8d
xorl %r15d, %r15d
xorl %edi, %edi
pushq $0x2
popq %rsi
movb $0x5d, %dl
xorl %r12d, %r12d
movl %r8d, 0x48(%rsp)
movl %edi, 0x44(%rsp)
movl %esi, 0x14(%rsp)
movb %dl, 0xf(%rsp)
andl $0x0, 0x28(%rsp)
andl $0x0, 0x24(%rsp)
movl $0xff, 0x20(%rsp)
andl $0x0, 0x1c(%rsp)
movl 0x8(%rsp), %edi
leaq 0x28(%rsp), %rsi
leaq 0x1c(%rsp), %rdx
leaq 0x24(%rsp), %rcx
leaq 0x20(%rsp), %r8
callq 0x371c
testb %r12b, %r12b
movl %r15d, 0x40(%rsp)
je 0x2796
movsd 0x30(%rsp), %xmm0
cvttsd2si %xmm0, %rax
movq %rax, 0x50(%rsp)
cvtsi2sd %rax, %xmm1
subsd %xmm1, %xmm0
mulsd 0x187e(%rip), %xmm0     # 0x4008
cvttsd2si %xmm0, %rax
movq %rax, 0x58(%rsp)
jmp 0x27a7
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
callq 0x2120
movq 0x58(%rsp), %rax
cvtsi2sd %rax, %xmm0
divsd 0x185c(%rip), %xmm0     # 0x4010
callq 0x20d0
movq %rax, %r15
cmpl $0x3e8, %r15d           # imm = 0x3E8
jl 0x27d1
addl $0xfffffc18, %r15d      # imm = 0xFFFFFC18
incq 0x50(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x2210
leaq 0x60(%rsp), %rdi
pushq $0x14
popq %rsi
leaq 0x1914(%rip), %rdx      # 0x40fe
movq %rax, %rcx
callq 0x21e0
movl 0x28(%rsp), %r8d
movl 0x1c(%rsp), %r9d
movl 0x24(%rsp), %r10d
movl 0x20(%rsp), %r11d
movl 0x10(%rsp), %r12d
subq $0x8, %rsp
movq 0x40(%rsp), %rdi
leaq 0x18e9(%rip), %rsi      # 0x4104
leaq 0x68(%rsp), %rdx
movl %r15d, %ecx
xorl %eax, %eax
pushq %r12
pushq %r11
pushq %r10
callq 0x2160
addq $0x20, %rsp
cmpb $0x0, 0x48(%rsp)
je 0x284f
movq %rbp, %rdi
pushq $0x20
popq %rsi
callq 0x20c0
addq $0x18, %rax
movb $0x20, %dil
jmp 0x28bf
cmpb $0x0, 0x40(%rsp)
je 0x2886
movq %rbp, %rdi
leaq 0x18bb(%rip), %rsi      # 0x411b
callq 0x2270
addq $0x6, %rax
leaq 0x850(%rsp), %rdx
movb $0x20, %dil
cmpq %rdx, %rax
jae 0x28bf
movzbl (%rax), %ecx
testb $-0x21, %cl
je 0x28bf
incq %rax
jmp 0x2871
movl $0x7d0, %ecx            # imm = 0x7D0
leaq 0x7f(%rsp), %rax
leaq 0x850(%rsp), %rsi
movb 0xf(%rsp), %dil
subq $0x1, %rcx
jb 0x28bc
movb 0x1(%rax), %dl
incq %rax
testb %dl, %dl
je 0x28bf
cmpb %dil, %dl
jne 0x289d
jmp 0x28bf
pushq $0x5
popq %rcx
jmp 0x24a3
movq %rsi, %rax
cmpb %dil, (%rax)
jne 0x2921
cmpb $0x0, 0x44(%rsp)
je 0x28d3
incq %rax
movq %rax, %r12
jmp 0x28e0
leaq 0x1(%rax), %r12
cmpb $0x20, 0x1(%rax)
movq %r12, %rax
je 0x28d3
movl 0x18(%rsp), %r15d
xorl %ebp, %ebp
cmpl 0x10(%rsp), %ebp
jge 0x2921
movq %r12, %rdi
leaq 0x182a(%rip), %rsi      # 0x4121
leaq 0x4c(%rsp), %rdx
xorl %eax, %eax
callq 0x21c0
movl 0x4c(%rsp), %edx
movq 0x38(%rsp), %rdi
leaq 0x1812(%rip), %rsi      # 0x4125
xorl %eax, %eax
callq 0x2160
incl %ebp
addq %r15, %r12
jmp 0x28e7
pushq $0xa
popq %rdi
movq 0x38(%rsp), %r15
movq %r15, %rsi
callq 0x2140
movq %r15, %rdi
callq 0x21b0
movl 0x14(%rsp), %ecx
jmp 0x24a3
movq %rbp, %rdi
leaq 0x172f(%rip), %rsi      # 0x407b
leaq 0x30(%rsp), %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x21c0
movq %rbp, %rdi
cmpl $0x2, %eax
jne 0x299a
pushq $0x23
popq %rsi
callq 0x20c0
movq %rax, %rdi
callq 0x20b0
decq %rax
shrq %rax
movl %eax, 0x10(%rsp)
movb $0x23, %dl
movb $0x1, %r12b
xorl %r8d, %r8d
xorl %r15d, %r15d
movb $0x1, %dil
pushq $0x3
popq %rsi
pushq $0x2
popq %rax
movl %eax, 0x18(%rsp)
jmp 0x271a
leaq 0x16e9(%rip), %rsi      # 0x408a
callq 0x2270
testq %rax, %rax
je 0x29c0
movb $0x1, %r12b
xorl %r8d, %r8d
movb $0x1, %r15b
xorl %edi, %edi
pushq $0x4
popq %rsi
movb $0x5d, %dl
jmp 0x271a
subq $0x8, %rsp
movq %rbp, %rdi
leaq 0x16c8(%rip), %rsi      # 0x4096
leaq 0x34(%rsp), %rdx
leaq 0x10(%rsp), %rcx
leaq 0x11(%rsp), %r8
leaq 0x12(%rsp), %r9
xorl %eax, %eax
leaq 0x13(%rsp), %r10
pushq %r10
callq 0x21c0
popq %rcx
popq %rdx
movb $0x1, %r12b
movb $0x1, %r8b
movl $0x0, %r15d
movl $0x0, %edi
pushq $0x5
popq %rsi
xorl %ecx, %ecx
cmpl $0x5, %eax
movb $0x5d, %dl
jne 0x24a3
jmp 0x271a
xorl %eax, %eax
addq $0x858, %rsp            # imm = 0x858
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1602(%rip), %rdi      # 0x4035
callq 0x2080
xorl %edi, %edi
callq 0x2230
nop