// File: /workspace/github_cpp_makefile_star2_O3/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 $0x38, %rsp
movl %r8d, 0x20(%rsp)
movq %rcx, 0x28(%rsp)
movq %rdx, %r12
movq %rdi, %r14
movabsq $-0x100000000, %r10     # imm = 0xFFFFFFFF00000000
movq %r9, 0x30(%rsp)
movq $0x0, (%r9)
movl %esi, 0x24(%rsp)
movl %esi, %eax
decl %eax
movl %eax, 0x1c(%rsp)
movl %eax, %r9d
movq %r9, 0x10(%rsp)
je 0x11415a
xorl %ebx, %ebx
xorl %ebp, %ebp
movq (%r14,%rbx,8), %r13
movq %r13, %r8
shrq $0x20, %r8
movl 0xc(%r14,%rbx,8), %eax
movq 0x28(%r12), %rcx
movq (%rcx,%r8,8), %rcx
movl $0x0, %edx
movl $0x0, %r15d
testl %ecx, %ecx
je 0x114099
movq 0x8(%r12), %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 0x114082
cmpl %eax, -0x4(%rdi)
je 0x114095
incq %rdx
addq $0x18, %rdi
cmpq %rdx, %rsi
jne 0x114078
movl %ecx, %edx
xorl %r15d, %r15d
jmp 0x114099
movl -0xc(%rdi), %r15d
cmpl %ecx, %edx
jne 0x1140fc
movq 0x28(%rsp), %rdx
movq (%rdx), %rcx
testq %rcx, %rcx
je 0x11411a
movq 0x10(%rdx), %rsi
movl $0x1, %edi
xorl %edx, %edx
leaq (%rdx,%rdx,2), %r9
cmpl $0x0, 0xc(%rsi,%r9,8)
js 0x1140da
leaq (%rsi,%r9,8), %r10
movq (%r10), %r9
movq %r9, %r11
shrq $0x20, %r11
cmpl %r8d, %r11d
jne 0x1140da
cmpl %eax, 0x8(%r10)
je 0x1140e5
movl %edi, %edx
incl %edi
cmpq %rdx, %rcx
ja 0x1140b5
jmp 0x1140e8
movl %r9d, %r15d
cmpq %rdx, %rcx
movq 0x10(%rsp), %r9
movabsq $-0x100000000, %r10     # imm = 0xFFFFFFFF00000000
je 0x11411a
andq %r10, %r13
movl %r15d, %eax
orq %r13, %rax
movq %rax, (%r14,%rbx,8)
incq %rbx
addl %r15d, %ebp
cmpq %r9, %rbx
jne 0x114034
jmp 0x11415c
movq 0x180ebf(%rip), %rcx    # 0x294fe0
movq (%rcx), %rdi
movl %r8d, %ecx
shrl %ecx
andl $0x1, %r8d
movl %eax, %r9d
shrl %r9d
andl $0x1, %eax
movl %eax, (%rsp)
leaq 0x125872(%rip), %rsi    # 0x2399b2
movl %ebx, %edx
xorl %eax, %eax
callq 0x22b0
movabsq $-0x100000000, %r10     # imm = 0xFFFFFFFF00000000
movq 0x10(%rsp), %r9
jmp 0x1140fc
xorl %ebp, %ebp
cmpl $0x0, 0x24(%rsp)
je 0x114259
movq (%r14,%r9,8), %rbx
cmpl $0x0, 0x20(%rsp)
je 0x1141c3
movq %rbx, %r8
shrq $0x20, %r8
movl 0x4(%r14), %eax
movq 0x28(%r12), %rcx
movq (%rcx,%r8,8), %rcx
testl %ecx, %ecx
je 0x1141db
movq 0x8(%r12), %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 0x1141b0
cmpl %eax, -0x4(%rdi)
je 0x1141e2
incq %rdx
addq $0x18, %rdi
cmpq %rdx, %rsi
jne 0x1141a6
xorl %r12d, %r12d
movl %ecx, %edx
jmp 0x1141e6
movq 0x20(%r12), %rax
movq %rbx, %rcx
shrq $0x21, %rcx
movl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
andl (%rax,%rcx,8), %r12d
jmp 0x114249
xorl %edx, %edx
xorl %r12d, %r12d
jmp 0x1141e6
movl -0xc(%rdi), %r12d
cmpl %ecx, %edx
jne 0x114249
movq 0x28(%rsp), %rdx
movq (%rdx), %rcx
testq %rcx, %rcx
je 0x114272
movq 0x10(%rdx), %rsi
movl $0x1, %edi
xorl %edx, %edx
leaq (%rdx,%rdx,2), %r9
cmpl $0x0, 0xc(%rsi,%r9,8)
js 0x114227
leaq (%rsi,%r9,8), %r10
movq (%r10), %r9
movq %r9, %r11
shrq $0x20, %r11
cmpl %r8d, %r11d
jne 0x114227
cmpl %eax, 0x8(%r10)
je 0x114232
movl %edi, %edx
incl %edi
cmpq %rdx, %rcx
ja 0x114202
jmp 0x114235
movl %r9d, %r12d
cmpq %rdx, %rcx
movq 0x10(%rsp), %r9
movabsq $-0x100000000, %r10     # imm = 0xFFFFFFFF00000000
je 0x114272
andq %r10, %rbx
movl %r12d, %eax
orq %rbx, %rax
movq %rax, (%r14,%r9,8)
addl %r12d, %ebp
movl %ebp, %eax
movq 0x30(%rsp), %rcx
movq %rax, (%rcx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x180d67(%rip), %rcx    # 0x294fe0
movq (%rcx), %rdi
movl %r8d, %ecx
shrl %ecx
andl $0x1, %r8d
movl %eax, %r9d
shrl %r9d
andl $0x1, %eax
movl %eax, (%rsp)
leaq 0x125758(%rip), %rsi    # 0x2399f0
movl 0x1c(%rsp), %edx
xorl %eax, %eax
callq 0x22b0
movabsq $-0x100000000, %r10     # imm = 0xFFFFFFFF00000000
movq 0x10(%rsp), %r9
jmp 0x114249