// File: /workspace/github_cpp_makefile_star2_O3/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
movq %rdi, 0x10(%rsp)
testl %r8d, %r8d
je 0xeaf47
movq %rcx, %r13
movl %r8d, 0x18(%rsp)
movl %r9d, 0x20(%rsp)
movl %esi, 0x24(%rsp)
movq %rdx, 0x30(%rsp)
movq 0x10(%rsp), %rax
movq 0x18(%rax), %r14
movq 0x10(%rax), %rax
movl (%rcx), %ecx
movl %ecx, %edx
shrl %edx
movq 0x20(%r14), %rsi
orl $0x80000000, (%rsi,%rdx,8) # imm = 0x80000000
andl $-0x2, %ecx
movb $0x1, %sil
xorl %edi, %edi
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
orq %rcx, %rdi
movq 0x28(%r14), %r9
movq (%r9,%rdi,8), %r10
movq %r10, %r9
andq %r8, %r9
je 0xeabe1
shrq $0x20, %r10
leaq (%r10,%r10,2), %r10
shlq $0x3, %r10
addq 0x8(%r14), %r10
xorl %r11d, %r11d
leaq (%r11,%r11,2), %rbx
orb $-0x80, 0xf(%r10,%rbx,8)
movl 0x8(%r10,%rbx,8), %ebx
xorq $0x1, %rbx
movq 0x28(%r14), %r15
movq (%r15,%rbx,8), %r15
movq %r15, %rbx
andq %r8, %rbx
je 0xeabd9
movq 0x8(%r14), %r12
shrq $0x20, %r15
shlq $0x3, %rbx
leaq (%rbx,%rbx,2), %rbx
leaq (%r15,%r15,2), %r15
leaq (%r12,%r15,8), %r15
addq $0xc, %r15
xorl %r12d, %r12d
movl -0x4(%r15,%r12), %ebp
xorl %edi, %ebp
cmpl $0x1, %ebp
jne 0xeabd0
orb $-0x80, 0x3(%r15,%r12)
addq $0x18, %r12
cmpq %r12, %rbx
jne 0xeabbe
incq %r11
cmpq %r9, %r11
jne 0xeab7c
movl $0x1, %edi
testb $0x1, %sil
movl $0x0, %esi
jne 0xeab56
leaq (%rdx,%rdx,4), %rcx
leaq (%rax,%rcx,8), %rdx
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
movl (%r13), %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
movl 0x4(%rax,%rcx,8), %ebx
movq %rdx, 0x28(%rsp)
movl (%rdx), %eax
movl %eax, 0x1c(%rsp)
movl 0x18(%rsp), %eax
cmpl $0x1, %eax
jne 0xeac31
movl %ebx, 0xc(%rsp)
jmp 0xead55
movl %eax, %eax
movq %rax, 0x38(%rsp)
movl $0x1, %ebp
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl %ebx, 0xc(%rsp)
movq %r13, 0x40(%rsp)
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
movl (%r13,%rbp,4), %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
leaq (%rax,%rcx,8), %rsi
movl 0x4(%rsi), %r15d
cmpl %r15d, 0xc(%rsp)
ja 0xeac7a
movl (%rsi), %eax
movl %eax, 0x1c(%rsp)
movl %r15d, 0xc(%rsp)
movq 0x28(%rsp), %rdi
callq 0xeaa85
movl (%r13,%rbp,4), %eax
movl %eax, %ecx
shrl %ecx
movq 0x20(%r14), %rdx
orl $0x80000000, (%rdx,%rcx,8) # imm = 0x80000000
andl $-0x2, %eax
movb $0x1, %cl
xorl %edx, %edx
orq %rax, %rdx
movq 0x28(%r14), %rsi
movq (%rsi,%rdx,8), %rdi
movq %rdi, %rsi
andq %r12, %rsi
je 0xead2c
shrq $0x20, %rdi
leaq (%rdi,%rdi,2), %rdi
shlq $0x3, %rdi
addq 0x8(%r14), %rdi
xorl %r8d, %r8d
leaq (%r8,%r8,2), %r9
orb $-0x80, 0xf(%rdi,%r9,8)
movl 0x8(%rdi,%r9,8), %r9d
xorq $0x1, %r9
movq 0x28(%r14), %r10
movq (%r10,%r9,8), %r10
movq %r10, %r9
andq %r12, %r9
je 0xead24
movq 0x8(%r14), %r11
shrq $0x20, %r10
shlq $0x3, %r9
leaq (%r9,%r9,2), %r9
leaq (%r10,%r10,2), %r10
leaq (%r11,%r10,8), %r10
addq $0xc, %r10
xorl %r11d, %r11d
movl -0x4(%r10,%r11), %r13d
xorl %edx, %r13d
cmpl $0x1, %r13d
jne 0xead1b
orb $-0x80, 0x3(%r10,%r11)
addq $0x18, %r11
cmpq %r11, %r9
jne 0xead07
incq %r8
cmpq %rsi, %r8
jne 0xeacc5
movl $0x1, %edx
testb $0x1, %cl
movl $0x0, %ecx
jne 0xeac9f
addl %r15d, %ebx
incq %rbp
cmpq 0x38(%rsp), %rbp
movq 0x40(%rsp), %r13
jne 0xeac4c
cmpl $0x0, 0x18(%rsp)
movq 0x30(%rsp), %r12
movl 0x24(%rsp), %r15d
movl 0x20(%rsp), %esi
je 0xeaf47
movq 0x20(%r14), %rax
movl (%r13), %ecx
shrl %ecx
andl $0x7fffffff, (%rax,%rcx,8) # imm = 0x7FFFFFFF
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
movl (%r13), %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
movl 0xc(%rsp), %edx
movl %edx, 0x4(%rax,%rcx,8)
shrl %ebx
movl (%r13), %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rdx
cmpl 0x4(%rax,%rdx,8), %ebx
jbe 0xeadbc
leaq (%rax,%rdx,8), %rcx
addq $0x4, %rcx
movl %ebx, (%rcx)
movl (%r13), %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
incl 0x8(%rax,%rcx,8)
movl 0x1c(%rsp), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl $0x80000000, %eax       # imm = 0x80000000
movq 0x28(%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), %ebx
cmpl $0x1, %esi
je 0xeae66
testl %esi, %esi
jne 0xeaf47
cmpl $-0x1, %r15d
je 0xeaed5
movq 0x10(%rsp), %r13
movq 0x10(%r13), %rax
movl %r15d, %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl (%rax,%rcx,8), %edx
andl %ebp, %edx
xorl %eax, %eax
movq %r14, %rdi
movl %r15d, %esi
movl %ebx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %rax
pushq %rax
callq 0xeaa02
addq $0x10, %rsp
movl %ebx, %esi
xorl $0x1, %esi
movq 0x10(%r13), %rax
movl %ebx, %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
andl (%rax,%rcx,8), %ebp
xorl $0x1, %r15d
movq %r14, %rdi
movl %ebp, %edx
movl %r15d, %ecx
jmp 0xeaec2
cmpl $-0x1, %r15d
je 0xeaed5
movq 0x10(%rsp), %r13
movq 0x10(%r13), %rax
movl %ebx, %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl (%rax,%rcx,8), %edx
andl %ebp, %edx
movl %r15d, %ecx
xorl $0x1, %ecx
xorl %eax, %eax
movq %r14, %rdi
movl %ebx, %esi
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %rax
pushq %rax
callq 0xeaa02
addq $0x10, %rsp
movq 0x10(%r13), %rax
movl %r15d, %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
andl (%rax,%rcx,8), %ebp
movl %ebx, %ecx
xorl $0x1, %ecx
movq %r14, %rdi
movl %r15d, %esi
movl %ebp, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
xorl %eax, %eax
pushq %rax
pushq %rax
callq 0xeaa02
addq $0x10, %rsp
cmpl $-0x1, %r12d
je 0xeaf47
movq 0x10(%rsp), %r13
movq 0x10(%r13), %rax
movl %ebx, %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl (%rax,%rcx,8), %edx
andl %ebp, %edx
xorl %r15d, %r15d
movq %r14, %rdi
movl %ebx, %esi
movl %r12d, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %r15
pushq %r15
callq 0xeaa02
addq $0x10, %rsp
movl %r12d, %esi
xorl $0x1, %esi
movq 0x10(%r13), %rax
shrl %r12d
leaq (%r12,%r12,4), %rcx
andl (%rax,%rcx,8), %ebp
xorl $0x1, %ebx
movq %r14, %rdi
movl %ebp, %edx
movl %ebx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %r15
pushq %r15
callq 0xeaa02
addq $0x58, %rsp
jmp 0xeaf4b
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq