// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: collect_hp_regions(overlap_region_alloc*, All_reads*, kvec_t_u8_warp*, float, int, _IO_FILE*)
int collect_hp_regions(overlap_region_alloc* olist, All_reads* R_INF, kvec_t_u8_warp* k_flag, 
float hp_rate, int rlen, FILE* fp)
{
    int i, ava_k_mer = 0, hp_k_mer = 0, vLen, min_dp;
    int32_t w, n[4];
    n[0] = n[1] = n[2] = n[3] = 0;
    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;
    overlap_region* ov = NULL;

    for (i = 0; i < (long long)olist->length; i++)
    {
        ov = &(olist->list[i]);
        if (ov->is_match != 1 && ov->is_match != 2) continue;

        w = ha_ov_type(ov, rlen);
		++n[w];
    }

    if(fp) fprintf(fp, "n[0]: %d, n[1]: %d, n[2]: %d, n[3]: %d\n", n[0], n[1], n[2], n[3]);

    // n[0] += n[2];
    // n[1] += n[2];

    if(n[0] < min_dp)
    {
        ava_k_mer = hp_k_mer = 0;
        vLen = MIN(k_flag->a.n, RESEED_LEN);
        for (i = 0; i < vLen; i++)
        {
            if(k_flag->a.a[i] > 1) ava_k_mer++;
            if(k_flag->a.a[i] > 2) hp_k_mer++;
        }
        if(hp_k_mer > ava_k_mer*hp_rate) return 1;
    }


    if(n[1] < min_dp)
    {
        ava_k_mer = hp_k_mer = 0;
        vLen = MIN(k_flag->a.n, RESEED_LEN);
        for (i = k_flag->a.n - vLen; i < (int)k_flag->a.n; i++)
        {
            if(k_flag->a.a[i] > 1) ava_k_mer++;
            if(k_flag->a.a[i] > 2) hp_k_mer++;
        }
        if(hp_k_mer > ava_k_mer*hp_rate) return 1;
    }


    if(fp) fprintf(fp, "ava_k_mer: %d, hp_k_mer: %d, hp_rate: %f, min_dp: %d\n", ava_k_mer, hp_k_mer, hp_rate, min_dp);

    // 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]);
    //     }
    // }
    
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r8, %rbx
movl %ecx, %r15d
movss %xmm0, 0xc(%rsp)
movq %rdx, 0x20(%rsp)
xorps %xmm0, %xmm0
leaq 0x231ade(%rip), %rax    # 0x28a758
movl 0xd0(%rax), %eax
cvtsi2sd %eax, %xmm1
mulsd 0x1cc82c(%rip), %xmm1   # 0x2254b8
movaps %xmm0, 0x10(%rsp)
cvttsd2si %xmm1, %ebp
movl $0x4, %ecx
cmpl %ecx, %ebp
cmovgel %ecx, %ebp
testl %eax, %eax
cmovlel %ecx, %ebp
cmpq $0x0, 0x10(%rdi)
jle 0x58ce5
movq %rdi, %r12
xorl %r13d, %r13d
xorl %r14d, %r14d
movq (%r12), %rdi
movb 0x2c(%rdi,%r13), %al
decb %al
cmpb $0x1, %al
ja 0x58cd4
addq %r13, %rdi
movl %r15d, %esi
callq 0x122b6d
cltq
incl 0x10(%rsp,%rax,4)
incq %r14
addq $0xa8, %r13
cmpq %r14, 0x10(%r12)
jg 0x58cb4
movq %rbx, %r14
testq %rbx, %rbx
je 0x58d10
movl 0x10(%rsp), %edx
movl 0x14(%rsp), %ecx
movl 0x18(%rsp), %r8d
movl 0x1c(%rsp), %r9d
leaq 0x1cd410(%rip), %rsi    # 0x226116
movq %r14, %rdi
xorl %eax, %eax
callq 0x22b0
xorl %edx, %edx
movl $0x0, %ecx
cmpl %ebp, 0x10(%rsp)
movq 0x20(%rsp), %rbx
jge 0x58d86
movq (%rbx), %rax
cmpq $0x7d1, %rax            # imm = 0x7D1
movl $0x7d0, %ecx            # imm = 0x7D0
cmovbl %eax, %ecx
testl %ecx, %ecx
jle 0x58d5f
movq 0x10(%rbx), %rax
movl %ecx, %esi
xorl %edi, %edi
xorl %ecx, %ecx
xorl %edx, %edx
movb (%rax,%rdi), %r8b
cmpb $0x2, %r8b
sbbl $-0x1, %edx
cmpb $0x3, %r8b
sbbl $-0x1, %ecx
incq %rdi
cmpq %rdi, %rsi
jne 0x58d43
jmp 0x58d63
xorl %edx, %edx
xorl %ecx, %ecx
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %edx, %xmm1
mulss 0xc(%rsp), %xmm1
movl $0x1, %r15d
ucomiss %xmm1, %xmm0
ja 0x58e18
cmpl %ebp, 0x14(%rsp)
jge 0x58df3
movq (%rbx), %rdi
xorl %edx, %edx
movq %rdi, %rax
subq $0x7d0, %rax            # imm = 0x7D0
movl $0x0, %esi
cmovaeq %rax, %rsi
movl $0x0, %ecx
cmpl %edi, %esi
jge 0x58dd4
movq 0x10(%rbx), %rax
movslq %esi, %rsi
movslq %edi, %rdi
xorl %ecx, %ecx
xorl %edx, %edx
movb (%rax,%rsi), %r8b
cmpb $0x2, %r8b
sbbl $-0x1, %edx
cmpb $0x3, %r8b
sbbl $-0x1, %ecx
incq %rsi
cmpq %rsi, %rdi
jne 0x58dba
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %edx, %xmm1
mulss 0xc(%rsp), %xmm1
movl $0x1, %r15d
ucomiss %xmm1, %xmm0
ja 0x58e18
xorl %r15d, %r15d
testq %r14, %r14
je 0x58e18
xorps %xmm0, %xmm0
cvtss2sd 0xc(%rsp), %xmm0
leaq 0x1cd333(%rip), %rsi    # 0x22613e
movq %r14, %rdi
movl %ebp, %r8d
movb $0x1, %al
callq 0x22b0
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq