// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x968, %rsp            # imm = 0x968
movq %rsi, %rbx
movl %edi, %ebp
movq 0x5b7f(%rip), %rax      # 0x7fd8
movq (%rax), %rax
movq %rax, 0x28(%rsp)
movq (%rsi), %rdi
callq 0x2c91
cmpl $0x2, %ebp
jne 0x27ba
pushq $0x1
popq %rbp
movq 0x8(%rbx), %rbx
leaq 0x2c0a(%rip), %rsi      # 0x508a
movq %rbx, %rdi
callq 0x2170
testl %eax, %eax
je 0x251f
cmpb $0x2d, (%rbx)
jne 0x249f
cmpb $0x0, 0x1(%rbx)
je 0x251f
pushq $0x1d
popq %rdi
pushq $0x3
popq %rsi
pushq $0x1
popq %rdx
callq 0x2330
testl %eax, %eax
jns 0x24ba
leaq 0x2bd9(%rip), %rdi      # 0x5091
jmp 0x2512
movl %eax, %ebp
leaq 0x190(%rsp), %r14
pushq $0xf
popq %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2050
movb $0x0, 0xf(%r14)
movq %r14, %rdi
callq 0x2190
movl %eax, 0x10(%r14)
testl %eax, %eax
jne 0x24f0
leaq 0x2baa(%rip), %rdi      # 0x5098
jmp 0x2512
leaq 0x68(%rsp), %rsi
movw $0x1d, (%rsi)
movl %eax, 0x4(%rsi)
pushq $0x18
popq %rdx
movl %ebp, %edi
callq 0x2230
testl %eax, %eax
jns 0x251f
leaq 0x2b95(%rip), %rdi      # 0x50a7
callq 0x2260
pushq $0x1
popq %rdi
callq 0x22a0
leaq 0x190(%rsp), %r12
movq %rsp, %rbx
movabsq $0x700000000, %r15      # imm = 0x700000000
movq $0x0, 0x18(%rsp)
xorl %r13d, %r13d
movq %r15, %r14
movq %r12, %rdi
movl $0x7cf, %esi            # imm = 0x7CF
movq 0x28(%rsp), %rdx
callq 0x2160
testq %rax, %rax
je 0x27ac
movq %r12, %rdi
leaq 0x68(%rsp), %r15
movq %r15, %rsi
xorl %edx, %edx
callq 0x3c33
testl %eax, %eax
je 0x2579
movq %r14, %r15
jmp 0x2540
movq %r15, %rdi
callq 0x20c0
cmpq $0x13, %rax
jb 0x262d
movb $0x54, 0x72(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x50(%rsp)
andq $0x0, 0x60(%rsp)
movq %r15, %rdi
leaq 0x2aa3(%rip), %rsi      # 0x5055
leaq 0x30(%rsp), %rbx
movq %rbx, %rdx
callq 0x22e0
movq %rax, %r12
testq %rax, %rax
sete %al
movq %r12, %rcx
subq %r15, %rcx
cmpq $0x13, %rcx
setl %cl
orb %al, %cl
jne 0x2634
movq %rbx, %rdi
callq 0x2200
movq %rax, 0x20(%rsp)
movq %r12, %rdi
leaq 0x2a77(%rip), %rsi      # 0x5067
movq %rsp, %rbx
movq %rbx, %rdx
xorl %eax, %eax
callq 0x21f0
imull $0x3e8, (%rsp), %ecx    # imm = 0x3E8
cmpl $0x1, %eax
movl $0x0, %eax
cmovnel %eax, %ecx
movslq %ecx, %r12
movq %r13, %rax
movq 0x18(%rsp), %rsi
orq %rsi, %rax
movq %r14, %r15
jne 0x277f
movq 0x20(%rsp), %r13
jmp 0x263f
movq 0x18(%rsp), %r12
jmp 0x263c
movq 0x18(%rsp), %r12
movq %rsp, %rbx
movq %r14, %r15
movq %r12, 0x18(%rsp)
movq %r13, 0x20(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movl 0xa8(%rsp), %esi
cmpl $0x40000, %esi          # imm = 0x40000
jb 0x266f
leaq 0x2a46(%rip), %rdi      # 0x50ac
xorl %eax, %eax
callq 0x2aed
jmp 0x26c2
movzbl 0xa4(%rsp), %edi
movzbl 0xad(%rsp), %edx
movzbl 0xac(%rsp), %ecx
callq 0x35b6
movl %eax, (%rsp)
movzbl 0xae(%rsp), %r14d
cmpq $0x8, %r14
ja 0x26d4
movb %r14b, 0x4(%rsp)
leaq 0x8(%rsp), %rdi
leaq 0xaf(%rsp), %rsi
movq %r14, %rdx
callq 0x21b0
movq %rbx, %rdi
movl %ebp, %esi
callq 0x283f
movq 0x20(%rsp), %r13
leaq 0x190(%rsp), %r12
jmp 0x2540
movabsq $-0x700000000, %r13     # imm = 0xFFFFFFF900000000
xorl %r12d, %r12d
testl %r14d, %r14d
jle 0x26c2
movb %r12b, 0x8(%rsp)
testq %r12, %r12
je 0x2722
movq %r13, %rsi
sarq $0x20, %rsi
leaq 0xb5(%rsp), %rax
addq %rax, %rsi
cmpl $0x8, %r14d
jb 0x2752
movl (%rsi), %eax
movl 0x3(%rsi), %ecx
leaq 0x9(%rsp), %rdx
movl %ecx, 0x3(%rdx)
movl %eax, (%rdx)
movb $0x8, 0x4(%rsp)
addl $-0x7, %r14d
jmp 0x276a
movb 0xae(%rsp), %al
movb %al, 0x9(%rsp)
leaq 0xaf(%rsp), %rdx
movzwl 0x4(%rdx), %eax
leaq 0x8(%rsp), %rcx
movw %ax, 0x6(%rcx)
movl (%rdx), %eax
movl %eax, 0x2(%rcx)
movb $0x8, 0x4(%rsp)
addl $-0x6, %r14d
jmp 0x276a
movl %r14d, %edx
leaq 0x9(%rsp), %rdi
callq 0x21b0
incb %r14b
movb %r14b, 0x4(%rsp)
xorl %r14d, %r14d
movq %rbx, %rdi
movl %ebp, %esi
callq 0x283f
incq %r12
addq %r15, %r13
jmp 0x26e1
movq %r13, %rdi
movq 0x20(%rsp), %r13
movq %r13, %rdx
movq %r12, %rcx
leaq 0x68(%rsp), %r8
callq 0x27cb
testq %rax, %rax
je 0x263f
movl %eax, %edi
callq 0x2310
jmp 0x263f
movl %ebp, %edi
callq 0x2140
xorl %edi, %edi
callq 0x22a0
leaq 0x286f(%rip), %rdi      # 0x5030
callq 0x2090
jmp 0x2517