// File: probonopd[P]decodeir/DecodeIR.cpp
// Function: raw_to_pronto(int*, char***)
int raw_to_pronto(int* argcp, char** argvp[]) {
  if (*argcp<2)
    return 0;

  char *p1 = (*argvp)[1];
  if (strcmp(p1,"0000")==0)
    return 0;

  const int max_size = 2048;
  static char* argv[max_size];
  argv[0] = strdup((*argvp)[0]);

  char *st = strdup(p1);
  char *ch = strtok(st, ",");
  int comma_separated = strcmp(ch,p1)!=0;

  char buf[8];
  int size = 5;

  for (int i=1; ch && size<max_size && i<(*argcp)+1;) {
    int value = abs(atoi(ch));
    sprintf(buf,"%04X", value*38000/1000000);
    argv[size++] = strdup(buf);
    ch = comma_separated ? strtok(NULL, ",") : (*argvp)[++i];
  }

  free(st);

  argv[1] = strdup("0000");
  argv[2] = strdup("006D");
  sprintf(buf,"%04X", (size-4)/2);
  argv[3] = strdup(buf);
  argv[4] = strdup(argv[1]);

  *argvp = argv;
  *argcp = size;

  return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x2, (%rax)
jge 0x17af5
movl $0x0, -0x4(%rbp)
jmp 0x17cf7
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
leaq 0x36f6(%rip), %rsi      # 0x1b205
callq 0x21c0
cmpl $0x0, %eax
jne 0x17b25
movl $0x0, -0x4(%rbp)
jmp 0x17cf7
movl $0x800, -0x24(%rbp)     # imm = 0x800
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rdi
callq 0x2250
movq %rax, 0x77ee(%rip)      # 0x1f330
movq -0x20(%rbp), %rdi
callq 0x2250
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
leaq 0x36b0(%rip), %rsi      # 0x1b20a
callq 0x21f0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x21c0
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x3c(%rbp)
movl $0x5, -0x48(%rbp)
movl $0x1, -0x4c(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x38(%rbp)
movb %al, -0x51(%rbp)
je 0x17bba
xorl %eax, %eax
cmpl $0x800, -0x48(%rbp)     # imm = 0x800
movb %al, -0x51(%rbp)
jge 0x17bba
movl -0x4c(%rbp), %eax
movq -0x10(%rbp), %rcx
movl (%rcx), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
setl %al
movb %al, -0x51(%rbp)
movb -0x51(%rbp), %al
testb $0x1, %al
jne 0x17bc6
jmp 0x17c65
movq -0x38(%rbp), %rdi
callq 0x21e0
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
movl %eax, -0x50(%rbp)
leaq -0x44(%rbp), %rdi
imull $0x9470, -0x50(%rbp), %eax # imm = 0x9470
movl $0xf4240, %ecx          # imm = 0xF4240
cltd
idivl %ecx
movl %eax, %edx
leaq 0x3617(%rip), %rsi      # 0x1b20c
movb $0x0, %al
callq 0x2060
leaq -0x44(%rbp), %rdi
callq 0x2250
movq %rax, %rdx
movl -0x48(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x48(%rbp)
movslq %eax, %rcx
leaq 0x7713(%rip), %rax      # 0x1f330
movq %rdx, (%rax,%rcx,8)
cmpl $0x0, -0x3c(%rbp)
je 0x17c3d
xorl %eax, %eax
movl %eax, %edi
leaq 0x35d8(%rip), %rsi      # 0x1b20a
callq 0x21f0
movq %rax, -0x60(%rbp)
jmp 0x17c58
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl -0x4c(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x4c(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0x17b8c
movq -0x30(%rbp), %rdi
callq 0x2190
leaq 0x3590(%rip), %rdi      # 0x1b205
callq 0x2250
movq %rax, 0x76b7(%rip)      # 0x1f338
leaq 0x3589(%rip), %rdi      # 0x1b211
callq 0x2250
movq %rax, 0x76ac(%rip)      # 0x1f340
leaq -0x44(%rbp), %rdi
movl -0x48(%rbp), %eax
subl $0x4, %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %edx
leaq 0x355d(%rip), %rsi      # 0x1b20c
movb $0x0, %al
callq 0x2060
leaq -0x44(%rbp), %rdi
callq 0x2250
movq %rax, 0x7682(%rip)      # 0x1f348
movq 0x766b(%rip), %rdi      # 0x1f338
callq 0x2250
movq %rax, 0x7677(%rip)      # 0x1f350
movq -0x18(%rbp), %rax
leaq 0x764c(%rip), %rcx      # 0x1f330
movq %rcx, (%rax)
movl -0x48(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq