// File: /workspace/github_c_makefile_star2_O3/c0stya[P]fslib/src/compose.c
// Function: fst_compose
struct _fst * fst_compose(
    const struct _fst * fst_a, 
    const struct _fst * fst_b, 
    struct _fst * fst_c )
{
    struct _sr sr = sr_get(fst_a->sr_type);
    struct _queue * q = queue_create(sizeof(struct _spair));
    struct _queue * mq = queue_create(sizeof(struct _arc_pair)); 

    struct _hash * marked = hash_create(
                    hash_pair,
                    cmp_pair,
                    sizeof(struct _spair),
                    sizeof(state_t), 
                    1000 ); // try to predict

    struct _spair      pair;
    struct _arc_pair   mi;    // composition item

    state_t sc, dst_sc;

    struct _state * state_a, * state_b;
    struct _state * dst_state_a, * dst_state_b;
    struct _arc * arc_a, * arc_b;

    // Add initial stack to agenda
    pair.a = fst_a->start;
    pair.b = fst_b->start;
    
    queue_enque(q, &pair);

    while( (queue_deque(q, &pair)) != NULL ) {

        state_a = &fst_a->states[pair.a];
        state_b = &fst_b->states[pair.b];


        if (hash_get(marked, &pair, &sc) == NULL) {

            // Create a new state
            sc = fst_add_state(fst_c);
            
            if (state_a->final && state_b->final)
                fst_set_final(fst_c, sc, sr.one);

            if (pair.a == fst_a->start && pair.b == fst_b->start)
                fst_c->start = sc;

            hash_add(marked, &pair, &sc);
        }

        // Match procedure
        match_arcs(     fst_a, fst_b, 
                        &pair,
                        &sr,
                        mq  );

        while ( queue_deque(mq, &mi) != NULL ) {

            arc_a = &mi.a;
            arc_b = &mi.b;
            
            pair.a = arc_a->state;
            pair.b = arc_b->state;

                
            // State lookup
            if (hash_get(marked, &pair, &dst_sc) == NULL) {
                
                // Create a new state
                dst_state_a = &fst_a->states[pair.a];
                dst_state_b = &fst_b->states[pair.b];

                dst_sc = fst_add_state(fst_c);
                
                if (dst_state_a->final && dst_state_b->final) {
                    fst_set_final(fst_c, dst_sc, sr.one);
                }

                queue_enque(q, &pair);

                // Add to a makred table
                hash_add(marked, &pair, &dst_sc);
            }

            fst_add_arc(
                fst_c,
                sc, 
                dst_sc, 
                arc_a->ilabel, 
                arc_b->olabel,
                sr.prod(arc_a->weight, arc_b->weight));
        }
    }

    queue_remove(q);
    queue_remove(mq);
    hash_remove(marked);

    return fst_c;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdx, -0x40(%rbp)
movq %rsi, %rbx
movq %rdi, %r15
movzbl 0xc(%rdi), %esi
leaq -0xb8(%rbp), %rdi
callq 0x20d4
movl $0x8, %edi
callq 0x2df0
movq %rax, %r13
movl $0x20, %edi
callq 0x2df0
movq %rax, -0x68(%rbp)
leaq 0x278(%rip), %rdi       # 0x3c9b
leaq 0x290(%rip), %rsi       # 0x3cba
movl $0x8, %edx
movl $0x4, %ecx
movl $0x3e8, %r8d            # imm = 0x3E8
callq 0x1c58
movq %rax, %r12
movl (%r15), %eax
leaq -0x30(%rbp), %r14
movl %eax, (%r14)
movl (%rbx), %eax
movl %eax, 0x4(%r14)
movq %r13, %rdi
movq %r14, %rsi
callq 0x2e1c
movq %r13, -0x58(%rbp)
movq %r13, %rdi
movq %r14, %rsi
callq 0x2ea0
testq %rax, %rax
je 0x3c6b
movq %rbx, -0x50(%rbp)
movq %r15, -0x60(%rbp)
movq 0x10(%r15), %rax
movq %rax, -0x80(%rbp)
movl -0x30(%rbp), %eax
movq %rax, -0x78(%rbp)
movl -0x2c(%rbp), %eax
movq %rax, -0x70(%rbp)
movq 0x10(%rbx), %r13
movq %r12, %rdi
movq %r14, %rsi
leaq -0x34(%rbp), %rdx
callq 0x1f8d
testq %rax, %rax
jne 0x3b27
movq -0x70(%rbp), %r14
movq -0x40(%rbp), %rdi
callq 0x212d
movl %eax, -0x34(%rbp)
movq -0x78(%rbp), %rcx
leaq (%rcx,%rcx,2), %rcx
movq -0x80(%rbp), %rdx
cmpl $0x0, 0xc(%rdx,%rcx,8)
je 0x3aed
leaq (%r14,%r14,2), %rcx
cmpl $0x0, 0xc(%r13,%rcx,8)
je 0x3aed
movss -0xa4(%rbp), %xmm0
movq -0x40(%rbp), %rdi
movl %eax, %esi
callq 0x22a0
movl -0x30(%rbp), %eax
movq -0x60(%rbp), %rcx
cmpl (%rcx), %eax
leaq -0x30(%rbp), %r14
jne 0x3b10
movl -0x2c(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpl (%rcx), %eax
jne 0x3b10
movl -0x34(%rbp), %eax
movq -0x40(%rbp), %rcx
movl %eax, (%rcx)
movq %r12, %rdi
movq %r14, %rsi
leaq -0x34(%rbp), %rdx
callq 0x1d96
movq -0x50(%rbp), %rbx
movq -0x60(%rbp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
leaq -0xb8(%rbp), %rcx
movq -0x68(%rbp), %rbx
movq %rbx, %r8
callq 0x3864
movq %rbx, %rdi
leaq -0xa0(%rbp), %rsi
callq 0x2ea0
testq %rax, %rax
je 0x3c52
movl -0xa0(%rbp), %eax
movl -0x90(%rbp), %ecx
movl %eax, -0x30(%rbp)
movl %ecx, -0x2c(%rbp)
movq %r12, %rdi
movq %r14, %rsi
leaq -0x44(%rbp), %rdx
callq 0x1f8d
testq %rax, %rax
jne 0x3bf1
movq 0x10(%r15), %r15
movl -0x30(%rbp), %r13d
movl -0x2c(%rbp), %ebx
movq -0x50(%rbp), %rax
movq 0x10(%rax), %r14
movq -0x40(%rbp), %rdi
callq 0x212d
movl %eax, -0x44(%rbp)
leaq (,%r13,2), %rcx
addq %r13, %rcx
cmpl $0x0, 0xc(%r15,%rcx,8)
je 0x3bd2
leaq (%rbx,%rbx,2), %rcx
cmpl $0x0, 0xc(%r14,%rcx,8)
je 0x3bd2
movss -0xa4(%rbp), %xmm0
movq -0x40(%rbp), %rdi
movl %eax, %esi
callq 0x22a0
movq -0x58(%rbp), %rdi
leaq -0x30(%rbp), %rbx
movq %rbx, %rsi
callq 0x2e1c
movq %r12, %rdi
movq %rbx, %rsi
leaq -0x44(%rbp), %rdx
callq 0x1d96
movl -0x34(%rbp), %r15d
movl -0x44(%rbp), %r13d
movl -0x98(%rbp), %r14d
movl -0x84(%rbp), %ebx
movss -0x9c(%rbp), %xmm0
movss -0x8c(%rbp), %xmm1
callq *-0xb0(%rbp)
movq -0x40(%rbp), %rdi
movl %r15d, %esi
movq -0x60(%rbp), %r15
movl %r13d, %edx
movl %r14d, %ecx
movl %ebx, %r8d
callq 0x2189
movq -0x68(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x2ea0
testq %rax, %rax
leaq -0x30(%rbp), %r14
jne 0x3b5b
movq -0x58(%rbp), %rdi
movq %r14, %rsi
callq 0x2ea0
testq %rax, %rax
movq -0x50(%rbp), %rbx
jne 0x3a7d
movq -0x58(%rbp), %rdi
callq 0x2f4a
movq -0x68(%rbp), %rdi
callq 0x2f4a
movq %r12, %rdi
callq 0x1f40
movq -0x40(%rbp), %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq