// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, %rbx
movq (%rsi), %rax
movslq (%rax), %r15
movl 0x4(%rax), %r12d
movq 0x8(%rdi), %r9
subq (%rdi), %r9
sarq $0x6, %r9
movl %r12d, %ecx
notl %ecx
leal (%r9,%r15), %r14d
addl %ecx, %r14d
cmpl %r12d, %r15d
jle 0xd2d9
decq %r9
leaq 0x18e9d(%rip), %rsi     # 0x2615c
movl $0x3, %edi
movl %r15d, %edx
movl %r12d, %ecx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x1cede
jmp 0xd542
movq %rsi, %r13
movq 0x8(%rsi), %rdx
movq %rdx, %rsi
subq %rax, %rsi
sarq $0x2, %rsi
addq $-0x4, %rdx
addl %r15d, %ecx
movl $0x2, %edi
movslq %edi, %rdi
cmpq %rdi, %rsi
leaq (%rax,%rdi,4), %r8
cmovbeq %rdx, %r8
cmpl $0x1, (%r8)
adcl $0x0, %r14d
leal (%rcx,%rdi), %r8d
incl %r8d
incl %edi
cmpl $0x2, %r8d
jne 0xd2f6
decq %r9
leaq 0x18e37(%rip), %rsi     # 0x2615c
movl $0x3, %edi
movl %r15d, %edx
movl %r12d, %ecx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x1cede
cmpl %r12d, %r15d
jg 0xd542
incl %r12d
movq %r15, %rdi
xorl %r8d, %r8d
movq %rbx, 0x20(%rsp)
movq %r15, 0x40(%rsp)
movl %r12d, 0x1c(%rsp)
movq %r13, 0x38(%rsp)
movl %edi, %eax
subl %r15d, %eax
addl $0x2, %eax
cltq
movq (%r13), %rcx
movq 0x8(%r13), %rdx
movq %rdx, %rsi
subq %rcx, %rsi
sarq $0x2, %rsi
addq $-0x4, %rdx
cmpq %rax, %rsi
leaq (%rcx,%rax,4), %rax
cmovbeq %rdx, %rax
movl (%rax), %ebp
testl %ebp, %ebp
je 0xd536
movq (%rbx), %rax
movq %rdi, %rsi
shlq $0x6, %rsi
addq %rax, %rsi
leal (%r8,%r14), %ecx
movslq %ecx, %rcx
shlq $0x6, %rcx
leaq (%rax,%rcx), %r10
movq 0x8(%rax,%rcx), %rdx
cmpq (%rax,%rcx), %rdx
movq %rsi, 0x30(%rsp)
movq %r10, 0x10(%rsp)
movq %rdi, 0x28(%rsp)
je 0xd3d4
movl 0x28(%r10), %eax
movl %eax, 0x28(%rsi)
jmp 0xd40d
movl $0x3, %edi
leaq 0x18dc8(%rip), %rsi     # 0x261a8
xorl %eax, %eax
movl %r12d, %ebx
movq %r15, %r12
movq %r8, %r15
callq 0x1cede
movq 0x10(%rsp), %r10
movq 0x30(%rsp), %rsi
movq %r15, %r8
movq %r12, %r15
movl %ebx, %r12d
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rbx
incl %r8d
movslq 0x1c(%rsi), %rax
movq %rax, 0x60(%rsp)
testq %rax, %rax
jle 0xd536
movq %r8, 0x48(%rsp)
movq %r14, 0x50(%rsp)
movl 0x18(%rsi), %ecx
leaq 0x24(%rsi), %r9
incl %ebp
leaq 0x24(%r10), %rsi
leaq (%rcx,%rcx), %rax
movq %rax, 0x58(%rsp)
xorl %r8d, %r8d
movq $0x0, 0x8(%rsp)
movq $0x0, (%rsp)
testl %ecx, %ecx
jle 0xd4eb
movq 0x30(%rsp), %rax
movq (%rax), %r11
movq 0x8(%rax), %rbx
subq %r11, %rbx
sarq %rbx
movq 0x10(%rsp), %rax
movq (%rax), %r13
movq 0x8(%rax), %r14
movq %r14, %rax
subq %r13, %rax
sarq %rax
addq 0x8(%rsp), %r11
movslq (%rsp), %rdi
xorl %r15d, %r15d
leaq (%r8,%r15), %rdx
cmpq %rdx, %rbx
movq %r9, %rdx
cmovaq %r11, %rdx
movzwl (%rdx), %r10d
imulw %bp, %r10w
movw %r10w, (%rdx)
cmpq %r13, %r14
je 0xd4d5
movq 0x10(%rsp), %r12
movslq 0x18(%r12), %r12
imulq %rdi, %r12
addq %r15, %r12
cmpq %r12, %rax
leaq (%r13,%r12,2), %r12
cmovbeq %rsi, %r12
movzwl (%r12), %r12d
jmp 0xd4d8
xorl %r12d, %r12d
addl %r10d, %r12d
movw %r12w, (%rdx)
incq %r15
addq $0x2, %r11
cmpq %r15, %rcx
jne 0xd491
movq (%rsp), %rdx
incq %rdx
movq 0x8(%rsp), %rax
addq 0x58(%rsp), %rax
movq %rax, 0x8(%rsp)
addq %rcx, %r8
movq %rdx, (%rsp)
cmpq 0x60(%rsp), %rdx
jne 0xd456
movq 0x20(%rsp), %rbx
movq 0x50(%rsp), %r14
movq 0x40(%rsp), %r15
movl 0x1c(%rsp), %r12d
movq 0x38(%rsp), %r13
movq 0x28(%rsp), %rdi
movq 0x48(%rsp), %r8
incq %rdi
cmpl %edi, %r12d
jne 0xd360
movslq %r14d, %rsi
shlq $0x6, %rsi
addq (%rbx), %rsi
movq 0x8(%rbx), %rdx
movq %rbx, %rdi
callq 0xfcc2
movb $0x1, %al
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq