// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, 0x20(%rsp)
movq %r8, 0x48(%rsp)
movq %rcx, %r13
movq %rdx, 0x60(%rsp)
movq %rsi, %rbx
movq %rdi, 0x18(%rsp)
movq 0xa0(%rsp), %rbp
movq 0x18(%rdi), %r15
movl 0x14(%r15), %r14d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %r14
pushq $0x8
popq %rsi
movq %rbp, %rdi
callq 0x2120
movq %rax, %r12
movq %rbx, 0x10(%rsp)
movl 0x14(%rbx), %edx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %rdx
movq 0x20(%rsp), %rbx
shlq $0x3, %rdx
movq %r13, %rdi
movl $0xff, %esi
callq 0x20e0
movq %r13, %r10
movq 0x18(%rsp), %r13
xorl %eax, %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %r11d, %r11d
cmpq %r14, %rax
je 0xc7570
movq 0x20(%r15), %rdx
cmpl $0x0, (%rdx,%rax,8)
js 0xc756b
cmpb $0x1, 0x4(%rdx,%rax,8)
je 0xc756b
movq 0x10(%r13), %rdx
imulq $0x28, %rax, %rsi
cmpl $0x0, 0xc(%rdx,%rsi)
je 0xc756b
addq %rsi, %rdx
movl 0x10(%rdx), %esi
xorl %edi, %edi
cmpq %rdi, %rsi
je 0xc756b
movq 0x18(%rdx), %r8
movq (%r8,%rdi,8), %r8
shrq $0x21, %r8
andq $0x0, (%r10,%r8,8)
imulq $0xc, %r8, %r8
movl 0x4(%rbx,%r8), %r9d
movl (%rbx,%r8), %r8d
andl %ecx, %r8d
subl %r8d, %r9d
addq %r9, %r11
incq %rdi
jmp 0xc753a
incq %rax
jmp 0xc750d
xorl %ecx, %ecx
xorl %esi, %esi
movq 0x10(%rsp), %rdi
movq %r12, 0x38(%rsp)
movq %r11, 0x30(%rsp)
movl 0x14(%rdi), %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
cmpq %r14, %rcx
jae 0xc770f
cmpq $0x0, (%r10,%rcx,8)
je 0xc7707
movq 0x20(%rdi), %rax
cmpl $0x0, (%rax,%rcx,8)
js 0xc7707
movq %rsi, 0x58(%rsp)
imulq $0xc, %rcx, %rax
movl 0x4(%rbx,%rax), %esi
movl (%rbx,%rax), %eax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %eax
subl %eax, %esi
movq %rsi, 0x50(%rsp)
movq %rcx, 0x40(%rsp)
imulq $0x18, %rcx, %r15
addq 0x48(%rsp), %r15
xorl %ebx, %ebx
xorl %ebp, %ebp
movq $0x0, 0x18(%rsp)
xorl %r13d, %r13d
movl 0xc(%r15), %eax
cmpq %rax, %rbp
jae 0xc7687
movq (%r15), %r14
cmpb $0x1, 0x20(%r14,%rbx)
jne 0xc767b
movl 0xc(%r14,%rbx), %esi
movl %esi, 0xc(%rsp)
movq 0x20(%rdi), %rax
cmpl $0x0, (%rax,%rsi,8)
js 0xc762c
movl 0x8(%r14,%rbx), %eax
subl (%r14,%rbx), %eax
cmpq $0x0, (%r10,%rsi,8)
je 0xc766c
addq %rax, %r13
jmp 0xc767b
movq %r10, %r12
movq 0x60(%rsp), %rdi
leaq 0xc(%rsp), %rdx
leaq 0x2c(%rsp), %rcx
callq 0x6c2a2
movl 0xc(%rsp), %esi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %rsi
je 0xc7673
cmpl $0x1, 0x2c(%rsp)
movq %r12, %r10
je 0xc7676
movq 0x10(%rsp), %rdi
movq 0x20(%rdi), %rax
cmpl $0x0, (%rax,%rsi,8)
jns 0xc7617
jmp 0xc767b
addq %rax, 0x18(%rsp)
jmp 0xc767b
movq %r12, %r10
movq 0x10(%rsp), %rdi
incq %rbp
addq $0x28, %rbx
jmp 0xc75ec
movq 0x18(%rsp), %rax
addq %r13, %rax
cvtsi2sd %r13, %xmm0
cvtsi2sd %rax, %xmm1
movq 0x58(%rsp), %rsi
movq 0x50(%rsp), %rcx
addq %rcx, %rsi
mulsd 0x11be92(%rip), %xmm1   # 0x1e3540
ucomisd %xmm0, %xmm1
jbe 0xc76d2
movq 0x38(%rsp), %r12
movq 0xa0(%rsp), %rbp
movq 0x30(%rsp), %r11
movq 0x20(%rsp), %rbx
movq 0x40(%rsp), %rcx
jmp 0xc7707
cqto
idivq %rcx
testq %rax, %rax
sets %cl
movq 0xa0(%rsp), %rbp
cmpq %rbp, %rax
setge %dl
orb %cl, %dl
movq 0x38(%rsp), %r12
movq 0x30(%rsp), %r11
movq 0x20(%rsp), %rbx
movq 0x40(%rsp), %rcx
jne 0xc7707
incq (%r12,%rax,8)
incq %rcx
jmp 0xc7583
xorl %eax, %eax
testq %rbp, %rbp
cmovleq %rax, %rbp
pushq $-0x1
popq %r15
movq %r15, %rcx
cmpq %rax, %rbp
je 0xc7738
movq (%r12,%rax,8), %rdx
cmpq %rcx, %rdx
cmovgq %rax, %r15
cmovgq %rdx, %rcx
incq %rax
jmp 0xc771f
movq %rsi, %xmm0
movq 0x11bb2b(%rip), %xmm1   # 0x1e3270
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x11bb2f(%rip), %xmm2   # 0x1e3280
subpd %xmm2, %xmm0
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
addsd %xmm0, %xmm3
movq %r11, %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
subpd %xmm2, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x11bd9e(%rip), %xmm1   # 0x1e3520
ucomisd %xmm3, %xmm1
pushq $-0x1
popq %rax
cmovaq %rax, %r15
movq %r12, %rdi
movq %r10, %rbx
callq 0x2220
shlq $0x3, %r14
movq %rbx, %rdi
movl $0xff, %esi
movq %r14, %rdx
callq 0x20e0
movq %r15, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq