// File: /workspace/github_cpp_makefile_star2_O1/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 $0x68, %rsp
cmpl $0x0, 0x6c(%rdi)
jle 0xb50f
movq %rsi, %r14
movq %rdi, %r15
movq 0x8(%rsi), %rax
cmpq (%rsi), %rax
jne 0xb4f6
movq %r14, %rdi
movq %r15, %rsi
callq 0xb3ef
movq (%r14), %rax
movq 0x8(%r14), %rcx
subq %rax, %rcx
cmpq $0x8, %rcx
ja 0xb530
leaq 0x1a8ea(%rip), %rdi     # 0x25df7
jmp 0xb516
leaq 0x1a8ba(%rip), %rdi     # 0x25dd0
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x3796
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r15), %rsi
movq 0x8(%r15), %rcx
movl 0x6c(%r15), %edi
movl (%rax), %edx
movq %rdx, 0x40(%rsp)
leal (%rdx,%rdi), %r12d
movslq %r12d, %rdx
subq %rsi, %rcx
sarq $0x6, %rcx
cmpq %rdx, %rcx
jbe 0xb506
movq %rdx, %r8
movl 0x4(%rax), %edx
movl %edx, 0x4(%rsp)
addl %edx, %edi
movslq %edi, %rdx
cmpq %rdx, %rcx
jbe 0xb506
movl 0x8(%rax), %ebx
shlq $0x6, %r8
movl 0x18(%rsi,%r8), %eax
movq %rax, 0x10(%rsp)
movq %r8, 0x20(%rsp)
movl 0x1c(%rsi,%r8), %ebp
movl 0x28(%rsi), %r8d
cmpl $0x1, %r8d
movq %rsi, 0x8(%rsp)
movl %ebp, (%rsp)
movq %r15, 0x38(%rsp)
jne 0xb6f2
movq %rdi, %r14
movswq 0x22(%rsi), %rsi
incq %rsi
leaq 0x50(%rsp), %r15
leaq 0x1f(%rsp), %rdx
movq %r15, %rdi
callq 0xe11e
movq %r15, %rdi
callq 0xb38b
testl %ebx, %ebx
je 0xb84a
testl %ebp, %ebp
jle 0xb96d
movq 0x8(%rsp), %rax
addq $0x24, %rax
cmpl %r14d, %r12d
cmovgl %r12d, %r14d
addq $0x24, 0x20(%rsp)
subl %r12d, %r14d
incl %r14d
movq %r14, 0x28(%rsp)
xorl %ecx, %ecx
cmpl $0x0, 0x10(%rsp)
jle 0xb6e2
movq 0x8(%rsp), %rdi
movslq 0x18(%rdi), %rsi
movslq %ecx, %rdx
imulq %rsi, %rdx
movq (%rdi), %rsi
movq 0x8(%rdi), %rdi
subq %rsi, %rdi
sarq %rdi
movq 0x50(%rsp), %r8
movq 0x38(%rsp), %r9
movq (%r9), %r9
addq 0x20(%rsp), %r9
movq %r9, 0x30(%rsp)
xorl %r10d, %r10d
movl 0x4(%rsp), %r9d
cmpl %r9d, 0x40(%rsp)
setg %r11b
leaq (%r10,%rdx), %r9
cmpq %r9, %rdi
leaq (%rsi,%r9,2), %r9
cmovbeq %rax, %r9
movswq (%r9), %r9
testq %r9, %r9
sete %bl
orb %r11b, %bl
jne 0xb6d4
movl 0x4(%r8,%r9,8), %r11d
addl %ecx, %r11d
movl (%r8,%r9,8), %ebx
addl %r10d, %ebx
movq 0x28(%rsp), %r9
movl %r9d, %r15d
movq 0x30(%rsp), %r9
movl -0xc(%r9), %r14d
movl %r14d, %r13d
imull %r11d, %r13d
addl %ebx, %r13d
movslq %r13d, %r13
movq -0x24(%r9), %rbp
movq -0x1c(%r9), %r12
subq %rbp, %r12
sarq %r12
cmpq %r13, %r12
leaq (%rbp,%r13,2), %r13
cmovbeq %r9, %r13
imull %ecx, %r14d
movslq %r14d, %r14
addq %r10, %r14
cmpq %r14, %r12
leaq (%rbp,%r14,2), %r14
cmovbeq %r9, %r14
movzwl (%r13), %ebp
addw %bp, (%r14)
addq $0x40, %r9
decl %r15d
jne 0xb681
incq %r10
cmpq 0x10(%rsp), %r10
jne 0xb639
incl %ecx
cmpl (%rsp), %ecx
jne 0xb5f7
jmp 0xb96d
movl %ebp, %eax
cltd
idivl 0x38(%r15)
movl %eax, %ecx
imull %ecx, %ecx
movl %eax, 0x28(%rsp)
movl %eax, %edx
andl $0x1, %edx
leal (%rdx,%rcx,2), %eax
movl %eax, 0x4c(%rsp)
cmpl %eax, %r8d
movl %r8d, 0x30(%rsp)
jne 0xb822
testl %ebx, %ebx
je 0xb99f
testl %ebp, %ebp
jle 0xb830
movq 0x8(%rsp), %rdx
leaq 0x24(%rdx), %rcx
cmpl %edi, %r12d
cmovgl %r12d, %edi
movq 0x20(%rsp), %rax
addq %rdx, %rax
addq $0x24, %rax
subl %r12d, %edi
incl %edi
movq %rdi, %rdx
xorl %esi, %esi
cmpl $0x0, 0x10(%rsp)
jle 0xb815
movq 0x8(%rsp), %r9
movslq 0x18(%r9), %r8
movslq %esi, %rdi
imulq %r8, %rdi
movq (%r9), %r8
movq 0x8(%r9), %r9
subq %r8, %r9
sarq %r9
xorl %r10d, %r10d
movl 0x4(%rsp), %r11d
cmpl %r11d, 0x40(%rsp)
setg %r11b
leaq (%r10,%rdi), %rbx
cmpq %rbx, %r9
leaq (%r8,%rbx,2), %rbx
cmovbeq %rcx, %rbx
movswl (%rbx), %ebx
testl %ebx, %ebx
sete %bpl
orb %r11b, %bpl
jne 0xb807
imull 0x28(%rsp), %ebx
movl %esi, %r11d
subl %ebx, %r11d
movl %edx, %ebx
movq %rax, %r15
movl -0xc(%r15), %ebp
movl %ebp, %r14d
imull %r11d, %r14d
movslq %r14d, %r14
addq %r10, %r14
movq -0x24(%r15), %r12
movq -0x1c(%r15), %r13
subq %r12, %r13
sarq %r13
cmpq %r14, %r13
leaq (%r12,%r14,2), %r14
cmovbeq %r15, %r14
imull %esi, %ebp
movslq %ebp, %rbp
addq %r10, %rbp
cmpq %rbp, %r13
leaq (%r12,%rbp,2), %r12
cmovbeq %r15, %r12
movzwl (%r14), %ebp
addw %bp, (%r12)
addq $0x40, %r15
decl %ebx
jne 0xb7b8
incq %r10
cmpq 0x10(%rsp), %r10
jne 0xb77d
incl %esi
cmpl (%rsp), %esi
jne 0xb752
jmp 0xb830
leaq 0x1a601(%rip), %rdi     # 0x25e2a
xorl %eax, %eax
callq 0x3796
movl 0x4c(%rsp), %eax
cmpl %eax, 0x30(%rsp)
movq 0x38(%rsp), %rdi
je 0xb989
xorl %ebx, %ebx
jmp 0xb51f
testl %ebp, %ebp
jle 0xb96d
movq 0x8(%rsp), %rax
addq $0x24, %rax
cmpl %r14d, %r12d
cmovgl %r12d, %r14d
addq $0x24, 0x20(%rsp)
subl %r12d, %r14d
incl %r14d
movq %r14, 0x28(%rsp)
xorl %ecx, %ecx
cmpl $0x0, 0x10(%rsp)
jle 0xb962
movq 0x8(%rsp), %rdi
movslq 0x18(%rdi), %rsi
movslq %ecx, %rdx
imulq %rsi, %rdx
movq (%rdi), %rsi
movq 0x8(%rdi), %rdi
subq %rsi, %rdi
sarq %rdi
movq 0x50(%rsp), %r8
movq 0x38(%rsp), %r9
movq (%r9), %r9
addq 0x20(%rsp), %r9
movq %r9, 0x30(%rsp)
xorl %r10d, %r10d
movl 0x4(%rsp), %r9d
cmpl %r9d, 0x40(%rsp)
setg %r11b
leaq (%r10,%rdx), %rbx
cmpq %rbx, %rdi
leaq (%rsi,%rbx,2), %rbx
cmovbeq %rax, %rbx
movswq (%rbx), %rbx
testq %rbx, %rbx
sete %bpl
orb %r11b, %bpl
jne 0xb954
movl 0x4(%r8,%rbx,8), %r11d
addl %ecx, %r11d
movl (%r8,%rbx,8), %ebx
addl %r10d, %ebx
movq 0x28(%rsp), %r9
movl %r9d, %r15d
movq 0x30(%rsp), %r12
movl -0xc(%r12), %ebp
movl %ebp, %r14d
imull %r11d, %r14d
addl %ebx, %r14d
movslq %r14d, %r14
movq -0x24(%r12), %r13
movq -0x1c(%r12), %r9
subq %r13, %r9
sarq %r9
cmpq %r14, %r9
leaq (%r13,%r14,2), %r14
cmovbeq %r12, %r14
imull %ecx, %ebp
movslq %ebp, %rbp
addq %r10, %rbp
cmpq %rbp, %r9
leaq (%r13,%rbp,2), %r9
cmovbeq %r12, %r9
movzwl (%r14), %ebp
movw %bp, (%r9)
addq $0x40, %r12
decl %r15d
jne 0xb900
incq %r10
cmpq 0x10(%rsp), %r10
jne 0xb8b7
incl %ecx
cmpl (%rsp), %ecx
jne 0xb875
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0xb984
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x32c0
movq 0x38(%rsp), %rdi
decl 0x6c(%rdi)
movq (%rdi), %rsi
leaq 0x40(%rsi), %rdx
callq 0xfd3a
movb $0x1, %bl
jmp 0xb51f
testl %ebp, %ebp
jle 0xb830
movq 0x8(%rsp), %rdx
leaq 0x24(%rdx), %rcx
cmpl %edi, %r12d
cmovgl %r12d, %edi
movq 0x20(%rsp), %rax
addq %rax, %rdx
addq $0x24, %rdx
subl %r12d, %edi
incl %edi
movq %rdi, %rax
xorl %esi, %esi
cmpl $0x0, 0x10(%rsp)
jle 0xba90
movq 0x8(%rsp), %r9
movslq 0x18(%r9), %r8
movslq %esi, %rdi
imulq %r8, %rdi
movq (%r9), %r8
movq 0x8(%r9), %r9
subq %r8, %r9
sarq %r9
xorl %r10d, %r10d
movl 0x4(%rsp), %r11d
cmpl %r11d, 0x40(%rsp)
setg %r11b
leaq (%r10,%rdi), %rbx
cmpq %rbx, %r9
leaq (%r8,%rbx,2), %rbx
cmovbeq %rcx, %rbx
movswl (%rbx), %ebx
testl %ebx, %ebx
sete %bpl
orb %r11b, %bpl
jne 0xba82
imull 0x28(%rsp), %ebx
movl %esi, %r11d
subl %ebx, %r11d
movl %eax, %ebx
movq %rdx, %r15
movl -0xc(%r15), %ebp
movl %ebp, %r14d
imull %r11d, %r14d
movslq %r14d, %r14
addq %r10, %r14
movq -0x24(%r15), %r12
movq -0x1c(%r15), %r13
subq %r12, %r13
sarq %r13
cmpq %r14, %r13
leaq (%r12,%r14,2), %r14
cmovbeq %r15, %r14
imull %esi, %ebp
movslq %ebp, %rbp
addq %r10, %rbp
cmpq %rbp, %r13
leaq (%r12,%rbp,2), %r12
cmovbeq %r15, %r12
movzwl (%r14), %ebp
movw %bp, (%r12)
addq $0x40, %r15
decl %ebx
jne 0xba33
incq %r10
cmpq 0x10(%rsp), %r10
jne 0xb9f8
incl %esi
cmpl (%rsp), %esi
jne 0xb9cd
jmp 0xb830