// File: 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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movss %xmm0, -0x24(%rbp)
movl %ecx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x54(%rbp)
movl $0x0, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movl $0x0, -0x60(%rbp)
movl $0x4, -0x44(%rbp)
leaq 0x3788ed(%rip), %rax    # 0x3c47f8
cmpl $0x0, 0xd0(%rax)
jle 0x4bf36
leaq 0x3788dd(%rip), %rax    # 0x3c47f8
cvtsi2sdl 0xd0(%rax), %xmm0
movsd 0x33631d(%rip), %xmm1   # 0x382248
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x44(%rbp)
cmpl $0x4, -0x44(%rbp)
jle 0x4bf43
movl $0x4, -0x44(%rbp)
movq $0x0, -0x68(%rbp)
movl $0x0, -0x34(%rbp)
movslq -0x34(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jge 0x4bfbe
movq -0x10(%rbp), %rax
movq (%rax), %rax
movslq -0x34(%rbp), %rcx
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movzbl 0x2c(%rax), %eax
cmpl $0x1, %eax
je 0x4bf95
movq -0x68(%rbp), %rax
movzbl 0x2c(%rax), %eax
cmpl $0x2, %eax
je 0x4bf95
jmp 0x4bfb3
movq -0x68(%rbp), %rdi
movl -0x28(%rbp), %esi
callq 0x1a08b0
movl %eax, -0x48(%rbp)
movslq -0x48(%rbp), %rax
movl -0x60(%rbp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, -0x60(%rbp,%rax,4)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x4bf52
cmpq $0x0, -0x30(%rbp)
je 0x4bfe5
movq -0x30(%rbp), %rdi
movl -0x60(%rbp), %edx
movl -0x5c(%rbp), %ecx
movl -0x58(%rbp), %r8d
movl -0x54(%rbp), %r9d
leaq 0x336e10(%rip), %rsi    # 0x382dee
movb $0x0, %al
callq 0x22a0
movl -0x60(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0x4c0a5
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x38(%rbp)
movq -0x20(%rbp), %rax
cmpq $0x7d0, (%rax)          # imm = 0x7D0
ja 0x4c019
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x70(%rbp)
jmp 0x4c024
movl $0x7d0, %eax            # imm = 0x7D0
movq %rax, -0x70(%rbp)
jmp 0x4c024
movq -0x70(%rbp), %rax
movl %eax, -0x40(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x40(%rbp), %eax
jge 0x4c083
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x34(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x1, %eax
jle 0x4c058
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x34(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x2, %eax
jle 0x4c076
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x4c078
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x4c032
cvtsi2ssl -0x3c(%rbp), %xmm0
cvtsi2ssl -0x38(%rbp), %xmm1
mulss -0x24(%rbp), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x4c0a3
movl $0x1, -0x4(%rbp)
jmp 0x4c1a5
jmp 0x4c0a5
movl -0x5c(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0x4c172
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x38(%rbp)
movq -0x20(%rbp), %rax
cmpq $0x7d0, (%rax)          # imm = 0x7D0
ja 0x4c0d9
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x78(%rbp)
jmp 0x4c0e4
movl $0x7d0, %eax            # imm = 0x7D0
movq %rax, -0x78(%rbp)
jmp 0x4c0e4
movq -0x78(%rbp), %rax
movl %eax, -0x40(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movslq -0x40(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x20(%rbp), %rcx
movq (%rcx), %rcx
cmpl %ecx, %eax
jge 0x4c153
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x34(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x1, %eax
jle 0x4c128
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x34(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x2, %eax
jle 0x4c146
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x4c148
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x4c0fc
cvtsi2ssl -0x3c(%rbp), %xmm0
cvtsi2ssl -0x38(%rbp), %xmm1
mulss -0x24(%rbp), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x4c170
movl $0x1, -0x4(%rbp)
jmp 0x4c1a5
jmp 0x4c172
cmpq $0x0, -0x30(%rbp)
je 0x4c19e
movq -0x30(%rbp), %rdi
movl -0x38(%rbp), %edx
movl -0x3c(%rbp), %ecx
movss -0x24(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movl -0x44(%rbp), %r8d
leaq 0x336c7f(%rip), %rsi    # 0x382e16
movb $0x1, %al
callq 0x22a0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)