// File: /workspace/github_cpp_makefile_star2_O2/cloudinary[P]fuif/transform/2dmatch.h
// Function: inv_match(Image&, std::vector<int, std::allocator<int>>)
bool inv_match(Image &input, std::vector<int> parameters) {
    if (input.nb_meta_channels < 1) {
        e_printf("Error: match transform without match.\n");
        return false;
    }
    if (!parameters.size()) default_match_parameters(parameters,input);
    if (parameters.size() < 3) {
        e_printf("Error: match transform with incorrect parameters.\n");
        return false;
    }
    const Channel &m = input.channel[0];
    int c0 = input.nb_meta_channels + parameters[0];
    int cn = input.nb_meta_channels + parameters[1];
    if (c0 >= input.channel.size() || cn >= input.channel.size()) {
        e_printf("Error: match transform with incorrect parameters.\n");
        return false;
    }
    bool softmatch = parameters[2];
    int w = input.channel[c0].w;
    int h = input.channel[c0].h;


    if (m.q == 1) {
        offsets offsets_table(m.maxval + 1);
        make_offsets_table(offsets_table);
        if (softmatch)
        for (int y=0; y<h; y++) {
          for (int x=0; x<w; x++) {
            pixel_type z = m.value(y,x);
            if (z) {
                offset o = offsets_table[z];
                for (int c=c0; c<=cn; c++)
                    input.channel[c].value(y,x) += input.channel[c].value(y+o.second, x+o.first);
            }
          }
        }
        else
        for (int y=0; y<h; y++) {
          for (int x=0; x<w; x++) {
            pixel_type z = m.value(y,x);
            if (z) {
                offset o = offsets_table[z];
                for (int c=c0; c<=cn; c++)
                    input.channel[c].value(y,x) = input.channel[c].value(y+o.second, x+o.first);
            }
          }
        }
    } else {
        int fh = h/input.nb_frames;
        int offsetcode = 2*fh*fh + (fh&1); // this simple formula corresponds to the code to go exactly one frame up
        if (m.q == offsetcode) {
            // this is the special case where we are matching with corresponding pixels from previous frames; optimize it a bit...
            if (softmatch)
            for (int y=0; y<h; y++) {
              for (int x=0; x<w; x++) {
                pixel_type z = m.value(y,x);
                if (z) {
                    for (int c=c0; c<=cn; c++)
                        input.channel[c].value(y,x) += input.channel[c].value(y-z*fh, x);
                }
              }
            }
            else
            for (int y=0; y<h; y++) {
              for (int x=0; x<w; x++) {
                pixel_type z = m.value(y,x);
                if (z) {
                    for (int c=c0; c<=cn; c++)
                        input.channel[c].value(y,x) = input.channel[c].value(y-z*fh, x);
                }
              }
            }
        } else {
            e_printf("Error: match transform with unexpected quantization factor. Not implemented.\n");
            return false;
        }
    }
    input.nb_meta_channels--;
    input.channel.erase(input.channel.begin(),input.channel.begin()+1);
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, 0x18(%rsp)
cmpl $0x0, 0x6c(%rdi)
jle 0x8754
movq %rsi, %r14
movq (%rsi), %rax
movq 0x8(%rsi), %rcx
cmpq %rax, %rcx
jne 0x8742
movq %r14, %rdi
movq 0x18(%rsp), %rsi
callq 0x868b
movq (%r14), %rax
movq 0x8(%r14), %rcx
subq %rax, %rcx
cmpq $0x8, %rcx
ja 0x8775
leaq 0x15625(%rip), %rdi     # 0x1dd77
jmp 0x875b
leaq 0x155f5(%rip), %rdi     # 0x1dd50
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x16b80
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rsp), %rdx
movq (%rdx), %rbx
movq 0x8(%rdx), %rcx
movslq 0x6c(%rdx), %rdx
movslq (%rax), %rsi
addq %rdx, %rsi
subq %rbx, %rcx
sarq $0x6, %rcx
movq %rsi, 0x10(%rsp)
cmpq %rsi, %rcx
jbe 0x874b
movslq 0x4(%rax), %rsi
addq %rdx, %rsi
movq %rsi, 0x38(%rsp)
cmpq %rsi, %rcx
jbe 0x874b
movl 0x8(%rax), %ebp
movq 0x10(%rsp), %rax
shlq $0x6, %rax
movl 0x18(%rbx,%rax), %r14d
movq %rax, 0x20(%rsp)
movl 0x1c(%rbx,%rax), %r12d
movl 0x28(%rbx), %ecx
cmpl $0x1, %ecx
movq %rbx, 0x30(%rsp)
jne 0x88d9
movswq 0x22(%rbx), %rsi
incq %rsi
leaq 0x40(%rsp), %r15
leaq 0x2f(%rsp), %rdx
movq %r15, %rdi
callq 0xa9bc
movq %r15, %rdi
callq 0x862d
xorl %esi, %esi
testl %r14d, %r14d
cmovlel %esi, %r14d
testl %r12d, %r12d
cmovlel %esi, %r12d
testl %ebp, %ebp
movl %r14d, 0xc(%rsp)
movl %r12d, 0x8(%rsp)
je 0x89c1
movq 0x10(%rsp), %rax
shlq $0x6, %rax
movq %rax, 0x20(%rsp)
cmpl %r12d, %esi
je 0x8a7f
xorl %edx, %edx
movl %esi, 0x4(%rsp)
cmpl %r14d, %edx
je 0x88d2
movq %rbx, %rdi
movl %edx, (%rsp)
callq 0xa79e
testw %ax, %ax
je 0x88b5
movswq %ax, %rax
movq 0x40(%rsp), %rcx
movl 0x4(%rcx,%rax,8), %r13d
addl 0x4(%rsp), %r13d
movl (%rcx,%rax,8), %ebp
addl (%rsp), %ebp
movq 0x20(%rsp), %r14
movq 0x10(%rsp), %r15
cmpq 0x38(%rsp), %r15
jg 0x88b5
movq 0x18(%rsp), %r12
movq (%r12), %rdi
addq %r14, %rdi
movl %r13d, %esi
movl %ebp, %edx
callq 0xa44a
movzwl (%rax), %ebx
movq (%r12), %rdi
addq %r14, %rdi
movl 0x4(%rsp), %esi
movl (%rsp), %edx
callq 0xa44a
addw %bx, (%rax)
incq %r15
addq $0x40, %r14
jmp 0x8876
movl (%rsp), %edx
incl %edx
movq 0x30(%rsp), %rbx
movl 0xc(%rsp), %r14d
movl 0x8(%rsp), %r12d
movl 0x4(%rsp), %esi
jmp 0x883a
incl %esi
jmp 0x882b
movl %r12d, %eax
cltd
movq 0x18(%rsp), %rsi
idivl 0x38(%rsi)
movl %eax, %esi
imull %esi, %esi
movl %eax, 0x4(%rsp)
movl %eax, %edx
andl $0x1, %edx
leal (%rdx,%rsi,2), %eax
cmpl %eax, %ecx
jne 0x89b5
xorl %esi, %esi
testl %r14d, %r14d
cmovlel %esi, %r14d
testl %r12d, %r12d
cmovlel %esi, %r12d
testl %ebp, %ebp
movl %r14d, 0xc(%rsp)
movl %r12d, 0x8(%rsp)
je 0x8aa4
cmpl %r12d, %esi
je 0x8a89
xorl %r13d, %r13d
movl %esi, (%rsp)
cmpl %r14d, %r13d
je 0x89a9
movq %rbx, %rdi
movl %r13d, %edx
callq 0xa79e
testw %ax, %ax
je 0x8997
cwtl
imull 0x4(%rsp), %eax
movl (%rsp), %ebp
subl %eax, %ebp
movq 0x20(%rsp), %r14
movq 0x10(%rsp), %r15
cmpq 0x38(%rsp), %r15
jg 0x8997
movq 0x18(%rsp), %r12
movq (%r12), %rdi
addq %r14, %rdi
movl %ebp, %esi
movl %r13d, %edx
callq 0xa44a
movzwl (%rax), %ebx
movq (%r12), %rdi
addq %r14, %rdi
movl (%rsp), %esi
movl %r13d, %edx
callq 0xa44a
addw %bx, (%rax)
incq %r15
addq $0x40, %r14
jmp 0x8959
incl %r13d
movq 0x30(%rsp), %rbx
movl 0xc(%rsp), %r14d
movl (%rsp), %esi
jmp 0x892f
incl %esi
movl 0x8(%rsp), %r12d
jmp 0x8920
leaq 0x153ee(%rip), %rdi     # 0x1ddaa
jmp 0x875b
movq 0x10(%rsp), %rax
shlq $0x6, %rax
movq %rax, 0x20(%rsp)
xorl %esi, %esi
cmpl %r12d, %esi
je 0x8a7f
xorl %edx, %edx
movl %esi, 0x4(%rsp)
cmpl %r14d, %edx
je 0x8a78
movq %rbx, %rdi
movl %edx, (%rsp)
callq 0xa79e
testw %ax, %ax
je 0x8a5b
movswq %ax, %rax
movq 0x40(%rsp), %rcx
movl 0x4(%rcx,%rax,8), %r13d
addl 0x4(%rsp), %r13d
movl (%rcx,%rax,8), %ebp
addl (%rsp), %ebp
movq 0x20(%rsp), %r14
movq 0x10(%rsp), %r15
cmpq 0x38(%rsp), %r15
jg 0x8a5b
movq 0x18(%rsp), %r12
movq (%r12), %rdi
addq %r14, %rdi
movl %r13d, %esi
movl %ebp, %edx
callq 0xa44a
movzwl (%rax), %ebx
movq (%r12), %rdi
addq %r14, %rdi
movl 0x4(%rsp), %esi
movl (%rsp), %edx
callq 0xa44a
movw %bx, (%rax)
incq %r15
addq $0x40, %r14
jmp 0x8a1c
movl (%rsp), %edx
incl %edx
movq 0x30(%rsp), %rbx
movl 0xc(%rsp), %r14d
movl 0x8(%rsp), %r12d
movl 0x4(%rsp), %esi
jmp 0x89e0
incl %esi
jmp 0x89d1
leaq 0x40(%rsp), %rdi
callq 0xc00e
movq 0x18(%rsp), %rdi
decl 0x6c(%rdi)
movq (%rdi), %rsi
leaq 0x40(%rsi), %rdx
callq 0xa676
movb $0x1, %bl
jmp 0x8764
movq 0x10(%rsp), %rax
shlq $0x6, %rax
movq %rax, 0x20(%rsp)
xorl %esi, %esi
cmpl %r12d, %esi
je 0x8a89
xorl %r13d, %r13d
movl %esi, (%rsp)
cmpl %r14d, %r13d
je 0x8b39
movq %rbx, %rdi
movl %r13d, %edx
callq 0xa79e
testw %ax, %ax
je 0x8b27
cwtl
imull 0x4(%rsp), %eax
movl (%rsp), %ebp
subl %eax, %ebp
movq 0x20(%rsp), %r14
movq 0x10(%rsp), %r15
cmpq 0x38(%rsp), %r15
jg 0x8b27
movq 0x18(%rsp), %r12
movq (%r12), %rdi
addq %r14, %rdi
movl %ebp, %esi
movl %r13d, %edx
callq 0xa44a
movzwl (%rax), %ebx
movq (%r12), %rdi
addq %r14, %rdi
movl (%rsp), %esi
movl %r13d, %edx
callq 0xa44a
movw %bx, (%rax)
incq %r15
addq $0x40, %r14
jmp 0x8ae9
incl %r13d
movq 0x30(%rsp), %rbx
movl 0xc(%rsp), %r14d
movl (%rsp), %esi
jmp 0x8abf
incl %esi
movl 0x8(%rsp), %r12d
jmp 0x8ab4