// File: chhylp123[P]hifiasm/Overlaps.cpp
// Function: calculate_peak(unsigned long*, long long, kvec_t_u32_warp*, long long, long long, long long*, long long*)
void calculate_peak(uint64_t* counts, long long len, kvec_t_u32_warp* a, 
long long TotalMean, long long HapMean, long long* peak0, long long* peak1)
{
    if(len == 0) return;
    long long i, j, total_coverage = 0, current_coverage, step, pre, after, is_found = 0;
    for (i = 0; i < len; i++)
    {
        total_coverage = total_coverage + ((counts[i]>>32) * ((uint32_t)counts[i]));
    }
    current_coverage = total_coverage;
    for (i = len - 1; i >= 0; i--)
    {
        current_coverage = current_coverage - ((counts[i]>>32) * ((uint32_t)counts[i]));
        if(current_coverage <= total_coverage*0.9) break;
    }
    i++;
    step = i*0.1;
    if(step < 2) step = 2;
    ///fprintf(stderr, "step: %lld\n", step);
    

    a->a.n = 0;
    ///for (i = 0; i < len; i++)
    for (i = 1; i < len; i++)
    {
        pre = i - step/2;
        after = i + step/2;
        if(pre < 0) pre = 0;
        if(after >= len) after = len - 1;
        is_found = 0;
        for (j = pre; j < after; j++)
        {
            if(j == i) continue;
            if(((uint32_t)counts[i]) <= ((uint32_t)counts[j]))
            {
                is_found = 1;
                break;
            } 
        }
        
        if(is_found == 0)
        {
            kv_push(uint32_t, a->a, i);
        }
    }

    

    uint32_t peak_0_i, peak_1_i;
    peak_0_i = peak_1_i = 0;

    for (i = 0; i < (long long)a->a.n; i++)
    {
        if(((uint32_t)counts[a->a.a[peak_0_i]]) < ((uint32_t)counts[a->a.a[i]]))
        {
            peak_0_i = i;
        }
    }

    peak_1_i = (uint32_t)-1;
    for (i = 0; i < (long long)a->a.n; i++)
    {
        if(peak_0_i == i) continue;
        if(peak_1_i == (uint32_t)-1)
        {
            peak_1_i = i;
            continue;
        }
        if(((uint32_t)counts[a->a.a[peak_1_i]]) < ((uint32_t)counts[a->a.a[i]]))
        {
            peak_1_i = i;
        }
    }

    if(peak_0_i == peak_1_i) peak_1_i = (uint32_t)-1;

    
    // for (i = 0; i < (long long)a->a.n; i++)
    // {
    //     fprintf(stderr, "i:%lld, freq: %u, num: %u\n", i, 
    //     (uint32_t)(counts[a->a.a[i]]>>32), (uint32_t)counts[a->a.a[i]]);
    // }
    // fprintf(stderr, "peak_0_i: %u, peak_1_i: %u\n", peak_0_i, peak_1_i); 

    (*peak0) = (*peak1) = -1;
    /**
     * There are three cases:
     * 1. very low het rate: don't need purge_dup
     * 2. very high het rate: TotalMean should be equal to peak_0
     * 3. not such high het rate: two peaks, HapMean should be useful
     * **/
    
    if(peak_1_i == (uint32_t)-1)
    {
        (*peak0) = (uint32_t)(counts[a->a.a[peak_0_i]]>>32);
        return;
    }

    (*peak0) = (uint32_t)(counts[a->a.a[peak_0_i]]>>32);
    (*peak1) = (uint32_t)(counts[a->a.a[peak_1_i]]>>32);


    if(TotalMean >= (long long)((uint32_t)(counts[a->a.a[peak_0_i]]>>32)) && 
       TotalMean >= (long long)((uint32_t)(counts[a->a.a[peak_1_i]]>>32)))
    {
        (*peak1) = -1;
        return;
    }

    long long max_cov = MAX((*peak0), TotalMean);
    long long min_cov = MIN((*peak0), TotalMean);
    if(min_cov >= max_cov*0.8)
    {
        max_cov = MAX((*peak1), TotalMean);
        min_cov = MIN((*peak1), TotalMean);
        if(min_cov < max_cov*0.6)
        {
            (*peak1) = -1;
            return;
        }
    }


    long long hap_cov = ((uint32_t)(counts[a->a.a[MIN(peak_0_i, peak_1_i)]]>>32));
    max_cov = MAX(hap_cov, HapMean);
    min_cov = MIN(hap_cov, HapMean);
    ///fprintf(stderr, "hap_cov: %lld, max_cov: %lld, min_cov: %lld\n", hap_cov, max_cov, min_cov); 
    if(min_cov < max_cov*0.70)
    {
        (*peak1) = -1;
        return;
    }

    min_cov = MIN((*peak0), (*peak1));
    max_cov = MAX((*peak0), (*peak1));
    (*peak0) = min_cov;
    (*peak1) = max_cov;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x145973
jmp 0x1460bd
movq $0x0, -0x48(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x10(%rbp), %rax
jge 0x1459d0
movq -0x48(%rbp), %rax
movq -0x8(%rbp), %rcx
movq -0x38(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
movq -0x8(%rbp), %rdx
movq -0x38(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
movl %edx, %edx
imulq %rdx, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x14598b
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jl 0x145a48
movq -0x50(%rbp), %rax
movq -0x8(%rbp), %rcx
movq -0x38(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
movq -0x8(%rbp), %rdx
movq -0x38(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
movl %edx, %edx
imulq %rdx, %rcx
subq %rcx, %rax
movq %rax, -0x50(%rbp)
cvtsi2sdq -0x50(%rbp), %xmm1
cvtsi2sdq -0x48(%rbp), %xmm0
movsd 0x23c83c(%rip), %xmm2   # 0x382268
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jb 0x145a38
jmp 0x145a48
jmp 0x145a3a
movq -0x38(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x1459e4
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
cvtsi2sdq -0x38(%rbp), %xmm0
movsd 0x2395b6(%rip), %xmm1   # 0x37f018
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, -0x58(%rbp)
cmpq $0x2, -0x58(%rbp)
jge 0x145a7e
movq $0x2, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq $0x0, (%rax)
movq $0x1, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x10(%rbp), %rax
jge 0x145c29
movq -0x38(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x58(%rbp), %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0xa0(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x58(%rbp), %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x98(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x60(%rbp)
jge 0x145b02
movq $0x0, -0x60(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x10(%rbp), %rax
jl 0x145b18
movq -0x10(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x68(%rbp), %rax
jge 0x145b74
movq -0x40(%rbp), %rax
cmpq -0x38(%rbp), %rax
jne 0x145b3e
jmp 0x145b66
movq -0x8(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x8(%rbp), %rcx
movq -0x40(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
cmpl %ecx, %eax
ja 0x145b64
movq $0x1, -0x70(%rbp)
jmp 0x145b74
jmp 0x145b66
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x145b28
cmpq $0x0, -0x70(%rbp)
jne 0x145c16
jmp 0x145b81
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x145bf2
movq -0x18(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x145bb1
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xa8(%rbp)
jmp 0x145bbf
movl $0x2, %eax
movq %rax, -0xa8(%rbp)
jmp 0x145bbf
movq -0xa8(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x38(%rbp), %rax
movl %eax, %edx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x18(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movl %edx, (%rax,%rcx,4)
jmp 0x145c16
jmp 0x145c18
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x145a91
movl $0x0, -0x78(%rbp)
movl $0x0, -0x74(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq (%rcx), %rax
jge 0x145c94
movq -0x8(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x74(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
movq (%rax,%rcx,8), %rax
movq -0x8(%rbp), %rcx
movq -0x18(%rbp), %rdx
movq 0x10(%rdx), %rdx
movq -0x38(%rbp), %rsi
movl (%rdx,%rsi,4), %edx
movq (%rcx,%rdx,8), %rcx
cmpl %ecx, %eax
jae 0x145c84
movq -0x38(%rbp), %rax
movl %eax, -0x74(%rbp)
jmp 0x145c86
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x145c3f
movl $0xffffffff, -0x78(%rbp) # imm = 0xFFFFFFFF
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq (%rcx), %rax
jge 0x145d12
movl -0x74(%rbp), %eax
cmpq -0x38(%rbp), %rax
jne 0x145cbb
jmp 0x145d04
cmpl $-0x1, -0x78(%rbp)
jne 0x145cca
movq -0x38(%rbp), %rax
movl %eax, -0x78(%rbp)
jmp 0x145d04
movq -0x8(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x78(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
movq (%rax,%rcx,8), %rax
movq -0x8(%rbp), %rcx
movq -0x18(%rbp), %rdx
movq 0x10(%rdx), %rdx
movq -0x38(%rbp), %rsi
movl (%rdx,%rsi,4), %edx
movq (%rcx,%rdx,8), %rcx
cmpl %ecx, %eax
jae 0x145d02
movq -0x38(%rbp), %rax
movl %eax, -0x78(%rbp)
jmp 0x145d04
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x145ca3
movl -0x74(%rbp), %eax
cmpl -0x78(%rbp), %eax
jne 0x145d21
movl $0xffffffff, -0x78(%rbp) # imm = 0xFFFFFFFF
movq 0x10(%rbp), %rax
movq $-0x1, (%rax)
movq -0x30(%rbp), %rax
movq $-0x1, (%rax)
cmpl $-0x1, -0x78(%rbp)
jne 0x145d67
movq -0x8(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x74(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, %eax
movl %eax, %ecx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
jmp 0x1460bd
movq -0x8(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x74(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, %eax
movl %eax, %ecx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x78(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, %eax
movl %eax, %ecx
movq 0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
movq -0x8(%rbp), %rcx
movq -0x18(%rbp), %rdx
movq 0x10(%rdx), %rdx
movl -0x74(%rbp), %esi
movl (%rdx,%rsi,4), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
movl %ecx, %ecx
cmpq %rcx, %rax
jl 0x145e0b
movq -0x20(%rbp), %rax
movq -0x8(%rbp), %rcx
movq -0x18(%rbp), %rdx
movq 0x10(%rdx), %rdx
movl -0x78(%rbp), %esi
movl (%rdx,%rsi,4), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
movl %ecx, %ecx
cmpq %rcx, %rax
jl 0x145e0b
movq 0x10(%rbp), %rax
movq $-0x1, (%rax)
jmp 0x1460bd
movq -0x30(%rbp), %rax
movq (%rax), %rax
cmpq -0x20(%rbp), %rax
jl 0x145e28
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xb0(%rbp)
jmp 0x145e33
movq -0x20(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rax
cmpq -0x20(%rbp), %rax
jg 0x145e5b
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xb8(%rbp)
jmp 0x145e66
movq -0x20(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
movq %rax, -0x88(%rbp)
cvtsi2sdq -0x88(%rbp), %xmm0
cvtsi2sdq -0x80(%rbp), %xmm1
movsd 0x23c3a5(%rip), %xmm2   # 0x382230
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jb 0x145f35
movq 0x10(%rbp), %rax
movq (%rax), %rax
cmpq -0x20(%rbp), %rax
jl 0x145eb6
movq 0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xc0(%rbp)
jmp 0x145ec1
movq -0x20(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0x80(%rbp)
movq 0x10(%rbp), %rax
movq (%rax), %rax
cmpq -0x20(%rbp), %rax
jg 0x145ee9
movq 0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xc8(%rbp)
jmp 0x145ef4
movq -0x20(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0x88(%rbp)
cvtsi2sdq -0x88(%rbp), %xmm1
cvtsi2sdq -0x80(%rbp), %xmm0
movsd 0x23c2f7(%rip), %xmm2   # 0x382210
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x145f33
movq 0x10(%rbp), %rax
movq $-0x1, (%rax)
jmp 0x1460bd
jmp 0x145f35
movq -0x8(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0xd0(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x78(%rbp), %eax
ja 0x145f62
movl -0x74(%rbp), %eax
movl %eax, -0xdc(%rbp)
jmp 0x145f6b
movl -0x78(%rbp), %eax
movl %eax, -0xdc(%rbp)
movq -0xd8(%rbp), %rax
movq -0xd0(%rbp), %rcx
movl -0xdc(%rbp), %edx
movl %edx, %edx
movl (%rcx,%rdx,4), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, %eax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
cmpq -0x28(%rbp), %rax
jl 0x145fb2
movq -0x90(%rbp), %rax
movq %rax, -0xe8(%rbp)
jmp 0x145fbd
movq -0x28(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x90(%rbp), %rax
cmpq -0x28(%rbp), %rax
jg 0x145fe5
movq -0x90(%rbp), %rax
movq %rax, -0xf0(%rbp)
jmp 0x145ff0
movq -0x28(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0x88(%rbp)
cvtsi2sdq -0x88(%rbp), %xmm1
cvtsi2sdq -0x80(%rbp), %xmm0
movsd 0x23c213(%rip), %xmm2   # 0x382228
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x14602f
movq 0x10(%rbp), %rax
movq $-0x1, (%rax)
jmp 0x1460bd
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rbp), %rcx
cmpq (%rcx), %rax
jg 0x14604f
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xf8(%rbp)
jmp 0x14605d
movq 0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xf8(%rbp)
movq -0xf8(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rbp), %rcx
cmpq (%rcx), %rax
jl 0x14608b
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x100(%rbp)
jmp 0x146099
movq 0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x100(%rbp)
movq -0x100(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x88(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x80(%rbp), %rcx
movq 0x10(%rbp), %rax
movq %rcx, (%rax)
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw %cs:(%rax,%rax)