// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/ecovlp.cpp
// Function: quick_exact_match(ma_hit_t*, All_reads*, UC_Read*, UC_Read*, cc_v*)
uint32_t quick_exact_match(ma_hit_t *z, All_reads *rref, UC_Read* qu, UC_Read* tu, cc_v *sc)
{
    uint64_t rts, rte, rqs, rqe, f = 0; int64_t ql, tl, qr, tr, qs, qe, ts, te;

    // fprintf(stderr, "-0-[M::%s]\tf::%lu\n", __func__, f);
    if(adjust_exact_match(&(sc->a[z->tn]), z->ts, z->te, ((uint32_t)(z->qns)), z->qe, &rts, &rte, &rqs, &rqe, z->rev)) {
        z->ts = rts; z->te = rte; f = 1;
        z->qns >>= 32; z->qns <<= 32; z->qns |= ((uint64_t)(rqs)); z->qe = rqe;

        ///debug
        // qs = rqs; qe = rqe; ts = rts; te = rte; 
        // resize_UC_Read(tu, te - ts); 
        // recover_UC_Read_sub_region(tu->seq, ts, te - ts, z->rev, rref, z->tn);
        
        // fprintf(stderr, "[M::%s]\trq::[%lu,%lu)\trt::[%lu,%lu)\n", __func__, rqs, rqe, rts, rte);
        // fprintf(stderr, "-0-[M::%s] qstr::%.*s\n", __func__, ((int)(qe - qs)), qu->seq + qs);
        // fprintf(stderr, "-0-[M::%s] tstr::%.*s\n", __func__, ((int)(te - ts)), tu->seq);

        // if(memcmp(qu->seq + qs, tu->seq, qe - qs) == 0) {
        //     fprintf(stderr, "-0-[M::%s]\tsb\n", __func__);
        // } else {
        //     fprintf(stderr, "-1-[M::%s]\tsa\n", __func__);
        // }
    }
    // fprintf(stderr, "-1-[M::%s]\tf::%lu\n", __func__, f);
    ql = qu->length; tl = Get_READ_LENGTH((*rref), z->tn);
    qs = ((uint32_t)(z->qns)); qe = z->qe; ts = z->ts; te = z->te;
    if(qs >= ql) qs = ql; if(qe > ql) qe = ql; if(qe <= qs) f = 0;
    // fprintf(stderr, "-2-[M::%s]\tf::%lu\n", __func__, f);
    if(ts >= tl) ts = tl; if(te > tl) te = tl; if(te <= ts) f = 0;
    // fprintf(stderr, "-3-[M::%s]\tf::%lu\n", __func__, f);
    if((qe - qs) != (te - ts)) f = 0;
    // fprintf(stderr, "-4-[M::%s]\tf::%lu\n", __func__, f);

    if(qs <= ts) {
        ts -= qs; qs = 0;
    } else {
        qs -= ts; ts = 0;
    }


    qr = ql - qe; tr = tl - te;
    if(qr <= tr) {
        qe = ql; te += qr;        
    } else {
        te = tl; qe += tr; 
    }

    // fprintf(stderr, "-5-[M::%s]\tzq::[%ld,\t%ld)\tzt::[%ld,\t%ld)\teq::[%u,\t%u)\tet::[%u,\t%u)\tql::%ld\ttl::%ld\tf::%lu\n", 
    // __func__, qs, qe, ts, te, ((uint32_t)(z->qns)), z->qe, z->ts, z->te, ql, tl, f);

    z->qns >>= 32; z->qns <<= 32; z->qns |= ((uint64_t)(qs)); z->qe = qe;
    z->ts = ts; z->te = te;

    if((f) && ((te - ts) == (qe - qs)) && (qe > qs)) {
        resize_UC_Read(tu, te - ts); 
        recover_UC_Read_sub_region(tu->seq, ts, te - ts, z->rev, rref, z->tn);

        // fprintf(stderr, "[M::%s] qstr::%.*s\n", __func__, ((int)(qe - qs)), qu->seq + qs);
        // fprintf(stderr, "[M::%s] tstr::%.*s\n", __func__, ((int)(te - ts)), tu->seq);

        if(memcmp(qu->seq + qs, tu->seq, qe - qs) == 0) return 1;
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rcx, 0x28(%rsp)
movq %rdx, 0x18(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, %r13
movl 0xc(%rdi), %eax
movq %rax, 0x8(%rsp)
leaq (%rax,%rax,2), %rdi
shlq $0x3, %rdi
addq 0x10(%r8), %rdi
movl 0x10(%r13), %ebx
movl 0x14(%r13), %r14d
movq (%r13), %rbp
movl %ebp, %r15d
movl 0x8(%r13), %r12d
movl 0x18(%r13), %r11d
shrl $0x1f, %r11d
leaq 0x38(%rsp), %rax
leaq 0x40(%rsp), %r10
leaq 0x50(%rsp), %r9
movq %rbx, %rsi
movq %r14, %rdx
movq %r15, %rcx
movq %r12, %r8
movq %r11, 0x20(%rsp)
pushq %r11
pushq %rax
pushq %r10
leaq 0x60(%rsp), %rax
pushq %rax
callq 0x1896e6
addq $0x20, %rsp
testl %eax, %eax
je 0x189c19
movl 0x50(%rsp), %r11d
movl 0x48(%rsp), %r14d
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
andq %rcx, %rbp
movq 0x40(%rsp), %rcx
orq %rcx, %rbp
movq %rbp, (%r13)
movl 0x38(%rsp), %r12d
movl %ecx, %r15d
jmp 0x189c1c
movq %rbx, %r11
movq 0x18(%rsp), %rcx
movq 0x8(%rcx), %r8
movq 0x10(%rsp), %rcx
movq 0x18(%rcx), %rcx
movq 0x8(%rsp), %rdx
movq (%rcx,%rdx,8), %rdi
cmpq %r8, %r15
movq %r8, %rcx
movq %r15, 0x30(%rsp)
cmovlq %r15, %rcx
cmpq %r11, %rdi
movq %r11, %r9
cmovlq %rdi, %r9
movq %rcx, %rbx
subq %r9, %rbx
negq %rbx
xorl %esi, %esi
movq %rcx, %r15
subq %r9, %r15
cmovleq %rsi, %r15
cmovgq %rsi, %rbx
cmpq %r12, %r8
cmovlq %r8, %r12
cmpq %r14, %rdi
cmovlq %rdi, %r14
movq %r8, %r10
subq %r12, %r10
movq %rdi, %rsi
subq %r14, %rsi
cmpq %rsi, %r10
leaq (%rsi,%r12), %rsi
cmovleq %r8, %rsi
leaq (%r14,%r10), %rdx
cmovgq %rdi, %rdx
movabsq $-0x100000000, %rdi     # imm = 0xFFFFFFFF00000000
andq %rdi, %rbp
orq %r15, %rbp
movq %rbp, (%r13)
movl %esi, 0x8(%r13)
movl %ebx, 0x10(%r13)
movl %edx, 0x14(%r13)
testl %eax, %eax
je 0x189d53
movq %r12, %rax
subq %rcx, %rax
movq %r14, %rcx
subq %r9, %rcx
cmpq %rcx, %rax
jne 0x189d53
cmpq %r11, %r14
jle 0x189d53
cmpq %r12, 0x30(%rsp)
jge 0x189d53
subq %r15, %rsi
jle 0x189d53
subq %rbx, %rdx
cmpq %rsi, %rdx
jne 0x189d53
movq 0x28(%rsp), %r14
movq (%r14), %rdi
cmpq %rdx, 0x10(%r14)
jge 0x189d1f
movq %rdx, %rsi
movq %rdx, %r12
callq 0x2370
movq %r12, %rdx
movq %rax, %rdi
movq %rax, (%r14)
movq %r12, 0x10(%r14)
movl 0xc(%r13), %r9d
movl 0x18(%r13), %ecx
shrl $0x1f, %ecx
jmp 0x189d29
movq 0x20(%rsp), %rcx
movq 0x8(%rsp), %r9
movq %rbx, %rsi
movq %rdx, %rbx
movq 0x10(%rsp), %r8
callq 0x32ef8
movq 0x18(%rsp), %rax
addq (%rax), %r15
movq (%r14), %rsi
movq %r15, %rdi
movq %rbx, %rdx
callq 0x21a0
testl %eax, %eax
je 0x189d64
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %eax
jmp 0x189d55