// File: /workspace/github_cpp_makefile_star2_O1/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 $0x78, %rsp
movq %r9, %rbp
movq %r8, 0x40(%rsp)
movq %rcx, %r12
movq %rdx, 0x58(%rsp)
movq %rsi, %r14
movq %rdi, %r15
movq 0xb0(%rsp), %rdi
movq 0x18(%r15), %rbx
movl 0x14(%rbx), %r13d
movl $0x8, %esi
callq 0x2120
movq %rax, 0x30(%rsp)
movq %r14, 0x10(%rsp)
movl 0x14(%r14), %edx
movl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
andq %r14, %rdx
shlq $0x3, %rdx
movq %r12, %rdi
movl $0xff, %esi
callq 0x20e0
xorpd %xmm0, %xmm0
xorpd %xmm2, %xmm2
andq %r14, %r13
je 0x1054ce
movq 0x20(%rbx), %rcx
xorl %eax, %eax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
xorl %esi, %esi
cmpl $0x0, (%rcx,%rsi,8)
js 0x10549d
cmpb $0x1, 0x4(%rcx,%rsi,8)
je 0x10549d
movq 0x10(%r15), %rdi
leaq (%rsi,%rsi,4), %r8
cmpl $0x0, 0xc(%rdi,%r8,8)
je 0x10549d
leaq (%rdi,%r8,8), %r8
movl 0x10(%r8), %edi
testq %rdi, %rdi
je 0x10549d
movq 0x18(%r8), %r8
xorl %r9d, %r9d
movq (%r8,%r9,8), %r10
shrq $0x21, %r10
movq $0x0, (%r12,%r10,8)
leaq (%r10,%r10,2), %r10
movl 0x4(%rbp,%r10,4), %r11d
movl (%rbp,%r10,4), %r10d
andl %edx, %r10d
subl %r10d, %r11d
addq %r11, %rax
incq %r9
cmpq %r9, %rdi
jne 0x10546e
incq %rsi
cmpq %r13, %rsi
jne 0x10543d
movq %rax, %xmm1
punpckldq 0x11fd8e(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x11fd96(%rip), %xmm1   # 0x225250
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
mulsd 0x120002(%rip), %xmm2   # 0x2254d0
movq 0x10(%rsp), %rdi
movl 0x14(%rdi), %r13d
andq $0x7fffffff, %r13       # imm = 0x7FFFFFFF
je 0x1056a2
movapd %xmm2, 0x60(%rsp)
xorl %esi, %esi
xorl %ecx, %ecx
movq %r12, 0x20(%rsp)
movq %rbp, 0x38(%rsp)
cmpq $0x0, (%r12,%rcx,8)
je 0x105664
movq 0x20(%rdi), %rax
cmpl $0x0, (%rax,%rcx,8)
js 0x105664
movq %rsi, 0x50(%rsp)
movq %rcx, 0x18(%rsp)
leaq (%rcx,%rcx,2), %rax
movl 0x4(%rbp,%rax,4), %ecx
movq %rcx, 0x48(%rsp)
movl (%rbp,%rax,4), %edx
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %edx
movl %edx, 0x28(%rsp)
movq 0x40(%rsp), %rcx
cmpl $0x0, 0xc(%rcx,%rax,8)
je 0x1055ec
leaq (%rcx,%rax,8), %r14
xorl %ebx, %ebx
xorl %r15d, %r15d
xorl %ebp, %ebp
xorl %r12d, %r12d
movq (%r14), %r13
cmpb $0x1, 0x20(%r13,%rbx)
jne 0x1055d6
movl 0xc(%r13,%rbx), %esi
movl %esi, 0xc(%rsp)
movq 0x20(%rdi), %rcx
cmpl $0x0, (%rcx,%rsi,8)
js 0x105592
movl 0xc(%rsp), %ecx
movl 0x8(%r13,%rbx), %esi
subl (%r13,%rbx), %esi
movq 0x20(%rsp), %rdx
cmpq $0x0, (%rdx,%rcx,8)
je 0x1055cc
addq %rsi, %r15
jmp 0x1055d6
movq 0x58(%rsp), %rdi
leaq 0xc(%rsp), %rdx
leaq 0x2c(%rsp), %rcx
callq 0xa306f
movl 0xc(%rsp), %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rdx
je 0x1055d1
cmpl $0x1, 0x2c(%rsp)
je 0x1055d1
movq 0x10(%rsp), %rdi
movq 0x20(%rdi), %rcx
cmpl $0x0, (%rcx,%rdx,8)
jns 0x105573
jmp 0x1055d6
addq %rsi, %rbp
jmp 0x1055d6
movq 0x10(%rsp), %rdi
incq %r12
movl 0xc(%r14), %ecx
addq $0x28, %rbx
cmpq %rcx, %r12
jb 0x105555
jmp 0x1055f1
xorl %ebp, %ebp
xorl %r15d, %r15d
movq 0x48(%rsp), %rcx
subl 0x28(%rsp), %ecx
addq %r15, %rbp
xorps %xmm0, %xmm0
cvtsi2sd %r15, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rbp, %xmm1
movq 0x50(%rsp), %rsi
addq %rcx, %rsi
mulsd 0x11fedb(%rip), %xmm1   # 0x2254f8
ucomisd %xmm0, %xmm1
jbe 0x10562f
movq 0x20(%rsp), %r12
movq 0x38(%rsp), %rbp
jmp 0x10565f
movq %rbp, %rax
cqto
idivq %rcx
testq %rax, %rax
sets %cl
cmpq 0xb0(%rsp), %rax
setge %dl
orb %cl, %dl
movq 0x20(%rsp), %r12
movq 0x38(%rsp), %rbp
jne 0x10565f
movq 0x30(%rsp), %rcx
incq (%rcx,%rax,8)
movq 0x18(%rsp), %rcx
incq %rcx
movl 0x14(%rdi), %r13d
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
cmpq %r13, %rcx
jb 0x1054f8
movq %rsi, %xmm1
punpckldq 0x11fbb8(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x11fbc0(%rip), %xmm1   # 0x225250
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movapd 0x60(%rsp), %xmm2
movq $-0x1, %rax
movq $-0x1, %r14
movq 0xb0(%rsp), %r8
testq %r8, %r8
movq 0x30(%rsp), %rdi
jle 0x1056e2
movq $-0x1, %rcx
xorl %edx, %edx
movq (%rdi,%rdx,8), %rsi
cmpq %rcx, %rsi
cmovgq %rdx, %r14
cmovgq %rsi, %rcx
incq %rdx
cmpq %rdx, %r8
jne 0x1056cb
ucomisd %xmm0, %xmm2
cmovaq %rax, %r14
callq 0x2220
shlq $0x3, %r13
movq %r12, %rdi
movl $0xff, %esi
movq %r13, %rdx
callq 0x20e0
movq %r14, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq