// File: /workspace/github_c_makefile_star2_O1/c0stya[P]fslib/src/shortest.c
// Function: fst_shortest
struct _fst * fst_shortest(const struct _fst * fst, struct _fst * path) {
    struct _state *     state;
    struct _arc   *     arc;
    struct _arc         r_arc;
    state_t             p,q;
    size_t              i;

    assert ( fst->sr_type == SR_TROPICAL );
    sr = sr_get(fst->sr_type);
    
    struct _heap * Q = heap_create(states_cmp, sizeof(state_t), 0, 0);
    heap_index(Q, states_hash, states_key_eq);

    W = malloc(sizeof(weight_t) * fst->n_states);
    B = malloc(sizeof(struct _arc) * fst->n_states);

    // initialize arrays
    for (state_t i=0; i < fst->n_states; ++i)
        W[i] = sr.zero;  

    q = fst->start; 
    W[q] = sr.one;

    heap_insert(Q, &q); 

    while( ( heap_pop(Q, &p)) !=NULL ) {
        state = &fst->states[p];

        if (state->final == 1) {
            backtrace(path, p);
            break;
        }
    
        for (arc_t a=0; a < state->n_arcs; ++a) {
            arc = &state->arcs[a];
            q = arc->state;
            
            // Technical check: do not explore unreacheble states
            if (arc->weight == sr.zero)
                continue;
            
            // Using the zero weights as marker of unexplored states

            if (W[q] == sr.zero) {
                heap_insert(Q, &q);
            }
        
            if (W[q] != sr.sum( 
                    W[q], sr.prod(W[p], arc->weight) ) ) {
    
                W[q] = sr.prod(W[p], arc->weight);  

                r_arc = *arc;
                r_arc.state = p;
                
                B[q] = r_arc;

                if ((heap_find(Q, &q, &i)) != NULL) {
                    heap_update(Q, &q, i);
                }
            }

        }
    }

    heap_remove(Q);

    free(W);
    free(B);

    return path;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movb 0xc(%rdi), %al
testb %al, %al
jne 0x3fd2
movq %rsi, %r13
movq %rdi, %r15
movzbl %al, %esi
leaq -0x70(%rbp), %r14
movq %r14, %rdi
callq 0x20d8
movq 0x10(%r14), %rax
movq %rax, 0x5430(%rip)      # 0x9110
movups (%r14), %xmm0
movups %xmm0, 0x5415(%rip)     # 0x9100
leaq 0x2ff(%rip), %rdi       # 0x3ff1
movl $0x4, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x44ec
leaq 0x325(%rip), %rsi       # 0x402c
leaq 0x327(%rip), %rdx       # 0x4035
movq %rax, -0x38(%rbp)
movq %rax, %rdi
callq 0x4565
movl 0x4(%r15), %ebx
leaq (,%rbx,4), %rdi
callq 0x1150
movq %rax, %r12
movq %rax, 0x53e3(%rip)      # 0x9118
movq %rbx, %rdi
shlq $0x4, %rdi
callq 0x1150
movq %rax, 0x53d8(%rip)      # 0x9120
testq %rbx, %rbx
je 0x3d65
movss 0x53bb(%rip), %xmm0     # 0x9110
xorl %eax, %eax
movss %xmm0, (%r12,%rax,4)
incq %rax
cmpq %rax, %rbx
jne 0x3d57
movl (%r15), %eax
leaq -0x2c(%rbp), %rsi
movl %eax, (%rsi)
movss 0x539e(%rip), %xmm0     # 0x9114
movss %xmm0, (%r12,%rax,4)
movq -0x38(%rbp), %rbx
movq %rbx, %rdi
callq 0x489a
leaq -0x30(%rbp), %rsi
movq %rbx, %rdi
callq 0x4662
testq %rax, %rax
je 0x3f17
movq %r13, -0x50(%rbp)
movq %r15, -0x48(%rbp)
movq 0x10(%r15), %rax
movl -0x30(%rbp), %ebx
leaq (%rbx,%rbx,2), %rcx
cmpl $0x1, 0xc(%rax,%rcx,8)
je 0x3f4a
leaq (%rax,%rcx,8), %rbx
cmpl $0x0, (%rbx)
je 0x3ef9
movl $0x4, %r15d
xorl %r12d, %r12d
movq 0x10(%rbx), %r13
movl -0x4(%r13,%r15), %eax
movl %eax, -0x2c(%rbp)
movss (%r13,%r15), %xmm1
movss 0x5324(%rip), %xmm0     # 0x9110
ucomiss %xmm0, %xmm1
jne 0x3e06
jp 0x3e06
incq %r12
movl (%rbx), %eax
addq $0x10, %r15
cmpq %rax, %r12
jb 0x3dd1
jmp 0x3ef9
movq 0x530b(%rip), %rcx      # 0x9118
movss (%rcx,%rax,4), %xmm1
ucomiss %xmm0, %xmm1
jne 0x3e26
jp 0x3e26
movq -0x38(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x489a
addq %r15, %r13
movq 0x52e8(%rip), %rax      # 0x9118
movl -0x2c(%rbp), %ecx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, -0x3c(%rbp)
movq 0x52bc(%rip), %r14      # 0x9100
movl -0x30(%rbp), %ecx
movss (%rax,%rcx,4), %xmm0
movss (%r13), %xmm1
callq *0x52b0(%rip)           # 0x9108
movaps %xmm0, %xmm1
movss -0x3c(%rbp), %xmm0
callq *%r14
movss -0x3c(%rbp), %xmm1
ucomiss %xmm0, %xmm1
jne 0x3e6f
jnp 0x3df3
movq 0x52a2(%rip), %rax      # 0x9118
movl -0x30(%rbp), %ecx
movss (%rax,%rcx,4), %xmm0
movss (%r13), %xmm1
callq *0x527e(%rip)           # 0x9108
movq 0x5287(%rip), %rax      # 0x9118
movl -0x2c(%rbp), %ecx
movss %xmm0, (%rax,%rcx,4)
movl 0x8(%r13), %eax
movl %eax, -0x68(%rbp)
movq (%r13), %rax
movq %rax, -0x70(%rbp)
movl -0x30(%rbp), %eax
movq 0x526e(%rip), %rdx      # 0x9120
shlq $0x4, %rcx
movl %eax, (%rdx,%rcx)
movq -0x70(%rbp), %rax
movq %rax, 0x4(%rdx,%rcx)
movl -0x68(%rbp), %eax
movl %eax, 0xc(%rdx,%rcx)
movq -0x38(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
leaq -0x58(%rbp), %rdx
callq 0x4ae8
testq %rax, %rax
je 0x3df3
movq -0x58(%rbp), %rdx
movq -0x38(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x49de
jmp 0x3df3
movq -0x38(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x4662
testq %rax, %rax
movq -0x50(%rbp), %r13
movq -0x48(%rbp), %r15
jne 0x3da5
movq -0x38(%rbp), %rdi
callq 0x45ea
movq 0x51f1(%rip), %rdi      # 0x9118
callq 0x1030
movq 0x51ed(%rip), %rdi      # 0x9120
callq 0x1030
movq %r13, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, %rdi
callq 0x2131
xorl %r15d, %r15d
testl %ebx, %ebx
je 0x3f7e
movl %ebx, %r12d
movq %r13, %rdi
callq 0x2131
movq 0x51b5(%rip), %rax      # 0x9120
movl %r12d, %ecx
shlq $0x4, %rcx
movl (%rax,%rcx), %r12d
incl %r15d
testl %r12d, %r12d
jne 0x3f5c
movss 0x518e(%rip), %xmm0     # 0x9114
movq %r13, %rdi
movl %r15d, %esi
callq 0x2294
testl %ebx, %ebx
je 0x3f17
movq 0x5184(%rip), %rax      # 0x9120
movl %ebx, %ebx
shlq $0x4, %rbx
movss 0x4(%rax,%rbx), %xmm0
movl 0x8(%rax,%rbx), %ecx
movl 0xc(%rax,%rbx), %r8d
leal -0x1(%r15), %r12d
movq %r13, %rdi
movl %r12d, %esi
movl %r15d, %edx
callq 0x2185
movq 0x5156(%rip), %rax      # 0x9120
movl (%rax,%rbx), %ebx
movl %r12d, %r15d
jmp 0x3f91
leaq 0x2442(%rip), %rdi      # 0x641b
leaq 0x2457(%rip), %rsi      # 0x6437
leaq 0x245f(%rip), %rcx      # 0x6446
movl $0x37, %edx
callq 0x10c0