// File: cloudinary[P]fuif/transform/approximate.h
// Function: inv_approximate(Image&, std::vector<int, std::allocator<int>> const&)
bool inv_approximate(Image &input, const std::vector<int> &parameters) {
    int beginc = parameters[0];
    int endc = parameters[1];
    int offset = input.channel.size() - (endc - beginc + 1);
    for (int c=beginc; c<=endc; c++) {
        int q = (c+2-beginc < parameters.size() ? parameters[c+2-beginc] : parameters.back());
        if (!q) offset++;
    }
    v_printf(3,"Reconstructing approximated channels %i-%i using remainder channels %i-%i.\n",beginc,endc,offset,input.channel.size()-1);
    int i=0;
    for (int c=beginc; c<=endc; c++) {
        int q = (c+2-beginc < parameters.size() ? parameters[c+2-beginc] : parameters.back());
        q += 1;
        if (q == 1) continue;
        Channel &ch = input.channel[c];
        const Channel &chr = input.channel[offset+i];
        i++;
        if (chr.data.size() == 0) v_printf(3,"Remainder channel is not available.\n");
        else ch.q = chr.q; // usually these are the same, except when approximated channel became all zeroes
        for (int y=0; y<ch.h; y++) {
          for (int x=0; x<ch.w; x++) {
            ch.value(y,x) *= q;
//            ch.value(y,x) += (chr.data.size() == 0 ? q/2 : chr.value(y,x));
            ch.value(y,x) += (chr.data.size() == 0 ? 0 : chr.value(y,x));
          }
        }
    }
    input.channel.erase(input.channel.begin() + offset, input.channel.end());
    return true;
}

// Assembly:
subq $0xb8, %rsp
movq %rdi, 0xb0(%rsp)
movq %rsi, 0xa8(%rsp)
movq 0xa8(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x13560
movl (%rax), %eax
movl %eax, 0xa4(%rsp)
movq 0xa8(%rsp), %rdi
movl $0x1, %esi
callq 0x13560
movl (%rax), %eax
movl %eax, 0xa0(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x4b80
movl 0xa0(%rsp), %ecx
subl 0xa4(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
subq %rcx, %rax
movl %eax, 0x9c(%rsp)
movl 0xa4(%rsp), %eax
movl %eax, 0x98(%rsp)
movl 0x98(%rsp), %eax
cmpl 0xa0(%rsp), %eax
jg 0x11535
movl 0x98(%rsp), %eax
addl $0x2, %eax
subl 0xa4(%rsp), %eax
cltq
movq %rax, 0x38(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x4d50
movq %rax, %rcx
movq 0x38(%rsp), %rax
cmpq %rcx, %rax
jae 0x114e4
movq 0xa8(%rsp), %rdi
movl 0x98(%rsp), %eax
addl $0x2, %eax
subl 0xa4(%rsp), %eax
movslq %eax, %rsi
callq 0x13560
movl (%rax), %eax
movl %eax, 0x34(%rsp)
jmp 0x114f7
movq 0xa8(%rsp), %rdi
callq 0x13830
movl (%rax), %eax
movl %eax, 0x34(%rsp)
movl 0x34(%rsp), %eax
movl %eax, 0x94(%rsp)
cmpl $0x0, 0x94(%rsp)
jne 0x1151d
movl 0x9c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x9c(%rsp)
jmp 0x1151f
movl 0x98(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x98(%rsp)
jmp 0x11475
movl 0xa4(%rsp), %eax
movl %eax, 0x28(%rsp)
movl 0xa0(%rsp), %eax
movl %eax, 0x2c(%rsp)
movl 0x9c(%rsp), %eax
movl %eax, 0x30(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x4b80
movl 0x28(%rsp), %edx
movl 0x2c(%rsp), %ecx
movl 0x30(%rsp), %r8d
movq %rax, %r9
subq $0x1, %r9
movl $0x3, %edi
leaq 0x35b09(%rip), %rsi     # 0x4708c
movb $0x0, %al
callq 0x3a770
movl $0x0, 0x90(%rsp)
movl 0xa4(%rsp), %eax
movl %eax, 0x8c(%rsp)
movl 0x8c(%rsp), %eax
cmpl 0xa0(%rsp), %eax
jg 0x117d2
movl 0x8c(%rsp), %eax
addl $0x2, %eax
subl 0xa4(%rsp), %eax
cltq
movq %rax, 0x20(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x4d50
movq %rax, %rcx
movq 0x20(%rsp), %rax
cmpq %rcx, %rax
jae 0x11612
movq 0xa8(%rsp), %rdi
movl 0x8c(%rsp), %eax
addl $0x2, %eax
subl 0xa4(%rsp), %eax
movslq %eax, %rsi
callq 0x13560
movl (%rax), %eax
movl %eax, 0x1c(%rsp)
jmp 0x11625
movq 0xa8(%rsp), %rdi
callq 0x13830
movl (%rax), %eax
movl %eax, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
movl %eax, 0x88(%rsp)
movl 0x88(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x88(%rsp)
cmpl $0x1, 0x88(%rsp)
jne 0x11650
jmp 0x117bc
movq 0xb0(%rsp), %rdi
movslq 0x8c(%rsp), %rsi
callq 0x4ba0
movq %rax, 0x80(%rsp)
movq 0xb0(%rsp), %rdi
movl 0x9c(%rsp), %eax
addl 0x90(%rsp), %eax
movslq %eax, %rsi
callq 0x4ba0
movq %rax, 0x78(%rsp)
movl 0x90(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x90(%rsp)
movq 0x78(%rsp), %rdi
callq 0x4a30
cmpq $0x0, %rax
jne 0x116c6
movl $0x3, %edi
leaq 0x35a1b(%rip), %rsi     # 0x470d8
movb $0x0, %al
callq 0x3a770
jmp 0x116d9
movq 0x78(%rsp), %rax
movl 0x28(%rax), %ecx
movq 0x80(%rsp), %rax
movl %ecx, 0x28(%rax)
movl $0x0, 0x74(%rsp)
movl 0x74(%rsp), %eax
movq 0x80(%rsp), %rcx
cmpl 0x1c(%rcx), %eax
jge 0x117ba
movl $0x0, 0x70(%rsp)
movl 0x70(%rsp), %eax
movq 0x80(%rsp), %rcx
cmpl 0x18(%rcx), %eax
jge 0x117a8
movl 0x88(%rsp), %eax
movl %eax, 0x18(%rsp)
movq 0x80(%rsp), %rdi
movl 0x74(%rsp), %esi
movl 0x70(%rsp), %edx
callq 0x12110
movl 0x18(%rsp), %edx
movswl (%rax), %ecx
imull %edx, %ecx
movw %cx, (%rax)
movq 0x78(%rsp), %rdi
callq 0x4a30
cmpq $0x0, %rax
jne 0x11758
xorl %eax, %eax
movl %eax, 0x14(%rsp)
jmp 0x1176f
movq 0x78(%rsp), %rdi
movl 0x74(%rsp), %esi
movl 0x70(%rsp), %edx
callq 0x12ba0
cwtl
movl %eax, 0x14(%rsp)
movl 0x14(%rsp), %eax
movl %eax, 0x10(%rsp)
movq 0x80(%rsp), %rdi
movl 0x74(%rsp), %esi
movl 0x70(%rsp), %edx
callq 0x12110
movl 0x10(%rsp), %edx
movswl (%rax), %ecx
addl %edx, %ecx
movw %cx, (%rax)
movl 0x70(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x70(%rsp)
jmp 0x116fe
jmp 0x117aa
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
jmp 0x116e1
jmp 0x117bc
movl 0x8c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8c(%rsp)
jmp 0x115a3
movq 0xb0(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x127d0
movq %rax, 0x58(%rsp)
movslq 0x9c(%rsp), %rsi
leaq 0x58(%rsp), %rdi
callq 0x12800
movq %rax, 0x60(%rsp)
leaq 0x68(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x12840
movq 0xb0(%rsp), %rdi
callq 0x13870
movq %rax, 0x48(%rsp)
leaq 0x50(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x12840
movq 0x8(%rsp), %rdi
movq 0x68(%rsp), %rsi
movq 0x50(%rsp), %rdx
callq 0x12730
movq %rax, 0x40(%rsp)
movb $0x1, %al
andb $0x1, %al
addq $0xb8, %rsp
retq
nopl (%rax)