// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Purge_Dups.cpp
// Function: fill_unitig(unsigned long*, unsigned int, asg_t*, kvec_asg_arc_t_warp*, unsigned int, unsigned long*)
void fill_unitig(uint64_t* buffer, uint32_t bufferLen, asg_t* read_g, kvec_asg_arc_t_warp* edge,
uint32_t is_circle, uint64_t* rLen)
{
    uint32_t i, k, totalLen, v, w, nv, l;
    asg_arc_t *av  = NULL;
    (*rLen) = totalLen = 0;
    for (i = 0; i < bufferLen - 1; i++)
    {
        v = (uint64_t)(buffer[i])>>32;
        w = (uint64_t)(buffer[i + 1])>>32; 
        av = asg_arc_a(read_g, v);
        nv = asg_arc_n(read_g, v);
        l = 0;
        
        for (k = 0; k < nv; k++)
        {
            if(av[k].del) continue;
            if(av[k].v == w) 
            {
                l = asg_arc_len(av[k]);
                break;
            }
        }

        if(k == nv)
        {
            for (k = 0; k < edge->a.n; k++)
            {
                if(edge->a.a[k].del) continue;
                if((edge->a.a[k].ul>>32) == v && edge->a.a[k].v == w)
                {
                    l = asg_arc_len(edge->a.a[k]);
                    break;
                }
            }

            if(k == edge->a.n)
            {
                fprintf(stderr, "####ERROR1-fill: i: %u, v>>1: %u, v&1: %u, w>>1: %u, w&1: %u\n",
                i, v>>1, v&1, w>>1, w&1);
            }
        } 


        
        buffer[i] = v; buffer[i] = buffer[i]<<32; buffer[i] = buffer[i] | (uint64_t)(l);
        totalLen += l;
    }
    
    if(i < bufferLen)
    {
        if(is_circle)
        {
            v = (uint64_t)(buffer[i])>>32;
            w = (uint64_t)(buffer[0])>>32; 
            av = asg_arc_a(read_g, v);
            nv = asg_arc_n(read_g, v);
            l = 0;
            
            for (k = 0; k < nv; k++)
            {
                if(av[k].del) continue;
                if(av[k].v == w) 
                {
                    l = asg_arc_len(av[k]);
                    break;
                }
            }

            if(k == nv)
            {
                for (k = 0; k < edge->a.n; k++)
                {
                    if(edge->a.a[k].del) continue;
                    if((edge->a.a[k].ul>>32) == v && edge->a.a[k].v == w)
                    {
                        l = asg_arc_len(edge->a.a[k]);
                        break;
                    }
                }

                if(k == edge->a.n)
                {
                    fprintf(stderr, "####ERROR2-fill: i: %u, v>>1: %u, v&1: %u, w>>1: %u, w&1: %u\n",
                    i, v>>1, v&1, w>>1, w&1);
                }
            } 

            buffer[i] = v; buffer[i] = buffer[i]<<32; buffer[i] = buffer[i] | (uint64_t)(l);
            totalLen += l;
        }
        else
        {
            v = (uint64_t)(buffer[i])>>32;
            l = read_g->seq[v>>1].len;
            buffer[i] = v;
            buffer[i] = buffer[i]<<32;
            buffer[i] = buffer[i] | (uint64_t)(l);
            totalLen += l;
        }
    }

    (*rLen) = totalLen;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, 0x20(%rsp)
movq %rdx, %r13
movq %rdi, %r14
movabsq $-0x100000000, %r10     # imm = 0xFFFFFFFF00000000
movq %r9, 0x18(%rsp)
movq $0x0, (%r9)
movl %esi, %eax
decl %eax
je 0x10eb72
movl %esi, 0x10(%rsp)
movl %r8d, 0x14(%rsp)
movl %eax, %r9d
xorl %r15d, %r15d
xorl %ebp, %ebp
movq %r9, 0x8(%rsp)
movq (%r14,%r15,8), %r12
movq %r12, %r8
shrq $0x20, %r8
movl 0xc(%r14,%r15,8), %eax
movq 0x28(%r13), %rcx
movq (%rcx,%r8,8), %rcx
movl $0x0, %edx
movl $0x0, %ebx
testl %ecx, %ecx
je 0x10eaa3
movq 0x8(%r13), %rdx
movq %rcx, %rdi
shrq $0x20, %rdi
movl %ecx, %esi
leaq (%rdi,%rdi,2), %rdi
leaq (%rdx,%rdi,8), %rdi
addq $0xc, %rdi
xorl %edx, %edx
cmpl $0x0, (%rdi)
js 0x10ea8e
cmpl %eax, -0x4(%rdi)
je 0x10eaa0
incq %rdx
addq $0x18, %rdi
cmpq %rdx, %rsi
jne 0x10ea84
movl %ecx, %edx
xorl %ebx, %ebx
jmp 0x10eaa3
movl -0xc(%rdi), %ebx
cmpl %ecx, %edx
jne 0x10eb0a
movq 0x20(%rsp), %rdx
movq (%rdx), %rcx
testq %rcx, %rcx
je 0x10eaef
movq 0x10(%rdx), %rsi
movl $0x1, %edi
xorl %edx, %edx
leaq (%rdx,%rdx,2), %r9
cmpl $0x0, 0xc(%rsi,%r9,8)
js 0x10eae4
leaq (%rsi,%r9,8), %r10
movq (%r10), %r9
movq %r9, %r11
shrq $0x20, %r11
cmpl %r8d, %r11d
jne 0x10eae4
cmpl %eax, 0x8(%r10)
je 0x10eaf3
movl %edi, %edx
incl %edi
cmpq %rdx, %rcx
ja 0x10eabf
jmp 0x10eaf6
xorl %edx, %edx
jmp 0x10eb05
movl %r9d, %ebx
movabsq $-0x100000000, %r10     # imm = 0xFFFFFFFF00000000
movq 0x8(%rsp), %r9
cmpq %rdx, %rcx
je 0x10eb26
andq %r10, %r12
movl %ebx, %eax
orq %r12, %rax
movq %rax, (%r14,%r15,8)
incq %r15
addl %ebx, %ebp
cmpq %r9, %r15
jne 0x10ea43
jmp 0x10eb67
movq 0x17b4b3(%rip), %rcx    # 0x289fe0
movq (%rcx), %rdi
movl %r8d, %ecx
shrl %ecx
andl $0x1, %r8d
movl %eax, %r9d
shrl %r9d
andl $0x1, %eax
movl %eax, (%rsp)
leaq 0x11ec56(%rip), %rsi    # 0x22d7a2
movl %r15d, %edx
xorl %eax, %eax
callq 0x22b0
movq 0x8(%rsp), %r9
movabsq $-0x100000000, %r10     # imm = 0xFFFFFFFF00000000
jmp 0x10eb0a
movl 0x14(%rsp), %r8d
movl 0x10(%rsp), %esi
jmp 0x10eb77
xorl %r15d, %r15d
xorl %ebp, %ebp
cmpl %esi, %r15d
jae 0x10ec75
movl %r15d, %r9d
movq (%r14,%r9,8), %rbx
testl %r8d, %r8d
je 0x10ebdb
movq %rbx, %r8
shrq $0x20, %r8
movl 0x4(%r14), %eax
movq 0x28(%r13), %rcx
movq (%rcx,%r8,8), %rcx
testl %ecx, %ecx
je 0x10ebf2
movq 0x8(%r13), %rdx
movq %rcx, %rdi
shrq $0x20, %rdi
movl %ecx, %esi
leaq (%rdi,%rdi,2), %rdi
leaq (%rdx,%rdi,8), %rdi
addq $0xc, %rdi
xorl %edx, %edx
cmpl $0x0, (%rdi)
js 0x10ebc8
cmpl %eax, -0x4(%rdi)
je 0x10ebf9
incq %rdx
addq $0x18, %rdi
cmpq %rdx, %rsi
jne 0x10ebbe
xorl %r12d, %r12d
movl %ecx, %edx
jmp 0x10ebfd
movq 0x20(%r13), %rax
movq %rbx, %rcx
shrq $0x21, %rcx
movl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
andl (%rax,%rcx,8), %r12d
jmp 0x10ec65
xorl %edx, %edx
xorl %r12d, %r12d
jmp 0x10ebfd
movl -0xc(%rdi), %r12d
cmpl %ecx, %edx
jne 0x10ec65
movq 0x20(%rsp), %rdx
movq (%rdx), %rcx
testq %rcx, %rcx
je 0x10ec4c
movq %r9, %r13
movq 0x10(%rdx), %rsi
movl $0x1, %edi
xorl %edx, %edx
leaq (%rdx,%rdx,2), %r9
cmpl $0x0, 0xc(%rsi,%r9,8)
js 0x10ec41
leaq (%rsi,%r9,8), %r10
movq (%r10), %r9
movq %r9, %r11
shrq $0x20, %r11
cmpl %r8d, %r11d
jne 0x10ec41
cmpl %eax, 0x8(%r10)
je 0x10ec50
movl %edi, %edx
incl %edi
cmpq %rdx, %rcx
ja 0x10ec1c
jmp 0x10ec53
xorl %edx, %edx
jmp 0x10ec60
movl %r9d, %r12d
movabsq $-0x100000000, %r10     # imm = 0xFFFFFFFF00000000
movq %r13, %r9
cmpq %rdx, %rcx
je 0x10ec8e
andq %r10, %rbx
movl %r12d, %eax
orq %rbx, %rax
movq %rax, (%r14,%r9,8)
addl %r12d, %ebp
movq 0x18(%rsp), %rcx
movl %ebp, %eax
movq %rax, (%rcx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x17b34b(%rip), %rcx    # 0x289fe0
movq (%rcx), %rdi
movl %r8d, %ecx
shrl %ecx
andl $0x1, %r8d
movq %r9, %r13
movl %eax, %r9d
shrl %r9d
andl $0x1, %eax
movl %eax, (%rsp)
leaq 0x11eb29(%rip), %rsi    # 0x22d7e0
movl %r15d, %edx
xorl %eax, %eax
callq 0x22b0
movq %r13, %r9
movabsq $-0x100000000, %r10     # imm = 0xFFFFFFFF00000000
jmp 0x10ec65