// File: /workspace/github_cpp_makefile_star2_O2/cloudinary[P]fuif/transform/approximate.h
// Function: fwd_approximate(Image&, std::vector<int, std::allocator<int>>&)
bool fwd_approximate(Image &input, std::vector<int> &parameters) {
    int offset = input.channel.size();
    meta_approximate(input,parameters);
    int beginc = parameters[0];
    int endc = parameters[1];
    v_printf(3,"Approximating channels %i-%i\n",beginc,endc);
    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];
        Channel &chr = input.channel[offset+i];
        i++;
        for (int y=0; y<ch.h; y++) {
          for (int x=0; x<ch.w; x++) {
            pixel_type p = ch.value(y,x);
            pixel_type quotient = p/q;
            pixel_type r = p%q;
            if (r < 0) { quotient--; r+=q; }
            ch.value(y,x) = quotient;
            chr.value(y,x) = r;
          }
        }
        ch.minval /= q;
        ch.maxval /= q;
        chr.minval = 0;
        chr.maxval = q-1;
        chr.q = ch.q; // need to duplicate the quantization factor in case approximated channel becomes all zeroes
    }
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %rbx
movq 0x8(%rdi), %rax
subq (%rdi), %rax
shrq $0x6, %rax
movq %rax, 0x10(%rsp)
movq %rdi, 0x18(%rsp)
callq 0x9fbe
movq %rbx, 0x20(%rsp)
movq (%rbx), %rax
movl (%rax), %ebx
movl 0x4(%rax), %r14d
leaq 0x140f3(%rip), %rsi     # 0x1e1bd
xorl %r12d, %r12d
pushq $0x3
popq %rdi
movl %ebx, %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x16c30
movl %ebx, 0x4(%rsp)
movslq %ebx, %r15
movslq %r14d, %rax
movq %rax, 0x28(%rsp)
cmpq 0x28(%rsp), %r15
jg 0xa219
movl %r15d, %eax
subl 0x4(%rsp), %eax
addl $0x2, %eax
cltq
movq 0x20(%rsp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %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), %eax
testl %eax, %eax
je 0xa211
movq %rax, 0x30(%rsp)
leal 0x1(%rax), %ebx
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq %r15, 0x38(%rsp)
shlq $0x6, %r15
addq %rax, %r15
movq 0x10(%rsp), %rcx
movq %r12, 0x40(%rsp)
addl %r12d, %ecx
movslq %ecx, %rcx
shlq $0x6, %rcx
addq %rax, %rcx
movq %rcx, 0x8(%rsp)
xorl %r13d, %r13d
cmpl 0x1c(%r15), %r13d
jge 0xa1d0
xorl %r14d, %r14d
cmpl 0x18(%r15), %r14d
jge 0xa1cb
movq %r15, %rdi
movl %r13d, %esi
movl %r14d, %edx
callq 0xa44a
movswl (%rax), %eax
cltd
idivl %ebx
movl %eax, %ebp
movl %edx, %r12d
sarl $0x1f, %r12d
addl %r12d, %ebp
andl %ebx, %r12d
addl %edx, %r12d
movq %r15, %rdi
movl %r13d, %esi
movl %r14d, %edx
callq 0xa44a
movw %bp, (%rax)
movq 0x8(%rsp), %rdi
movl %r13d, %esi
movl %r14d, %edx
callq 0xa44a
movw %r12w, (%rax)
incl %r14d
jmp 0xa175
incl %r13d
jmp 0xa16c
movswl 0x20(%r15), %eax
cltd
idivl %ebx
movw %ax, 0x20(%r15)
movswl 0x22(%r15), %eax
cltd
idivl %ebx
movw %ax, 0x22(%r15)
movq 0x8(%rsp), %rcx
andw $0x0, 0x20(%rcx)
movq 0x40(%rsp), %r12
incl %r12d
movq 0x30(%rsp), %rax
movw %ax, 0x22(%rcx)
movl 0x28(%r15), %eax
movl %eax, 0x28(%rcx)
movq 0x38(%rsp), %r15
incq %r15
jmp 0xa0eb
movb $0x1, %al
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq