// File: lucaong[P]elixir_mlx90640/src/MLX90640_API.cpp
// Function: MLX90640_GetImage(unsigned short*, paramsMLX90640 const*, float*)
void MLX90640_GetImage(uint16_t *frameData, const paramsMLX90640 *params, float *result)
{
    float vdd;
    float ta;
    float gain;
    float irDataCP[2];
    float irData;
    float alphaCompensated;
    uint8_t mode;
    int8_t ilPattern;
    int8_t chessPattern;
    int8_t pattern;
    int8_t conversionPattern;
    float image;
    uint16_t subPage;
    
    subPage = frameData[833];
    vdd = MLX90640_GetVdd(frameData, params);
    ta = MLX90640_GetTa(frameData, params);
    
//------------------------- Gain calculation -----------------------------------    
    gain = frameData[778];
    if(gain > 32767)
    {
        gain = gain - 65536;
    }
    
    gain = params->gainEE / gain; 
  
//------------------------- Image calculation -------------------------------------    
    mode = (frameData[832] & 0x1000) >> 5;
    
    irDataCP[0] = frameData[776];  
    irDataCP[1] = frameData[808];
    for( int i = 0; i < 2; i++)
    {
        if(irDataCP[i] > 32767)
        {
            irDataCP[i] = irDataCP[i] - 65536;
        }
        irDataCP[i] = irDataCP[i] * gain;
    }
    irDataCP[0] = irDataCP[0] - params->cpOffset[0] * (1 + params->cpKta * (ta - 25)) * (1 + params->cpKv * (vdd - 3.3));
    if( mode ==  params->calibrationModeEE)
    {
        irDataCP[1] = irDataCP[1] - params->cpOffset[1] * (1 + params->cpKta * (ta - 25)) * (1 + params->cpKv * (vdd - 3.3));
    }
    else
    {
      irDataCP[1] = irDataCP[1] - (params->cpOffset[1] + params->ilChessC[0]) * (1 + params->cpKta * (ta - 25)) * (1 + params->cpKv * (vdd - 3.3));
    }

    for( int pixelNumber = 0; pixelNumber < 768; pixelNumber++)
    {
        ilPattern = pixelNumber / 32 - (pixelNumber / 64) * 2; 
        chessPattern = ilPattern ^ (pixelNumber - (pixelNumber/2)*2); 
        conversionPattern = ((pixelNumber + 2) / 4 - (pixelNumber + 3) / 4 + (pixelNumber + 1) / 4 - pixelNumber / 4) * (1 - 2 * ilPattern);
        
        if(mode == 0)
        {
          pattern = ilPattern; 
        }
        else 
        {
          pattern = chessPattern; 
        }
        
        if(pattern == frameData[833])
        {    
            irData = frameData[pixelNumber];
            if(irData > 32767)
            {
                irData = irData - 65536;
            }
            irData = irData * gain;
            
            irData = irData - params->offset[pixelNumber]*(1 + params->kta[pixelNumber]*(ta - 25))*(1 + params->kv[pixelNumber]*(vdd - 3.3));
            if(mode !=  params->calibrationModeEE)
            {
              irData = irData + params->ilChessC[2] * (2 * ilPattern - 1) - params->ilChessC[1] * conversionPattern; 
            }
            
            irData = irData - params->tgc * irDataCP[subPage];
            
            alphaCompensated = (params->alpha[pixelNumber] - params->tgc * params->cpAlpha[subPage])*(1 + params->KsTa * (ta - 25));
            
            image = irData/alphaCompensated;
            
            result[pixelNumber] = image;
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movw 0x682(%rax), %ax
movw %ax, -0x42(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x4710
movss %xmm0, -0x1c(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x4810
movss %xmm0, -0x20(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x614(%rax), %eax
cvtsi2ss %eax, %xmm0
movss %xmm0, -0x24(%rbp)
movss -0x24(%rbp), %xmm0
movss 0x1694(%rip), %xmm1     # 0x607c
ucomiss %xmm1, %xmm0
jbe 0x4a03
movss -0x24(%rbp), %xmm0
movss 0x168a(%rip), %xmm1     # 0x6084
subss %xmm1, %xmm0
movss %xmm0, -0x24(%rbp)
movq -0x10(%rbp), %rax
movswl 0x14(%rax), %eax
cvtsi2ss %eax, %xmm0
movss -0x24(%rbp), %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x24(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x680(%rax), %eax
andl $0x1000, %eax           # imm = 0x1000
shrl $0x5, %eax
movb %al, -0x35(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x610(%rax), %eax
cvtsi2ss %eax, %xmm0
movss %xmm0, -0x2c(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x650(%rax), %eax
cvtsi2ss %eax, %xmm0
movss %xmm0, -0x28(%rbp)
movl $0x0, -0x48(%rbp)
cmpl $0x2, -0x48(%rbp)
jge 0x4ac3
movslq -0x48(%rbp), %rax
movss -0x2c(%rbp,%rax,4), %xmm0
movss 0x1602(%rip), %xmm1     # 0x607c
ucomiss %xmm1, %xmm0
jbe 0x4a9f
movslq -0x48(%rbp), %rax
movss -0x2c(%rbp,%rax,4), %xmm0
movss 0x15f3(%rip), %xmm1     # 0x6084
subss %xmm1, %xmm0
movslq -0x48(%rbp), %rax
movss %xmm0, -0x2c(%rbp,%rax,4)
movslq -0x48(%rbp), %rax
movss -0x2c(%rbp,%rax,4), %xmm0
mulss -0x24(%rbp), %xmm0
movslq -0x48(%rbp), %rax
movss %xmm0, -0x2c(%rbp,%rax,4)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x4a62
movss -0x2c(%rbp), %xmm0
cvtss2sd %xmm0, %xmm1
movq -0x10(%rbp), %rax
movswl 0x2a4c(%rax), %eax
cvtsi2ss %eax, %xmm0
movq -0x10(%rbp), %rax
movss 0x20(%rax), %xmm2
movss -0x20(%rbp), %xmm3
movss 0x1597(%rip), %xmm4     # 0x6088
subss %xmm4, %xmm3
mulss %xmm3, %xmm2
movss 0x150f(%rip), %xmm3     # 0x6010
addss %xmm3, %xmm2
mulss %xmm2, %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x10(%rbp), %rax
movss 0x1c(%rax), %xmm2
cvtss2sd %xmm2, %xmm2
movss -0x1c(%rbp), %xmm3
cvtss2sd %xmm3, %xmm3
movsd 0x158d(%rip), %xmm4     # 0x60b8
subsd %xmm4, %xmm3
mulsd %xmm3, %xmm2
movsd 0x1575(%rip), %xmm3     # 0x60b0
addsd %xmm3, %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x2c(%rbp)
movzbl -0x35(%rbp), %eax
movq -0x10(%rbp), %rcx
movzbl 0x25(%rcx), %ecx
cmpl %ecx, %eax
jne 0x4c24
movss -0x28(%rbp), %xmm0
cvtss2sd %xmm0, %xmm1
movq -0x10(%rbp), %rax
movswl 0x2a4e(%rax), %eax
cvtsi2ss %eax, %xmm0
movq -0x10(%rbp), %rax
movss 0x20(%rax), %xmm2
movss -0x20(%rbp), %xmm3
movss 0x14df(%rip), %xmm4     # 0x6088
subss %xmm4, %xmm3
mulss %xmm3, %xmm2
movss 0x1457(%rip), %xmm3     # 0x6010
addss %xmm3, %xmm2
mulss %xmm2, %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x10(%rbp), %rax
movss 0x1c(%rax), %xmm2
cvtss2sd %xmm2, %xmm2
movss -0x1c(%rbp), %xmm3
cvtss2sd %xmm3, %xmm3
movsd 0x14d5(%rip), %xmm4     # 0x60b8
subsd %xmm4, %xmm3
mulsd %xmm3, %xmm2
movsd 0x14bd(%rip), %xmm3     # 0x60b0
addsd %xmm3, %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x28(%rbp)
jmp 0x4cd4
movss -0x28(%rbp), %xmm0
cvtss2sd %xmm0, %xmm1
movq -0x10(%rbp), %rax
movswl 0x2a4e(%rax), %eax
cvtsi2ss %eax, %xmm0
movq -0x10(%rbp), %rax
addss 0x2a50(%rax), %xmm0
movq -0x10(%rbp), %rax
movss 0x20(%rax), %xmm2
movss -0x20(%rbp), %xmm3
movss 0x142a(%rip), %xmm4     # 0x6088
subss %xmm4, %xmm3
mulss %xmm3, %xmm2
movss 0x13a2(%rip), %xmm3     # 0x6010
addss %xmm3, %xmm2
mulss %xmm2, %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x10(%rbp), %rax
movss 0x1c(%rax), %xmm2
cvtss2sd %xmm2, %xmm2
movss -0x1c(%rbp), %xmm3
cvtss2sd %xmm3, %xmm3
movsd 0x1420(%rip), %xmm4     # 0x60b8
subsd %xmm4, %xmm3
mulsd %xmm3, %xmm2
movsd 0x1408(%rip), %xmm3     # 0x60b0
addsd %xmm3, %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x28(%rbp)
movl $0x0, -0x4c(%rbp)
cmpl $0x300, -0x4c(%rbp)     # imm = 0x300
jge 0x5000
movl -0x4c(%rbp), %eax
movl $0x20, %ecx
cltd
idivl %ecx
movl %eax, -0x60(%rbp)
movl -0x4c(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x60(%rbp), %eax
shll %ecx
subl %ecx, %eax
movb %al, -0x36(%rbp)
movsbl -0x36(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x4c(%rbp), %ecx
movl -0x4c(%rbp), %eax
movl $0x2, %esi
cltd
idivl %esi
movl %eax, %edx
movl -0x5c(%rbp), %eax
shll %edx
subl %edx, %ecx
xorl %ecx, %eax
movb %al, -0x37(%rbp)
movl -0x4c(%rbp), %eax
addl $0x2, %eax
movl $0x4, %ecx
cltd
idivl %ecx
movl %eax, -0x58(%rbp)
movl -0x4c(%rbp), %eax
addl $0x3, %eax
movl $0x4, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x58(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x54(%rbp)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl $0x4, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x54(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x50(%rbp)
movl -0x4c(%rbp), %eax
movl $0x4, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x50(%rbp), %eax
subl %ecx, %eax
movsbl -0x36(%rbp), %edx
shll %edx
movl $0x1, %ecx
subl %edx, %ecx
imull %ecx, %eax
movb %al, -0x39(%rbp)
movzbl -0x35(%rbp), %eax
cmpl $0x0, %eax
jne 0x4da7
movb -0x36(%rbp), %al
movb %al, -0x38(%rbp)
jmp 0x4dad
movb -0x37(%rbp), %al
movb %al, -0x38(%rbp)
movsbl -0x38(%rbp), %eax
movq -0x8(%rbp), %rcx
movzwl 0x682(%rcx), %ecx
cmpl %ecx, %eax
jne 0x4ff0
movq -0x8(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
cvtsi2ss %eax, %xmm0
movss %xmm0, -0x30(%rbp)
movss -0x30(%rbp), %xmm0
movss 0x1296(%rip), %xmm1     # 0x607c
ucomiss %xmm1, %xmm0
jbe 0x4e01
movss -0x30(%rbp), %xmm0
movss 0x128c(%rip), %xmm1     # 0x6084
subss %xmm1, %xmm0
movss %xmm0, -0x30(%rbp)
movss -0x30(%rbp), %xmm0
mulss -0x24(%rbp), %xmm0
movss %xmm0, -0x30(%rbp)
movss -0x30(%rbp), %xmm0
cvtss2sd %xmm0, %xmm1
movq -0x10(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movswl 0xc44(%rax,%rcx,2), %eax
cvtsi2ss %eax, %xmm0
movq -0x10(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movss 0x1244(%rax,%rcx,4), %xmm2
movss -0x20(%rbp), %xmm3
movss 0x123d(%rip), %xmm4     # 0x6088
subss %xmm4, %xmm3
mulss %xmm3, %xmm2
movss 0x11b5(%rip), %xmm3     # 0x6010
addss %xmm3, %xmm2
mulss %xmm2, %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x10(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movss 0x1e44(%rax,%rcx,4), %xmm2
cvtss2sd %xmm2, %xmm2
movss -0x1c(%rbp), %xmm3
cvtss2sd %xmm3, %xmm3
movsd 0x122b(%rip), %xmm4     # 0x60b8
subsd %xmm4, %xmm3
mulsd %xmm3, %xmm2
movsd 0x1213(%rip), %xmm3     # 0x60b0
addsd %xmm3, %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x30(%rbp)
movzbl -0x35(%rbp), %eax
movq -0x10(%rbp), %rcx
movzbl 0x25(%rcx), %ecx
cmpl %ecx, %eax
je 0x4f2d
movss -0x30(%rbp), %xmm0
movq -0x10(%rbp), %rax
movss 0x2a58(%rax), %xmm1
movsbl -0x36(%rbp), %eax
shll %eax
subl $0x1, %eax
cvtsi2ss %eax, %xmm2
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movq -0x10(%rbp), %rax
movss 0x2a54(%rax), %xmm0
movsbl -0x39(%rbp), %eax
cvtsi2ss %eax, %xmm2
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0x30(%rbp)
movss -0x30(%rbp), %xmm1
movq -0x10(%rbp), %rax
movss 0x18(%rax), %xmm0
movzwl -0x42(%rbp), %eax
movss -0x2c(%rbp,%rax,4), %xmm2
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0x30(%rbp)
movq -0x10(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movss 0x44(%rax,%rcx,4), %xmm1
movq -0x10(%rbp), %rax
movss 0x18(%rax), %xmm0
movq -0x10(%rbp), %rax
movzwl -0x42(%rbp), %ecx
movss 0x2a44(%rax,%rcx,4), %xmm2
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movq -0x10(%rbp), %rax
movss 0x28(%rax), %xmm1
movss -0x20(%rbp), %xmm2
movss 0x10d6(%rip), %xmm3     # 0x6088
subss %xmm3, %xmm2
mulss %xmm2, %xmm1
movss 0x104e(%rip), %xmm2     # 0x6010
addss %xmm2, %xmm1
mulss %xmm1, %xmm0
movss %xmm0, -0x34(%rbp)
movss -0x30(%rbp), %xmm0
divss -0x34(%rbp), %xmm0
movss %xmm0, -0x40(%rbp)
movss -0x40(%rbp), %xmm0
movq -0x18(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
jmp 0x4ff2
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x4cdb
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)