// File: /workspace/github_cpp_makefile_star2_O1/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 0x48(%rsp), %rbx
movq 0x40(%rsp), %r11
movq 0x38(%rsp), %r10
xorl %eax, %eax
movq %rax, (%rbx)
movq %rax, (%r11)
movq %rax, (%r10)
movq %r9, -0x38(%rsp)
movq %rax, (%r9)
movq %rsi, -0x28(%rsp)
cmpq %rsi, %rdx
setle %sil
movq %rcx, -0x30(%rsp)
cmpq %rcx, %r8
setle %cl
orb %sil, %cl
jne 0x182568
movq %rdi, -0x20(%rsp)
movq (%rdi), %rsi
testq %rsi, %rsi
setg %cl
testq %rdx, %rdx
setg %al
andb %cl, %al
cmpl $0x0, 0x50(%rsp)
movq %rdx, -0x18(%rsp)
je 0x182389
testb %al, %al
je 0x18255a
decq %rsi
movq -0x20(%rsp), %rax
movq 0x10(%rax), %rdi
movq -0x28(%rsp), %rax
subq %rax, -0x30(%rsp)
xorl %r15d, %r15d
xorl %r14d, %r14d
xorl %r12d, %r12d
movzwl (%rdi,%rsi,2), %r10d
movl %r10d, %ebx
shrl $0xe, %ebx
leal -0x2(%rbx), %eax
cmpl $0x2, %eax
jae 0x18220f
movl %r10d, %r11d
shrl $0xc, %r11d
andl $0x3, %r11d
movl %r10d, %eax
andl $0xfff, %eax            # imm = 0xFFF
jmp 0x182220
testl %ebx, %ebx
jne 0x182227
movl %r10d, %eax
andl $0x3fff, %eax           # imm = 0x3FFF
movw $0xffff, %r11w          # imm = 0xFFFF
movl $0xffff, %ecx           # imm = 0xFFFF
jmp 0x182243
movl %r10d, %r11d
shrl $0xc, %r11d
andl $0x3, %r11d
movl %r10d, %ecx
shrl $0xa, %ecx
andl $0x3, %ecx
movl %r10d, %eax
andl $0x3ff, %eax            # imm = 0x3FF
movzwl %ax, %r9d
leaq -0x1(%rsi), %r13
testq %rsi, %rsi
jle 0x1822cc
movzwl %bx, %eax
leal -0x2(%rax), %edx
movzwl (%rdi,%r13,2), %esi
movl %esi, %r8d
shrl $0xe, %r8d
cmpl %ebx, %r8d
jne 0x1822cc
cmpl $0x2, %edx
jae 0x18227c
movl %esi, %ebp
shrl $0xc, %ebp
andl $0x3, %ebp
andl $0xfff, %esi            # imm = 0xFFF
jmp 0x18228a
testl %eax, %eax
jne 0x182292
andl $0x3fff, %esi           # imm = 0x3FFF
movw $0xffff, %bp            # imm = 0xFFFF
movl $0xffff, %r8d           # imm = 0xFFFF
jmp 0x1822ab
movl %esi, %ebp
shrl $0xc, %ebp
andl $0x3, %ebp
movl %esi, %r8d
shrl $0xa, %r8d
andl $0x3, %r8d
andl $0x3ff, %esi            # imm = 0x3FF
cmpl %ecx, %r8d
jne 0x1822cc
cmpw %bp, %r11w
jne 0x1822cc
addl %esi, %r9d
leaq -0x1(%r13), %rsi
testq %r13, %r13
movq %rsi, %r13
jg 0x182256
movq $-0x1, %r13
cmpw $0x2, %bx
movl %r9d, %ecx
movq %rcx, %rax
movl $0x0, %edx
cmoveq %rdx, %rax
addq %r12, %rax
cmpw $0x3, %bx
cmoveq %rdx, %rcx
cmpl $0x3fff, %r10d          # imm = 0x3FFF
movq -0x18(%rsp), %r10
ja 0x182363
movq -0x28(%rsp), %rdx
cmpq %rdx, %r12
cmovleq %rdx, %r12
cmpq %r10, %rax
movq %r10, %rdx
cmovlq %rax, %rdx
movq %rdx, %rsi
subq %r12, %rsi
jle 0x182363
movq 0x38(%rsp), %r8
movq (%r8), %r8
movq -0x38(%rsp), %r9
subq (%r9), %r8
cmpq %r8, %rsi
jbe 0x182363
leaq (%r12,%r15), %rsi
addq %r14, %rsi
movq -0x38(%rsp), %r8
movq %rsi, (%r8)
addq %r14, %r15
addq %rdx, %r15
movq 0x38(%rsp), %rsi
movq %r15, (%rsi)
movq -0x30(%rsp), %rsi
addq %rsi, %r12
movq 0x40(%rsp), %r8
movq %r12, (%r8)
addq %rsi, %rdx
movq 0x48(%rsp), %rsi
movq %rdx, (%rsi)
testq %r13, %r13
js 0x18255a
movq %r13, %rsi
addq %rcx, %r14
movq %rax, %r15
negq %r15
movq %rax, %r12
cmpq %r10, %rax
jl 0x1821e7
jmp 0x18255a
testb %al, %al
je 0x18255a
movq -0x20(%rsp), %rax
movq 0x10(%rax), %r8
movq -0x28(%rsp), %rax
subq %rax, -0x30(%rsp)
xorl %r13d, %r13d
xorl %r15d, %r15d
xorl %r11d, %r11d
xorl %ebp, %ebp
movq %rsi, -0x10(%rsp)
movzwl (%r8,%r15,2), %r14d
movl %r14d, %ebx
shrl $0xe, %ebx
leal -0x2(%rbx), %eax
cmpl $0x2, %eax
movq %r11, -0x8(%rsp)
jae 0x1823e1
movl %r14d, %r10d
shrl $0xc, %r10d
andl $0x3, %r10d
movl %r14d, %eax
andl $0xfff, %eax            # imm = 0xFFF
jmp 0x1823f2
testl %ebx, %ebx
jne 0x1823fa
movl %r14d, %eax
andl $0x3fff, %eax           # imm = 0x3FFF
movw $0xffff, %r10w          # imm = 0xFFFF
movl $0xffff, %r11d          # imm = 0xFFFF
jmp 0x182418
movl %r14d, %r10d
shrl $0xc, %r10d
andl $0x3, %r10d
movl %r14d, %r11d
shrl $0xa, %r11d
andl $0x3, %r11d
movl %r14d, %eax
andl $0x3ff, %eax            # imm = 0x3FF
movzwl %ax, %ecx
movq -0x20(%rsp), %rax
movq (%rax), %r9
incl %r15d
cmpq %r15, %r9
jbe 0x18249b
movzwl %bx, %eax
leal -0x2(%rax), %esi
movzwl (%r8,%r15,2), %edx
movl %edx, %edi
shrl $0xe, %edi
cmpl %ebx, %edi
jne 0x18249b
cmpl $0x2, %esi
jae 0x182457
movl %edx, %r12d
shrl $0xc, %r12d
andl $0x3, %r12d
andl $0xfff, %edx            # imm = 0xFFF
jmp 0x182466
testl %eax, %eax
jne 0x18246d
andl $0x3fff, %edx           # imm = 0x3FFF
movw $0xffff, %r12w          # imm = 0xFFFF
movl $0xffff, %edi           # imm = 0xFFFF
jmp 0x182486
movl %edx, %r12d
shrl $0xc, %r12d
andl $0x3, %r12d
movl %edx, %edi
shrl $0xa, %edi
andl $0x3, %edi
andl $0x3ff, %edx            # imm = 0x3FF
cmpl %r11d, %edi
jne 0x18249b
cmpw %r12w, %r10w
jne 0x18249b
addl %edx, %ecx
incl %r15d
cmpq %r15, %r9
ja 0x182431
cmpw $0x2, %bx
movl %ecx, %ecx
movq %rcx, %rax
movl $0x0, %edx
cmoveq %rdx, %rax
addq %rbp, %rax
cmpw $0x3, %bx
cmoveq %rdx, %rcx
cmpl $0x3fff, %r14d          # imm = 0x3FFF
movq -0x18(%rsp), %r10
movq -0x8(%rsp), %r11
ja 0x182538
movq -0x28(%rsp), %rdx
cmpq %rdx, %rbp
cmovleq %rdx, %rbp
cmpq %r10, %rax
movq %r10, %rdx
cmovlq %rax, %rdx
movq %rdx, %rsi
subq %rbp, %rsi
jle 0x182538
movq 0x38(%rsp), %rdi
movq (%rdi), %rdi
movq -0x38(%rsp), %r9
subq (%r9), %rdi
cmpq %rdi, %rsi
jbe 0x182538
movq %rbp, %rsi
addq %r13, %rsi
addq %r11, %rsi
movq -0x38(%rsp), %rdi
movq %rsi, (%rdi)
addq %r11, %r13
addq %rdx, %r13
movq 0x38(%rsp), %rsi
movq %r13, (%rsi)
movq -0x30(%rsp), %rsi
addq %rsi, %rbp
movq 0x40(%rsp), %rdi
movq %rbp, (%rdi)
addq %rsi, %rdx
movq 0x48(%rsp), %rsi
movq %rdx, (%rsi)
movl %r15d, %r15d
movq -0x10(%rsp), %rsi
cmpq %r15, %rsi
jle 0x18255a
addq %rcx, %r11
movq %rax, %r13
negq %r13
movq %rax, %rbp
cmpq %r10, %rax
jl 0x1823b4
movq 0x38(%rsp), %rax
movl (%rax), %eax
movq -0x38(%rsp), %rcx
subl (%rcx), %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq