// File: lucaong[P]elixir_mlx90640/src/MLX90640_API.cpp
// Function: ExtractKtaPixelParameters(unsigned short*, paramsMLX90640*)
void ExtractKtaPixelParameters(uint16_t *eeData, paramsMLX90640 *mlx90640)
{
    int p = 0;
    int8_t KtaRC[4];
    int8_t KtaRoCo;
    int8_t KtaRoCe;
    int8_t KtaReCo;
    int8_t KtaReCe;
    uint8_t ktaScale1;
    uint8_t ktaScale2;
    uint8_t split;

    KtaRoCo = (eeData[54] & 0xFF00) >> 8;
    if (KtaRoCo > 127)
    {
        KtaRoCo = KtaRoCo - 256;
    }
    KtaRC[0] = KtaRoCo;
    
    KtaReCo = (eeData[54] & 0x00FF);
    if (KtaReCo > 127)
    {
        KtaReCo = KtaReCo - 256;
    }
    KtaRC[2] = KtaReCo;
      
    KtaRoCe = (eeData[55] & 0xFF00) >> 8;
    if (KtaRoCe > 127)
    {
        KtaRoCe = KtaRoCe - 256;
    }
    KtaRC[1] = KtaRoCe;
      
    KtaReCe = (eeData[55] & 0x00FF);
    if (KtaReCe > 127)
    {
        KtaReCe = KtaReCe - 256;
    }
    KtaRC[3] = KtaReCe;
  
    ktaScale1 = ((eeData[56] & 0x00F0) >> 4) + 8;
    ktaScale2 = (eeData[56] & 0x000F);

    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->kta[p] = (eeData[64 + p] & 0x000E) >> 1;
            if (mlx90640->kta[p] > 3)
            {
                mlx90640->kta[p] = mlx90640->kta[p] - 8;
            }
            mlx90640->kta[p] = mlx90640->kta[p] * (1 << ktaScale2);
            mlx90640->kta[p] = KtaRC[split] + mlx90640->kta[p];
            mlx90640->kta[p] = mlx90640->kta[p] / pow(2,(double)ktaScale1);
        }
    }
}

// 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 0x6c(%rax), %eax
andl $0xff00, %eax           # imm = 0xFF00
sarl $0x8, %eax
movb %al, -0x19(%rbp)
movsbl -0x19(%rbp), %eax
cmpl $0x7f, %eax
jle 0x2f2f
movsbl -0x19(%rbp), %eax
subl $0x100, %eax            # imm = 0x100
movb %al, -0x19(%rbp)
movb -0x19(%rbp), %al
movb %al, -0x18(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x6c(%rax), %eax
andl $0xff, %eax
movb %al, -0x1b(%rbp)
movsbl -0x1b(%rbp), %eax
cmpl $0x7f, %eax
jle 0x2f5a
movsbl -0x1b(%rbp), %eax
subl $0x100, %eax            # imm = 0x100
movb %al, -0x1b(%rbp)
movb -0x1b(%rbp), %al
movb %al, -0x16(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x6e(%rax), %eax
andl $0xff00, %eax           # imm = 0xFF00
sarl $0x8, %eax
movb %al, -0x1a(%rbp)
movsbl -0x1a(%rbp), %eax
cmpl $0x7f, %eax
jle 0x2f88
movsbl -0x1a(%rbp), %eax
subl $0x100, %eax            # imm = 0x100
movb %al, -0x1a(%rbp)
movb -0x1a(%rbp), %al
movb %al, -0x17(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x6e(%rax), %eax
andl $0xff, %eax
movb %al, -0x1c(%rbp)
movsbl -0x1c(%rbp), %eax
cmpl $0x7f, %eax
jle 0x2fb3
movsbl -0x1c(%rbp), %eax
subl $0x100, %eax            # imm = 0x100
movb %al, -0x1c(%rbp)
movb -0x1c(%rbp), %al
movb %al, -0x15(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x70(%rax), %eax
andl $0xf0, %eax
sarl $0x4, %eax
addl $0x8, %eax
movb %al, -0x1d(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x70(%rax), %eax
andl $0xf, %eax
movb %al, -0x1e(%rbp)
movl $0x0, -0x24(%rbp)
cmpl $0x18, -0x24(%rbp)
jge 0x3186
movl $0x0, -0x28(%rbp)
cmpl $0x20, -0x28(%rbp)
jge 0x3176
movl -0x24(%rbp), %eax
shll $0x5, %eax
addl -0x28(%rbp), %eax
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %eax
movl $0x20, %ecx
cltd
idivl %ecx
movl %eax, -0x30(%rbp)
movl -0x14(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x30(%rbp), %eax
shll %ecx
subl %ecx, %eax
shll %eax
movl %eax, -0x2c(%rbp)
movl -0x14(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl -0x2c(%rbp), %eax
addl %edx, %eax
movb %al, -0x1f(%rbp)
movq -0x8(%rbp), %rax
movl -0x14(%rbp), %ecx
addl $0x40, %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0xe, %eax
sarl %eax
cvtsi2ss %eax, %xmm0
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movss %xmm0, 0x1244(%rax,%rcx,4)
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movss 0x1244(%rax,%rcx,4), %xmm0
movss 0x2fdf(%rip), %xmm1     # 0x6068
ucomiss %xmm1, %xmm0
jbe 0x30bc
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movss 0x1244(%rax,%rcx,4), %xmm0
movss 0x2fad(%rip), %xmm1     # 0x6054
subss %xmm1, %xmm0
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movss %xmm0, 0x1244(%rax,%rcx,4)
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq %rcx, -0x40(%rbp)
movss 0x1244(%rax,%rcx,4), %xmm0
movb -0x1e(%rbp), %cl
movl $0x1, %edx
shll %cl, %edx
movq -0x40(%rbp), %rcx
cvtsi2ss %edx, %xmm1
mulss %xmm1, %xmm0
movss %xmm0, 0x1244(%rax,%rcx,4)
movzbl -0x1f(%rbp), %eax
movsbl -0x18(%rbp,%rax), %eax
cvtsi2ss %eax, %xmm0
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movss 0x1244(%rax,%rcx,4), %xmm1
addss %xmm1, %xmm0
movss %xmm0, 0x1244(%rax,%rcx,4)
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movss 0x1244(%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
movzbl -0x1d(%rbp), %eax
cvtsi2sd %eax, %xmm0
movl $0x2, %edi
callq 0x5ab0
movaps %xmm0, %xmm1
movsd -0x38(%rbp), %xmm0
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movss %xmm0, 0x1244(%rax,%rcx,4)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x2ff5
jmp 0x3178
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x2fe4
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)