// File: /workspace/github_cpp_makefile_star2_O2/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, %r12
movq %rdx, %r13
movq %rdi, %r10
movq %r9, 0x18(%rsp)
andq $0x0, (%r9)
movq %rsi, 0x28(%rsp)
leal -0x1(%rsi), %eax
movq %rax, 0x10(%rsp)
xorl %ebx, %ebx
xorl %r11d, %r11d
movq %rdi, 0x30(%rsp)
cmpq 0x10(%rsp), %rbx
je 0xcfbc8
movq (%r10,%rbx,8), %rbp
movq %rbp, %r8
shrq $0x20, %r8
leaq 0x1(%rbx), %r15
movl 0xc(%r10,%rbx,8), %eax
movq 0x8(%r13), %rcx
movq 0x28(%r13), %rdx
movq (%rdx,%r8,8), %rdx
movq %rdx, %rdi
shrq $0x20, %rdi
movl %edx, %esi
imulq $0x18, %rdi, %rdi
addq %rcx, %rdi
addq $0xc, %rdi
xorl %r9d, %r9d
xorl %ecx, %ecx
cmpq %r9, %rsi
je 0xcfb1a
cmpl $0x0, (%rdi)
js 0xcfb11
cmpl %eax, -0x4(%rdi)
je 0xcfb1f
incq %r9
addq $0x18, %rdi
jmp 0xcfb00
xorl %r14d, %r14d
jmp 0xcfb28
movl -0xc(%rdi), %r14d
cmpl %r9d, %edx
jne 0xcfb66
movq (%r12), %rdx
movl %ecx, %ecx
cmpq %rcx, %rdx
jbe 0xcfb61
movq 0x10(%r12), %rsi
imulq $0x18, %rcx, %rdi
cmpl $0x0, 0xc(%rsi,%rdi)
js 0xcfb5a
addq %rdi, %rsi
movq (%rsi), %rdi
movq %rdi, %r9
shrq $0x20, %r9
cmpl %r8d, %r9d
jne 0xcfb5a
cmpl %eax, 0x8(%rsi)
je 0xcfb5e
incl %ecx
jmp 0xcfb2c
movl %edi, %r14d
cmpq %rcx, %rdx
je 0xcfb88
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
andq %rax, %rbp
movl %r14d, %eax
orq %rbp, %rax
movq %rax, (%r10,%rbx,8)
addl %r14d, %r11d
movq %r15, %rbx
jmp 0xcfabe
movq 0x183451(%rip), %rcx    # 0x252fe0
movq (%rcx), %rdi
movl %r8d, %ecx
shrl %ecx
andl $0x1, %r8d
movl %eax, %r9d
shrl %r9d
andl $0x1, %eax
movl %eax, (%rsp)
leaq 0x11bce4(%rip), %rsi    # 0x1eb892
movl %ebx, %edx
xorl %eax, %eax
movl %r11d, 0x24(%rsp)
callq 0x22b0
movl 0x24(%rsp), %r11d
movq 0x30(%rsp), %r10
jmp 0xcfb66
cmpl $0x0, 0x28(%rsp)
je 0xcfc2c
movq 0x10(%rsp), %rax
movq (%r10,%rax,8), %rbx
cmpl $0x0, 0x20(%rsp)
je 0xcfc36
movq %rbx, %r8
shrq $0x20, %r8
movl 0x4(%r10), %eax
movq 0x8(%r13), %rsi
movq 0x28(%r13), %rcx
movq (%rcx,%r8,8), %rcx
movq %rcx, %rdi
shrq $0x20, %rdi
movl %ecx, %edx
imulq $0x18, %rdi, %rdi
addq %rdi, %rsi
addq $0xc, %rsi
xorl %r15d, %r15d
xorl %edi, %edi
movq 0x18(%rsp), %r14
cmpq %rdi, %rdx
je 0xcfc5a
cmpl $0x0, (%rsi)
js 0xcfc23
cmpl %eax, -0x4(%rsi)
je 0xcfc52
incq %rdi
addq $0x18, %rsi
jmp 0xcfc14
movq 0x18(%rsp), %r14
jmp 0xcfcb9
movq 0x20(%r13), %rax
movq %rbx, %rcx
shrq $0x21, %rcx
movl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
andl (%rax,%rcx,8), %r15d
movq 0x18(%rsp), %r14
jmp 0xcfc9a
movl -0xc(%rsi), %r15d
cmpl %edi, %ecx
jne 0xcfc9a
movq (%r12), %rcx
xorl %edx, %edx
movl %edx, %edx
cmpq %rdx, %rcx
jbe 0xcfc95
movq 0x10(%r12), %rsi
imulq $0x18, %rdx, %rdi
cmpl $0x0, 0xc(%rsi,%rdi)
js 0xcfc8e
addq %rdi, %rsi
movq (%rsi), %rdi
movq %rdi, %r9
shrq $0x20, %r9
cmpl %r8d, %r9d
jne 0xcfc8e
cmpl %eax, 0x8(%rsi)
je 0xcfc92
incl %edx
jmp 0xcfc60
movl %edi, %r15d
cmpq %rdx, %rcx
je 0xcfcce
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
andq %rax, %rbx
movl %r15d, %eax
orq %rbx, %rax
movq 0x10(%rsp), %rcx
movq %rax, (%r10,%rcx,8)
addl %r15d, %r11d
movl %r11d, %eax
movq %rax, (%r14)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18330b(%rip), %rcx    # 0x252fe0
movq (%rcx), %rdi
movl %r8d, %ecx
shrl %ecx
andl $0x1, %r8d
movl %eax, %r9d
shrl %r9d
andl $0x1, %eax
movl %eax, (%rsp)
leaq 0x11bbdc(%rip), %rsi    # 0x1eb8d0
movq 0x10(%rsp), %rdx
xorl %eax, %eax
movq %r10, %r12
movl %r11d, %ebp
callq 0x22b0
movl %ebp, %r11d
movq %r12, %r10
jmp 0xcfc9a