// File: /workspace/github_c_makefile_star2_O1/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 0x20d8
movl $0x8, %edi
callq 0x2dc4
movq %rax, %r13
movl $0x20, %edi
callq 0x2dc4
movq %rax, -0x68(%rbp)
leaq 0x278(%rip), %rdi       # 0x3c73
leaq 0x290(%rip), %rsi       # 0x3c92
movl $0x8, %edx
movl $0x4, %ecx
movl $0x3e8, %r8d            # imm = 0x3E8
callq 0x1c50
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 0x2df0
movq %r13, -0x58(%rbp)
movq %r13, %rdi
movq %r14, %rsi
callq 0x2e74
testq %rax, %rax
je 0x3c43
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 0x1f92
testq %rax, %rax
jne 0x3aff
movq -0x70(%rbp), %r14
movq -0x40(%rbp), %rdi
callq 0x2131
movl %eax, -0x34(%rbp)
movq -0x78(%rbp), %rcx
leaq (%rcx,%rcx,2), %rcx
movq -0x80(%rbp), %rdx
cmpl $0x0, 0xc(%rdx,%rcx,8)
je 0x3ac5
leaq (%r14,%r14,2), %rcx
cmpl $0x0, 0xc(%r13,%rcx,8)
je 0x3ac5
movss -0xa4(%rbp), %xmm0
movq -0x40(%rbp), %rdi
movl %eax, %esi
callq 0x2294
movl -0x30(%rbp), %eax
movq -0x60(%rbp), %rcx
cmpl (%rcx), %eax
leaq -0x30(%rbp), %r14
jne 0x3ae8
movl -0x2c(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpl (%rcx), %eax
jne 0x3ae8
movl -0x34(%rbp), %eax
movq -0x40(%rbp), %rcx
movl %eax, (%rcx)
movq %r12, %rdi
movq %r14, %rsi
leaq -0x34(%rbp), %rdx
callq 0x1d8e
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 0x383c
movq %rbx, %rdi
leaq -0xa0(%rbp), %rsi
callq 0x2e74
testq %rax, %rax
je 0x3c2a
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 0x1f92
testq %rax, %rax
jne 0x3bc9
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 0x2131
movl %eax, -0x44(%rbp)
leaq (,%r13,2), %rcx
addq %r13, %rcx
cmpl $0x0, 0xc(%r15,%rcx,8)
je 0x3baa
leaq (%rbx,%rbx,2), %rcx
cmpl $0x0, 0xc(%r14,%rcx,8)
je 0x3baa
movss -0xa4(%rbp), %xmm0
movq -0x40(%rbp), %rdi
movl %eax, %esi
callq 0x2294
movq -0x58(%rbp), %rdi
leaq -0x30(%rbp), %rbx
movq %rbx, %rsi
callq 0x2df0
movq %r12, %rdi
movq %rbx, %rsi
leaq -0x44(%rbp), %rdx
callq 0x1d8e
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 0x2185
movq -0x68(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x2e74
testq %rax, %rax
leaq -0x30(%rbp), %r14
jne 0x3b33
movq -0x58(%rbp), %rdi
movq %r14, %rsi
callq 0x2e74
testq %rax, %rax
movq -0x50(%rbp), %rbx
jne 0x3a55
movq -0x58(%rbp), %rdi
callq 0x2f1e
movq -0x68(%rbp), %rdi
callq 0x2f1e
movq %r12, %rdi
callq 0x1f45
movq -0x40(%rbp), %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq