// File: /workspace/github_c_makefile_star2_O3/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), %r14
movq (%rsi), %rdi
callq 0x2d5a
cmpl $0x2, %ebp
jne 0x2556
movq 0x8(%rbx), %rbx
leaq 0x2c12(%rip), %rsi      # 0x508a
movq %rbx, %rdi
callq 0x2170
movl $0x1, %ebp
testl %eax, %eax
je 0x252e
cmpb $0x2d, (%rbx)
jne 0x249c
cmpb $0x0, 0x1(%rbx)
je 0x252e
movl $0x1d, %edi
movl $0x3, %esi
movl $0x1, %edx
callq 0x2330
testl %eax, %eax
jns 0x24bd
leaq 0x2bd6(%rip), %rdi      # 0x5091
jmp 0x251f
movl %eax, %ebp
movq %r14, %r15
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 0x24f8
leaq 0x2ba2(%rip), %rdi      # 0x5098
jmp 0x251f
leaq 0x68(%rsp), %rsi
movw $0x1d, (%rsi)
movl %eax, 0x4(%rsi)
movl %ebp, %edi
movl $0x18, %edx
callq 0x2230
testl %eax, %eax
movq %r15, %r14
jns 0x252e
leaq 0x2b88(%rip), %rdi      # 0x50a7
callq 0x2260
movl $0x1, %edi
callq 0x22a0
leaq 0x190(%rsp), %rdi
movl $0x7cf, %esi            # imm = 0x7CF
movq %r14, %rdx
callq 0x2160
testq %rax, %rax
jne 0x2564
movl %ebp, %edi
callq 0x2140
xorl %edi, %edi
callq 0x22a0
leaq 0x2ad3(%rip), %rdi      # 0x5030
callq 0x2090
jmp 0x2524
leaq 0x190(%rsp), %r12
xorl %ebx, %ebx
xorl %r13d, %r13d
movq %r14, 0x28(%rsp)
movl %ebp, 0x14(%rsp)
movq %r12, %rdi
leaq 0x68(%rsp), %r15
movq %r15, %rsi
xorl %edx, %edx
callq 0x3dff
testl %eax, %eax
jne 0x280e
movq %r15, %rdi
callq 0x20c0
cmpq $0x13, %rax
jb 0x26a5
movb $0x54, 0x72(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
movapd %xmm0, 0x40(%rsp)
movapd %xmm0, 0x50(%rsp)
movq $0x0, 0x60(%rsp)
movq %r15, %rdi
leaq 0x2a81(%rip), %rsi      # 0x5055
leaq 0x30(%rsp), %rbp
movq %rbp, %rdx
callq 0x22e0
movq %rax, %r14
testq %rax, %rax
sete %al
movq %r14, %rcx
subq %r15, %rcx
cmpq $0x13, %rcx
setl %cl
orb %al, %cl
jne 0x26b6
movq %rbp, %rdi
callq 0x2200
movq %rax, 0x18(%rsp)
movq %r14, %rdi
leaq 0x2a51(%rip), %rsi      # 0x5067
movq %rsp, %rdx
xorl %eax, %eax
callq 0x21f0
imull $0x3e8, (%rsp), %ecx    # imm = 0x3E8
cmpl $0x1, %eax
movl $0x0, %eax
cmovnel %eax, %ecx
movslq %ecx, %rax
movq %rax, 0x20(%rsp)
movq %rbx, %rax
orq %r13, %rax
leaq 0x68(%rsp), %rsi
leaq 0x9(%rsp), %r15
movl 0x14(%rsp), %ebp
je 0x26c9
cvtsi2sd %rbx, %xmm1
cvtsi2sd %r13, %xmm2
movsd 0x29a6(%rip), %xmm3     # 0x5008
mulsd %xmm3, %xmm1
xorps %xmm0, %xmm0
cvtsi2sdq 0x18(%rsp), %xmm0
addsd %xmm2, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %ecx, %xmm2
mulsd %xmm3, %xmm0
addsd %xmm2, %xmm0
subsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x282c
leaq 0x29d0(%rip), %rdi      # 0x506c
xorl %eax, %eax
callq 0x2bb0
jmp 0x26c9
movq %rbx, 0x18(%rsp)
movq %r13, 0x20(%rsp)
leaq 0x9(%rsp), %r15
jmp 0x26c9
movq %rbx, 0x18(%rsp)
movq %r13, 0x20(%rsp)
leaq 0x9(%rsp), %r15
movl 0x14(%rsp), %ebp
xorpd %xmm0, %xmm0
movapd %xmm0, (%rsp)
movl 0xa8(%rsp), %esi
cmpl $0x40000, %esi          # imm = 0x40000
jb 0x26f4
leaq 0x29c4(%rip), %rdi      # 0x50ac
xorl %eax, %eax
callq 0x2bb0
jmp 0x27ff
movzbl 0xa4(%rsp), %edi
movzbl 0xad(%rsp), %edx
movzbl 0xac(%rsp), %ecx
callq 0x3724
movl %eax, (%rsp)
movzbl 0xae(%rsp), %r14d
cmpq $0x8, %r14
ja 0x2742
movb %r14b, 0x4(%rsp)
leaq 0x8(%rsp), %rdi
leaq 0xaf(%rsp), %rsi
movq %r14, %rdx
callq 0x21b0
jmp 0x27f5
movabsq $-0x700000000, %rbx     # imm = 0xFFFFFFF900000000
xorl %r13d, %r13d
movb %r13b, 0x8(%rsp)
testq %r13, %r13
je 0x2785
movq %rbx, %rsi
sarq $0x20, %rsi
leaq 0xb5(%rsp), %rax
addq %rax, %rsi
cmpl $0x8, %r14d
jb 0x27e2
movl (%rsi), %eax
movl 0x3(%rsi), %ecx
movl %ecx, 0x3(%r15)
movl %eax, (%r15)
movl $0xfffffff9, %r12d      # imm = 0xFFFFFFF9
jmp 0x27b0
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)
movl $0xfffffffa, %r12d      # imm = 0xFFFFFFFA
movb $0x8, 0x4(%rsp)
movq %rsp, %rdi
movl %ebp, %esi
callq 0x28d2
incq %r13
movabsq $0x700000000, %rax      # imm = 0x700000000
addq %rax, %rbx
addl %r12d, %r14d
leaq 0x190(%rsp), %r12
jg 0x274f
jmp 0x27ff
movl %r14d, %edx
movq %r15, %rdi
callq 0x21b0
incb %r14b
movb %r14b, 0x4(%rsp)
movq %rsp, %rdi
movl %ebp, %esi
callq 0x28d2
movq 0x18(%rsp), %rbx
movq 0x20(%rsp), %r13
movq 0x28(%rsp), %r14
movq %r12, %rdi
movl $0x7cf, %esi            # imm = 0x7CF
movq %r14, %rdx
callq 0x2160
testq %rax, %rax
jne 0x257a
jmp 0x2548
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x27d0(%rip), %xmm0     # 0x5010
cvttsd2si %xmm0, %rdi
andq %rcx, %rdi
orq %rax, %rdi
je 0x26c9
callq 0x2310
jmp 0x26c9