// File: /workspace/github_cpp_makefile_star2_O1/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, 0xc(%rsp)
movq %rdx, 0x30(%rsp)
movl %esi, 0x10(%rsp)
movq %rdi, %r13
movq 0x18(%rdi), %rbx
testl %r8d, %r8d
movq %rcx, 0x18(%rsp)
movl %r8d, 0x14(%rsp)
je 0xe928b
movq 0x10(%r13), %rax
movl (%rcx), %ecx
movl %ecx, %edx
shrl %edx
movq 0x20(%rbx), %rsi
orl $0x80000000, (%rsi,%rdx,8) # imm = 0x80000000
andl $-0x2, %ecx
xorl %esi, %esi
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
leaq (%rsi,%rcx), %r8
movq 0x28(%rbx), %r9
movq (%r9,%r8,8), %r10
movq %r10, %r9
andq %rdi, %r9
je 0xe924a
shrq $0x20, %r10
leaq (%r10,%r10,2), %r10
shlq $0x3, %r10
addq 0x8(%rbx), %r10
xorl %r11d, %r11d
leaq (%r11,%r11,2), %r14
orb $-0x80, 0xf(%r10,%r14,8)
movl 0x8(%r10,%r14,8), %r14d
xorq $0x1, %r14
movq 0x28(%rbx), %r15
movq (%r15,%r14,8), %r15
movq %r15, %r14
andq %rdi, %r14
je 0xe9242
movq 0x8(%rbx), %r12
shrq $0x20, %r15
shlq $0x3, %r14
leaq (%r14,%r14,2), %r14
leaq (%r15,%r15,2), %r15
leaq (%r12,%r15,8), %r15
addq $0xc, %r15
xorl %r12d, %r12d
movl -0x4(%r15,%r12), %ebp
xorl %r8d, %ebp
cmpl $0x1, %ebp
jne 0xe9239
orb $-0x80, 0x3(%r15,%r12)
addq $0x18, %r12
cmpq %r12, %r14
jne 0xe9226
incq %r11
cmpq %r9, %r11
jne 0xe91e4
leaq 0x1(%rsi), %r8
testq %rsi, %rsi
movq %r8, %rsi
je 0xe91bd
leaq (%rdx,%rdx,4), %rcx
leaq (%rax,%rcx,8), %rdx
movq 0x10(%r13), %rax
movq 0x18(%rsp), %rcx
movl (%rcx), %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
movl 0x4(%rax,%rcx,8), %r12d
movq %rdx, 0x28(%rsp)
movl (%rdx), %eax
movq %rax, 0x20(%rsp)
movl 0x14(%rsp), %r8d
jmp 0xe92a0
movq $0x0, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
xorl %r12d, %r12d
cmpl $0x2, %r8d
jb 0xe93cf
movl %r8d, %eax
movq %rax, 0x38(%rsp)
movl $0x1, %ebp
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl %r12d, 0x8(%rsp)
movq %r13, 0x40(%rsp)
movq 0x10(%r13), %rax
movq 0x18(%rsp), %r15
movl (%r15,%rbp,4), %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
leaq (%rax,%rcx,8), %rsi
movl 0x4(%rsi), %edx
cmpl %edx, 0x8(%rsp)
ja 0xe92f2
movl (%rsi), %edi
movq %rdi, 0x20(%rsp)
movl %edx, 0x8(%rsp)
movl 0x4(%rax,%rcx,8), %r13d
movq 0x28(%rsp), %rdi
callq 0xe90fe
movl (%r15,%rbp,4), %eax
movl %eax, %ecx
shrl %ecx
movq 0x20(%rbx), %rdx
orl $0x80000000, (%rdx,%rcx,8) # imm = 0x80000000
andl $-0x2, %eax
xorl %ecx, %ecx
leaq (%rcx,%rax), %rdx
movq 0x28(%rbx), %rsi
movq (%rsi,%rdx,8), %rdi
movq %rdi, %rsi
andq %r14, %rsi
je 0xe93a7
shrq $0x20, %rdi
leaq (%rdi,%rdi,2), %rdi
shlq $0x3, %rdi
addq 0x8(%rbx), %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(%rbx), %r10
movq (%r10,%r9,8), %r10
movq %r10, %r9
andq %r14, %r9
je 0xe939f
movq 0x8(%rbx), %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), %r15d
xorl %edx, %r15d
cmpl $0x1, %r15d
jne 0xe9396
orb $-0x80, 0x3(%r10,%r11)
addq $0x18, %r11
cmpq %r11, %r9
jne 0xe9382
incq %r8
cmpq %rsi, %r8
jne 0xe9340
leaq 0x1(%rcx), %rdx
testq %rcx, %rcx
movq %rdx, %rcx
je 0xe9319
addl %r13d, %r12d
incq %rbp
cmpq 0x38(%rsp), %rbp
movq 0x40(%rsp), %r13
jne 0xe92c7
jmp 0xe93d4
movl %r12d, 0x8(%rsp)
cmpl $0x0, 0x14(%rsp)
movq 0x30(%rsp), %r15
je 0xe9651
movq 0x20(%rbx), %rax
movq 0x18(%rsp), %rdx
movl (%rdx), %ecx
shrl %ecx
andl $0x7fffffff, (%rax,%rcx,8) # imm = 0x7FFFFFFF
movq 0x10(%r13), %rax
movl (%rdx), %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
movl 0x8(%rsp), %esi
movl %esi, 0x4(%rax,%rcx,8)
shrl %r12d
movl (%rdx), %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
cmpl 0x4(%rax,%rcx,8), %r12d
jbe 0xe9429
leaq (%rax,%rcx,8), %rcx
addq $0x4, %rcx
movl %r12d, (%rcx)
movq 0x18(%rsp), %rsi
movl (%rsi), %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
incl 0x8(%rax,%rcx,8)
movq 0x20(%rsp), %rdi
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(%rbx), %rcx
movl (%rsi), %edx
shrl %edx
andl (%rcx,%rdx,8), %eax
orl %edi, %eax
movl %eax, (%rcx,%rdx,8)
movl (%rsi), %r12d
cmpl $0x1, 0xc(%rsp)
je 0xe956e
cmpl $0x0, 0xc(%rsp)
jne 0xe9651
movl 0x10(%rsp), %r14d
cmpl $-0x1, %r14d
je 0xe94f3
movq 0x10(%r13), %rax
movl %r14d, %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 %rbx, %rdi
movl %r14d, %esi
movl %r12d, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %rax
pushq %rax
callq 0xe907b
addq $0x10, %rsp
movl %r12d, %esi
xorl $0x1, %esi
movq 0x10(%r13), %rax
movl %r12d, %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
andl (%rax,%rcx,8), %ebp
xorl $0x1, %r14d
movq %rbx, %rdi
movl %ebp, %edx
movl %r14d, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
xorl %eax, %eax
pushq %rax
pushq %rax
callq 0xe907b
addq $0x10, %rsp
cmpl $-0x1, %r15d
je 0xe9563
movq 0x10(%r13), %rax
movl %r12d, %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl (%rax,%rcx,8), %edx
andl %ebp, %edx
xorl %r14d, %r14d
movq %rbx, %rdi
movl %r12d, %esi
movl %r15d, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %r14
pushq %r14
callq 0xe907b
addq $0x10, %rsp
movl %r15d, %esi
xorl $0x1, %esi
movq 0x10(%r13), %rax
movl %r15d, %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
andl (%rax,%rcx,8), %ebp
movl %r12d, %ecx
xorl $0x1, %ecx
movq %rbx, %rdi
movl %ebp, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %r14
pushq %r14
callq 0xe907b
addq $0x10, %rsp
cmpl $0x1, 0xc(%rsp)
jne 0xe9651
movl 0x10(%rsp), %r14d
cmpl $-0x1, %r14d
je 0xe95e0
movq 0x10(%r13), %rax
movl %r12d, %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl (%rax,%rcx,8), %edx
andl %ebp, %edx
movl %r14d, %ecx
xorl $0x1, %ecx
xorl %eax, %eax
movq %rbx, %rdi
movl %r12d, %esi
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %rax
pushq %rax
callq 0xe907b
addq $0x10, %rsp
movq 0x10(%r13), %rax
movl %r14d, %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
andl (%rax,%rcx,8), %ebp
movl %r12d, %ecx
xorl $0x1, %ecx
movq %rbx, %rdi
movl %r14d, %esi
movl %ebp, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
xorl %eax, %eax
pushq %rax
pushq %rax
callq 0xe907b
addq $0x10, %rsp
cmpl $-0x1, %r15d
je 0xe9651
movq 0x10(%r13), %rax
movl %r12d, %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl (%rax,%rcx,8), %edx
andl %ebp, %edx
xorl %r14d, %r14d
movq %rbx, %rdi
movl %r12d, %esi
movl %r15d, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %r14
pushq %r14
callq 0xe907b
addq $0x10, %rsp
movl %r15d, %esi
xorl $0x1, %esi
movq 0x10(%r13), %rax
shrl %r15d
leaq (%r15,%r15,4), %rcx
andl (%rax,%rcx,8), %ebp
xorl $0x1, %r12d
movq %rbx, %rdi
movl %ebp, %edx
movl %r12d, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %r14
pushq %r14
callq 0xe907b
addq $0x58, %rsp
jmp 0xe9655
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq