// File: /workspace/github_c_makefile_star2_O0/c0stya[P]fslib/src/trim.c
// Function: fst_rm_states
struct _fst * fst_rm_states(struct _fst * fst, struct _bitset * mask) {
    struct _state * state;
    struct _arc * arc;

    state_t idx[sizeof(state_t) * fst->n_states];

    state_t shift = 0;

    for(state_t s=0; s < fst->n_states; ++s) {
        if (bitset_get(mask, s) ) {
            ++shift;
        }
        else {
            idx[s] = shift;
            fst->states[s - shift] = fst->states[s];
        }
    } 

    fst->n_states -= shift;

    // TODO: do not forget to clean up arcs of removed states

    // fix arc's destination state id
    for(state_t s=0; s < fst->n_states; ++s) {
        state = &fst->states[s];

        arc_t sh = 0;
        
        for(arc_t a=0; a < state->n_arcs; ++a) {
            arc = &state->arcs[a];
            arc->state -= idx[arc->state];

            // found link to non-existing state
            if (arc->state >= fst->n_states) {
                ++sh;
            } else {
                state->arcs[a - sh] = state->arcs[a];
            }
        }
        
        state->n_arcs -= sh;
    }

    return fst;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, %edx
movq %rdx, %rax
shlq $0x2, %rax
movq %rsp, %rcx
movq %rcx, -0x28(%rbp)
shlq $0x4, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x50(%rbp)
movq %rcx, %rsp
movq %rax, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x6c5e
movq -0x10(%rbp), %rdi
movl -0x38(%rbp), %esi
callq 0x3ec0
cmpl $0x0, %eax
je 0x6c02
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x6c4e
movq -0x50(%rbp), %rax
movl -0x34(%rbp), %edx
movl -0x38(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x38(%rbp), %ecx
subl -0x34(%rbp), %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq -0x8(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x38(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x10(%rcx), %rcx
movq %rcx, 0x10(%rax)
jmp 0x6c50
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x6bda
movl -0x34(%rbp), %edx
movq -0x8(%rbp), %rax
movl 0x4(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x4(%rax)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x6d60
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x3c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x18(%rbp)
movl $0x0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
jae 0x6d45
movq -0x50(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x44(%rbp), %edx
shlq $0x4, %rdx
addq %rdx, %rcx
movq %rcx, -0x20(%rbp)
movq -0x20(%rbp), %rcx
movl (%rcx), %ecx
movl (%rax,%rcx,4), %edx
movq -0x20(%rbp), %rax
movl (%rax), %ecx
subl %edx, %ecx
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movl (%rax), %eax
movq -0x8(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jb 0x6cfe
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x6d35
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x44(%rbp), %ecx
subl -0x40(%rbp), %ecx
movl %ecx, %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq -0x18(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x44(%rbp), %edx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
jmp 0x6d37
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x6ca8
movl -0x40(%rbp), %edx
movq -0x18(%rbp), %rax
movl (%rax), %ecx
subl %edx, %ecx
movl %ecx, (%rax)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x6c74
movq -0x8(%rbp), %rax
movq -0x28(%rbp), %rcx
movq %rcx, %rsp
movq %rbp, %rsp
popq %rbp
retq