// File: lucaong[P]elixir_mlx90640/src/MLX90640_API.cpp
// Function: ExtractAlphaParameters(unsigned short*, paramsMLX90640*)
void ExtractAlphaParameters(uint16_t *eeData, paramsMLX90640 *mlx90640)
{
    int accRow[24];
    int accColumn[32];
    int p = 0;
    int alphaRef;
    uint8_t alphaScale;
    uint8_t accRowScale;
    uint8_t accColumnScale;
    uint8_t accRemScale;
    

    accRemScale = eeData[32] & 0x000F;
    accColumnScale = (eeData[32] & 0x00F0) >> 4;
    accRowScale = (eeData[32] & 0x0F00) >> 8;
    alphaScale = ((eeData[32] & 0xF000) >> 12) + 30;
    alphaRef = eeData[33];
    
    for(int i = 0; i < 6; i++)
    {
        p = i * 4;
        accRow[p + 0] = (eeData[34 + i] & 0x000F);
        accRow[p + 1] = (eeData[34 + i] & 0x00F0) >> 4;
        accRow[p + 2] = (eeData[34 + i] & 0x0F00) >> 8;
        accRow[p + 3] = (eeData[34 + i] & 0xF000) >> 12;
    }
    
    for(int i = 0; i < 24; i++)
    {
        if (accRow[i] > 7)
        {
            accRow[i] = accRow[i] - 16;
        }
    }
    
    for(int i = 0; i < 8; i++)
    {
        p = i * 4;
        accColumn[p + 0] = (eeData[40 + i] & 0x000F);
        accColumn[p + 1] = (eeData[40 + i] & 0x00F0) >> 4;
        accColumn[p + 2] = (eeData[40 + i] & 0x0F00) >> 8;
        accColumn[p + 3] = (eeData[40 + i] & 0xF000) >> 12;
    }
    
    for(int i = 0; i < 32; i ++)
    {
        if (accColumn[i] > 7)
        {
            accColumn[i] = accColumn[i] - 16;
        }
    }

    for(int i = 0; i < 24; i++)
    {
        for(int j = 0; j < 32; j ++)
        {
            p = 32 * i +j;
            mlx90640->alpha[p] = (eeData[64 + p] & 0x03F0) >> 4;
            if (mlx90640->alpha[p] > 31)
            {
                mlx90640->alpha[p] = mlx90640->alpha[p] - 64;
            }
            mlx90640->alpha[p] = mlx90640->alpha[p]*(1 << accRemScale);
            mlx90640->alpha[p] = (alphaRef + (accRow[i] << accRowScale) + (accColumn[j] << accColumnScale) + mlx90640->alpha[p]);
            mlx90640->alpha[p] = mlx90640->alpha[p] / pow(2,(double)alphaScale);
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0xf4(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x40(%rax), %eax
andl $0xf, %eax
movb %al, -0xfc(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x40(%rax), %eax
andl $0xf0, %eax
sarl $0x4, %eax
movb %al, -0xfb(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x40(%rax), %eax
andl $0xf00, %eax            # imm = 0xF00
sarl $0x8, %eax
movb %al, -0xfa(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x40(%rax), %eax
andl $0xf000, %eax           # imm = 0xF000
sarl $0xc, %eax
addl $0x1e, %eax
movb %al, -0xf9(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x42(%rax), %eax
movl %eax, -0xf8(%rbp)
movl $0x0, -0x100(%rbp)
cmpl $0x6, -0x100(%rbp)
jge 0x26e5
movl -0x100(%rbp), %eax
shll $0x2, %eax
movl %eax, -0xf4(%rbp)
movq -0x8(%rbp), %rax
movl -0x100(%rbp), %ecx
addl $0x22, %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %ecx
andl $0xf, %ecx
movl -0xf4(%rbp), %eax
addl $0x0, %eax
cltq
movl %ecx, -0x70(%rbp,%rax,4)
movq -0x8(%rbp), %rax
movl -0x100(%rbp), %ecx
addl $0x22, %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %ecx
andl $0xf0, %ecx
sarl $0x4, %ecx
movl -0xf4(%rbp), %eax
addl $0x1, %eax
cltq
movl %ecx, -0x70(%rbp,%rax,4)
movq -0x8(%rbp), %rax
movl -0x100(%rbp), %ecx
addl $0x22, %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %ecx
andl $0xf00, %ecx            # imm = 0xF00
sarl $0x8, %ecx
movl -0xf4(%rbp), %eax
addl $0x2, %eax
cltq
movl %ecx, -0x70(%rbp,%rax,4)
movq -0x8(%rbp), %rax
movl -0x100(%rbp), %ecx
addl $0x22, %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %ecx
andl $0xf000, %ecx           # imm = 0xF000
sarl $0xc, %ecx
movl -0xf4(%rbp), %eax
addl $0x3, %eax
cltq
movl %ecx, -0x70(%rbp,%rax,4)
movl -0x100(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x100(%rbp)
jmp 0x260b
movl $0x0, -0x104(%rbp)
cmpl $0x18, -0x104(%rbp)
jge 0x2732
movslq -0x104(%rbp), %rax
cmpl $0x7, -0x70(%rbp,%rax,4)
jle 0x271f
movslq -0x104(%rbp), %rax
movl -0x70(%rbp,%rax,4), %ecx
subl $0x10, %ecx
movslq -0x104(%rbp), %rax
movl %ecx, -0x70(%rbp,%rax,4)
jmp 0x2721
movl -0x104(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x104(%rbp)
jmp 0x26ef
movl $0x0, -0x108(%rbp)
cmpl $0x8, -0x108(%rbp)
jge 0x2822
movl -0x108(%rbp), %eax
shll $0x2, %eax
movl %eax, -0xf4(%rbp)
movq -0x8(%rbp), %rax
movl -0x108(%rbp), %ecx
addl $0x28, %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %ecx
andl $0xf, %ecx
movl -0xf4(%rbp), %eax
addl $0x0, %eax
cltq
movl %ecx, -0xf0(%rbp,%rax,4)
movq -0x8(%rbp), %rax
movl -0x108(%rbp), %ecx
addl $0x28, %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %ecx
andl $0xf0, %ecx
sarl $0x4, %ecx
movl -0xf4(%rbp), %eax
addl $0x1, %eax
cltq
movl %ecx, -0xf0(%rbp,%rax,4)
movq -0x8(%rbp), %rax
movl -0x108(%rbp), %ecx
addl $0x28, %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %ecx
andl $0xf00, %ecx            # imm = 0xF00
sarl $0x8, %ecx
movl -0xf4(%rbp), %eax
addl $0x2, %eax
cltq
movl %ecx, -0xf0(%rbp,%rax,4)
movq -0x8(%rbp), %rax
movl -0x108(%rbp), %ecx
addl $0x28, %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %ecx
andl $0xf000, %ecx           # imm = 0xF000
sarl $0xc, %ecx
movl -0xf4(%rbp), %eax
addl $0x3, %eax
cltq
movl %ecx, -0xf0(%rbp,%rax,4)
movl -0x108(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x108(%rbp)
jmp 0x273c
movl $0x0, -0x10c(%rbp)
cmpl $0x20, -0x10c(%rbp)
jge 0x2878
movslq -0x10c(%rbp), %rax
cmpl $0x7, -0xf0(%rbp,%rax,4)
jle 0x2865
movslq -0x10c(%rbp), %rax
movl -0xf0(%rbp,%rax,4), %ecx
subl $0x10, %ecx
movslq -0x10c(%rbp), %rax
movl %ecx, -0xf0(%rbp,%rax,4)
jmp 0x2867
movl -0x10c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10c(%rbp)
jmp 0x282c
movl $0x0, -0x110(%rbp)
cmpl $0x18, -0x110(%rbp)
jge 0x2a48
movl $0x0, -0x114(%rbp)
cmpl $0x20, -0x114(%rbp)
jge 0x2a32
movl -0x110(%rbp), %eax
shll $0x5, %eax
addl -0x114(%rbp), %eax
movl %eax, -0xf4(%rbp)
movq -0x8(%rbp), %rax
movl -0xf4(%rbp), %ecx
addl $0x40, %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x3f0, %eax            # imm = 0x3F0
sarl $0x4, %eax
cvtsi2ss %eax, %xmm0
movq -0x10(%rbp), %rax
movslq -0xf4(%rbp), %rcx
movss %xmm0, 0x44(%rax,%rcx,4)
movq -0x10(%rbp), %rax
movslq -0xf4(%rbp), %rcx
movss 0x44(%rax,%rcx,4), %xmm0
movss 0x373b(%rip), %xmm1     # 0x6040
ucomiss %xmm1, %xmm0
jbe 0x2938
movq -0x10(%rbp), %rax
movslq -0xf4(%rbp), %rcx
movss 0x44(%rax,%rcx,4), %xmm0
movss 0x3721(%rip), %xmm1     # 0x6044
subss %xmm1, %xmm0
movq -0x10(%rbp), %rax
movslq -0xf4(%rbp), %rcx
movss %xmm0, 0x44(%rax,%rcx,4)
movq -0x10(%rbp), %rax
movslq -0xf4(%rbp), %rcx
movq %rcx, -0x128(%rbp)
movss 0x44(%rax,%rcx,4), %xmm0
movb -0xfc(%rbp), %cl
movl $0x1, %edx
shll %cl, %edx
movq -0x128(%rbp), %rcx
cvtsi2ss %edx, %xmm1
mulss %xmm1, %xmm0
movss %xmm0, 0x44(%rax,%rcx,4)
movl -0xf8(%rbp), %eax
movslq -0x110(%rbp), %rcx
movl -0x70(%rbp,%rcx,4), %edx
movb -0xfa(%rbp), %cl
shll %cl, %edx
movl %edx, %ecx
addl %ecx, %eax
movslq -0x114(%rbp), %rcx
movl -0xf0(%rbp,%rcx,4), %edx
movb -0xfb(%rbp), %cl
shll %cl, %edx
movl %edx, %ecx
addl %ecx, %eax
cvtsi2ss %eax, %xmm0
movq -0x10(%rbp), %rax
movslq -0xf4(%rbp), %rcx
movss 0x44(%rax,%rcx,4), %xmm1
addss %xmm1, %xmm0
movss %xmm0, 0x44(%rax,%rcx,4)
movq -0x10(%rbp), %rax
movslq -0xf4(%rbp), %rcx
movss 0x44(%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x120(%rbp)
movzbl -0xf9(%rbp), %eax
cvtsi2sd %eax, %xmm0
movl $0x2, %edi
callq 0x5ab0
movaps %xmm0, %xmm1
movsd -0x120(%rbp), %xmm0
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x10(%rbp), %rax
movslq -0xf4(%rbp), %rcx
movss %xmm0, 0x44(%rax,%rcx,4)
movl -0x114(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x114(%rbp)
jmp 0x2899
jmp 0x2a34
movl -0x110(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x110(%rbp)
jmp 0x2882
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
nopw %cs:(%rax,%rax)