// File: /workspace/github_c_makefile_star2_O2/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 0x1f64
pushq $0x8
popq %r14
movq %r14, %rdi
callq 0x2c00
movq %rax, %r12
pushq $0x20
popq %rdi
callq 0x2c00
movq %rax, -0x68(%rbp)
leaq 0x23d(%rip), %rdi       # 0x399c
leaq 0x24e(%rip), %rsi       # 0x39b4
pushq $0x4
popq %rcx
movl $0x3e8, %r8d            # imm = 0x3E8
movq %r14, %rdx
callq 0x1b14
movq %rax, -0x50(%rbp)
movl (%r15), %eax
leaq -0x30(%rbp), %r14
movl %eax, (%r14)
movl (%rbx), %eax
movl %eax, 0x4(%r14)
movq %r12, -0x58(%rbp)
movq %r12, %rdi
movq %r14, %rsi
callq 0x2c27
movq %rbx, %r13
movq %r15, -0x60(%rbp)
movq -0x58(%rbp), %rdi
movq %r14, %rsi
callq 0x2ca6
testq %rax, %rax
je 0x396b
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), %r12
movq -0x50(%rbp), %rdi
movq %r14, %rsi
leaq -0x34(%rbp), %rdx
callq 0x1e21
testq %rax, %rax
jne 0x385c
movq -0x70(%rbp), %r14
movq -0x40(%rbp), %rdi
callq 0x1fbc
movl %eax, -0x34(%rbp)
imulq $0x18, -0x78(%rbp), %rcx
movq -0x80(%rbp), %rdx
cmpl $0x0, 0xc(%rdx,%rcx)
je 0x3824
imulq $0x18, %r14, %rcx
cmpl $0x0, 0xc(%r12,%rcx)
je 0x3824
movss -0xa4(%rbp), %xmm0
movq -0x40(%rbp), %rdi
movl %eax, %esi
callq 0x210b
movl -0x30(%rbp), %eax
movq -0x60(%rbp), %rcx
cmpl (%rcx), %eax
leaq -0x30(%rbp), %r14
jne 0x3845
movl -0x2c(%rbp), %eax
cmpl (%r13), %eax
jne 0x3845
movl -0x34(%rbp), %eax
movq -0x40(%rbp), %rcx
movl %eax, (%rcx)
movq -0x50(%rbp), %rdi
movq %r14, %rsi
leaq -0x34(%rbp), %rdx
callq 0x1c3c
movq %r13, %rbx
movq -0x60(%rbp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
leaq -0xb8(%rbp), %rcx
movq -0x68(%rbp), %r8
callq 0x35a0
movq -0x68(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x2ca6
testq %rax, %rax
je 0x37a1
movl -0xa0(%rbp), %eax
movl -0x90(%rbp), %ecx
movl %eax, -0x30(%rbp)
movl %ecx, -0x2c(%rbp)
movq -0x50(%rbp), %rdi
movq %r14, %rsi
leaq -0x44(%rbp), %rdx
callq 0x1e21
testq %rax, %rax
jne 0x391b
movq 0x10(%r15), %r15
movl -0x30(%rbp), %r12d
movl -0x2c(%rbp), %ebx
movq 0x10(%r13), %r14
movq -0x40(%rbp), %rdi
callq 0x1fbc
movl %eax, -0x44(%rbp)
imulq $0x18, %r12, %rcx
cmpl $0x0, 0xc(%r15,%rcx)
je 0x38fb
imulq $0x18, %rbx, %rcx
cmpl $0x0, 0xc(%r14,%rcx)
je 0x38fb
movss -0xa4(%rbp), %xmm0
movq -0x40(%rbp), %rdi
movl %eax, %esi
callq 0x210b
movq -0x58(%rbp), %rdi
leaq -0x30(%rbp), %rbx
movq %rbx, %rsi
callq 0x2c27
movq -0x50(%rbp), %rdi
movq %rbx, %rsi
leaq -0x44(%rbp), %rdx
callq 0x1c3c
movl -0x34(%rbp), %r15d
movl -0x44(%rbp), %r14d
movl -0x98(%rbp), %r12d
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 %r14d, %edx
movl %r12d, %ecx
movl %ebx, %r8d
callq 0x2011
movq %r13, %rbx
leaq -0x30(%rbp), %r14
jmp 0x3875
movq -0x58(%rbp), %rdi
callq 0x2d4b
movq -0x68(%rbp), %rdi
callq 0x2d4b
movq -0x50(%rbp), %rdi
callq 0x1ddc
movq -0x40(%rbp), %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq