// File: /workspace/github_cpp_makefile_star2_O3/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 0xb43a
movq %rsi, %r14
movq %rdi, %r15
movq (%rsi), %rax
movq 0x8(%rsi), %rcx
cmpq %rax, %rcx
jne 0xb428
movq %r14, %rdi
movq %r15, %rsi
callq 0xb309
movq (%r14), %rax
movq 0x8(%r14), %rcx
subq %rax, %rcx
cmpq $0x8, %rcx
ja 0xb45b
leaq 0x1a9bf(%rip), %rdi     # 0x25df7
jmp 0xb441
leaq 0x1a98f(%rip), %rdi     # 0x25dd0
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x379e
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r15), %rcx
movq 0x8(%r15), %rdx
movl 0x6c(%r15), %r8d
movl (%rax), %esi
movq %rsi, 0x48(%rsp)
leal (%rsi,%r8), %r12d
movslq %r12d, %rsi
subq %rcx, %rdx
sarq $0x6, %rdx
cmpq %rsi, %rdx
jbe 0xb431
movq %rsi, %rdi
movl 0x4(%rax), %esi
movq %rsi, 0x40(%rsp)
addl %r8d, %esi
movl %esi, %ebp
movslq %esi, %rsi
cmpq %rsi, %rdx
jbe 0xb431
movl 0x8(%rax), %ebx
shlq $0x6, %rdi
movl 0x18(%rcx,%rdi), %eax
movq %rax, 0x10(%rsp)
movq %rdi, 0x30(%rsp)
movl 0x1c(%rcx,%rdi), %r14d
movl 0x28(%rcx), %esi
cmpl $0x1, %esi
movq %rcx, 0x38(%rsp)
movl %r14d, 0xc(%rsp)
jne 0xb61a
movq %r15, 0x18(%rsp)
movswq 0x22(%rcx), %rsi
incq %rsi
leaq 0x50(%rsp), %r15
leaq 0x2f(%rsp), %rdx
movq %r15, %rdi
callq 0xe090
movq %r15, %rdi
callq 0xb2a5
testl %ebx, %ebx
je 0xb759
testl %r14d, %r14d
jle 0xb877
movq 0x38(%rsp), %rax
addq $0x24, %rax
cmpl %ebp, %r12d
cmovgl %r12d, %ebp
addq $0x24, 0x30(%rsp)
subl %r12d, %ebp
incl %ebp
movl %ebp, 0x8(%rsp)
xorl %ecx, %ecx
cmpl $0x0, 0x10(%rsp)
jle 0xb609
movq 0x38(%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 0x18(%rsp), %r9
movq (%r9), %r9
addq 0x30(%rsp), %r9
movq %r9, 0x20(%rsp)
xorl %r10d, %r10d
movq 0x40(%rsp), %r9
cmpl %r9d, 0x48(%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 0xb5fb
movl 0x4(%r8,%r9,8), %r11d
addl %ecx, %r11d
movl (%r8,%r9,8), %ebx
addl %r10d, %ebx
movl 0x8(%rsp), %r14d
movq 0x20(%rsp), %r9
movl -0xc(%r9), %r13d
movl %r13d, %r12d
imull %r11d, %r12d
addl %ebx, %r12d
movslq %r12d, %r12
movq -0x24(%r9), %rbp
movq -0x1c(%r9), %r15
subq %rbp, %r15
sarq %r15
cmpq %r12, %r15
leaq (%rbp,%r12,2), %r12
cmovbeq %r9, %r12
imull %ecx, %r13d
movslq %r13d, %r13
addq %r10, %r13
cmpq %r13, %r15
leaq (%rbp,%r13,2), %r15
cmovbeq %r9, %r15
movzwl (%r12), %ebp
addw %bp, (%r15)
addq $0x40, %r9
decl %r14d
jne 0xb5a8
incq %r10
cmpq 0x10(%rsp), %r10
jne 0xb563
incl %ecx
cmpl 0xc(%rsp), %ecx
jne 0xb521
jmp 0xb877
movl %r14d, %eax
cltd
idivl 0x38(%r15)
movl %eax, %edx
imull %edx, %edx
movl %eax, 0x8(%rsp)
movl %eax, %edi
andl $0x1, %edi
leal (%rdi,%rdx,2), %edx
cmpl %edx, %esi
jne 0xb74d
testl %ebx, %ebx
je 0xb89c
testl %r14d, %r14d
jle 0xb9a8
movq %r8, 0x20(%rsp)
movq %r15, 0x18(%rsp)
leaq 0x24(%rcx), %rdx
movl %ebp, %esi
cmpl %ebp, %r12d
cmovgl %r12d, %esi
movq 0x30(%rsp), %rax
addq %rcx, %rax
addq $0x24, %rax
subl %r12d, %esi
incl %esi
xorl %edi, %edi
cmpl $0x0, 0x10(%rsp)
jle 0xb73c
movq 0x38(%rsp), %rcx
movslq 0x18(%rcx), %r9
movslq %edi, %r8
imulq %r9, %r8
movq (%rcx), %r9
movq 0x8(%rcx), %r10
subq %r9, %r10
sarq %r10
xorl %r11d, %r11d
movq 0x40(%rsp), %rcx
cmpl %ecx, 0x48(%rsp)
setg %bl
leaq (%r11,%r8), %r14
cmpq %r14, %r10
leaq (%r9,%r14,2), %r14
cmovbeq %rdx, %r14
movswl (%r14), %r14d
testl %r14d, %r14d
sete %bpl
orb %bl, %bpl
jne 0xb72e
imull 0x8(%rsp), %r14d
movl %edi, %ebx
subl %r14d, %ebx
movl %esi, %r14d
movq %rax, %r15
movl -0xc(%r15), %ebp
movl %ebp, %r12d
imull %ebx, %r12d
movslq %r12d, %r12
addq %r11, %r12
movq -0x24(%r15), %r13
movq -0x1c(%r15), %rcx
subq %r13, %rcx
sarq %rcx
cmpq %r12, %rcx
leaq (%r13,%r12,2), %r12
cmovbeq %r15, %r12
imull %edi, %ebp
movslq %ebp, %rbp
addq %r11, %rbp
cmpq %rbp, %rcx
leaq (%r13,%rbp,2), %rcx
cmovbeq %r15, %rcx
movzwl (%r12), %ebp
addw %bp, (%rcx)
addq $0x40, %r15
decl %r14d
jne 0xb6dd
incq %r11
cmpq 0x10(%rsp), %r11
jne 0xb6a1
incl %edi
cmpl 0xc(%rsp), %edi
jne 0xb676
jmp 0xb99e
leaq 0x1a6d6(%rip), %rdi     # 0x25e2a
jmp 0xb441
testl %r14d, %r14d
jle 0xb877
movq 0x38(%rsp), %rax
addq $0x24, %rax
cmpl %ebp, %r12d
cmovgl %r12d, %ebp
addq $0x24, 0x30(%rsp)
subl %r12d, %ebp
incl %ebp
movl %ebp, 0x8(%rsp)
xorl %ecx, %ecx
cmpl $0x0, 0x10(%rsp)
jle 0xb86b
movq 0x38(%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 0x18(%rsp), %r9
movq (%r9), %r9
addq 0x30(%rsp), %r9
movq %r9, 0x20(%rsp)
xorl %r10d, %r10d
movq 0x40(%rsp), %r9
cmpl %r9d, 0x48(%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 0xb85d
movl 0x4(%r8,%rbx,8), %r11d
addl %ecx, %r11d
movl (%r8,%rbx,8), %ebx
addl %r10d, %ebx
movl 0x8(%rsp), %r14d
movq 0x20(%rsp), %r15
movl -0xc(%r15), %ebp
movl %ebp, %r12d
imull %r11d, %r12d
addl %ebx, %r12d
movslq %r12d, %r12
movq -0x24(%r15), %r13
movq -0x1c(%r15), %r9
subq %r13, %r9
sarq %r9
cmpq %r12, %r9
leaq (%r13,%r12,2), %r12
cmovbeq %r15, %r12
imull %ecx, %ebp
movslq %ebp, %rbp
addq %r10, %rbp
cmpq %rbp, %r9
leaq (%r13,%rbp,2), %r9
cmovbeq %r15, %r9
movzwl (%r12), %ebp
movw %bp, (%r9)
addq $0x40, %r15
decl %r14d
jne 0xb80b
incq %r10
cmpq 0x10(%rsp), %r10
jne 0xb7c5
incl %ecx
cmpl 0xc(%rsp), %ecx
jne 0xb783
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0xb88e
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x32c0
movq 0x18(%rsp), %r15
movl 0x6c(%r15), %r8d
jmp 0xb9a8
testl %r14d, %r14d
jle 0xb9a8
movq %r8, 0x20(%rsp)
movq %r15, 0x18(%rsp)
leaq 0x24(%rcx), %rdx
cmpl %ebp, %r12d
cmovgl %r12d, %ebp
movq 0x30(%rsp), %rax
leaq (%rcx,%rax), %rsi
addq $0x24, %rsi
subl %r12d, %ebp
incl %ebp
movl %ebp, %eax
xorl %edi, %edi
cmpl $0x0, 0x10(%rsp)
jle 0xb992
movq 0x38(%rsp), %r10
movslq 0x18(%r10), %rcx
movslq %edi, %r8
imulq %rcx, %r8
movq (%r10), %r9
movq 0x8(%r10), %r10
subq %r9, %r10
sarq %r10
xorl %r11d, %r11d
movq 0x40(%rsp), %rcx
cmpl %ecx, 0x48(%rsp)
setg %cl
leaq (%r11,%r8), %rbx
cmpq %rbx, %r10
leaq (%r9,%rbx,2), %rbx
cmovbeq %rdx, %rbx
movswl (%rbx), %r14d
testl %r14d, %r14d
sete %bl
orb %cl, %bl
jne 0xb984
imull 0x8(%rsp), %r14d
movl %edi, %ebx
subl %r14d, %ebx
movl %eax, %r14d
movq %rsi, %r15
movl -0xc(%r15), %ecx
movl %ecx, %ebp
imull %ebx, %ebp
movslq %ebp, %r12
addq %r11, %r12
movq -0x24(%r15), %r13
movq -0x1c(%r15), %rbp
subq %r13, %rbp
sarq %rbp
cmpq %r12, %rbp
leaq (%r13,%r12,2), %r12
cmovbeq %r15, %r12
imull %edi, %ecx
movslq %ecx, %rcx
addq %r11, %rcx
cmpq %rcx, %rbp
leaq (%r13,%rcx,2), %rcx
cmovbeq %r15, %rcx
movzwl (%r12), %ebp
movw %bp, (%rcx)
addq $0x40, %r15
decl %r14d
jne 0xb935
incq %r11
cmpq 0x10(%rsp), %r11
jne 0xb8fb
incl %edi
cmpl 0xc(%rsp), %edi
jne 0xb8d0
movq 0x18(%rsp), %r15
movq 0x20(%rsp), %r8
decl %r8d
movl %r8d, 0x6c(%r15)
movq (%r15), %rsi
leaq 0x40(%rsi), %rdx
movq %r15, %rdi
callq 0xfcc2
movb $0x1, %bl
jmp 0xb44a