// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: merge_nodes(ma_ug_t*, unsigned int, unsigned int, unsigned int*, unsigned int, unsigned int)
void merge_nodes(ma_ug_t *ug, uint32_t startID, uint32_t endId, uint32_t* a, uint32_t n, 
uint32_t type)
{
    ma_utg_t *v_x = NULL, *v_y = NULL;
    asg_t* nsg = ug->g;
    kvec_t(uint32_t) u_vecs;
    uint32_t i = 0, maxEvaluateLen = 0, maxBaseLen = 0, v, totalEvaluateLen = 0;
    u_vecs.a = a, u_vecs.n = n;
    if(u_vecs.n > 0) ///u_vecs does not contain startID && endId
    {
        v_x = &(ug->u.a[u_vecs.a[0]>>1]);
        asg_seq_del(nsg, u_vecs.a[0]>>1);
        maxEvaluateLen = EvaluateLen(ug->u, u_vecs.a[0]>>1);
        maxBaseLen = v_x->len;
        totalEvaluateLen += EvaluateLen(ug->u, u_vecs.a[0]>>1);
    }

    
    for (i = 1; i < u_vecs.n; i++)
    {
        v_y = &(ug->u.a[u_vecs.a[i]>>1]);
        if(maxEvaluateLen <= EvaluateLen(ug->u, u_vecs.a[i]>>1))
        {
            maxEvaluateLen = EvaluateLen(ug->u, u_vecs.a[i]>>1);
            maxBaseLen = v_y->len;
        }
        totalEvaluateLen += EvaluateLen(ug->u, u_vecs.a[i]>>1);

        
        append_ma_utg_t(v_x, v_y);
        asg_seq_del(nsg, u_vecs.a[i]>>1);
    }


    if(u_vecs.n > 0)
    {
        i = 0;
        nsg->seq[u_vecs.a[0]>>1].del = !!(0);


        EvaluateLen(ug->u, u_vecs.a[0]>>1) = maxEvaluateLen;
        totalEvaluateLen = totalEvaluateLen / 2;
        if(totalEvaluateLen > EvaluateLen(ug->u, u_vecs.a[0]>>1))
        {
            EvaluateLen(ug->u, u_vecs.a[0]>>1) = totalEvaluateLen;
        }
        IsMerge(ug->u, u_vecs.a[0]>>1)++;


        v_x->len = maxBaseLen;
        /****************************may have bugs********************************/
        nsg->seq[u_vecs.a[0]>>1].len = maxBaseLen;
        /****************************may have bugs********************************/
        v = u_vecs.a[0];

        if(type == UNROLL)
        {
            if(startID != (uint32_t)-1)
            {
                asg_append_edges_to_srt(nsg, startID, ug->u.a[startID>>1].len, v, 0, 0, 0 ,0);
                asg_append_edges_to_srt(nsg, v^1, ug->u.a[v>>1].len, startID^1, 0, 0, 0, 0);
            }
            
            if(endId != (uint32_t)-1)
            {
                asg_append_edges_to_srt(nsg, v, ug->u.a[v>>1].len, endId, 0, 0, 0 ,0);
                asg_append_edges_to_srt(nsg, endId^1, ug->u.a[endId>>1].len, v^1, 0, 0, 0 ,0);
            }
        }

        if(type == CONVEX)
        {
            if(startID != (uint32_t)-1)
            {
                asg_append_edges_to_srt(nsg, v, ug->u.a[v>>1].len, startID^1, 0, 0, 0, 0);
                asg_append_edges_to_srt(nsg, startID, ug->u.a[startID>>1].len, v^1, 0, 0, 0 ,0);
            }

            if(endId != (uint32_t)-1)
            {
                asg_append_edges_to_srt(nsg, v, ug->u.a[v>>1].len, endId, 0, 0, 0 ,0);
                asg_append_edges_to_srt(nsg, endId^1, ug->u.a[endId>>1].len, v^1, 0, 0, 0 ,0);
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %r9d, 0x40(%rsp)
movl %r8d, %ebp
movq %rcx, %r13
movq %rdx, 0x28(%rsp)
movl %esi, 0x24(%rsp)
movq %rdi, %rsi
movq 0x18(%rdi), %rdi
testl %r8d, %r8d
movq %rsi, 0x18(%rsp)
je 0xac1d7
movq 0x10(%rsi), %r15
movl (%r13), %esi
shrl %esi
imulq $0x28, %rsi, %r12
leaq (%r15,%r12), %rax
movq %rax, 0x38(%rsp)
movq %rdi, %r14
callq 0x6f8da
movq 0x18(%rsp), %rsi
movq 0x10(%rsi), %rax
movl (%r13), %ecx
shrl %ecx
imulq $0x28, %rcx, %rcx
movl 0x4(%rax,%rcx), %ebx
movl (%r15,%r12), %eax
movq %rax, 0x30(%rsp)
jmp 0xac1ee
movq %rdi, %r14
movq $0x0, 0x30(%rsp)
movq $0x0, 0x38(%rsp)
xorl %ebx, %ebx
movl %ebp, 0x44(%rsp)
movl %ebp, %r15d
pushq $0x1
popq %r12
movl %ebx, %ebp
cmpq %r15, %r12
jae 0xac249
movq 0x10(%rsi), %rax
movl (%r13,%r12,4), %ecx
shrl %ecx
imulq $0x28, %rcx, %rcx
leaq (%rax,%rcx), %rsi
movl 0x4(%rax,%rcx), %eax
cmpl %eax, %ebx
ja 0xac224
movl (%rsi), %ecx
movq %rcx, 0x30(%rsp)
movl %eax, %ebx
addl %eax, %ebp
movq 0x38(%rsp), %rdi
callq 0xac0f0
movl (%r13,%r12,4), %esi
shrl %esi
movq %r14, %rdi
callq 0x6f8da
incq %r12
movq 0x18(%rsp), %rsi
jmp 0xac1fb
cmpl $0x0, 0x44(%rsp)
je 0xac452
movq 0x20(%r14), %rax
movl (%r13), %ecx
shrl %ecx
andl $0x7fffffff, (%rax,%rcx,8) # imm = 0x7FFFFFFF
movq 0x10(%rsi), %rax
movl (%r13), %ecx
shrl %ecx
imulq $0x28, %rcx, %rcx
movl %ebx, 0x4(%rax,%rcx)
shrl %ebp
movl (%r13), %ecx
shrl %ecx
imulq $0x28, %rcx, %rdx
cmpl 0x4(%rax,%rdx), %ebp
jbe 0xac299
leaq (%rax,%rdx), %rcx
addq $0x4, %rcx
movl %ebp, (%rcx)
movl (%r13), %ecx
shrl %ecx
imulq $0x28, %rcx, %rcx
incl 0x8(%rax,%rcx)
movq 0x30(%rsp), %rdi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl $0x80000000, %eax       # imm = 0x80000000
movq 0x38(%rsp), %rdx
movl (%rdx), %ecx
andl %eax, %ecx
orl %edi, %ecx
movl %ecx, (%rdx)
movq 0x20(%r14), %rcx
movl (%r13), %edx
shrl %edx
andl (%rcx,%rdx,8), %eax
orl %edi, %eax
movl %eax, (%rcx,%rdx,8)
movl (%r13), %r13d
movl 0x40(%rsp), %eax
cmpl $0x1, %eax
je 0xac35a
testl %eax, %eax
jne 0xac452
movl 0x24(%rsp), %r15d
cmpl $-0x1, %r15d
je 0xac3d8
movq 0x10(%rsi), %rax
movl %r15d, %ecx
shrl %ecx
imulq $0x28, %rcx, %rcx
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl (%rax,%rcx), %edx
andl %ebp, %edx
andl $0x0, 0x8(%rsp)
andl $0x0, (%rsp)
movq %r14, %rdi
movl %r15d, %esi
movl %r13d, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xac072
movl %r13d, %esi
xorl $0x1, %esi
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
movl %r13d, %ecx
shrl %ecx
imulq $0x28, %rcx, %rcx
andl (%rax,%rcx), %ebp
xorl $0x1, %r15d
andl $0x0, 0x8(%rsp)
andl $0x0, (%rsp)
movq %r14, %rdi
movl %ebp, %edx
movl %r15d, %ecx
jmp 0xac3c8
movl 0x24(%rsp), %r15d
cmpl $-0x1, %r15d
je 0xac3d8
movq 0x10(%rsi), %rax
movl %r13d, %ecx
shrl %ecx
imulq $0x28, %rcx, %rcx
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl (%rax,%rcx), %edx
andl %ebp, %edx
movl %r15d, %ecx
xorl $0x1, %ecx
andl $0x0, 0x8(%rsp)
andl $0x0, (%rsp)
movq %r14, %rdi
movl %r13d, %esi
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xac072
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
movl %r15d, %ecx
shrl %ecx
imulq $0x28, %rcx, %rcx
andl (%rax,%rcx), %ebp
movl %r13d, %ecx
xorl $0x1, %ecx
andl $0x0, 0x8(%rsp)
andl $0x0, (%rsp)
movq %r14, %rdi
movl %r15d, %esi
movl %ebp, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xac072
movq 0x18(%rsp), %rsi
cmpl $-0x1, 0x28(%rsp)
je 0xac452
movq 0x10(%rsi), %rax
movl %r13d, %ecx
shrl %ecx
imulq $0x28, %rcx, %rcx
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl (%rax,%rcx), %edx
andl %ebp, %edx
andl $0x0, 0x8(%rsp)
andl $0x0, (%rsp)
movq %r14, %rbx
movq %r14, %rdi
movq %rsi, %r15
movl %r13d, %esi
movq 0x28(%rsp), %r14
movl %r14d, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xac072
movl %r14d, %esi
xorl $0x1, %esi
movq 0x10(%r15), %rax
shrl %r14d
imulq $0x28, %r14, %rcx
andl (%rax,%rcx), %ebp
xorl $0x1, %r13d
andl $0x0, 0x8(%rsp)
andl $0x0, (%rsp)
movq %rbx, %rdi
movl %ebp, %edx
movl %r13d, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xac072
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq