// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/ecovlp.cpp
// Function: adjust_exact_match(asg16_v*, long, long, long, long, unsigned long*, unsigned long*, unsigned long*, unsigned long*, unsigned int)
uint32_t adjust_exact_match(asg16_v *in, int64_t xs0, int64_t xe0, int64_t ys0, int64_t ye0, uint64_t *rxs, uint64_t *rxe, uint64_t *rys, uint64_t *rye, uint32_t rev)
{
    *rxs = *rxe = *rys = *rye = 0; 
    if((xe0 <= xs0) || (ye0 <= ys0)) return 0;
    int64_t xk, yk, ck, cn = in->n, wx[2], wy[2], os, oe; uint16_t op, bq, bt; uint32_t cl; uint64_t ovlp;
    xk = yk = 0;

    // fprintf(stderr, "[M::%s]\tx0::[%ld,%ld)\ty0::[%ld,%ld)\trev::%u\n", __func__, xs0, xe0, ys0, ye0, rev);
    
    if(!rev) {
        ck = 0;
        while (ck < cn && xk < xe0) {
            wx[0] = xk; wy[0] = yk; 
            // ck = pop_trace_bp(in, ck, &op, &b, &cl);
            ck = pop_trace_bp_f(in, ck, &op, &bq, &bt, &cl);
            if(op != 2) xk += cl;
            if(op != 3) yk += cl;
            wx[1] = xk; wy[1] = yk;  
            // fprintf(stderr, "[M::%s]\told::[%ld,%ld]\tnew::[%ld,%ld]\t%u%c\n", __func__, wx[0], wx[1], wy[0], wy[1], cl, "MSID"[op]);
            if(op == 0) {
                os = MAX(xs0, wx[0]); oe = MIN(xe0, wx[1]);
                ovlp = ((oe>os)? (oe-os):0);
                if((ovlp > 0) && (ovlp > (*rxe) - (*rxs))) {
                    // fprintf(stderr, "[M::%s]\to::[%ld,%ld)\n", __func__, os, oe);
                    (*rxs) = wy[0] + os - wx[0]; 
                    (*rxe) = wy[0] + oe - wx[0]; 

                    (*rys) = ys0 + os - xs0; 
                    (*rye) = ys0 + oe - xs0; 
                }
            }
            // if((op == 0) && (wx[0] <= s) && (wx[1] >= e)) {
            //     (*rs) = wy[0] + s - wx[0]; 
            //     (*re) = wy[0] + e - wx[0]; 
            // }
        }
    } else {
        ck = cn - 1;
        while (ck >= 0 && xk < xe0) {
            wx[0] = xk; wy[0] = yk; 
            // ck = pop_trace_bp_rev(in, ck, &op, &b, &cl);
            ck = pop_trace_bp_rev_f(in, ck, &op, &bq, &bt, &cl);
            if(op != 2) xk += cl;
            if(op != 3) yk += cl;
            wx[1] = xk; wy[1] = yk;  
            if(op == 0) {
                os = MAX(xs0, wx[0]); oe = MIN(xe0, wx[1]);
                ovlp = ((oe>os)? (oe-os):0);
                if((ovlp > 0) && (ovlp > (*rxe) - (*rxs))) {
                    (*rxs) = wy[0] + os - wx[0]; 
                    (*rxe) = wy[0] + oe - wx[0]; 

                    (*rys) = ys0 + os - xs0; 
                    (*rye) = ys0 + oe - xs0; 
                }
            }
            // if((op == 0) && (wx[0] <= s) && (wx[1] >= e)) {
            //     (*rs) = wy[0] + s - wx[0]; 
            //     (*re) = wy[0] + e - wx[0]; 
            // }
        }
    }
    

    return (*rxe) - (*rxs);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq 0x70(%rsp), %rax
andq $0x0, (%rax)
movq 0x68(%rsp), %rax
andq $0x0, (%rax)
movq 0x60(%rsp), %rax
andq $0x0, (%rax)
movq %rdx, %r15
movq %r9, 0x18(%rsp)
andq $0x0, (%r9)
movq %rsi, %r12
cmpq %rsi, %rdx
setle %sil
cmpq %rcx, %r8
setle %dl
xorl %eax, %eax
orb %sil, %dl
jne 0x14502b
movq %rdi, %r13
movq (%rdi), %rbp
cmpl $0x0, 0x78(%rsp)
je 0x144f3a
decq %rbp
subq %r12, %rcx
movq %rcx, 0x20(%rsp)
xorl %eax, %eax
xorl %ecx, %ecx
testq %rbp, %rbp
js 0x14501d
movq %rax, %rbx
cmpq %r15, %rax
jge 0x14501d
movq %rcx, %r14
movq %r13, %rdi
movq %rbp, %rsi
leaq 0xe(%rsp), %rdx
leaq 0x16(%rsp), %rcx
leaq 0x14(%rsp), %r8
leaq 0x10(%rsp), %r9
callq 0x14f7bb
movq %rax, %rbp
movzwl 0xe(%rsp), %esi
movl 0x10(%rsp), %edi
leaq (%rbx,%rdi), %rdx
cmpw $0x2, %si
movq %rdx, %rax
cmoveq %rbx, %rax
cmpw $0x3, %si
movl $0x0, %ecx
cmovneq %rdi, %rcx
addq %r14, %rcx
testw %si, %si
jne 0x144e64
cmpq %r12, %rbx
movq %r12, %rsi
cmovgq %rbx, %rsi
cmpq %r15, %rdx
cmovgeq %r15, %rdx
movq %rdx, %rdi
subq %rsi, %rdi
jle 0x144e64
movq 0x60(%rsp), %r8
movq (%r8), %r8
movq 0x18(%rsp), %r9
subq (%r9), %r8
cmpq %r8, %rdi
jbe 0x144e64
movq 0x20(%rsp), %r9
leaq (%r9,%rsi), %rdi
subq %rbx, %rsi
addq %r14, %rsi
movq 0x18(%rsp), %r8
movq %rsi, (%r8)
subq %rbx, %r14
addq %rdx, %r14
movq 0x60(%rsp), %rsi
movq %r14, (%rsi)
movq 0x68(%rsp), %rsi
movq %rdi, (%rsi)
addq %r9, %rdx
movq 0x70(%rsp), %rsi
movq %rdx, (%rsi)
jmp 0x144e64
subq %r12, %rcx
movq %rcx, 0x20(%rsp)
xorl %ecx, %ecx
xorl %edx, %edx
xorl %eax, %eax
movq %rcx, %rbx
movl %eax, %ecx
cmpq %rcx, %rbp
jle 0x14501d
cmpq %r15, %rbx
jge 0x14501d
movq %rdx, %r14
movq %r13, %rdi
movl %eax, %esi
leaq 0xe(%rsp), %rdx
leaq 0x16(%rsp), %rcx
leaq 0x14(%rsp), %r8
leaq 0x10(%rsp), %r9
callq 0xeeaa9
movzwl 0xe(%rsp), %edi
movl 0x10(%rsp), %r8d
leaq (%rbx,%r8), %rsi
cmpw $0x2, %di
movq %rsi, %rcx
cmoveq %rbx, %rcx
cmpw $0x3, %di
movl $0x0, %edx
cmovneq %r8, %rdx
addq %r14, %rdx
testw %di, %di
jne 0x144f48
cmpq %r12, %rbx
movq %r12, %rdi
cmovgq %rbx, %rdi
cmpq %r15, %rsi
cmovgeq %r15, %rsi
movq %rsi, %r8
subq %rdi, %r8
jle 0x144f48
movq 0x60(%rsp), %r9
movq (%r9), %r9
movq 0x18(%rsp), %r10
subq (%r10), %r9
cmpq %r9, %r8
jbe 0x144f48
movq 0x20(%rsp), %r10
leaq (%r10,%rdi), %r8
subq %rbx, %rdi
addq %r14, %rdi
movq 0x18(%rsp), %r9
movq %rdi, (%r9)
subq %rbx, %r14
addq %rsi, %r14
movq 0x60(%rsp), %rdi
movq %r14, (%rdi)
movq 0x68(%rsp), %rdi
movq %r8, (%rdi)
addq %r10, %rsi
movq 0x70(%rsp), %rdi
movq %rsi, (%rdi)
jmp 0x144f48
movq 0x60(%rsp), %rax
movl (%rax), %eax
movq 0x18(%rsp), %rcx
subl (%rcx), %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq