// File: /workspace/github_cpp_makefile_star2_O2/cloudinary[P]fuif/transform/2dmatch.h
// Function: find_good_match(Image&, int, int, int, int, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int>>>&)
pixel_type find_good_match(Image &img, int c0, int cn, int x, int y, offsets &ot) {
    int max_count=100;   // this is the minimal match size (number of nontrivial matching pixels) to consider
    int best_z=0;
    int w = img.channel[c0].w;
    int h = img.channel[c0].h;
    for (int z=1; z<ot.size(); z++) {
            if (!matches(img,c0,cn,x,y,z,ot,true)) continue;
            int count=0;
            int yy=0;
            for (; y+yy<h; yy++) {
                int xl=0;
                for (; x+xl>=0; xl--) {
                    // if it matches                              // and if it's a nontrivial pixel (not equal to its left neighbor)
                    if (matches(img,c0,cn,x+xl,y+yy,z,ot,true)) { if (!matches(img,c0,cn,x+xl,y+yy,1,ot,true)) count++; }       // then count it
                    else break;
                }
                xl++;
                int xr=1;
                for (; x+xr<w; xr++) {
                    if (matches(img,c0,cn,x+xr,y+yy,z,ot,true)) { if (!matches(img,c0,cn,x+xl,y+yy,1,ot,true)) count++; }
                    else break;
                }
                xr--;
                if (!xl && !xr) break;
                if (xr-xl < 8) break; // minimum 8 pixels wide
            }
            if (yy < 7) break; //minimum 8 pixels high
            if (count>max_count) { max_count=count; best_z=z; }
    }
    if (best_z) v_printf(6,"Found good match at %i,%i with offset %i (x+=%i,y+=%i) and count %i\n",x,y,best_z,ot[best_z].first,ot[best_z].second,max_count);
    return best_z;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, %r10
movq %r8, 0x20(%rsp)
movq %rcx, 0x10(%rsp)
movslq %esi, %rax
movq (%rdi), %rcx
shlq $0x6, %rax
movl 0x18(%rcx,%rax), %r8d
movl %r8d, 0x34(%rsp)
movl 0x1c(%rcx,%rax), %eax
movl %eax, 0x28(%rsp)
pushq $0x64
popq %r11
movl $0x0, (%rsp)
pushq $0x1
popq %r9
movq %r10, 0x8(%rsp)
movq 0x8(%r10), %rax
subq (%r10), %rax
sarq $0x3, %rax
cmpq %r9, %rax
jbe 0x8fe7
movq %r11, 0x38(%rsp)
movq %rdi, %r12
movl %esi, %r15d
movl %edx, %ebp
movq 0x10(%rsp), %rcx
movq 0x20(%rsp), %r8
movq %r9, 0x18(%rsp)
pushq $0x1
pushq %r10
callq 0x8c27
popq %rcx
popq %rdx
testb %al, %al
je 0x8f96
movl $0x0, 0x4(%rsp)
xorl %r8d, %r8d
movq 0x8(%rsp), %r10
movl %ebp, %edx
movl %r15d, %esi
movq %r12, %rdi
movq 0x20(%rsp), %rax
leal (%r8,%rax), %r14d
cmpl 0x28(%rsp), %r14d
jge 0x8fb7
movq %r8, 0x40(%rsp)
xorl %eax, %eax
movq %rax, %r13
movl %eax, %ebx
addl 0x10(%rsp), %ebx
js 0x8ee3
movq %r12, %rdi
movl %r15d, %esi
movl %ebp, %edx
movl %ebx, %ecx
movl %r14d, %r8d
movq 0x18(%rsp), %r9
pushq $0x1
pushq %r10
callq 0x8c27
movq %r12, %rdi
movl %r15d, %esi
movl %ebp, %edx
movq 0x18(%rsp), %r10
addq $0x10, %rsp
testb %al, %al
je 0x8ee3
movq %r12, %rdi
movl %r15d, %esi
movl %ebp, %edx
movl %ebx, %ecx
movl %r14d, %r8d
pushq $0x1
popq %r9
pushq $0x1
pushq %r10
callq 0x8c27
movq %r12, %rdi
movl %r15d, %esi
movl %ebp, %edx
movq 0x18(%rsp), %r10
addq $0x10, %rsp
xorb $0x1, %al
movzbl %al, %eax
addl %eax, 0x4(%rsp)
movq %r13, %rax
decl %eax
jmp 0x8e6d
leal 0x1(%r13), %eax
movl %eax, 0x2c(%rsp)
movq 0x10(%rsp), %rax
addl %r13d, %eax
incl %eax
movl %eax, 0x30(%rsp)
pushq $-0x1
popq %rax
movl %eax, %ebx
movq 0x10(%rsp), %rax
leal (%rax,%rbx), %ecx
addl $0x2, %ecx
cmpl 0x34(%rsp), %ecx
jge 0x8f77
movq %r12, %rdi
movl %r15d, %esi
movl %ebp, %edx
movl %r14d, %r8d
movq 0x18(%rsp), %r9
pushq $0x1
pushq %r10
callq 0x8c27
movq %r12, %rdi
movl %r15d, %esi
movl %ebp, %edx
movq 0x18(%rsp), %r10
addq $0x10, %rsp
testb %al, %al
je 0x8f77
movq %r12, %rdi
movl %r15d, %esi
movl %ebp, %edx
movl 0x30(%rsp), %ecx
movl %r14d, %r8d
pushq $0x1
popq %r9
pushq $0x1
pushq %r10
callq 0x8c27
movq %r12, %rdi
movl %r15d, %esi
movl %ebp, %edx
movq 0x18(%rsp), %r10
addq $0x10, %rsp
xorb $0x1, %al
movzbl %al, %eax
addl %eax, 0x4(%rsp)
incl %ebx
jmp 0x8efe
movl %ebx, %eax
notl %eax
orl %eax, 0x2c(%rsp)
je 0x8fb2
subl %r13d, %ebx
cmpl $0x8, %ebx
movq 0x40(%rsp), %r8
jl 0x8fb7
incl %r8d
jmp 0x8e52
movl (%rsp), %eax
movq 0x38(%rsp), %rcx
movq 0x8(%rsp), %r10
movl %ebp, %edx
movl %r15d, %esi
movq %r12, %rdi
movq 0x18(%rsp), %r9
jmp 0x8fd9
movq 0x40(%rsp), %r8
movq 0x38(%rsp), %r11
movl 0x4(%rsp), %ecx
cmpl %r11d, %ecx
movl (%rsp), %eax
movq 0x18(%rsp), %r9
cmovgl %r9d, %eax
cmovlel %r11d, %ecx
cmpl $0x7, %r8d
jb 0x8fe7
incq %r9
movl %eax, (%rsp)
movl %ecx, %r11d
jmp 0x8df7
movl (%rsp), %ebx
testl %ebx, %ebx
je 0x9021
movslq %ebx, %rax
movq (%r10), %rcx
movl (%rcx,%rax,8), %r9d
movl 0x4(%rcx,%rax,8), %r10d
leaq 0x14df4(%rip), %rsi     # 0x1ddf8
pushq $0x6
popq %rdi
movq 0x10(%rsp), %rdx
movq 0x20(%rsp), %rcx
movl %ebx, %r8d
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x16c30
popq %rax
popq %rcx
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq