// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: collect_hp_regions_back(overlap_region_alloc*, All_reads*, kvec_t_u32_warp*, kvec_t_u64_warp*, kvec_t_u8_warp*, float, _IO_FILE*)
int collect_hp_regions_back(overlap_region_alloc* olist, All_reads* R_INF, kvec_t_u32_warp* b, kvec_t_u64_warp* r, kvec_t_u8_warp* k_flag, float hp_rate, FILE* fp)
{
    int i, k, qs, qe, ava_k_mer = 0, hp_k_mer = 0, min_dp;
    // min_dp = RESEED_DP;
    // if(asm_opt.hom_cov > 0) min_dp = asm_opt.hom_cov * RESEED_PEAK_RATE;
    // if(min_dp > RESEED_DP) min_dp = RESEED_DP;
    min_dp = RESEED_DP;
    if(asm_opt.hom_cov > 0) min_dp = asm_opt.hom_cov * RESEED_PEAK_RATE;
    if(asm_opt.het_cov > 0) min_dp = asm_opt.het_cov * RESEED_PEAK_RATE;
    collect_no_cov_regions(olist, R_INF, b, r, min_dp, RESEED_LEN);

    for (i = 0; i < (int)r->a.n; i++)
    {
        ///[qs, qe]
        qs = r->a.a[i]>>32;
        qe = (r->a.a[i]<<32)>>32;
        
        for (k = qs; k <= qe; k++)
        {
            if(k_flag->a.a[k] > 1) ava_k_mer++;
            if(k_flag->a.a[k] > 2) hp_k_mer++;
        }

        if(fp) fprintf(fp, "qs: %d, qe: %d, ava_k_mer: %d, hp_k_mer: %d\n", qs, qe, ava_k_mer, hp_k_mer);        
    }

    if(fp) fprintf(fp, "ava_k_mer: %d, hp_k_mer: %d, hp_rate: %f, min_dp: %d, a.n: %d\n", ava_k_mer, hp_k_mer, hp_rate, min_dp, (int)r->a.n);

    // if(fp)
    // {
    //     for (k = 0; k < (int)k_flag->a.n; k++)
    //     {
    //         if(k_flag->a.a[k] > 0) fprintf(fp, "(%d) %u\n", k, k_flag->a.a[k]);
    //     }
    // }
    
    if(hp_k_mer > ava_k_mer*hp_rate) return 1; ///must use '>' instead of '>='
    r->a.n = 0;
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %r9, %r14
movss %xmm0, (%rsp)
movq %r8, %r15
movq %rcx, %rbx
leaq 0x23d803(%rip), %rax    # 0x295758
movl 0xd0(%rax), %ecx
movl 0xd4(%rax), %eax
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movsd 0x1d9648(%rip), %xmm1   # 0x2315b8
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %r8d
testl %ecx, %ecx
movl $0x4, %ecx
cmovgl %r8d, %ecx
testl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %ebp
cmovlel %ecx, %ebp
movq %rbx, %rcx
movl %ebp, %r8d
movl $0x7d0, %r9d            # imm = 0x7D0
callq 0x57cb1
movq (%rbx), %r9
testl %r9d, %r9d
jle 0x5803f
movl %ebp, 0x4(%rsp)
xorl %ebp, %ebp
xorl %r13d, %r13d
xorl %r12d, %r12d
movss (%rsp), %xmm2
movq 0x10(%rbx), %rax
movq (%rax,%rbp,8), %rcx
movq %rcx, %rdx
shrq $0x20, %rdx
cmpl %ecx, %edx
jg 0x5800a
movq %rcx, %rdi
sarq $0x20, %rdi
movq 0x10(%r15), %rax
addq %rdi, %rax
movl %ecx, %esi
subl %edi, %esi
incl %esi
xorl %edi, %edi
movb (%rax,%rdi), %r8b
cmpb $0x2, %r8b
sbbl $-0x1, %r12d
cmpb $0x3, %r8b
sbbl $-0x1, %r13d
incq %rdi
cmpl %edi, %esi
jne 0x57fef
testq %r14, %r14
je 0x5802e
movq %r14, %rdi
leaq 0x1da1a1(%rip), %rsi    # 0x2321ba
movl %r12d, %r8d
movl %r13d, %r9d
xorl %eax, %eax
callq 0x22b0
movss (%rsp), %xmm2
movq (%rbx), %r9
incq %rbp
movslq %r9d, %rax
cmpq %rax, %rbp
jl 0x57fc6
movl 0x4(%rsp), %ebp
jmp 0x5804a
xorl %r12d, %r12d
xorl %r13d, %r13d
movss (%rsp), %xmm2
testq %r14, %r14
je 0x58075
xorps %xmm0, %xmm0
cvtss2sd %xmm2, %xmm0
leaq 0x1da18a(%rip), %rsi    # 0x2321e7
movq %r14, %rdi
movl %r12d, %edx
movl %r13d, %ecx
movl %ebp, %r8d
movb $0x1, %al
callq 0x22b0
movss (%rsp), %xmm2
xorps %xmm0, %xmm0
cvtsi2ss %r13d, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %r12d, %xmm1
mulss %xmm2, %xmm1
movl $0x1, %eax
ucomiss %xmm1, %xmm0
ja 0x5809c
movq $0x0, (%rbx)
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq