// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: merge_unitig_content(ma_utg_t*, ma_ug_t*, asg_t*, kvec_asg_arc_t_warp*)
void merge_unitig_content(ma_utg_t* collection, ma_ug_t* ug, asg_t* read_g, kvec_asg_arc_t_warp* edge)
{
    if(collection->m == 0) return;
    uint32_t i, j, index = 0, uId, ori;
    uint64_t totalLen;
    ma_utg_t* query = NULL;
    for (i = 0; i < collection->n; i++)
    {
        uId = collection->a[i]>>33;
        ori = collection->a[i]>>32&1;
        query = &(ug->u.a[uId]);
        index += query->n;
    }

    uint64_t* buffer = (uint64_t*)malloc(sizeof(uint64_t)*index);
    uint64_t* aim = NULL;
    index = 0;
    for (i = 0; i < collection->n; i++)
    {
        uId = collection->a[i]>>33;
        ori = collection->a[i]>>32&1;
        query = &(ug->u.a[uId]);
        aim = buffer + index;
        if(ori == 1)
        {
            for (j = 0; j < query->n; j++)
            {
                aim[query->n - j - 1] = (query->a[j])^(uint64_t)(0x100000000);
            }
        }
        else
        {
            for (j = 0; j < query->n; j++)
            {
                aim[j] = query->a[j];
            }
        }
        index += query->n;
        free(query->a);query->m=0;query->a=NULL;
    }

    if(index == 0) return;

    ///fill_unitig(buffer, index, read_g, edge, collection->circ, &totalLen);
    fill_unitig(buffer, index, read_g, edge, /**collection->circ**/
                    (collection->n == 1 && ug->u.a[collection->a[0]>>33].circ), &totalLen);

    ///important. must be here
    if(collection->n == 1 && ug->u.a[collection->a[0]>>33].circ) collection->circ = 1;
    
    free(collection->a);
    collection->a = buffer;
    collection->n = collection->m = index;
    collection->len = totalLen;
    if(!collection->circ)
    {
        collection->start = collection->a[0]>>32;
        collection->end = (collection->a[collection->n-1]>>32)^1;
    }
    else
    {
        collection->start = collection->end = UINT32_MAX;
    }
    
    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, 0x8(%rsp)
cmpl $0x0, 0xc(%rdi)
je 0xcc5fc
movq %rcx, %r14
movq %rdx, %r15
movq %rdi, %rbx
movl 0x10(%rdi), %eax
xorl %edi, %edi
testq %rax, %rax
je 0xcc45e
movq 0x18(%rbx), %rcx
movq 0x8(%rsp), %rdx
movq 0x10(%rdx), %rdx
xorl %esi, %esi
movq (%rcx,%rsi,8), %r8
shrq $0x21, %r8
leaq (%r8,%r8,4), %r8
addl 0x10(%rdx,%r8,8), %edi
incq %rsi
cmpq %rsi, %rax
jne 0xcc43f
movl %edi, %edi
shlq $0x3, %rdi
callq 0x2280
movq %rax, %r13
cmpl $0x0, 0x10(%rbx)
je 0xcc540
movq %r15, 0x10(%rsp)
movq %r14, 0x18(%rsp)
xorl %r15d, %r15d
movabsq $0x100000000, %r12      # imm = 0x100000000
xorl %ebp, %ebp
movq 0x18(%rbx), %rax
movq (%rax,%r15,8), %rdx
movq %rdx, %rax
shrq $0x21, %rax
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rcx
leaq (%rax,%rax,4), %rax
leaq (%rcx,%rax,8), %r14
movl %ebp, %eax
leaq (,%rax,8), %rax
addq %r13, %rax
movl 0x10(%r14), %ecx
testq %r12, %rdx
jne 0xcc4dc
testq %rcx, %rcx
je 0xcc502
movq 0x18(%r14), %rdx
xorl %esi, %esi
movq (%rdx,%rsi,8), %rdi
movq %rdi, (%rax,%rsi,8)
incq %rsi
cmpq %rsi, %rcx
jne 0xcc4ca
jmp 0xcc502
testq %rcx, %rcx
je 0xcc502
movq 0x18(%r14), %rdx
leal -0x1(%rcx), %esi
xorl %edi, %edi
movq (%rdx,%rdi,8), %r8
xorq %r12, %r8
movl %esi, %r9d
movq %r8, (%rax,%r9,8)
incq %rdi
decl %esi
cmpq %rdi, %rcx
jne 0xcc4ea
addl 0x10(%r14), %ebp
movq 0x18(%r14), %rdi
callq 0x2220
movl $0x0, 0xc(%r14)
movq $0x0, 0x18(%r14)
incq %r15
movl 0x10(%rbx), %eax
cmpq %rax, %r15
jb 0xcc489
cmpl $0x1, %eax
sete %al
movq 0x18(%rsp), %r14
movq 0x10(%rsp), %r15
jmp 0xcc544
xorl %ebp, %ebp
xorl %eax, %eax
testl %ebp, %ebp
je 0xcc5fc
xorl %r8d, %r8d
testb %al, %al
je 0xcc573
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x18(%rbx), %rcx
movq (%rcx), %rcx
shrq $0x21, %rcx
leaq (%rcx,%rcx,4), %rcx
movl (%rax,%rcx,8), %r8d
shrl $0x1f, %r8d
leaq 0x20(%rsp), %r9
movq %r13, %rdi
movl %ebp, %esi
movq %r15, %rdx
movq %r14, %rcx
callq 0x10e9f4
cmpl $0x1, 0x10(%rbx)
jne 0xcc5b0
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x18(%rbx), %rcx
movq (%rcx), %rcx
shrq $0x21, %rcx
leaq (%rcx,%rcx,4), %rcx
cmpl $0x0, (%rax,%rcx,8)
jns 0xcc5b0
orb $-0x80, 0x3(%rbx)
movq 0x18(%rbx), %rdi
callq 0x2220
movq %r13, 0x18(%rbx)
movl %ebp, 0xc(%rbx)
movl %ebp, 0x10(%rbx)
movl (%rbx), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl 0x20(%rsp), %ecx
movl %eax, %edx
andl $0x80000000, %edx       # imm = 0x80000000
orl %ecx, %edx
movl %edx, (%rbx)
testl %eax, %eax
js 0xcc5f4
movl 0x4(%r13), %eax
movl %eax, 0x4(%rbx)
decl %ebp
movl 0x4(%r13,%rbp,8), %eax
xorl $0x1, %eax
movl %eax, 0x8(%rbx)
jmp 0xcc5fc
movq $-0x1, 0x4(%rbx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq