// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
xorl %r15d, %r15d
movq %rdi, -0x40(%rbp)
cmpl $0x2, (%rdi)
jl 0xfc3c
movq %rsi, %r12
movq (%rsi), %rbx
movq 0x8(%rbx), %r14
leaq 0x2625(%rip), %rsi      # 0x120d7
movq %r14, %rdi
callq 0x21d0
testl %eax, %eax
je 0xfc3c
movq %r12, -0x38(%rbp)
movq (%rbx), %rdi
callq 0x2260
movq %rax, 0x585b(%rip)      # 0x15330
movq %r14, %rdi
callq 0x2260
leaq 0x25f8(%rip), %rsi      # 0x120dc
movq %rax, -0x48(%rbp)
movq %rax, %rdi
callq 0x2200
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
callq 0x21d0
movl %eax, -0x2c(%rbp)
pushq $0x1
popq %rbx
pushq $0x5
popq %r15
leaq 0x5821(%rip), %rax      # 0x15330
leaq -0x50(%rbp), %r14
leaq (%rax,%r15,8), %r13
testq %r12, %r12
je 0xfbb1
cmpq $0x7ff, %r15            # imm = 0x7FF
ja 0xfbb1
movq -0x40(%rbp), %rax
cmpl (%rax), %ebx
jg 0xfbb1
movq %r12, %rdi
callq 0x21f0
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
imull $0x9470, %ecx, %eax     # imm = 0x9470
xorl %edx, %edx
movl $0xf4240, %ecx          # imm = 0xF4240
divl %ecx
movq %r14, %rdi
leaq 0x2581(%rip), %rsi      # 0x120de
movl %eax, %edx
xorl %eax, %eax
callq 0x2060
movq %r14, %rdi
callq 0x2260
movq %rax, (%r13)
cmpl $0x0, -0x2c(%rbp)
je 0xfb92
xorl %edi, %edi
leaq 0x255b(%rip), %rsi      # 0x120dc
callq 0x2200
movq %rax, %r12
addq $0x8, %r13
incq %r15
jmp 0xfb17
movq -0x38(%rbp), %rax
movq (%rax), %rax
movl %ebx, %ecx
incl %ebx
movq 0x8(%rax,%rcx,8), %r12
incq %r15
leaq 0x5784(%rip), %rax      # 0x15330
jmp 0xfb13
movq -0x48(%rbp), %rdi
callq 0x21a0
leaq 0x2516(%rip), %rdi      # 0x120d7
callq 0x2260
movq %rax, 0x576b(%rip)      # 0x15338
leaq 0x250f(%rip), %rdi      # 0x120e3
callq 0x2260
movq %rax, 0x5760(%rip)      # 0x15340
leal -0x4(%r15), %eax
pushq $0x2
popq %rcx
cltd
idivl %ecx
leaq 0x24ed(%rip), %rsi      # 0x120de
leaq -0x50(%rbp), %r14
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x2060
movq %r14, %rdi
callq 0x2260
movq %rax, 0x5738(%rip)      # 0x15348
movq 0x5721(%rip), %rdi      # 0x15338
callq 0x2260
movq %rax, 0x572d(%rip)      # 0x15350
movq -0x38(%rbp), %rax
leaq 0x5702(%rip), %rcx      # 0x15330
movq %rcx, (%rax)
movq -0x40(%rbp), %rax
movl %r15d, (%rax)
pushq $0x1
popq %r15
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq