// File: chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_bub_backtrack_primary_length(asg_t*, ma_ug_t*, unsigned int, buf_t*, unsigned long*, unsigned long*)
void asg_bub_backtrack_primary_length(asg_t *g, ma_ug_t *utg, uint32_t v0, buf_t *b, uint64_t* path_base_len, uint64_t* path_nodes)
{
	uint32_t i, v, u, nv;
    uint64_t len = 0, node = 0;
    ///b->S.a[0] is the sink of this bubble
    asg_arc_t *av = NULL;

    ///v is the sink of this bubble
	v = b->S.a[0];
    len = 0;
	///recover node
    while (1)
    {
        u = b->a[v].p; // u->v
        if(u == v0) break;
        if(v == b->S.a[0])
        {
            len += g->seq[u>>1].len;
        } 
        else
        {
            nv = asg_arc_n(g, u);
            av = asg_arc_a(g, u);
            for (i = 0; i < nv; ++i)
            {
                if(av[i].del) continue;
                if(av[i].v == v) break;
            }
            ///if(i == nv) fprintf(stderr, "ERROR\n");
            len += (uint32_t)av[i].ul;
        }

        if(utg)
        {
            node += utg->u.a[u>>1].n;
        }
        else
        {
            node++;
        }
         
        v = u;
    }
    
    if(path_base_len) (*path_base_len) = len;
    if(path_nodes) (*path_nodes) = node;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movl (%rax), %eax
movl %eax, -0x38(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movl -0x38(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jne 0x1357bb
jmp 0x1358d3
movl -0x38(%rbp), %eax
movq -0x20(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpl (%rcx), %eax
jne 0x1357f0
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x3c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x135892
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x3c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x3c(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x40(%rbp), %eax
jae 0x135877
movq -0x58(%rbp), %rax
movl -0x34(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x135852
jmp 0x13586c
movq -0x58(%rbp), %rax
movl -0x34(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpl -0x38(%rbp), %eax
jne 0x13586a
jmp 0x135877
jmp 0x13586c
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x13582f
movq -0x58(%rbp), %rax
movl -0x34(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, %eax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x1358bc
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x3c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x1358c8
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x38(%rbp)
jmp 0x135798
cmpq $0x0, -0x28(%rbp)
je 0x1358e5
movq -0x48(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
cmpq $0x0, -0x30(%rbp)
je 0x1358f7
movq -0x50(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
popq %rbp
retq
nopl (%rax)