// File: /workspace/github_cpp_makefile_star2_O3/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 $0x78, %rsp
movl %edx, %r13d
movq (%r9), %rax
movq 0x8(%r9), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
xorl %ebx, %ebx
movq %rdx, 0x68(%rsp)
cmpq $0x2, %rdx
jb 0xbf80
movq %r9, %r10
movl %r8d, %r14d
movl %esi, %ebp
movq %rax, 0x60(%rsp)
movl $0x0, 0x4(%rsp)
movq (%rdi), %rax
movslq %esi, %rdx
shlq $0x6, %rdx
movl 0x18(%rax,%rdx), %esi
movl 0x1c(%rax,%rdx), %eax
leal 0x1(%rcx), %edx
movl %edx, 0x48(%rsp)
movl %ecx, %edx
notl %edx
movl %edx, 0x24(%rsp)
movl %esi, %edx
subl %ecx, %edx
movl %edx, 0x44(%rsp)
movl %eax, 0x40(%rsp)
subl %r8d, %eax
movl %eax, 0x20(%rsp)
movl %ecx, %eax
movl %esi, 0x4c(%rsp)
subl %esi, %eax
movl %eax, 0x50(%rsp)
movl $0x64, %eax
movq %rax, 0x28(%rsp)
movl $0x1, %r9d
movq %r14, 0x58(%rsp)
movq %rcx, 0x8(%rsp)
movq %rdi, 0x38(%rsp)
movq %rdi, %r15
movl %ebp, %esi
movl %r13d, %edx
movl %r14d, %r8d
movq %r9, 0x18(%rsp)
pushq $0x1
movq %r10, %rbx
pushq %r10
callq 0xbb03
addq $0x10, %rsp
testb %al, %al
je 0xbedc
cmpl %r14d, 0x40(%rsp)
jle 0xbf39
xorl %eax, %eax
movl $0x0, 0x14(%rsp)
movq 0x8(%rsp), %rcx
movq %rbx, %r10
movq %r15, %rdi
movq 0x18(%rsp), %r9
movq %rax, 0x30(%rsp)
leal (%rax,%r14), %r12d
testl %ecx, %ecx
js 0xbdd4
xorl %r14d, %r14d
leal (%rcx,%r14), %ebx
movl %ebp, %esi
movl %r13d, %edx
movl %ebx, %ecx
movl %r12d, %r8d
pushq $0x1
pushq %r10
movq %r10, %r15
callq 0xbb03
addq $0x10, %rsp
testb %al, %al
je 0xbddb
movq 0x38(%rsp), %rdi
movl %ebp, %esi
movl %r13d, %edx
movl %ebx, %ecx
movl %r12d, %r8d
movl $0x1, %r9d
pushq $0x1
pushq %r15
callq 0xbb03
addq $0x10, %rsp
xorb $0x1, %al
movzbl %al, %eax
addl %eax, 0x14(%rsp)
decl %r14d
cmpl 0x24(%rsp), %r14d
movq 0x8(%rsp), %rcx
movq %r15, %r10
movq 0x38(%rsp), %rdi
movq 0x18(%rsp), %r9
jg 0xbd63
movl 0x24(%rsp), %eax
movl %eax, %edx
jmp 0xbdf0
movl $0x1, %edx
jmp 0xbdf2
movq 0x8(%rsp), %rcx
movq %r15, %r10
movq 0x38(%rsp), %rdi
movq 0x18(%rsp), %r9
movq %r14, %rdx
incl %edx
xorl %ebx, %ebx
movl 0x48(%rsp), %eax
cmpl 0x4c(%rsp), %eax
jge 0xbe8d
movq %rdx, 0x70(%rsp)
leal (%rdx,%rcx), %eax
movl %eax, 0x54(%rsp)
movl $0x1, %ebx
addl %ebx, %ecx
movq %rdi, %r15
movl %ebp, %esi
movl %r13d, %edx
movl %r12d, %r8d
pushq $0x1
pushq %r10
movl %ebp, %r14d
movl %r13d, %ebp
movq %r10, %r13
callq 0xbb03
addq $0x10, %rsp
testb %al, %al
je 0xbe94
movq %r15, %rdi
movl %r14d, %esi
movl %ebp, %edx
movl 0x54(%rsp), %ecx
movl %r12d, %r8d
movl $0x1, %r9d
pushq $0x1
pushq %r13
callq 0xbb03
addq $0x10, %rsp
xorb $0x1, %al
movzbl %al, %eax
addl %eax, 0x14(%rsp)
incl %ebx
movl 0x50(%rsp), %eax
addl %ebx, %eax
movq 0x8(%rsp), %rcx
movq %r13, %r10
movl %ebp, %r13d
movl %r14d, %ebp
movq %r15, %rdi
movq 0x18(%rsp), %r9
jne 0xbe13
movl 0x44(%rsp), %eax
movl %eax, %ebx
jmp 0xbeaa
movq 0x58(%rsp), %r14
jmp 0xbeb6
movq 0x8(%rsp), %rcx
movq %r13, %r10
movl %ebp, %r13d
movl %r14d, %ebp
movq %r15, %rdi
movq 0x18(%rsp), %r9
decl %ebx
movq 0x58(%rsp), %r14
movq 0x70(%rsp), %rdx
movl %edx, %eax
orl %ebx, %eax
je 0xbefa
subl %edx, %ebx
cmpl $0x8, %ebx
jl 0xbefa
movq 0x30(%rsp), %rax
incl %eax
cmpl 0x20(%rsp), %eax
jne 0xbd53
movl 0x20(%rsp), %eax
movl %eax, %esi
jmp 0xbeff
movl 0x4(%rsp), %eax
movq 0x28(%rsp), %rcx
movl %ecx, %r12d
movq 0x8(%rsp), %rcx
movq %rbx, %r10
movq %r15, %rdi
movq 0x18(%rsp), %r9
jmp 0xbf1d
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rdx
movl 0x14(%rsp), %r12d
cmpl %edx, %r12d
movl 0x4(%rsp), %eax
cmovgl %r9d, %eax
cmovlel %edx, %r12d
cmpl $0x7, %esi
jb 0xbf3e
incq %r9
movl %r12d, %edx
movq %rdx, 0x28(%rsp)
movl %eax, 0x4(%rsp)
cmpq 0x68(%rsp), %r9
jne 0xbd06
jmp 0xbf3e
movq 0x8(%rsp), %rcx
movl 0x4(%rsp), %ebx
testl %ebx, %ebx
je 0xbf7e
movslq %ebx, %rax
movq 0x60(%rsp), %rdx
movl (%rdx,%rax,8), %r9d
movl 0x4(%rdx,%rax,8), %r10d
leaq 0x19f1a(%rip), %rsi     # 0x25e78
movl $0x6, %edi
movl %ecx, %edx
movl %r14d, %ecx
movl %ebx, %r8d
xorl %eax, %eax
pushq 0x28(%rsp)
pushq %r10
callq 0x1cede
addq $0x10, %rsp
jmp 0xbf80
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq