// File: chhylp123[P]hifiasm/gfa_ut.cpp
// Function: cal_forward_dis(asg_t*, uc_block_t*, unsigned int, unsigned int)
uint64_t cal_forward_dis(asg_t *g, uc_block_t *a, uint32_t s, uint32_t e)
{
    uint32_t i, v, w, nv, z; int64_t l; asg_arc_t *av;
    //fprintf(stderr, "\n[M::%s::] s::%u, e::%u\n", __func__, s, e);
    ///TODO: a[i].aidx might be < i; if s == e, then i <= e might be wrong, cannot pass the assert(li == e);
    for(i = s, l = 0, v = w = (uint32_t)-1; i != (uint32_t)-1 && i != e; i = a[i].aidx) {
        w = (((uint32_t)(a[i].hid))<<1)|((uint32_t)(a[i].rev));
        if(v != (uint32_t)-1) {
            av = asg_arc_a(g, v); nv = asg_arc_n(g, v); 
            for (z = 0; z < nv; z++) {
                if(av[z].del) continue;
                if(av[z].v == w) break;
            }
            if(z < nv) {//found
                l += (uint32_t)av[z].ul;
            } else {
                l += a[i].pdis + g->seq[v>>1].len - g->seq[w>>1].len;
            }
        }
        v = w; 
        // fprintf(stderr, "[M::%s::] i::%u, a[i].aidx::%u, a[i].qs::%u, a[i].qe::%u\n", __func__, i, a[i].aidx, a[i].qs, a[i].qe);
    }


    assert(i == e);
    w = (((uint32_t)(a[i].hid))<<1)|((uint32_t)(a[i].rev));
    if(v != (uint32_t)-1) {
        av = asg_arc_a(g, v); nv = asg_arc_n(g, v); 
        for (z = 0; z < nv; z++) {
            if(av[z].del) continue;
            if(av[z].v == w) break;
        }
        if(z < nv) {//found
            l += (uint32_t)av[z].ul;
        } else {
            l += a[i].pdis + g->seq[v>>1].len - g->seq[w>>1].len;
        }
    }
    v = w; 


    if(l < 0) l = 0;
    return l;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x1c(%rbp)
movq $0x0, -0x38(%rbp)
movl $0xffffffff, -0x24(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x20(%rbp) # imm = 0xFFFFFFFF
xorl %eax, %eax
cmpl $-0x1, -0x1c(%rbp)
movb %al, -0x41(%rbp)
je 0x3343e9
movl -0x1c(%rbp), %eax
cmpl -0x18(%rbp), %eax
setne %al
movb %al, -0x41(%rbp)
movb -0x41(%rbp), %al
testb $0x1, %al
jne 0x3343f5
jmp 0x33454c
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
shll %eax
movq -0x10(%rbp), %rcx
movl -0x1c(%rbp), %edx
imulq $0x24, %rdx, %rdx
addq %rdx, %rcx
movb 0x20(%rcx), %cl
shrb $0x5, %cl
andb $0x1, %cl
movzbl %cl, %ecx
orl %ecx, %eax
movl %eax, -0x24(%rbp)
cmpl $-0x1, -0x20(%rbp)
je 0x33452d
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x20(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x20(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x28(%rbp), %eax
jae 0x3344b7
movq -0x40(%rbp), %rax
movl -0x2c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x334492
jmp 0x3344ac
movq -0x40(%rbp), %rax
movl -0x2c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpl -0x24(%rbp), %eax
jne 0x3344aa
jmp 0x3344b7
jmp 0x3344ac
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x33446f
movl -0x2c(%rbp), %eax
cmpl -0x28(%rbp), %eax
jae 0x3344dc
movq -0x40(%rbp), %rax
movl -0x2c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, %eax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0x33452b
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl 0x18(%rax), %eax
movq -0x8(%rbp), %rcx
movq 0x20(%rcx), %rcx
movl -0x20(%rbp), %edx
shrl %edx
movl %edx, %edx
movl (%rcx,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addl %ecx, %eax
movq -0x8(%rbp), %rcx
movq 0x20(%rcx), %rcx
movl -0x24(%rbp), %edx
shrl %edx
movl %edx, %edx
movl (%rcx,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl %ecx, %eax
movl %eax, %eax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0x33452d
movl -0x24(%rbp), %eax
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl 0x1c(%rax), %eax
movl %eax, -0x1c(%rbp)
jmp 0x3343d2
movl -0x1c(%rbp), %eax
cmpl -0x18(%rbp), %eax
jne 0x334556
jmp 0x334575
leaq 0x638b7(%rip), %rdi     # 0x397e14
leaq 0x61c55(%rip), %rsi     # 0x3961b9
movl $0x1828, %edx           # imm = 0x1828
leaq 0x638ab(%rip), %rcx     # 0x397e1b
callq 0x2130
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
shll %eax
movq -0x10(%rbp), %rcx
movl -0x1c(%rbp), %edx
imulq $0x24, %rdx, %rdx
addq %rdx, %rcx
movb 0x20(%rcx), %cl
shrb $0x5, %cl
andb $0x1, %cl
movzbl %cl, %ecx
orl %ecx, %eax
movl %eax, -0x24(%rbp)
cmpl $-0x1, -0x20(%rbp)
je 0x3346ad
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x20(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x20(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x28(%rbp), %eax
jae 0x334637
movq -0x40(%rbp), %rax
movl -0x2c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x334612
jmp 0x33462c
movq -0x40(%rbp), %rax
movl -0x2c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpl -0x24(%rbp), %eax
jne 0x33462a
jmp 0x334637
jmp 0x33462c
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x3345ef
movl -0x2c(%rbp), %eax
cmpl -0x28(%rbp), %eax
jae 0x33465c
movq -0x40(%rbp), %rax
movl -0x2c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, %eax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0x3346ab
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl 0x18(%rax), %eax
movq -0x8(%rbp), %rcx
movq 0x20(%rcx), %rcx
movl -0x20(%rbp), %edx
shrl %edx
movl %edx, %edx
movl (%rcx,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addl %ecx, %eax
movq -0x8(%rbp), %rcx
movq 0x20(%rcx), %rcx
movl -0x24(%rbp), %edx
shrl %edx
movl %edx, %edx
movl (%rcx,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl %ecx, %eax
movl %eax, %eax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0x3346ad
movl -0x24(%rbp), %eax
movl %eax, -0x20(%rbp)
cmpq $0x0, -0x38(%rbp)
jge 0x3346c2
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)