// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x8b0, %rsp            # imm = 0x8B0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq 0x4ba8(%rip), %rax      # 0x6fd8
movq (%rax), %rax
movq %rax, -0x7e8(%rbp)
movq 0x4b8f(%rip), %rax      # 0x6fd0
movq (%rax), %rax
movq %rax, -0x7f0(%rbp)
cmpl $0x1, -0x8(%rbp)
jle 0x2508
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x2bb4(%rip), %rsi      # 0x5018
callq 0x2040
cmpl $0x0, %eax
jne 0x248a
leaq 0x2f48(%rip), %rdi      # 0x53bd
leaq 0x2ba5(%rip), %rsi      # 0x5021
movb $0x0, %al
callq 0x20c0
xorl %edi, %edi
callq 0x2230
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x2b8e(%rip), %rsi      # 0x5027
callq 0x2200
movq %rax, -0x7e8(%rbp)
cmpq $0x0, -0x7e8(%rbp)
jne 0x2506
movq 0x4b42(%rip), %rax      # 0x6ff8
movq (%rax), %rax
movq %rax, -0x888(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x880(%rbp)
callq 0x2050
movl (%rax), %edi
callq 0x2260
movq -0x888(%rbp), %rdi
movq -0x880(%rbp), %rdx
movq %rax, %rcx
leaq 0x2b36(%rip), %rsi      # 0x5029
movb $0x0, %al
callq 0x2160
movl $0x1, -0x4(%rbp)
jmp 0x2d87
jmp 0x2508
movl $0x0, -0x7f4(%rbp)
movl $0x3, -0x7f8(%rbp)
movb $0x0, -0x11(%rbp)
leaq -0x7e0(%rbp), %rdi
movq -0x7e8(%rbp), %rdx
movl $0x7cf, %esi            # imm = 0x7CF
callq 0x2150
cmpq $0x0, %rax
je 0x2d87
leaq -0x7e0(%rbp), %rax
movq %rax, -0x800(%rbp)
callq 0x2280
movq (%rax), %rax
movq -0x800(%rbp), %rcx
movsbl (%rcx), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
je 0x2587
movq -0x800(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x800(%rbp)
jmp 0x2550
movq -0x800(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x25c3
movq -0x800(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xd, %eax
je 0x25c3
movq -0x800(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
je 0x25c3
movq -0x800(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0x25c8
jmp 0x2520
movq -0x800(%rbp), %rax
movq %rax, -0x890(%rbp)
leaq -0x7e0(%rbp), %rdi
callq 0x20a0
movq %rax, %rcx
movq -0x890(%rbp), %rax
addq %rcx, %rax
addq $-0x1, %rax
movq %rax, -0x808(%rbp)
movq -0x808(%rbp), %rcx
xorl %eax, %eax
cmpq -0x800(%rbp), %rcx
movb %al, -0x891(%rbp)
jb 0x263c
callq 0x2280
movq (%rax), %rax
movq -0x808(%rbp), %rcx
movsbl (%rcx), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
setne %al
movb %al, -0x891(%rbp)
movb -0x891(%rbp), %al
testb $0x1, %al
jne 0x2648
jmp 0x265c
movq -0x808(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x808(%rbp)
jmp 0x25fa
movq -0x808(%rbp), %rax
movb $0x0, 0x1(%rax)
movl $0x0, -0x80c(%rbp)
movl $0x0, -0x810(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x818(%rbp)
movl $0x0, -0x81c(%rbp)
leaq -0x80c(%rbp), %rax
movq %rax, -0x828(%rbp)
cmpl $0x0, -0x7f4(%rbp)
jne 0x2810
movq -0x800(%rbp), %rdi
leaq 0x2995(%rip), %rsi      # 0x504e
leaq -0x80c(%rbp), %rdx
leaq -0x810(%rbp), %rcx
movb $0x0, %al
callq 0x21c0
cmpl $0x2, %eax
jne 0x26e2
movl $0x1, -0x7f4(%rbp)
jmp 0x280b
movq -0x800(%rbp), %rdi
leaq 0x2969(%rip), %rsi      # 0x5059
leaq -0x80c(%rbp), %rdx
leaq -0x810(%rbp), %rcx
movb $0x0, %al
callq 0x21c0
cmpl $0x2, %eax
jne 0x2719
movl $0x2, -0x7f4(%rbp)
jmp 0x2809
movq -0x800(%rbp), %rdi
leaq 0x2940(%rip), %rsi      # 0x5067
leaq -0x818(%rbp), %rdx
leaq -0x80c(%rbp), %rcx
movb $0x0, %al
callq 0x21c0
cmpl $0x2, %eax
jne 0x2780
movl $0x3, -0x7f4(%rbp)
movl $0x2, -0x7f8(%rbp)
movq -0x800(%rbp), %rdi
movl $0x23, %esi
callq 0x20b0
movq %rax, %rdi
callq 0x20a0
subq $0x1, %rax
shrq %rax
movl %eax, -0x810(%rbp)
jmp 0x2807
movq -0x800(%rbp), %rdi
leaq 0x28e8(%rip), %rsi      # 0x5076
callq 0x2270
cmpq $0x0, %rax
je 0x27a5
movl $0x4, -0x7f4(%rbp)
jmp 0x2805
movq -0x800(%rbp), %rdi
movq -0x828(%rbp), %rcx
movq -0x828(%rbp), %r8
addq $0x1, %r8
movq -0x828(%rbp), %r9
addq $0x2, %r9
movq -0x828(%rbp), %rax
addq $0x3, %rax
leaq 0x28a7(%rip), %rsi      # 0x5082
leaq -0x81c(%rbp), %rdx
movq %rax, (%rsp)
movb $0x0, %al
callq 0x21c0
cmpl $0x5, %eax
jne 0x27fe
movl $0x5, -0x7f4(%rbp)
jmp 0x2803
jmp 0x2520
jmp 0x2805
jmp 0x2807
jmp 0x2809
jmp 0x280b
jmp 0x29a8
cmpl $0x1, -0x7f4(%rbp)
jne 0x284b
movq -0x800(%rbp), %rdi
leaq 0x2827(%rip), %rsi      # 0x504e
leaq -0x80c(%rbp), %rdx
leaq -0x810(%rbp), %rcx
movb $0x0, %al
callq 0x21c0
cmpl $0x2, %eax
je 0x2846
jmp 0x2520
jmp 0x29a6
cmpl $0x2, -0x7f4(%rbp)
jne 0x2886
movq -0x800(%rbp), %rdi
leaq 0x27f7(%rip), %rsi      # 0x5059
leaq -0x80c(%rbp), %rdx
leaq -0x810(%rbp), %rcx
movb $0x0, %al
callq 0x21c0
cmpl $0x2, %eax
je 0x2881
jmp 0x2520
jmp 0x29a4
cmpl $0x3, -0x7f4(%rbp)
jne 0x28e7
movq -0x800(%rbp), %rdi
leaq 0x27ca(%rip), %rsi      # 0x5067
leaq -0x818(%rbp), %rdx
leaq -0x80c(%rbp), %rcx
movb $0x0, %al
callq 0x21c0
cmpl $0x2, %eax
je 0x28bc
jmp 0x2520
movq -0x800(%rbp), %rdi
movl $0x23, %esi
callq 0x20b0
movq %rax, %rdi
callq 0x20a0
subq $0x1, %rax
shrq %rax
movl %eax, -0x810(%rbp)
jmp 0x29a2
cmpl $0x4, -0x7f4(%rbp)
jne 0x2935
movq -0x800(%rbp), %rdi
leaq 0x27a5(%rip), %rsi      # 0x50a3
leaq -0x818(%rbp), %rdx
leaq -0x810(%rbp), %rcx
leaq -0x80c(%rbp), %r8
movb $0x0, %al
callq 0x21c0
cmpl $0x3, %eax
je 0x2924
jmp 0x2520
movl -0x810(%rbp), %eax
subl $0x8, %eax
movl %eax, -0x810(%rbp)
jmp 0x29a0
cmpl $0x5, -0x7f4(%rbp)
jne 0x299e
movq -0x800(%rbp), %rdi
movq -0x828(%rbp), %rcx
movq -0x828(%rbp), %r8
addq $0x1, %r8
movq -0x828(%rbp), %r9
addq $0x2, %r9
movq -0x828(%rbp), %r10
addq $0x3, %r10
leaq 0x2750(%rip), %rsi      # 0x50c4
leaq -0x81c(%rbp), %rdx
leaq -0x810(%rbp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x21c0
cmpl $0x6, %eax
je 0x299c
jmp 0x2520
jmp 0x299e
jmp 0x29a0
jmp 0x29a2
jmp 0x29a4
jmp 0x29a6
jmp 0x29a8
movl $0x0, -0x82c(%rbp)
movl $0x0, -0x830(%rbp)
movl $0xff, -0x834(%rbp)
movl $0x0, -0x838(%rbp)
movl -0x80c(%rbp), %edi
leaq -0x82c(%rbp), %rsi
leaq -0x838(%rbp), %rdx
leaq -0x830(%rbp), %rcx
leaq -0x834(%rbp), %r8
callq 0x43a0
cmpl $0x3, -0x7f4(%rbp)
jl 0x2a16
movsd -0x818(%rbp), %xmm0
leaq -0x860(%rbp), %rdi
callq 0x23c0
jmp 0x2a26
leaq -0x860(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2120
cvtsi2sdq -0x858(%rbp), %xmm0
movsd 0x25d9(%rip), %xmm1     # 0x5010
divsd %xmm1, %xmm0
callq 0x20d0
movl %eax, -0x83c(%rbp)
cmpl $0x3e8, -0x83c(%rbp)    # imm = 0x3E8
jl 0x2a75
movl -0x83c(%rbp), %eax
subl $0x3e8, %eax            # imm = 0x3E8
movl %eax, -0x83c(%rbp)
movq -0x860(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x860(%rbp)
leaq -0x860(%rbp), %rdi
callq 0x2210
movq %rax, -0x868(%rbp)
leaq -0x850(%rbp), %rdi
movq -0x868(%rbp), %rcx
movl $0x14, %esi
leaq 0x2648(%rip), %rdx      # 0x50ea
callq 0x21e0
movq -0x7f0(%rbp), %rdi
leaq -0x850(%rbp), %rdx
movl -0x83c(%rbp), %ecx
movl -0x82c(%rbp), %r8d
movl -0x838(%rbp), %r9d
movl -0x830(%rbp), %r11d
movl -0x834(%rbp), %r10d
movl -0x810(%rbp), %eax
leaq 0x260c(%rip), %rsi      # 0x50f0
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movl %eax, 0x10(%rsp)
movb $0x0, %al
callq 0x2160
cmpl $0x5, -0x7f4(%rbp)
jne 0x2b39
movq -0x800(%rbp), %rdi
leaq 0x25b3(%rip), %rsi      # 0x50c2
callq 0x2270
movq %rax, -0x800(%rbp)
movq -0x800(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x800(%rbp)
movb $0x20, -0x86d(%rbp)
jmp 0x2c8c
cmpl $0x4, -0x7f4(%rbp)
jne 0x2be7
movq -0x800(%rbp), %rdi
leaq 0x25b3(%rip), %rsi      # 0x5107
callq 0x2270
addq $0x6, %rax
movq %rax, -0x800(%rbp)
movb $0x20, -0x86d(%rbp)
movq -0x800(%rbp), %rcx
leaq -0x7e0(%rbp), %rdx
addq $0x7d0, %rdx            # imm = 0x7D0
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, -0x892(%rbp)
jae 0x2bc0
movq -0x800(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x892(%rbp)
je 0x2bc0
movq -0x800(%rbp), %rax
movsbl (%rax), %eax
movsbl -0x86d(%rbp), %ecx
cmpl %ecx, %eax
setne %al
movb %al, -0x892(%rbp)
movb -0x892(%rbp), %al
testb $0x1, %al
jne 0x2bcc
jmp 0x2be2
jmp 0x2bce
movq -0x800(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x800(%rbp)
jmp 0x2b6b
jmp 0x2c8a
movl -0x7f4(%rbp), %edx
movl $0x5d, %eax
movl $0x23, %ecx
cmpl $0x3, %edx
cmovel %ecx, %eax
movb %al, -0x86d(%rbp)
leaq -0x7e0(%rbp), %rax
movq %rax, -0x800(%rbp)
movq -0x800(%rbp), %rcx
leaq -0x7e0(%rbp), %rdx
addq $0x7d0, %rdx            # imm = 0x7D0
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, -0x893(%rbp)
jae 0x2c66
movq -0x800(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x893(%rbp)
je 0x2c66
movq -0x800(%rbp), %rax
movsbl (%rax), %eax
movsbl -0x86d(%rbp), %ecx
cmpl %ecx, %eax
setne %al
movb %al, -0x893(%rbp)
movb -0x893(%rbp), %al
testb $0x1, %al
jne 0x2c72
jmp 0x2c88
jmp 0x2c74
movq -0x800(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x800(%rbp)
jmp 0x2c11
jmp 0x2c8a
jmp 0x2c8c
movq -0x800(%rbp), %rax
movsbl (%rax), %eax
movsbl -0x86d(%rbp), %ecx
cmpl %ecx, %eax
jne 0x2d61
cmpl $0x3, -0x7f4(%rbp)
jne 0x2cc2
movq -0x800(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x800(%rbp)
jmp 0x2ce6
jmp 0x2cc4
movq -0x800(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x800(%rbp)
movsbl 0x1(%rax), %eax
cmpl $0x20, %eax
jne 0x2ce4
jmp 0x2cc4
jmp 0x2ce6
movl $0x0, -0x86c(%rbp)
movl -0x86c(%rbp), %eax
cmpl -0x810(%rbp), %eax
jge 0x2d5f
movq -0x800(%rbp), %rdi
leaq 0x2401(%rip), %rsi      # 0x510d
leaq -0x874(%rbp), %rdx
movb $0x0, %al
callq 0x21c0
movq -0x7f0(%rbp), %rdi
movl -0x874(%rbp), %edx
leaq 0x23e3(%rip), %rsi      # 0x5111
movb $0x0, %al
callq 0x2160
movl -0x86c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x86c(%rbp)
movl -0x7f8(%rbp), %ecx
movq -0x800(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x800(%rbp)
jmp 0x2cf0
jmp 0x2d61
movq -0x7f0(%rbp), %rdi
leaq 0x251d(%rip), %rsi      # 0x528c
movb $0x0, %al
callq 0x2160
movq -0x7f0(%rbp), %rdi
callq 0x21b0
jmp 0x2520
movl -0x4(%rbp), %eax
addq $0x8b0, %rsp            # imm = 0x8B0
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)