// File: lucaong[P]elixir_mlx90640/src/MLX90640_API.cpp
// Function: ExtractKvPixelParameters(unsigned short*, paramsMLX90640*)
void ExtractKvPixelParameters(uint16_t *eeData, paramsMLX90640 *mlx90640)
{
    int p = 0;
    int8_t KvT[4];
    int8_t KvRoCo;
    int8_t KvRoCe;
    int8_t KvReCo;
    int8_t KvReCe;
    uint8_t kvScale;
    uint8_t split;

    KvRoCo = (eeData[52] & 0xF000) >> 12;
    if (KvRoCo > 7)
    {
        KvRoCo = KvRoCo - 16;
    }
    KvT[0] = KvRoCo;
    
    KvReCo = (eeData[52] & 0x0F00) >> 8;
    if (KvReCo > 7)
    {
        KvReCo = KvReCo - 16;
    }
    KvT[2] = KvReCo;
      
    KvRoCe = (eeData[52] & 0x00F0) >> 4;
    if (KvRoCe > 7)
    {
        KvRoCe = KvRoCe - 16;
    }
    KvT[1] = KvRoCe;
      
    KvReCe = (eeData[52] & 0x000F);
    if (KvReCe > 7)
    {
        KvReCe = KvReCe - 16;
    }
    KvT[3] = KvReCe;
  
    kvScale = (eeData[56] & 0x0F00) >> 8;


    for(int i = 0; i < 24; i++)
    {
        for(int j = 0; j < 32; j ++)
        {
            p = 32 * i +j;
            split = 2*(p/32 - (p/64)*2) + p%2;
            mlx90640->kv[p] = KvT[split];
            mlx90640->kv[p] = mlx90640->kv[p] / pow(2,(double)kvScale);
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x68(%rax), %eax
andl $0xf000, %eax           # imm = 0xF000
sarl $0xc, %eax
movb %al, -0x19(%rbp)
movsbl -0x19(%rbp), %eax
cmpl $0x7, %eax
jle 0x31cd
movsbl -0x19(%rbp), %eax
subl $0x10, %eax
movb %al, -0x19(%rbp)
movb -0x19(%rbp), %al
movb %al, -0x18(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x68(%rax), %eax
andl $0xf00, %eax            # imm = 0xF00
sarl $0x8, %eax
movb %al, -0x1b(%rbp)
movsbl -0x1b(%rbp), %eax
cmpl $0x7, %eax
jle 0x31f9
movsbl -0x1b(%rbp), %eax
subl $0x10, %eax
movb %al, -0x1b(%rbp)
movb -0x1b(%rbp), %al
movb %al, -0x16(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x68(%rax), %eax
andl $0xf0, %eax
sarl $0x4, %eax
movb %al, -0x1a(%rbp)
movsbl -0x1a(%rbp), %eax
cmpl $0x7, %eax
jle 0x3225
movsbl -0x1a(%rbp), %eax
subl $0x10, %eax
movb %al, -0x1a(%rbp)
movb -0x1a(%rbp), %al
movb %al, -0x17(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x68(%rax), %eax
andl $0xf, %eax
movb %al, -0x1c(%rbp)
movsbl -0x1c(%rbp), %eax
cmpl $0x7, %eax
jle 0x324c
movsbl -0x1c(%rbp), %eax
subl $0x10, %eax
movb %al, -0x1c(%rbp)
movb -0x1c(%rbp), %al
movb %al, -0x15(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x70(%rax), %eax
andl $0xf00, %eax            # imm = 0xF00
sarl $0x8, %eax
movb %al, -0x1d(%rbp)
movl $0x0, -0x24(%rbp)
cmpl $0x18, -0x24(%rbp)
jge 0x3360
movl $0x0, -0x28(%rbp)
cmpl $0x20, -0x28(%rbp)
jge 0x3350
movl -0x24(%rbp), %eax
shll $0x5, %eax
movl -0x28(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %edx
movl %edx, -0x34(%rbp)
movl %edx, %ecx
addl $0x1f, %ecx
movl %edx, %esi
addl $0x3f, %esi
testl %edx, %edx
movl %edx, %eax
cmovsl %ecx, %eax
movl %edx, %ecx
cmovsl %esi, %ecx
shrl $0x5, %eax
shrl $0x5, %ecx
andl $-0x2, %ecx
subl %ecx, %eax
movl %edx, %esi
shrl $0x1f, %esi
movl %edx, %ecx
addl %esi, %ecx
andl $0xfe, %ecx
subl %ecx, %edx
movl %eax, %ecx
movl %edx, %eax
leal (%rax,%rcx,2), %eax
movb %al, -0x1e(%rbp)
movzbl -0x1e(%rbp), %eax
movsbl -0x18(%rbp,%rax), %eax
cvtsi2ss %eax, %xmm0
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movss %xmm0, 0x1e44(%rax,%rcx,4)
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movss 0x1e44(%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movzbl -0x1d(%rbp), %eax
cvtsi2sd %eax, %xmm0
movl $0x2, %edi
callq 0x5ab0
movaps %xmm0, %xmm1
movsd -0x30(%rbp), %xmm0
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movss %xmm0, 0x1e44(%rax,%rcx,4)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x327d
jmp 0x3352
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x326c
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)