// File: /workspace/github_cpp_makefile_star2_O2/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 0x22a8c2(%rip), %rax    # 0x253758
movl 0xd0(%rax), %ecx
movl 0xd4(%rax), %eax
cvtsi2sd %ecx, %xmm0
movsd 0x1ba65a(%rip), %xmm1   # 0x1e3508
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %r8d
testl %ecx, %ecx
pushq $0x4
popq %rcx
cmovgl %r8d, %ecx
testl %eax, %eax
cvtsi2sd %eax, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %r8d
cmovlel %ecx, %r8d
movq %rbx, %rcx
movl %r8d, 0x4(%rsp)
movl $0x7d0, %r9d            # imm = 0x7D0
callq 0x28c21
xorl %ebp, %ebp
xorl %r12d, %r12d
xorl %r13d, %r13d
movq (%rbx), %r9
movslq %r9d, %rax
cmpq %rax, %rbp
jge 0x28f55
movq 0x10(%rbx), %rax
movq (%rax,%rbp,8), %rcx
movq %rcx, %rdx
shrq $0x20, %rdx
movq %rcx, %rax
sarq $0x20, %rax
movslq %ecx, %rsi
cmpq %rsi, %rax
jg 0x28f34
movq 0x10(%r15), %rdi
movb (%rdi,%rax), %dil
cmpb $0x2, %dil
sbbl $-0x1, %r12d
cmpb $0x3, %dil
sbbl $-0x1, %r13d
incq %rax
jmp 0x28f12
testq %r14, %r14
je 0x28f50
movq %r14, %rdi
leaq 0x1bb1c7(%rip), %rsi    # 0x1e410a
movl %r12d, %r8d
movl %r13d, %r9d
xorl %eax, %eax
callq 0x22b0
incq %rbp
jmp 0x28eee
testq %r14, %r14
movss (%rsp), %xmm2
je 0x28f84
cvtss2sd %xmm2, %xmm0
leaq 0x1bb1cd(%rip), %rsi    # 0x1e4137
movq %r14, %rdi
movl %r12d, %edx
movl %r13d, %ecx
movl 0x4(%rsp), %r8d
movb $0x1, %al
callq 0x22b0
movss (%rsp), %xmm2
cvtsi2ss %r13d, %xmm0
cvtsi2ss %r12d, %xmm1
mulss %xmm2, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x28f9c
pushq $0x1
popq %rax
jmp 0x28fa2
andq $0x0, (%rbx)
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq