// File: /workspace/github_c_makefile_star2_O0/canboat[P]canboat/socketcan-writer/socketcan-writer.c
// Function: main
int main(int argc, char **argv)
{
  FILE          *file = stdin;
  char           msg[2000];
  char          *milliSecond;
  int            socket;
  struct timeval frameTime;
  struct timeval prevFrameTime;
  struct tm      ctime;
  unsigned long  usWait;

  setProgName(argv[0]);
  if (argc != 2)
  {
    puts("Usage: socketcan-writer <can-device>");
    exit(1);
  }

  if (openCanDevice(argv[1], &socket))
  {
    exit(1);
  }

  prevFrameTime.tv_sec  = 0;
  prevFrameTime.tv_usec = 0;

  while (fgets(msg, sizeof(msg) - 1, file))
  {
    RawMessage m;
    if (parseRawFormatFast(msg, &m, false))
    {
      continue; // Parsing failed -> skip the line
    }
    if (strlen(m.timestamp) >= 19)
    {
      m.timestamp[10] = 'T'; // to support 'T', '-' and ' ' separators
      memset(&ctime, 0, sizeof(struct tm));
      milliSecond = strptime(m.timestamp, "%Y-%m-%dT%H:%M:%S", &ctime);
      if ((milliSecond != NULL) && (milliSecond - m.timestamp >= 19)) // convert in tm struct => OK
      {
        int usec;

        frameTime.tv_sec  = mktime(&ctime);
        frameTime.tv_usec = (sscanf(milliSecond, ".%3d", &usec) == 1) ? usec * 1000 : 0;
        usWait
            = ((prevFrameTime.tv_sec == 0) && (prevFrameTime.tv_usec == 0)) ? 0 : time_diff(prevFrameTime, frameTime, m.timestamp);
        prevFrameTime = frameTime;
      }
      else // convert in tm struct failed
      {
        usWait = 0;
      }
    }
    else // bad timestamp format YYYY-mm-dd[T|-| ]HH:MM:SS[.xxx] & min length 19 chrs
    {
      usWait = 0;
    }
    if (usWait > 0)
    {
      usleep(usWait);
    }
    writeRawPGNToCanSocket(&m, socket);
  }

  close(socket);
  exit(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x9a0, %rsp            # imm = 0x9A0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq 0x7b88(%rip), %rax      # 0x9fd8
movq (%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x30c0
cmpl $0x2, -0x8(%rbp)
je 0x247f
leaq 0x4ba8(%rip), %rdi      # 0x7018
callq 0x2080
movl $0x1, %edi
callq 0x2290
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x7fc(%rbp), %rsi
callq 0x26a0
cmpl $0x0, %eax
je 0x24a2
movl $0x1, %edi
callq 0x2290
movq $0x0, -0x820(%rbp)
movq $0x0, -0x818(%rbp)
leaq -0x7f0(%rbp), %rdi
movq -0x18(%rbp), %rdx
movl $0x7cf, %esi            # imm = 0x7CF
callq 0x2150
cmpq $0x0, %rax
je 0x268d
leaq -0x7f0(%rbp), %rdi
leaq -0x988(%rbp), %rsi
xorl %edx, %edx
callq 0x4f00
cmpl $0x0, %eax
je 0x24f3
jmp 0x24b8
leaq -0x988(%rbp), %rdi
callq 0x20b0
cmpq $0x13, %rax
jb 0x2653
movb $0x54, -0x97e(%rbp)
leaq -0x858(%rbp), %rdi
xorl %esi, %esi
movl $0x38, %edx
callq 0x2120
leaq -0x988(%rbp), %rdi
leaq 0x4b0c(%rip), %rsi      # 0x703d
leaq -0x858(%rbp), %rdx
callq 0x22d0
movq %rax, -0x7f8(%rbp)
cmpq $0x0, -0x7f8(%rbp)
je 0x2646
movq -0x7f8(%rbp), %rax
leaq -0x988(%rbp), %rcx
subq %rcx, %rax
cmpq $0x13, %rax
jl 0x2646
leaq -0x858(%rbp), %rdi
callq 0x21f0
movq %rax, -0x810(%rbp)
movq -0x7f8(%rbp), %rdi
leaq 0x4ac1(%rip), %rsi      # 0x704f
leaq -0x98c(%rbp), %rdx
movb $0x0, %al
callq 0x21e0
cmpl $0x1, %eax
jne 0x25b3
imull $0x3e8, -0x98c(%rbp), %eax # imm = 0x3E8
movl %eax, -0x990(%rbp)
jmp 0x25bd
xorl %eax, %eax
movl %eax, -0x990(%rbp)
jmp 0x25bd
movl -0x990(%rbp), %eax
cltq
movq %rax, -0x808(%rbp)
cmpq $0x0, -0x820(%rbp)
jne 0x25eb
cmpq $0x0, -0x818(%rbp)
jne 0x25eb
xorl %eax, %eax
movq %rax, -0x998(%rbp)
jmp 0x261a
leaq -0x988(%rbp), %r8
movq -0x820(%rbp), %rdi
movq -0x818(%rbp), %rsi
movq -0x810(%rbp), %rdx
movq -0x808(%rbp), %rcx
callq 0x27c0
movq %rax, -0x998(%rbp)
movq -0x998(%rbp), %rax
movq %rax, -0x860(%rbp)
movq -0x810(%rbp), %rax
movq %rax, -0x820(%rbp)
movq -0x808(%rbp), %rax
movq %rax, -0x818(%rbp)
jmp 0x2651
movq $0x0, -0x860(%rbp)
jmp 0x265e
movq $0x0, -0x860(%rbp)
cmpq $0x0, -0x860(%rbp)
jbe 0x2676
movq -0x860(%rbp), %rax
movl %eax, %edi
callq 0x2300
movl -0x7fc(%rbp), %esi
leaq -0x988(%rbp), %rdi
callq 0x2890
jmp 0x24b8
movl -0x7fc(%rbp), %edi
callq 0x2130
xorl %edi, %edi
callq 0x2290
nop