// File: /workspace/github_cpp_makefile_star2_O1/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 0x231c5d(%rip), %rax    # 0x28a758
movl 0xd0(%rax), %ecx
movl 0xd4(%rax), %eax
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movsd 0x1cc9a2(%rip), %xmm1   # 0x2254b8
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 0x58871
movl (%rbx), %r9d
testl %r9d, %r9d
jle 0x58be5
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 0x58bb0
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 0x58b95
testq %r14, %r14
je 0x58bd1
movq %r14, %rdi
leaq 0x1cd4eb(%rip), %rsi    # 0x2260aa
movl %r12d, %r8d
movl %r13d, %r9d
xorl %eax, %eax
callq 0x22b0
movss (%rsp), %xmm2
incq %rbp
movq (%rbx), %r9
movslq %r9d, %rax
cmpq %rax, %rbp
jl 0x58b6c
movl 0x4(%rsp), %ebp
jmp 0x58bf0
xorl %r12d, %r12d
xorl %r13d, %r13d
movss (%rsp), %xmm2
testq %r14, %r14
je 0x58c1b
xorps %xmm0, %xmm0
cvtss2sd %xmm2, %xmm0
leaq 0x1cd4d4(%rip), %rsi    # 0x2260d7
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 0x58c42
movq $0x0, (%rbx)
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq