// File: chhylp123[P]hifiasm/Purge_Dups.cpp
// Function: get_alter_peak(ma_ug_t*, asg_t*, R_to_U*, unsigned long*, ma_hit_t_alloc*, ma_sub_t*, long long)
long long get_alter_peak(ma_ug_t *ug, asg_t *read_g, R_to_U* ruIndex, uint64_t* position_index, 
ma_hit_t_alloc* sources, ma_sub_t* coverage_cut, long long cov_buf_length)
{
    
    ma_utg_t* u = NULL;
    asg_t* nsg = ug->g;
    uint64_t v, j, k, qn, n_vtx = nsg->n_seq, primary_bases = 0, alter_bases = 0;
    uint32_t tn, is_Unitig;
    long long* cov_buf = NULL;
    ma_hit_t *h;
    cov_buf = (long long*)calloc(cov_buf_length, sizeof(long long));
    long long R_bases = 0, C_bases_primary = 0, C_bases_alter = 0, C_bases = 0;
    memset(position_index, -1, sizeof(uint64_t)*read_g->n_seq);


    for (v = 0; v < n_vtx; ++v) 
    {
        if(nsg->seq[v].del) continue;
        if(nsg->seq[v].c == ALTER_LABLE) continue;
        u = &(ug->u.a[v]);
        if(u->m == 0) continue;
        for (k = 0; k < u->n; k++)
        {
            qn = u->a[k]>>33;
            position_index[qn] = 0;
            R_bases = coverage_cut[qn].e - coverage_cut[qn].s;
            primary_bases += R_bases;
        }
    }

    for (qn = 0; qn < read_g->n_seq; qn++)
    {
        if(position_index[qn] == 0) continue;
        if(read_g->seq[qn].del) continue;

        C_bases = C_bases_primary = C_bases_alter = 0;
        R_bases = coverage_cut[qn].e - coverage_cut[qn].s;
        alter_bases += R_bases;
        for (j = 0; j < (uint64_t)(sources[qn].length); j++)
        {
            h = &(sources[qn].buffer[j]);
            if(h->el != 1) continue;
            tn = Get_tn((*h));

            if(read_g->seq[tn].del == 1)
            {
                ///get the id of read that contains it 
                get_R_to_U(ruIndex, tn, &tn, &is_Unitig);
                if(tn == (uint32_t)-1 || is_Unitig == 1 || read_g->seq[tn].del == 1) continue;
            }

            if(position_index[tn] == 0)
            {
                C_bases_primary += Get_qe((*h)) - Get_qs((*h));
            }
            else
            {
                C_bases_alter += Get_qe((*h)) - Get_qs((*h));
            }
        }

        C_bases = C_bases_primary + C_bases_alter;
        if(C_bases_alter < C_bases * ALTER_COV_THRES) continue;

        C_bases = C_bases/R_bases;
        if(C_bases < 0 || C_bases >= cov_buf_length) continue;
        cov_buf[C_bases]++;
    }
    
    long long max_i = -1, max = -1;
    for (j = 0; (long long)j < cov_buf_length; ++j)
    {
        if (cov_buf[j] > max)
        {
            max = cov_buf[j];
            max_i = j;
        }
    }

    if(alter_bases < primary_bases * REAL_ALTER_THRES) max_i = max = -1;

    free(cov_buf);
    memset(position_index, -1, sizeof(uint64_t)*read_g->n_seq);

    return max_i;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
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)
movq $0x0, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x88(%rbp)
movq 0x10(%rbp), %rdi
movl $0x8, %esi
callq 0x2140
movq %rax, -0x88(%rbp)
movq $0x0, -0x98(%rbp)
movq $0x0, -0xa0(%rbp)
movq $0x0, -0xa8(%rbp)
movq $0x0, -0xb0(%rbp)
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edx
shlq $0x3, %rdx
movl $0xff, %esi
callq 0x2100
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x68(%rbp), %rax
jae 0x16f31c
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x48(%rbp), %rcx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x16f23d
jmp 0x16f30b
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x48(%rbp), %rcx
movzbl 0x4(%rax,%rcx,8), %eax
cmpl $0x1, %eax
jne 0x16f258
jmp 0x16f30b
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x28, -0x48(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jne 0x16f27b
jmp 0x16f30b
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x38(%rbp), %rcx
movl 0x10(%rcx), %ecx
cmpq %rcx, %rax
jae 0x16f309
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0x60(%rbp)
movq -0x20(%rbp), %rax
movq -0x60(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
movq -0x30(%rbp), %rax
imulq $0xc, -0x60(%rbp), %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x30(%rbp), %rcx
imulq $0xc, -0x60(%rbp), %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl %ecx, %eax
movl %eax, %eax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
addq -0x70(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x16f283
jmp 0x16f30b
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x16f213
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq -0x10(%rbp), %rcx
movl 0x14(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %ecx, %ecx
cmpq %rcx, %rax
jae 0x16f587
movq -0x20(%rbp), %rax
movq -0x60(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x16f354
jmp 0x16f576
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x60(%rbp), %rcx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x16f370
jmp 0x16f576
movq $0x0, -0xa8(%rbp)
movq $0x0, -0xa0(%rbp)
movq $0x0, -0xb0(%rbp)
movq -0x30(%rbp), %rax
imulq $0xc, -0x60(%rbp), %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x30(%rbp), %rcx
imulq $0xc, -0x60(%rbp), %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl %ecx, %eax
movl %eax, %eax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
addq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq -0x28(%rbp), %rcx
imulq $0x18, -0x60(%rbp), %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jae 0x16f4f1
movq -0x28(%rbp), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
imulq $0x28, -0x50(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
movzbl 0x20(%rax), %eax
cmpl $0x1, %eax
je 0x16f425
jmp 0x16f4e0
movq -0x90(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x7c(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x7c(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x1, %eax
jne 0x16f482
movq -0x18(%rbp), %rdi
movl -0x7c(%rbp), %esi
leaq -0x7c(%rbp), %rdx
leaq -0x80(%rbp), %rcx
callq 0xd3d50
cmpl $-0x1, -0x7c(%rbp)
je 0x16f47e
cmpl $0x1, -0x80(%rbp)
je 0x16f47e
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x7c(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x1, %eax
jne 0x16f480
jmp 0x16f4e0
jmp 0x16f482
movq -0x20(%rbp), %rax
movl -0x7c(%rbp), %ecx
cmpq $0x0, (%rax,%rcx,8)
jne 0x16f4b8
movq -0x90(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x90(%rbp), %rcx
movq (%rcx), %rcx
subl %ecx, %eax
movl %eax, %eax
addq -0xa0(%rbp), %rax
movq %rax, -0xa0(%rbp)
jmp 0x16f4de
movq -0x90(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x90(%rbp), %rcx
movq (%rcx), %rcx
subl %ecx, %eax
movl %eax, %eax
addq -0xa8(%rbp), %rax
movq %rax, -0xa8(%rbp)
jmp 0x16f4e0
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x16f3d6
movq -0xa0(%rbp), %rax
addq -0xa8(%rbp), %rax
movq %rax, -0xb0(%rbp)
cvtsi2sdq -0xa8(%rbp), %xmm1
cvtsi2sdq -0xb0(%rbp), %xmm0
movsd 0x212d48(%rip), %xmm2   # 0x382268
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x16f52c
jmp 0x16f576
movq -0xb0(%rbp), %rax
cqto
idivq -0x98(%rbp)
movq %rax, -0xb0(%rbp)
cmpq $0x0, -0xb0(%rbp)
jl 0x16f55a
movq -0xb0(%rbp), %rax
cmpq 0x10(%rbp), %rax
jl 0x16f55c
jmp 0x16f576
movq -0x88(%rbp), %rax
movq -0xb0(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x16f324
movq $-0x1, -0xb8(%rbp)
movq $-0x1, -0xc0(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq 0x10(%rbp), %rax
jge 0x16f5f8
movq -0x88(%rbp), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpq -0xc0(%rbp), %rax
jle 0x16f5e8
movq -0x88(%rbp), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xc0(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0xb8(%rbp)
jmp 0x16f5ea
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x16f5a5
movq -0x78(%rbp), %xmm1
movaps 0x212a0c(%rip), %xmm3   # 0x382010
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
movapd 0x212a10(%rip), %xmm2   # 0x382020
subpd %xmm2, %xmm1
movaps %xmm1, %xmm0
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm0, %xmm1
movq -0x70(%rbp), %xmm0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
subpd %xmm2, %xmm0
movaps %xmm0, %xmm2
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm2, %xmm0
movsd 0x212c11(%rip), %xmm2   # 0x382250
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x16f65f
movq $-0x1, -0xc0(%rbp)
movq $-0x1, -0xb8(%rbp)
movq -0x88(%rbp), %rdi
callq 0x2210
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edx
shlq $0x3, %rdx
movl $0xff, %esi
callq 0x2100
movq -0xb8(%rbp), %rax
addq $0xc0, %rsp
popq %rbp
retq
nopl (%rax)