// File: /workspace/github_cpp_makefile_star2_O3/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
movq %rdi, %r12
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %r11
movq 0x38(%rsp), %r10
xorl %eax, %eax
movq %rax, (%rdi)
movq %rax, (%r11)
movq %rax, (%r10)
movq %rax, (%r9)
movq %rsi, -0x18(%rsp)
cmpq %rsi, %rdx
setle %dil
movq %rcx, -0x20(%rsp)
cmpq %rcx, %r8
setle %sil
orb %dil, %sil
jne 0x189b5a
movq %r9, -0x10(%rsp)
movq (%r12), %rax
testq %rax, %rax
setg %sil
testq %rdx, %rdx
setg %cl
andb %sil, %cl
cmpl $0x0, 0x50(%rsp)
je 0x189946
testb %cl, %cl
je 0x189b4c
decq %rax
movq 0x10(%r12), %rdi
movq -0x18(%rsp), %rcx
subq %rcx, -0x20(%rsp)
xorl %r14d, %r14d
movw $0xffff, %r8w           # imm = 0xFFFF
xorl %r11d, %r11d
xorl %r13d, %r13d
movzwl (%rdi,%rax,2), %esi
movl %esi, %ebx
shrl $0xe, %ebx
movw %r8w, -0x2c(%rsp)
movw %r8w, -0x2a(%rsp)
leal -0x2(%rbx), %ecx
movzwl %si, %ebp
cmpl $0x2, %ecx
jae 0x1897ad
movw $0xfff, %r9w            # imm = 0xFFF
movw $0xc, %cx
leaq -0x2c(%rsp), %r10
jmp 0x1897d1
movw $0x3fff, %r9w           # imm = 0x3FFF
testl %ebx, %ebx
je 0x1897df
movl %esi, %ecx
shrl $0xc, %ecx
andl $0x3, %ecx
movw %cx, -0x2c(%rsp)
movw $0x3ff, %r9w            # imm = 0x3FF
movw $0xa, %cx
leaq -0x2a(%rsp), %r10
movl %ebp, %r15d
shrl %cl, %r15d
andl $0x3, %r15d
movw %r15w, (%r10)
movzwl %r9w, %ecx
andl %esi, %ecx
leaq -0x1(%rax), %r10
testq %rax, %rax
jle 0x189885
movzwl -0x2a(%rsp), %eax
movzwl -0x2c(%rsp), %r9d
movzwl (%rdi,%r10,2), %esi
movl %esi, %r15d
shrl $0xe, %r15d
cmpl %ebx, %r15d
jne 0x189885
movzwl %bx, %r15d
leal -0x2(%r15), %r12d
cmpl $0x2, %r12d
jae 0x18982f
movl %esi, %r12d
shrl $0xc, %r12d
andl $0x3, %r12d
andl $0xfff, %esi            # imm = 0xFFF
jmp 0x18983f
testl %r15d, %r15d
jne 0x189847
andl $0x3fff, %esi           # imm = 0x3FFF
movw $0xffff, %r12w          # imm = 0xFFFF
movl $0xffff, %r15d          # imm = 0xFFFF
jmp 0x189863
movl %esi, %r12d
shrl $0xc, %r12d
andl $0x3, %r12d
movl %esi, %r15d
shrl $0xa, %r15d
andl $0x3, %r15d
andl $0x3ff, %esi            # imm = 0x3FF
cmpl %eax, %r15d
jne 0x189885
cmpw %r12w, %r9w
jne 0x189885
addl %esi, %ecx
leaq -0x1(%r10), %rsi
testq %r10, %r10
movq %rsi, %r10
jg 0x1897fd
movq $-0x1, %rax
jmp 0x189888
movq %r10, %rax
movl %ecx, %ecx
cmpw $0x3, %bx
jne 0x1898a9
movzwl -0x2c(%rsp), %esi
movw %si, -0x2a(%rsp)
movw $0xffff, -0x2c(%rsp)    # imm = 0xFFFF
addq %rcx, %r13
jmp 0x189929
leaq (%rcx,%r13), %r9
cmpw $0x2, %bx
movq %r9, %rsi
cmoveq %r13, %rsi
addq %r11, %rcx
cmpl $0x3fff, %ebp           # imm = 0x3FFF
ja 0x189923
movq -0x18(%rsp), %r10
cmpq %r10, %r13
cmovleq %r10, %r13
cmpq %rdx, %r9
cmovgeq %rdx, %r9
movq %r9, %r10
subq %r13, %r10
jle 0x189923
movq 0x38(%rsp), %r12
movq (%r12), %rbx
movq -0x10(%rsp), %r15
subq (%r15), %rbx
cmpq %rbx, %r10
jbe 0x189923
leaq (%r14,%r13), %r10
addq %r11, %r10
movq %r10, (%r15)
addq %r14, %r11
addq %r9, %r11
movq %r11, (%r12)
movq -0x20(%rsp), %r10
addq %r10, %r13
movq 0x40(%rsp), %r11
movq %r13, (%r11)
addq %r10, %r9
movq 0x48(%rsp), %r10
movq %r9, (%r10)
movq %rcx, %r11
movq %rsi, %r13
testq %rax, %rax
js 0x189b4c
movq %r13, %r14
negq %r14
cmpq %rdx, %r13
jl 0x18977d
jmp 0x189b4c
testb %cl, %cl
je 0x189b4c
movq 0x10(%r12), %r8
movq -0x18(%rsp), %rcx
subq %rcx, -0x20(%rsp)
xorl %r11d, %r11d
xorl %r13d, %r13d
movq $0x0, -0x28(%rsp)
xorl %r14d, %r14d
movq %rdx, -0x8(%rsp)
movzwl (%r8,%r13,2), %r9d
movl %r9d, %r15d
shrl $0xe, %r15d
movw $0xffff, %cx            # imm = 0xFFFF
movw %cx, -0x2c(%rsp)
movw %cx, -0x2a(%rsp)
leal -0x2(%r15), %ecx
movzwl %r9w, %edx
cmpl $0x2, %ecx
jae 0x1899aa
movw $0xfff, %si             # imm = 0xFFF
movw $0xc, %cx
leaq -0x2c(%rsp), %rdi
jmp 0x1899ce
movw $0x3fff, %si            # imm = 0x3FFF
testl %r15d, %r15d
je 0x1899dc
movl %r9d, %ecx
shrl $0xc, %ecx
andl $0x3, %ecx
movw %cx, -0x2c(%rsp)
movw $0x3ff, %si             # imm = 0x3FF
movw $0xa, %cx
leaq -0x2a(%rsp), %rdi
movl %edx, %r10d
shrl %cl, %r10d
andl $0x3, %r10d
movw %r10w, (%rdi)
movzwl %si, %ecx
andl %r9d, %ecx
movq %r12, %rbx
movq (%r12), %r10
incl %r13d
cmpq %r13, %r10
jbe 0x189a6e
movzwl -0x2a(%rsp), %ebp
movzwl -0x2c(%rsp), %r9d
movzwl (%r8,%r13,2), %r12d
movl %r12d, %esi
shrl $0xe, %esi
cmpl %r15d, %esi
jne 0x189a6e
movzwl %r15w, %esi
leal -0x2(%rsi), %edi
cmpl $0x2, %edi
jae 0x189a2a
movl %r12d, %esi
shrl $0xc, %esi
andl $0x3, %esi
andl $0xfff, %r12d           # imm = 0xFFF
jmp 0x189a39
testl %esi, %esi
jne 0x189a40
andl $0x3fff, %r12d          # imm = 0x3FFF
movw $0xffff, %si            # imm = 0xFFFF
movl $0xffff, %edi           # imm = 0xFFFF
jmp 0x189a59
movl %r12d, %esi
shrl $0xc, %esi
andl $0x3, %esi
movl %r12d, %edi
shrl $0xa, %edi
andl $0x3, %edi
andl $0x3ff, %r12d           # imm = 0x3FF
cmpl %ebp, %edi
jne 0x189a6e
cmpw %si, %r9w
jne 0x189a6e
addl %r12d, %ecx
incl %r13d
cmpq %r13, %r10
ja 0x1899fc
cmpw $0x3, %r15w
jne 0x189a93
movzwl -0x2c(%rsp), %esi
movw %si, -0x2a(%rsp)
movw $0xffff, -0x2c(%rsp)    # imm = 0xFFFF
movl %ecx, %ecx
addq %rcx, %r14
movq %rbx, %r12
jmp 0x189b30
movl %ecx, %r9d
leaq (%r14,%r9), %r10
cmpw $0x2, %r15w
movq %r10, %rcx
cmoveq %r14, %rcx
addq -0x28(%rsp), %r9
cmpl $0x3fff, %edx           # imm = 0x3FFF
movq %rbx, %r12
ja 0x189b28
movq -0x18(%rsp), %rsi
cmpq %rsi, %r14
cmovleq %rsi, %r14
movq -0x8(%rsp), %rdx
cmpq %rdx, %r10
cmovgeq %rdx, %r10
movq %r10, %rsi
subq %r14, %rsi
jle 0x189b1e
movq 0x38(%rsp), %r15
movq (%r15), %rdi
movq -0x10(%rsp), %rbx
subq (%rbx), %rdi
cmpq %rdi, %rsi
jbe 0x189b1e
leaq (%r14,%r11), %rsi
movq -0x28(%rsp), %rdi
addq %rdi, %rsi
movq %rsi, (%rbx)
addq %r11, %rdi
addq %r10, %rdi
movq %rdi, (%r15)
movq -0x20(%rsp), %rsi
addq %rsi, %r14
movq 0x40(%rsp), %rdi
movq %r14, (%rdi)
addq %rsi, %r10
movq 0x48(%rsp), %rsi
movq %r10, (%rsi)
movq %r9, -0x28(%rsp)
movq %rcx, %r14
jmp 0x189b35
movq %r9, -0x28(%rsp)
movq %rcx, %r14
movq -0x8(%rsp), %rdx
movl %r13d, %r13d
cmpq %r13, %rax
jle 0x189b4c
movq %r14, %r11
negq %r11
cmpq %rdx, %r14
jl 0x189974
movq 0x38(%rsp), %rax
movl (%rax), %eax
movq -0x10(%rsp), %rcx
subl (%rcx), %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq