// File: lucaong[P]elixir_mlx90640/src/MLX90640_API.cpp
// Function: ExtractKsToParameters(unsigned short*, paramsMLX90640*)
void ExtractKsToParameters(uint16_t *eeData, paramsMLX90640 *mlx90640)
{
    int KsToScale;
    int8_t step;
    
    step = ((eeData[63] & 0x3000) >> 12) * 10;
    
    mlx90640->ct[0] = -40;
    mlx90640->ct[1] = 0;
    mlx90640->ct[2] = (eeData[63] & 0x00F0) >> 4;
    mlx90640->ct[3] = (eeData[63] & 0x0F00) >> 8;
    
    mlx90640->ct[2] = mlx90640->ct[2]*step;
    mlx90640->ct[3] = mlx90640->ct[2] + mlx90640->ct[3]*step;
    
    KsToScale = (eeData[63] & 0x000F) + 8;
    KsToScale = 1 << KsToScale;
    
    mlx90640->ksTo[0] = eeData[61] & 0x00FF;
    mlx90640->ksTo[1] = (eeData[61] & 0xFF00) >> 8;
    mlx90640->ksTo[2] = eeData[62] & 0x00FF;
    mlx90640->ksTo[3] = (eeData[62] & 0xFF00) >> 8;
    
    
    for(int i = 0; i < 4; i++)
    {
        if(mlx90640->ksTo[i] > 127)
        {
            mlx90640->ksTo[i] = mlx90640->ksTo[i] -256;
        }
        mlx90640->ksTo[i] = mlx90640->ksTo[i] / KsToScale;
    } 
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x7e(%rax), %eax
andl $0x3000, %eax           # imm = 0x3000
sarl $0xc, %eax
imull $0xa, %eax, %eax
movb %al, -0x15(%rbp)
movq -0x10(%rbp), %rax
movw $0xffd8, 0x3c(%rax)     # imm = 0xFFD8
movq -0x10(%rbp), %rax
movw $0x0, 0x3e(%rax)
movq -0x8(%rbp), %rax
movzwl 0x7e(%rax), %eax
andl $0xf0, %eax
sarl $0x4, %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x40(%rax)
movq -0x8(%rbp), %rax
movzwl 0x7e(%rax), %eax
andl $0xf00, %eax            # imm = 0xF00
sarl $0x8, %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x42(%rax)
movq -0x10(%rbp), %rax
movswl 0x40(%rax), %eax
movsbl -0x15(%rbp), %ecx
imull %ecx, %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x40(%rax)
movq -0x10(%rbp), %rax
movswl 0x40(%rax), %eax
movq -0x10(%rbp), %rcx
movswl 0x42(%rcx), %ecx
movsbl -0x15(%rbp), %edx
imull %edx, %ecx
addl %ecx, %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x42(%rax)
movq -0x8(%rbp), %rax
movzwl 0x7e(%rax), %eax
andl $0xf, %eax
addl $0x8, %eax
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, -0x14(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x7a(%rax), %eax
andl $0xff, %eax
cvtsi2ss %eax, %xmm0
movq -0x10(%rbp), %rax
movss %xmm0, 0x2c(%rax)
movq -0x8(%rbp), %rax
movzwl 0x7a(%rax), %eax
andl $0xff00, %eax           # imm = 0xFF00
sarl $0x8, %eax
cvtsi2ss %eax, %xmm0
movq -0x10(%rbp), %rax
movss %xmm0, 0x30(%rax)
movq -0x8(%rbp), %rax
movzwl 0x7c(%rax), %eax
andl $0xff, %eax
cvtsi2ss %eax, %xmm0
movq -0x10(%rbp), %rax
movss %xmm0, 0x34(%rax)
movq -0x8(%rbp), %rax
movzwl 0x7c(%rax), %eax
andl $0xff00, %eax           # imm = 0xFF00
sarl $0x8, %eax
cvtsi2ss %eax, %xmm0
movq -0x10(%rbp), %rax
movss %xmm0, 0x38(%rax)
movl $0x0, -0x1c(%rbp)
cmpl $0x4, -0x1c(%rbp)
jge 0x2576
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movss 0x2c(%rax,%rcx,4), %xmm0
movss 0x3b3f(%rip), %xmm1     # 0x6058
ucomiss %xmm1, %xmm0
jbe 0x2546
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movss 0x2c(%rax,%rcx,4), %xmm0
movss 0x3b28(%rip), %xmm1     # 0x605c
subss %xmm1, %xmm0
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movss %xmm0, 0x2c(%rax,%rcx,4)
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movss 0x2c(%rax,%rcx,4), %xmm0
cvtsi2ssl -0x14(%rbp), %xmm1
divss %xmm1, %xmm0
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movss %xmm0, 0x2c(%rax,%rcx,4)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x24fd
popq %rbp
retq
nopl (%rax,%rax)