// File: /workspace/github_c_makefile_star2_O1/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), %r12
movq (%rsi), %rdi
callq 0x2d66
cmpl $0x2, %ebp
jne 0x24c6
movq 0x8(%rbx), %rbx
leaq 0x2c16(%rip), %rsi      # 0x508a
movq %rbx, %rdi
callq 0x2170
movb $0x1, %r15b
movl $0x1, %ebp
testl %eax, %eax
je 0x2539
leaq 0x2bfe(%rip), %rsi      # 0x5091
movq %rbx, %rdi
callq 0x2170
testl %eax, %eax
je 0x2539
movl $0x1d, %edi
movl $0x3, %esi
movl $0x1, %edx
callq 0x2330
movl %eax, %ebp
testl %eax, %eax
jns 0x24d7
leaq 0x2bcf(%rip), %rdi      # 0x5093
jmp 0x2531
leaq 0x2b63(%rip), %rdi      # 0x5030
callq 0x2090
jmp 0x285a
leaq 0x190(%rsp), %r14
movl $0xf, %edx
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 0x250d
leaq 0x2b8f(%rip), %rdi      # 0x509a
jmp 0x2531
leaq 0x68(%rsp), %rsi
movw $0x1d, (%rsi)
movl %eax, 0x4(%rsi)
movl %ebp, %edi
movl $0x18, %edx
callq 0x2230
testl %eax, %eax
jns 0x2539
leaq 0x2b78(%rip), %rdi      # 0x50a9
callq 0x2260
xorl %r15d, %r15d
testb %r15b, %r15b
je 0x285a
leaq 0x190(%rsp), %rdi
movl $0x7cf, %esi            # imm = 0x7CF
movq %r12, %rdx
callq 0x2160
testq %rax, %rax
je 0x284c
leaq 0x9(%rsp), %r14
leaq 0x190(%rsp), %r15
leaq 0x68(%rsp), %r13
movq %rsp, %rbx
movq $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movq %r12, 0x28(%rsp)
movq %r15, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x3e3b
testl %eax, %eax
jne 0x2833
movq %r13, %rdi
callq 0x20c0
cmpq $0x13, %rax
jb 0x265b
movb $0x54, 0x72(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
movapd %xmm0, 0x40(%rsp)
movapd %xmm0, 0x50(%rsp)
movq $0x0, 0x60(%rsp)
movq %r13, %rdi
leaq 0x2a74(%rip), %rsi      # 0x5055
leaq 0x30(%rsp), %r15
movq %r15, %rdx
callq 0x22e0
movq %rax, %r12
testq %rax, %rax
sete %al
movq %r12, %rcx
subq %r13, %rcx
cmpq $0x13, %rcx
setl %cl
orb %al, %cl
jne 0x265b
movq %r15, %rdi
callq 0x2200
movq %rax, 0x10(%rsp)
movq %r12, %rdi
leaq 0x2a48(%rip), %rsi      # 0x5067
movq %rbx, %rdx
xorl %eax, %eax
callq 0x21f0
imull $0x3e8, (%rsp), %r15d   # imm = 0x3E8
cmpl $0x1, %eax
movl $0x0, %eax
cmovnel %eax, %r15d
movq 0x18(%rsp), %rdx
movq %rdx, %rax
movq 0x20(%rsp), %rcx
orq %rcx, %rax
jne 0x2662
xorl %edi, %edi
movq 0x10(%rsp), %r12
jmp 0x26db
xorl %edi, %edi
jmp 0x26e8
cvtsi2sd %rdx, %xmm1
cvtsi2sd %rcx, %xmm2
movsd 0x2994(%rip), %xmm3     # 0x5008
mulsd %xmm3, %xmm1
movq 0x10(%rsp), %r12
xorps %xmm0, %xmm0
cvtsi2sd %r12, %xmm0
addsd %xmm2, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %r15d, %xmm2
mulsd %xmm3, %xmm0
addsd %xmm2, %xmm0
subsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x26bc
leaq 0x29be(%rip), %rdi      # 0x506c
movq %r13, %rsi
xorl %eax, %eax
callq 0x2bb8
xorl %edi, %edi
jmp 0x26db
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x2940(%rip), %xmm0     # 0x5010
cvttsd2si %xmm0, %rdi
andq %rcx, %rdi
orq %rax, %rdi
movslq %r15d, %rax
movq %rax, 0x20(%rsp)
movq %r12, 0x18(%rsp)
testq %rdi, %rdi
je 0x26f2
callq 0x2310
xorpd %xmm0, %xmm0
movapd %xmm0, (%rsp)
movl 0xa8(%rsp), %esi
cmpl $0x40000, %esi          # imm = 0x40000
jb 0x271d
leaq 0x299d(%rip), %rdi      # 0x50ae
xorl %eax, %eax
callq 0x2bb8
jmp 0x2821
movzbl 0xa4(%rsp), %edi
movzbl 0xad(%rsp), %edx
movzbl 0xac(%rsp), %ecx
callq 0x3731
movl %eax, (%rsp)
movzbl 0xae(%rsp), %r12d
cmpq $0x8, %r12
ja 0x2775
movb %r12b, 0x4(%rsp)
leaq 0x8(%rsp), %rdi
leaq 0xaf(%rsp), %rsi
movq %r12, %rdx
callq 0x21b0
movq %rbx, %rdi
movl %ebp, %esi
callq 0x28db
jmp 0x2821
movabsq $-0x700000000, %r15     # imm = 0xFFFFFFF900000000
xorl %r13d, %r13d
movb %r13b, 0x8(%rsp)
testq %r13, %r13
je 0x27bb
movq %r15, %rsi
sarq $0x20, %rsi
leaq 0xb5(%rsp), %rax
addq %rax, %rsi
cmpl $0x8, %r12d
jb 0x27e8
movl (%rsi), %eax
movl 0x3(%rsi), %ecx
movl %ecx, 0x3(%r14)
movl %eax, (%r14)
movb $0x8, 0x4(%rsp)
addl $-0x7, %r12d
jmp 0x27fe
movb 0xae(%rsp), %al
movb %al, 0x9(%rsp)
leaq 0xaf(%rsp), %rcx
movzwl 0x4(%rcx), %eax
movw %ax, 0x5(%r14)
movl (%rcx), %eax
movl %eax, 0x1(%r14)
movb $0x8, 0x4(%rsp)
addl $-0x6, %r12d
jmp 0x27fe
movl %r12d, %edx
movq %r14, %rdi
callq 0x21b0
incb %r12b
movb %r12b, 0x4(%rsp)
xorl %r12d, %r12d
movq %rbx, %rdi
movl %ebp, %esi
callq 0x28db
incq %r13
movabsq $0x700000000, %rax      # imm = 0x700000000
addq %rax, %r15
testl %r12d, %r12d
jg 0x2782
movq 0x28(%rsp), %r12
leaq 0x190(%rsp), %r15
leaq 0x68(%rsp), %r13
movq %r15, %rdi
movl $0x7cf, %esi            # imm = 0x7CF
movq %r12, %rdx
callq 0x2160
testq %rax, %rax
jne 0x258c
movl %ebp, %edi
callq 0x2140
xorl %edi, %edi
callq 0x22a0
movl $0x1, %edi
callq 0x22a0