// File: /workspace/github_cpp_makefile_star2_O3/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, %r14
xorps %xmm0, %xmm0
leaq 0x23d686(%rip), %rax    # 0x295758
movl 0xd0(%rax), %eax
cvtsi2sd %eax, %xmm1
mulsd 0x1d94d4(%rip), %xmm1   # 0x2315b8
movaps %xmm0, 0x10(%rsp)
cvttsd2si %xmm1, %ebp
movl $0x4, %ecx
cmpl %ecx, %ebp
cmovgel %ecx, %ebp
testl %eax, %eax
cmovlel %ecx, %ebp
movq 0x10(%rdi), %rax
testq %rax, %rax
jle 0x5815a
movq %rdi, %r12
movq %rbx, 0x20(%rsp)
movq %r14, %rbx
xorl %r13d, %r13d
xorl %r14d, %r14d
movq (%r12), %rdi
movb 0x2c(%rdi,%r13), %cl
decb %cl
cmpb $0x1, %cl
ja 0x5813c
addq %r13, %rdi
movl %r15d, %esi
callq 0x128a41
cltq
incl 0x10(%rsp,%rax,4)
movq 0x10(%r12), %rax
incq %r14
addq $0xa8, %r13
cmpq %r14, %rax
jg 0x58116
movl 0x10(%rsp), %r15d
movq %rbx, %r14
movq 0x20(%rsp), %rbx
jmp 0x5815d
xorl %r15d, %r15d
testq %rbx, %rbx
je 0x58184
movl 0x14(%rsp), %ecx
movl 0x18(%rsp), %r8d
movl 0x1c(%rsp), %r9d
leaq 0x1da0af(%rip), %rsi    # 0x232226
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0x22b0
xorl %edx, %edx
movl $0x0, %ecx
cmpl %ebp, %r15d
jge 0x581f4
movq (%r14), %rax
cmpq $0x7d1, %rax            # imm = 0x7D1
movl $0x7d0, %ecx            # imm = 0x7D0
cmovbl %eax, %ecx
testl %ecx, %ecx
jle 0x581cd
movq 0x10(%r14), %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 0x581b1
jmp 0x581d1
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 0x58286
cmpl %ebp, 0x14(%rsp)
jge 0x58261
movq (%r14), %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 0x58242
movq 0x10(%r14), %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 0x58228
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %edx, %xmm1
mulss 0xc(%rsp), %xmm1
movl $0x1, %r15d
ucomiss %xmm1, %xmm0
ja 0x58286
xorl %r15d, %r15d
testq %rbx, %rbx
je 0x58286
xorps %xmm0, %xmm0
cvtss2sd 0xc(%rsp), %xmm0
leaq 0x1d9fd5(%rip), %rsi    # 0x23224e
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