// File: lucaong[P]elixir_mlx90640/src/MLX90640_API.cpp
// Function: ExtractOffsetParameters(unsigned short*, paramsMLX90640*)
void ExtractOffsetParameters(uint16_t *eeData, paramsMLX90640 *mlx90640)
{
    int occRow[24];
    int occColumn[32];
    int p = 0;
    int16_t offsetRef;
    uint8_t occRowScale;
    uint8_t occColumnScale;
    uint8_t occRemScale;
    

    occRemScale = (eeData[16] & 0x000F);
    occColumnScale = (eeData[16] & 0x00F0) >> 4;
    occRowScale = (eeData[16] & 0x0F00) >> 8;
    offsetRef = eeData[17];
    if (offsetRef > 32767)
    {
        offsetRef = offsetRef - 65536;
    }
    
    for(int i = 0; i < 6; i++)
    {
        p = i * 4;
        occRow[p + 0] = (eeData[18 + i] & 0x000F);
        occRow[p + 1] = (eeData[18 + i] & 0x00F0) >> 4;
        occRow[p + 2] = (eeData[18 + i] & 0x0F00) >> 8;
        occRow[p + 3] = (eeData[18 + i] & 0xF000) >> 12;
    }
    
    for(int i = 0; i < 24; i++)
    {
        if (occRow[i] > 7)
        {
            occRow[i] = occRow[i] - 16;
        }
    }
    
    for(int i = 0; i < 8; i++)
    {
        p = i * 4;
        occColumn[p + 0] = (eeData[24 + i] & 0x000F);
        occColumn[p + 1] = (eeData[24 + i] & 0x00F0) >> 4;
        occColumn[p + 2] = (eeData[24 + i] & 0x0F00) >> 8;
        occColumn[p + 3] = (eeData[24 + i] & 0xF000) >> 12;
    }
    
    for(int i = 0; i < 32; i ++)
    {
        if (occColumn[i] > 7)
        {
            occColumn[i] = occColumn[i] - 16;
        }
    }

    for(int i = 0; i < 24; i++)
    {
        for(int j = 0; j < 32; j ++)
        {
            p = 32 * i +j;
            mlx90640->offset[p] = (eeData[64 + p] & 0xFC00) >> 10;
            if (mlx90640->offset[p] > 31)
            {
                mlx90640->offset[p] = mlx90640->offset[p] - 64;
            }
            mlx90640->offset[p] = mlx90640->offset[p]*(1 << occRemScale);
            mlx90640->offset[p] = (offsetRef + (occRow[i] << occRowScale) + (occColumn[j] << occColumnScale) + mlx90640->offset[p]);
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0xf4(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x20(%rax), %eax
andl $0xf, %eax
movb %al, -0xf9(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x20(%rax), %eax
andl $0xf0, %eax
sarl $0x4, %eax
movb %al, -0xf8(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x20(%rax), %eax
andl $0xf00, %eax            # imm = 0xF00
sarl $0x8, %eax
movb %al, -0xf7(%rbp)
movq -0x8(%rbp), %rax
movw 0x22(%rax), %ax
movw %ax, -0xf6(%rbp)
movswl -0xf6(%rbp), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jle 0x2aea
movswl -0xf6(%rbp), %eax
subl $0x10000, %eax          # imm = 0x10000
movw %ax, -0xf6(%rbp)
movl $0x0, -0x100(%rbp)
cmpl $0x6, -0x100(%rbp)
jge 0x2bce
movl -0x100(%rbp), %eax
shll $0x2, %eax
movl %eax, -0xf4(%rbp)
movq -0x8(%rbp), %rax
movl -0x100(%rbp), %ecx
addl $0x12, %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 $0x12, %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 $0x12, %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 $0x12, %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 0x2af4
movl $0x0, -0x104(%rbp)
cmpl $0x18, -0x104(%rbp)
jge 0x2c1b
movslq -0x104(%rbp), %rax
cmpl $0x7, -0x70(%rbp,%rax,4)
jle 0x2c08
movslq -0x104(%rbp), %rax
movl -0x70(%rbp,%rax,4), %ecx
subl $0x10, %ecx
movslq -0x104(%rbp), %rax
movl %ecx, -0x70(%rbp,%rax,4)
jmp 0x2c0a
movl -0x104(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x104(%rbp)
jmp 0x2bd8
movl $0x0, -0x108(%rbp)
cmpl $0x8, -0x108(%rbp)
jge 0x2d0b
movl -0x108(%rbp), %eax
shll $0x2, %eax
movl %eax, -0xf4(%rbp)
movq -0x8(%rbp), %rax
movl -0x108(%rbp), %ecx
addl $0x18, %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 $0x18, %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 $0x18, %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 $0x18, %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 0x2c25
movl $0x0, -0x10c(%rbp)
cmpl $0x20, -0x10c(%rbp)
jge 0x2d61
movslq -0x10c(%rbp), %rax
cmpl $0x7, -0xf0(%rbp,%rax,4)
jle 0x2d4e
movslq -0x10c(%rbp), %rax
movl -0xf0(%rbp,%rax,4), %ecx
subl $0x10, %ecx
movslq -0x10c(%rbp), %rax
movl %ecx, -0xf0(%rbp,%rax,4)
jmp 0x2d50
movl -0x10c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10c(%rbp)
jmp 0x2d15
movl $0x0, -0x110(%rbp)
cmpl $0x18, -0x110(%rbp)
jge 0x2ee5
movl $0x0, -0x114(%rbp)
cmpl $0x20, -0x114(%rbp)
jge 0x2ecf
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 $0xfc00, %eax           # imm = 0xFC00
sarl $0xa, %eax
movw %ax, %dx
movq -0x10(%rbp), %rax
movslq -0xf4(%rbp), %rcx
movw %dx, 0xc44(%rax,%rcx,2)
movq -0x10(%rbp), %rax
movslq -0xf4(%rbp), %rcx
movswl 0xc44(%rax,%rcx,2), %eax
cmpl $0x1f, %eax
jle 0x2e1a
movq -0x10(%rbp), %rax
movslq -0xf4(%rbp), %rcx
movswl 0xc44(%rax,%rcx,2), %eax
subl $0x40, %eax
movw %ax, %dx
movq -0x10(%rbp), %rax
movslq -0xf4(%rbp), %rcx
movw %dx, 0xc44(%rax,%rcx,2)
movq -0x10(%rbp), %rax
movslq -0xf4(%rbp), %rcx
movswl 0xc44(%rax,%rcx,2), %eax
movzbl -0xf9(%rbp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
imull %ecx, %eax
movw %ax, %dx
movq -0x10(%rbp), %rax
movslq -0xf4(%rbp), %rcx
movw %dx, 0xc44(%rax,%rcx,2)
movswl -0xf6(%rbp), %eax
movslq -0x110(%rbp), %rcx
movl -0x70(%rbp,%rcx,4), %edx
movzbl -0xf7(%rbp), %ecx
shll %cl, %edx
movl %edx, %ecx
addl %ecx, %eax
movslq -0x114(%rbp), %rcx
movl -0xf0(%rbp,%rcx,4), %edx
movzbl -0xf8(%rbp), %ecx
shll %cl, %edx
movl %edx, %ecx
addl %ecx, %eax
movq -0x10(%rbp), %rcx
movslq -0xf4(%rbp), %rdx
movswl 0xc44(%rcx,%rdx,2), %ecx
addl %ecx, %eax
movw %ax, %dx
movq -0x10(%rbp), %rax
movslq -0xf4(%rbp), %rcx
movw %dx, 0xc44(%rax,%rcx,2)
movl -0x114(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x114(%rbp)
jmp 0x2d82
jmp 0x2ed1
movl -0x110(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x110(%rbp)
jmp 0x2d6b
addq $0xa0, %rsp
popq %rbp
retq
nop