// File: 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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movss %xmm0, -0x34(%rbp)
movq %r9, -0x40(%rbp)
movl $0x0, -0x54(%rbp)
movl $0x0, -0x58(%rbp)
movl $0x4, -0x5c(%rbp)
leaq 0x378b17(%rip), %rax    # 0x3c47f8
cmpl $0x0, 0xd0(%rax)
jle 0x4bd0c
leaq 0x378b07(%rip), %rax    # 0x3c47f8
cvtsi2sdl 0xd0(%rax), %xmm0
movsd 0x336547(%rip), %xmm1   # 0x382248
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x5c(%rbp)
leaq 0x378ae5(%rip), %rax    # 0x3c47f8
cmpl $0x0, 0xd4(%rax)
jle 0x4bd3e
leaq 0x378ad5(%rip), %rax    # 0x3c47f8
cvtsi2sdl 0xd4(%rax), %xmm0
movsd 0x336515(%rip), %xmm1   # 0x382248
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x5c(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
movl -0x5c(%rbp), %r8d
movl $0x7d0, %r9d            # imm = 0x7D0
callq 0x4b7f0
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x28(%rbp), %rcx
movq (%rcx), %rcx
cmpl %ecx, %eax
jge 0x4be36
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x44(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0x4c(%rbp)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x44(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shlq $0x20, %rax
shrq $0x20, %rax
movl %eax, -0x50(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x50(%rbp), %eax
jg 0x4bdff
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x48(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x1, %eax
jle 0x4bdd4
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x48(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x2, %eax
jle 0x4bdf2
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x4bdf4
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x4bdae
cmpq $0x0, -0x40(%rbp)
je 0x4be26
movq -0x40(%rbp), %rdi
movl -0x4c(%rbp), %edx
movl -0x50(%rbp), %ecx
movl -0x54(%rbp), %r8d
movl -0x58(%rbp), %r9d
leaq 0x336f63(%rip), %rsi    # 0x382d82
movb $0x0, %al
callq 0x22a0
jmp 0x4be28
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x4bd64
cmpq $0x0, -0x40(%rbp)
je 0x4be6c
movq -0x40(%rbp), %rdi
movl -0x54(%rbp), %edx
movl -0x58(%rbp), %ecx
movss -0x34(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movl -0x5c(%rbp), %r8d
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl %eax, %r9d
leaq 0x336f4a(%rip), %rsi    # 0x382daf
movb $0x1, %al
callq 0x22a0
cvtsi2ssl -0x58(%rbp), %xmm0
cvtsi2ssl -0x54(%rbp), %xmm1
mulss -0x34(%rbp), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x4be89
movl $0x1, -0x4(%rbp)
jmp 0x4be9b
movq -0x28(%rbp), %rax
movq $0x0, (%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)