// File: /workspace/github_cpp_makefile_star2_O2/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, 0x20(%rsp)
movl %ecx, %r15d
movss %xmm0, 0x8(%rsp)
movq %rdx, %r14
movq %rdi, %r12
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
leaq 0x22a776(%rip), %rax    # 0x253758
movl 0xd0(%rax), %eax
movl %eax, 0xc(%rsp)
cvtsi2sd %eax, %xmm0
mulsd 0x1ba510(%rip), %xmm0   # 0x1e3508
cvttsd2si %xmm0, %ebx
cmpl $0x4, %ebx
pushq $0x4
popq %rax
cmovgel %eax, %ebx
xorl %ebp, %ebp
xorl %r13d, %r13d
cmpq %r13, 0x10(%r12)
jle 0x2903c
movq (%r12), %rdi
movb 0x2c(%rdi,%rbp), %al
decb %al
cmpb $0x1, %al
ja 0x29030
addq %rbp, %rdi
movl %r15d, %esi
callq 0xe3aee
cltq
incl 0x10(%rsp,%rax,4)
incq %r13
addq $0xa8, %rbp
jmp 0x2900a
cmpl $0x0, 0xc(%rsp)
pushq $0x4
popq %rbp
cmovgl %ebx, %ebp
movl 0x10(%rsp), %r12d
movq 0x20(%rsp), %rbx
testq %rbx, %rbx
je 0x29078
movl 0x14(%rsp), %ecx
movl 0x18(%rsp), %r8d
movl 0x1c(%rsp), %r9d
leaq 0x1bb10b(%rip), %rsi    # 0x1e4176
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0x22b0
xorl %edx, %edx
pushq $0x1
popq %r15
movl $0x0, %ecx
cmpl %ebp, %r12d
jge 0x290de
movq (%r14), %rcx
xorl %eax, %eax
testl %ecx, %ecx
movl $0x0, %edx
cmovgl %ecx, %edx
cmpq $0x7d1, %rcx            # imm = 0x7D1
movl $0x7d0, %esi            # imm = 0x7D0
cmovbq %rdx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
cmpq %rax, %rsi
je 0x290cb
movq 0x10(%r14), %rdi
movb (%rdi,%rax), %dil
cmpb $0x2, %dil
sbbl $-0x1, %edx
cmpb $0x3, %dil
sbbl $-0x1, %ecx
incq %rax
jmp 0x290ab
cvtsi2ss %ecx, %xmm0
cvtsi2ss %edx, %xmm1
mulss 0x8(%rsp), %xmm1
ucomiss %xmm1, %xmm0
ja 0x29156
cmpl %ebp, 0x14(%rsp)
jge 0x29134
movq (%r14), %rcx
xorl %edx, %edx
movslq %ecx, %rax
subq $0x7d0, %rcx            # imm = 0x7D0
movl $0x0, %esi
cmovaeq %rcx, %rsi
movslq %esi, %rsi
xorl %ecx, %ecx
cmpq %rax, %rsi
jge 0x29121
movq 0x10(%r14), %rdi
movb (%rdi,%rsi), %dil
cmpb $0x2, %dil
sbbl $-0x1, %edx
cmpb $0x3, %dil
sbbl $-0x1, %ecx
incq %rsi
jmp 0x29101
cvtsi2ss %ecx, %xmm0
cvtsi2ss %edx, %xmm1
mulss 0x8(%rsp), %xmm1
ucomiss %xmm1, %xmm0
ja 0x29156
xorl %r15d, %r15d
testq %rbx, %rbx
je 0x29156
cvtss2sd 0x8(%rsp), %xmm0
leaq 0x1bb055(%rip), %rsi    # 0x1e419e
movq %rbx, %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