// File: /workspace/github_cpp_makefile_star2_O1/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 %rsi, %rbx
movq (%rsi), %rax
movslq (%rax), %r14
movl 0x4(%rax), %ebp
movq 0x8(%rdi), %r9
movq %rdi, 0x28(%rsp)
subq (%rdi), %r9
sarq $0x6, %r9
movl %ebp, %ecx
notl %ecx
leal (%r9,%r14), %r15d
addl %ecx, %r15d
cmpl %ebp, %r14d
jg 0xd3c5
movq 0x8(%rbx), %rdx
movq %rdx, %rsi
subq %rax, %rsi
sarq $0x2, %rsi
addq $-0x4, %rdx
addl %r14d, %ecx
movl $0x2, %edi
movslq %edi, %rdi
cmpq %rdi, %rsi
leaq (%rax,%rdi,4), %r8
cmovbeq %rdx, %r8
cmpl $0x1, (%r8)
adcl $0x0, %r15d
leal (%rcx,%rdi), %r8d
incl %r8d
incl %edi
cmpl $0x2, %r8d
jne 0xd3a0
decq %r9
leaq 0x18d8d(%rip), %rsi     # 0x2615c
movl $0x3, %edi
movl %r14d, %edx
movl %ebp, %ecx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x1d0d6
cmpl %ebp, %r14d
jg 0xd5da
incl %ebp
movq %r14, %r12
xorl %edi, %edi
movq %rbx, 0x20(%rsp)
movq %r14, 0x40(%rsp)
movl %ebp, 0x1c(%rsp)
movq %r15, 0x38(%rsp)
movl %r12d, %eax
subl %r14d, %eax
addl $0x2, %eax
cltq
movq (%rbx), %rcx
movq 0x8(%rbx), %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), %r13d
testl %r13d, %r13d
je 0xd5ce
movq 0x28(%rsp), %rax
movq (%rax), %rax
movq %r12, %rsi
shlq $0x6, %rsi
addq %rax, %rsi
leal (%rdi,%r15), %ecx
movslq %ecx, %rcx
shlq $0x6, %rcx
leaq (%rax,%rcx), %r8
movq 0x8(%rax,%rcx), %rdx
cmpq (%rax,%rcx), %rdx
movq %rsi, 0x30(%rsp)
movq %r8, 0x10(%rsp)
je 0xd47c
movl 0x28(%r8), %eax
movl %eax, 0x28(%rsi)
jmp 0xd4ae
movl %ebp, %ebx
movq %r14, %rbp
movq %rdi, %r14
movl $0x3, %edi
leaq 0x18d18(%rip), %rsi     # 0x261a8
xorl %eax, %eax
callq 0x1d0d6
movq 0x10(%rsp), %r8
movq 0x30(%rsp), %rsi
movq %r14, %rdi
movq %rbp, %r14
movl %ebx, %ebp
movq 0x20(%rsp), %rbx
incl %edi
movslq 0x1c(%rsi), %rax
movq %rax, 0x60(%rsp)
testq %rax, %rax
jle 0xd5ce
movq %rdi, 0x48(%rsp)
movq %r12, 0x50(%rsp)
movl 0x18(%rsi), %ecx
leaq 0x24(%rsi), %r9
incl %r13d
leaq 0x24(%r8), %rsi
leaq (%rcx,%rcx), %rax
movq %rax, 0x58(%rsp)
xorl %r8d, %r8d
movq $0x0, 0x8(%rsp)
movq $0x0, (%rsp)
testl %ecx, %ecx
jle 0xd589
movq 0x30(%rsp), %rax
movq (%rax), %r11
movq 0x8(%rax), %rbx
subq %r11, %rbx
sarq %rbx
movq 0x10(%rsp), %rax
movq (%rax), %r12
movq 0x8(%rax), %rbp
movq %rbp, %rax
subq %r12, %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 %r13w, %r10w
movw %r10w, (%rdx)
cmpq %r12, %rbp
je 0xd573
movq 0x10(%rsp), %r14
movslq 0x18(%r14), %r14
imulq %rdi, %r14
addq %r15, %r14
cmpq %r14, %rax
leaq (%r12,%r14,2), %r14
cmovbeq %rsi, %r14
movzwl (%r14), %r14d
jmp 0xd576
xorl %r14d, %r14d
addl %r10d, %r14d
movw %r14w, (%rdx)
incq %r15
addq $0x2, %r11
cmpq %r15, %rcx
jne 0xd532
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 0xd4f7
movq 0x20(%rsp), %rbx
movq 0x40(%rsp), %r14
movl 0x1c(%rsp), %ebp
movq 0x38(%rsp), %r15
movq 0x50(%rsp), %r12
movq 0x48(%rsp), %rdi
incq %r12
cmpl %r12d, %ebp
jne 0xd406
movslq %r15d, %rsi
shlq $0x6, %rsi
movq 0x28(%rsp), %rdi
addq (%rdi), %rsi
movq 0x8(%rdi), %rdx
callq 0xfd3a
movb $0x1, %al
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq