// File: /workspace/github_c_makefile_star2_O3/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 $0x868, %rsp            # imm = 0x868
movq 0x3b99(%rip), %rax      # 0x5fd0
movq (%rax), %rbx
cmpl $0x2, %edi
jl 0x248a
movq %rsi, %r15
movq 0x8(%rsi), %r14
leaq 0x1bdf(%rip), %rsi      # 0x402c
movq %r14, %rdi
callq 0x2040
testl %eax, %eax
je 0x2a94
leaq 0x1bd7(%rip), %rsi      # 0x403b
movq %r14, %rdi
callq 0x2200
movq %rax, %r12
testq %rax, %rax
jne 0x2494
addq $0x8, %r15
movq %r15, %rdi
callq 0x22d0
movl $0x1, %eax
jmp 0x2a82
movq 0x3b47(%rip), %rax      # 0x5fd8
movq (%rax), %r12
leaq 0x90(%rsp), %rdi
movb $0x0, 0x7cf(%rdi)
movl $0x7cf, %esi            # imm = 0x7CF
movq %r12, %rdx
callq 0x2150
testq %rax, %rax
je 0x2a80
movq %rbx, 0x30(%rsp)
callq 0x2280
movq %rax, 0x68(%rsp)
movl $0x3, 0x10(%rsp)
leaq 0xc(%rsp), %r15
movabsq $0x800002401, %rbx      # imm = 0x800002401
xorl %r14d, %r14d
movq %r12, 0x60(%rsp)
movq 0x68(%rsp), %rax
movq (%rax), %r13
leaq 0x8f(%rsp), %r12
movq %r12, %rbp
incq %r12
movsbq 0x1(%rbp), %rax
testb $0x20, 0x1(%r13,%rax,2)
jne 0x24f7
movzbl %al, %eax
cmpl $0x23, %eax
ja 0x253b
movl %eax, %eax
btq %rax, %rbx
jae 0x253b
leaq 0x90(%rsp), %rdi
movl $0x7cf, %esi            # imm = 0x7CF
movq 0x60(%rsp), %rdx
callq 0x2150
testq %rax, %rax
jne 0x24e7
jmp 0x2a80
leaq 0x90(%rsp), %rdi
callq 0x20b0
addq %rax, %rbp
movq %rbp, %rax
cmpq %r12, %rbp
jb 0x2563
movsbq (%rax), %rcx
leaq -0x1(%rax), %rbp
testb $0x20, 0x1(%r13,%rcx,2)
jne 0x254b
movb $0x0, 0x1(%rax)
xorl %eax, %eax
movl %eax, 0xc(%rsp)
movl %eax, 0x8(%rsp)
movq $0x0, 0x28(%rsp)
movl %eax, 0x24(%rsp)
cmpl $0x4, %r14d
ja 0x26e0
movl %r14d, %eax
leaq 0x1a86(%rip), %rcx      # 0x4018
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r12, %rdi
leaq 0x1abd(%rip), %rsi      # 0x4062
movq %r15, %rdx
leaq 0x8(%rsp), %r13
movq %r13, %rcx
xorl %eax, %eax
callq 0x21c0
movl $0x1, %r14d
movb $0x5d, %sil
cmpl $0x2, %eax
jne 0x2736
xorl %edx, %edx
xorl %r13d, %r13d
xorl %ecx, %ecx
xorl %ebp, %ebp
jmp 0x281f
movq %r12, %rdi
leaq 0x1ad6(%rip), %rsi      # 0x40b7
leaq 0x28(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movq %r15, %r8
xorl %eax, %eax
callq 0x21c0
movl $0x4, %r14d
cmpl $0x3, %eax
jne 0x251a
addl $-0x8, 0x8(%rsp)
movb $0x5d, %sil
movb $0x1, %bpl
xorl %edx, %edx
movb $0x1, %r13b
jmp 0x272f
xorl %ebp, %ebp
movq %r12, %rdi
leaq 0x1a48(%rip), %rsi      # 0x406d
movq %r15, %rdx
leaq 0x8(%rsp), %rcx
xorl %eax, %eax
callq 0x21c0
movl $0x2, %r14d
jmp 0x26bf
movq %r12, %rdi
leaq 0x1a32(%rip), %rsi      # 0x407b
leaq 0x28(%rsp), %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x21c0
movl $0x3, %r14d
cmpl $0x2, %eax
jne 0x251a
movq %r12, %rdi
movl $0x23, %esi
callq 0x20c0
movq %rax, %rdi
callq 0x20b0
decq %rax
shrq %rax
movl %eax, 0x8(%rsp)
movl $0x3, %r14d
movb $0x23, %sil
movb $0x1, %bpl
xorl %edx, %edx
xorl %r13d, %r13d
movb $0x1, %cl
jmp 0x281f
xorl %ebp, %ebp
movq %r12, %rdi
leaq 0x19b8(%rip), %rsi      # 0x4062
movq %r15, %rdx
leaq 0x8(%rsp), %rcx
xorl %eax, %eax
callq 0x21c0
movl $0x1, %r14d
movb $0x5d, %sil
movl $0x0, %edx
movl $0x0, %r13d
movl $0x0, %ecx
cmpl $0x2, %eax
jne 0x251a
jmp 0x281f
xorl %r13d, %r13d
movq %r12, %rdi
leaq 0x19eb(%rip), %rsi      # 0x40d8
leaq 0x24(%rsp), %rdx
movq %r15, %rcx
leaq 0xd(%rsp), %r8
leaq 0xe(%rsp), %r9
xorl %eax, %eax
leaq 0x8(%rsp), %r10
pushq %r10
leaq 0x17(%rsp), %r10
pushq %r10
callq 0x21c0
addq $0x10, %rsp
movl $0x5, %r14d
cmpl $0x6, %eax
jne 0x251a
movb $0x5d, %sil
movb $0x1, %bpl
movb $0x1, %dl
xorl %ecx, %ecx
jmp 0x281f
movq %r12, %rdi
leaq 0x192d(%rip), %rsi      # 0x406d
movq %r15, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x21c0
movl $0x2, %r14d
cmpl $0x2, %eax
jne 0x2766
xorl %edx, %edx
xorl %r13d, %r13d
xorl %ecx, %ecx
xorl %ebp, %ebp
jmp 0x281c
movq %r12, %rdi
leaq 0x190b(%rip), %rsi      # 0x407b
leaq 0x28(%rsp), %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x21c0
movq %r12, %rdi
cmpl $0x2, %eax
jne 0x27b0
movl $0x23, %esi
callq 0x20c0
movq %rax, %rdi
callq 0x20b0
decq %rax
shrq %rax
movl %eax, 0x8(%rsp)
movl $0x2, 0x10(%rsp)
jmp 0x2686
leaq 0x18d3(%rip), %rsi      # 0x408a
callq 0x2270
xorl %r13d, %r13d
testq %rax, %rax
je 0x27d4
movl $0x4, %r14d
movb $0x1, %bpl
xorl %edx, %edx
movb $0x1, %r13b
jmp 0x281a
subq $0x8, %rsp
movq %r12, %rdi
leaq 0x18b4(%rip), %rsi      # 0x4096
leaq 0x2c(%rsp), %rdx
movq %r15, %rcx
leaq 0x15(%rsp), %r8
leaq 0x16(%rsp), %r9
xorl %eax, %eax
leaq 0x17(%rsp), %r10
pushq %r10
callq 0x21c0
addq $0x10, %rsp
cmpl $0x5, %eax
jne 0x2a78
movl $0x5, %r14d
movb $0x1, %bpl
movb $0x1, %dl
xorl %ecx, %ecx
movb $0x5d, %sil
movl %esi, %ebx
movl %edx, 0x44(%rsp)
movl %ecx, 0x40(%rsp)
xorl %eax, %eax
movl %eax, 0x20(%rsp)
movl %eax, 0x1c(%rsp)
movl $0xff, 0x18(%rsp)
movl %eax, 0x14(%rsp)
movl 0xc(%rsp), %edi
leaq 0x20(%rsp), %rsi
leaq 0x14(%rsp), %rdx
leaq 0x1c(%rsp), %rcx
leaq 0x18(%rsp), %r8
callq 0x385e
testb %bpl, %bpl
movl %r13d, 0x3c(%rsp)
je 0x2893
movsd 0x28(%rsp), %xmm0
cvttsd2si %xmm0, %rax
movq %rax, 0x50(%rsp)
cvtsi2sd %rax, %xmm1
subsd %xmm1, %xmm0
mulsd 0x1781(%rip), %xmm0     # 0x4008
cvttsd2si %xmm0, %rax
movq %rax, 0x58(%rsp)
jmp 0x28a4
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
callq 0x2120
movq 0x58(%rsp), %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
divsd 0x175c(%rip), %xmm0     # 0x4010
callq 0x20d0
movq %rax, %r13
cmpl $0x3e8, %r13d           # imm = 0x3E8
jl 0x28d1
addl $0xfffffc18, %r13d      # imm = 0xFFFFFC18
incq 0x50(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x2210
movl $0x14, %esi
leaq 0x70(%rsp), %rbp
movq %rbp, %rdi
leaq 0x180f(%rip), %rdx      # 0x40fe
movq %rax, %rcx
callq 0x21e0
movl 0x20(%rsp), %r8d
movl 0x14(%rsp), %r9d
movl 0x1c(%rsp), %r10d
movl 0x18(%rsp), %r11d
movl 0x8(%rsp), %r15d
subq $0x8, %rsp
movq 0x38(%rsp), %rdi
leaq 0x17e4(%rip), %rsi      # 0x4104
movq %rbp, %rdx
movl %r13d, %ecx
xorl %eax, %eax
pushq %r15
pushq %r11
pushq %r10
callq 0x2160
addq $0x20, %rsp
cmpb $0x0, 0x44(%rsp)
movl %r14d, 0x48(%rsp)
je 0x2959
movq %r12, %rdi
movl $0x20, %esi
callq 0x20c0
addq $0x18, %rax
movb $0x20, %sil
jmp 0x29d5
cmpb $0x0, 0x3c(%rsp)
je 0x29a3
movq %r12, %rdi
leaq 0x17b1(%rip), %rsi      # 0x411b
callq 0x2270
movq %rax, %rcx
addq $0x6, %rax
movb $0x20, %sil
leaq 0x860(%rsp), %rdx
cmpq %rdx, %rax
jae 0x29d5
leaq 0x85a(%rsp), %rdx
subq %rcx, %rdx
movzbl (%rax), %ecx
testb $-0x21, %cl
je 0x29d5
incq %rax
decq %rdx
jne 0x2991
jmp 0x29c2
xorl %eax, %eax
movl %ebx, %esi
movb 0x90(%rsp,%rax), %cl
testb %cl, %cl
je 0x29cc
cmpb %sil, %cl
je 0x29cc
incq %rax
cmpq $0x7d0, %rax            # imm = 0x7D0
jne 0x29a7
leaq 0x860(%rsp), %rax
jmp 0x29d5
addq %rsp, %rax
addq $0x90, %rax
cmpb %sil, (%rax)
movq 0x30(%rsp), %r15
jne 0x2a4a
cmpb $0x0, 0x40(%rsp)
je 0x29ee
incq %rax
movq %rax, %r12
jmp 0x29fb
leaq 0x1(%rax), %r12
cmpb $0x20, 0x1(%rax)
movq %r12, %rax
je 0x29ee
cmpl $0x0, 0x8(%rsp)
movq 0x30(%rsp), %r15
leaq 0x1715(%rip), %rbx      # 0x4121
leaq 0x4c(%rsp), %r14
jle 0x2a4a
movl 0x10(%rsp), %r13d
xorl %ebp, %ebp
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x21c0
movl 0x4c(%rsp), %edx
movq %r15, %rdi
leaq 0x16ed(%rip), %rsi      # 0x4125
xorl %eax, %eax
callq 0x2160
incl %ebp
addq %r13, %r12
cmpl 0x8(%rsp), %ebp
jl 0x2a1a
movl $0xa, %edi
movq %r15, %rsi
callq 0x2140
movq %r15, %rdi
callq 0x21b0
leaq 0xc(%rsp), %r15
movabsq $0x800002401, %rbx      # imm = 0x800002401
movl 0x48(%rsp), %r14d
jmp 0x251a
xorl %r14d, %r14d
jmp 0x251a
xorl %eax, %eax
addq $0x868, %rsp            # imm = 0x868
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x159a(%rip), %rdi      # 0x4035
callq 0x2080
xorl %edi, %edi
callq 0x2230
nop