// File: 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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movq %r9, -0x30(%rbp)
movq $0x0, -0x58(%rbp)
movl $0x0, -0x3c(%rbp)
movq -0x30(%rbp), %rax
movq $0x0, (%rax)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl -0xc(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jae 0x17e886
movq -0x8(%rbp), %rax
movl -0x34(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movl -0x34(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0x44(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x40(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x40(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x48(%rbp), %eax
jae 0x17e73c
movq -0x58(%rbp), %rax
movl -0x38(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x17e703
jmp 0x17e731
movq -0x58(%rbp), %rax
movl -0x38(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpl -0x44(%rbp), %eax
jne 0x17e72f
movq -0x58(%rbp), %rax
movl -0x38(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, -0x4c(%rbp)
jmp 0x17e73c
jmp 0x17e731
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x17e6e0
movl -0x38(%rbp), %eax
cmpl -0x48(%rbp), %eax
jne 0x17e829
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x17e7e3
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x38(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x17e77e
jmp 0x17e7d5
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x38(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
movl -0x40(%rbp), %ecx
cmpq %rcx, %rax
jne 0x17e7d3
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x38(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpl -0x44(%rbp), %eax
jne 0x17e7d3
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x38(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, -0x4c(%rbp)
jmp 0x17e7e3
jmp 0x17e7d5
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x17e74f
movl -0x38(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpq (%rcx), %rax
jne 0x17e827
movq 0x2457ea(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
movl -0x34(%rbp), %edx
movl -0x40(%rbp), %ecx
shrl %ecx
movl -0x40(%rbp), %r8d
andl $0x1, %r8d
movl -0x44(%rbp), %r9d
shrl %r9d
movl -0x44(%rbp), %eax
andl $0x1, %eax
leaq 0x20bd4d(%rip), %rsi    # 0x38a56a
movl %eax, (%rsp)
movb $0x0, %al
callq 0x22a0
jmp 0x17e829
movl -0x40(%rbp), %eax
movl %eax, %edx
movq -0x8(%rbp), %rax
movl -0x34(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x8(%rbp), %rax
movl -0x34(%rbp), %ecx
movq (%rax,%rcx,8), %rdx
shlq $0x20, %rdx
movq -0x8(%rbp), %rax
movl -0x34(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x8(%rbp), %rax
movl -0x34(%rbp), %ecx
movq (%rax,%rcx,8), %rdx
movl -0x4c(%rbp), %eax
orq %rax, %rdx
movq -0x8(%rbp), %rax
movl -0x34(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movl -0x4c(%rbp), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x17e660
movl -0x34(%rbp), %eax
cmpl -0xc(%rbp), %eax
jae 0x17eb19
cmpl $0x0, -0x24(%rbp)
je 0x17ea9c
movq -0x8(%rbp), %rax
movl -0x34(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
shrq $0x20, %rax
movl %eax, -0x44(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x40(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x40(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x48(%rbp), %eax
jae 0x17e95e
movq -0x58(%rbp), %rax
movl -0x38(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x17e925
jmp 0x17e953
movq -0x58(%rbp), %rax
movl -0x38(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpl -0x44(%rbp), %eax
jne 0x17e951
movq -0x58(%rbp), %rax
movl -0x38(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, -0x4c(%rbp)
jmp 0x17e95e
jmp 0x17e953
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x17e902
movl -0x38(%rbp), %eax
cmpl -0x48(%rbp), %eax
jne 0x17ea4b
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x17ea05
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x38(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x17e9a0
jmp 0x17e9f7
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x38(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
movl -0x40(%rbp), %ecx
cmpq %rcx, %rax
jne 0x17e9f5
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x38(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpl -0x44(%rbp), %eax
jne 0x17e9f5
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x38(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, -0x4c(%rbp)
jmp 0x17ea05
jmp 0x17e9f7
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x17e971
movl -0x38(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpq (%rcx), %rax
jne 0x17ea49
movq 0x2455c8(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
movl -0x34(%rbp), %edx
movl -0x40(%rbp), %ecx
shrl %ecx
movl -0x40(%rbp), %r8d
andl $0x1, %r8d
movl -0x44(%rbp), %r9d
shrl %r9d
movl -0x44(%rbp), %eax
andl $0x1, %eax
leaq 0x20bb69(%rip), %rsi    # 0x38a5a8
movl %eax, (%rsp)
movb $0x0, %al
callq 0x22a0
jmp 0x17ea4b
movl -0x40(%rbp), %eax
movl %eax, %edx
movq -0x8(%rbp), %rax
movl -0x34(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x8(%rbp), %rax
movl -0x34(%rbp), %ecx
movq (%rax,%rcx,8), %rdx
shlq $0x20, %rdx
movq -0x8(%rbp), %rax
movl -0x34(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x8(%rbp), %rax
movl -0x34(%rbp), %ecx
movq (%rax,%rcx,8), %rdx
movl -0x4c(%rbp), %eax
orq %rax, %rdx
movq -0x8(%rbp), %rax
movl -0x34(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movl -0x4c(%rbp), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0x17eb17
movq -0x8(%rbp), %rax
movl -0x34(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x40(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x4c(%rbp)
movl -0x40(%rbp), %eax
movl %eax, %edx
movq -0x8(%rbp), %rax
movl -0x34(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x8(%rbp), %rax
movl -0x34(%rbp), %ecx
movq (%rax,%rcx,8), %rdx
shlq $0x20, %rdx
movq -0x8(%rbp), %rax
movl -0x34(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x8(%rbp), %rax
movl -0x34(%rbp), %ecx
movq (%rax,%rcx,8), %rdx
movl -0x4c(%rbp), %eax
orq %rax, %rdx
movq -0x8(%rbp), %rax
movl -0x34(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movl -0x4c(%rbp), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0x17eb19
movl -0x3c(%rbp), %eax
movl %eax, %ecx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)