// File: /workspace/github_c_makefile_star2_O2/MaoKo[P]FGFX/fgfl/src/lexical_spec.c
// Function: spec_action_pair
int
spec_action_pair(lexical_spec_t* spec, spec_entry_t* entry, size_t* count_pos) {
    int kind_action = advance_token(spec->lex);
    if ((kind_action != T_BEGIN)
                        && (kind_action != T_PUSH) && (kind_action != T_POP)) {
        errorf(CURRENT_LINE(spec->lex),
                        "Missing a $BEGIN, $PUSH or $POP directive.");
        return (ERROR);
    }
    else if ((kind_action == T_POP)) {
        if (!in_first(spec->lex, T_COMMA, T_RPAREN, -1)) {
            errorf(CURRENT_LINE(spec->lex), 
                    "A comma or a close paren must follow a $POP directive.");
            return (ERROR);
        }
    }
    else if (advance_token(spec->lex) != T_LPAREN) {
        errorf(CURRENT_LINE(spec->lex),
                        "Missing an open paren after the %s directive.",
                                            KIND_IN2_ACTION(kind_action));
        return (ERROR);
    }

    int kind_atom = peek_token(spec->lex);
    bool valid_kind = !((kind_atom != T_TERMINAL)
                        && (kind_atom != T_STAR) && (kind_atom != T_STAY));

    if (!valid_kind && (kind_action != T_POP)) {
        errorf(CURRENT_LINE(spec->lex),
                "Either a token name, a * or $STAY must follow the %s.",
                                            KIND_IN2_ACTION(kind_action));
        return (ERROR);
    }

    size_t seen_all = 0;

    size_t local_count = 0;
    size_t count_stay = 0;

    while (valid_kind || (kind_action == T_POP)) {
        if (kind_action != T_POP)
            { advance_token(spec->lex); }

        if (kind_atom == T_STAR)
            { ++seen_all; }
        else if (kind_atom == T_STAY)
            { ++count_stay; }

        if (seen_all) {
            if (seen_all > 1) {
                errorf(CURRENT_LINE(spec->lex),
                                        "A '*' can't follow another '*'.");
                return (ERROR);
            }
            else if (!local_count) {
                errorf(CURRENT_LINE(spec->lex),
                            "There must be a state name before the * token.");
                return (ERROR);
            }
            else if (kind_atom != T_STAR) {
                errorf(CURRENT_LINE(spec->lex),
                        "A state name can't follow the * token.");
                return (ERROR);
            }
        }

        trans_list_t* equiv_state =
                                trans_list_at(entry->state_lst, (*count_pos));

        if ((!equiv_state) && (*count_pos) && (!entry->all_state)) {
            errorf(CURRENT_LINE(spec->lex),
                        "More action move than the number of state.");
            return (ERROR);
        }
        else if ((kind_atom == T_TERMINAL) || (kind_action == T_POP)) {
            spec_entry_t* crt_state = NULL;
            size_t index_state = 0;

            if (kind_action != T_POP) {
                crt_state = add_entry_lexeme(spec, T_STATE);
                if (!crt_state)
                    { return (ERROR); }
                else if (!entry->fragment)
                    { crt_state->is_reach = true; }
                index_state = GET_INDEX(crt_state);
            }

            bool useless_move = false;
            if (equiv_state) {
                equiv_state->state = BUILD_ACTION(kind_action, index_state);
                if ((size_t)equiv_state->input == index_state)
                    { useless_move = true; }
            }
            else {
                entry->default_action =
                                    BUILD_ACTION(kind_action, index_state);
            }

            if (useless_move && (kind_action == T_BEGIN)) {
                warnf(0, "Useless to do a $BEGIN on state %s when"
                            " this state is already active.", crt_state->name);
            }
        }
        else if (seen_all) {
            if (entry->all_state) {
                errorf(CURRENT_LINE(spec->lex),
                    "You can't use star in token which is valid in all state.");
                return (ERROR);
            }

            int prev_index = (trans_list_at(entry->state_lst,
                                                    (*count_pos) - 1))->state;
            while (equiv_state) {
                equiv_state->state = prev_index;
                equiv_state = equiv_state->next;
            }
        }

        ++(*count_pos);
        ++(local_count);

        if (kind_action == T_POP)
            { return (DONE); }
        else if (peek_token(spec->lex) == T_COMMA)
            { advance_token(spec->lex); }

        kind_atom = peek_token(spec->lex);
        valid_kind = !((kind_atom != T_TERMINAL)
                        && (kind_atom != T_STAR) && (kind_atom != T_STAY));
    }

    if (advance_token(spec->lex) != T_RPAREN) {
        errorf(CURRENT_LINE(spec->lex),
                        "Missing a close paren after the %s directive.",
                                            KIND_IN2_ACTION(kind_action));
        return (ERROR);
    }
    else if (entry->all_state && (*count_pos) > 1) {
        errorf(CURRENT_LINE(spec->lex),
            "A token in all state can't have more than one outgoing begin.");
        return (ERROR);
    }
    else if ((kind_action == T_BEGIN)
            && (count_stay == ((seen_all) ? local_count - 1 : local_count))) {

        warnf(CURRENT_LINE(spec->lex), "The $BEGIN directive is useless"
                " because it goes always to the $STAY directive.");
    }
    return (DONE);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r13
movq %rsi, -0x30(%rbp)
movq %rdi, %r14
movq (%rdi), %rdi
callq 0x306d
movq %rax, %r15
addl $-0x4a, %eax
movq (%r14), %rdi
pushq $-0x1
popq %r12
cmpl $-0x4, %eax
ja 0x4d0c
movslq 0x28(%rdi), %rdi
leaq 0x66d2(%rip), %rsi      # 0xb3d2
xorl %eax, %eax
callq 0x3bdc
jmp 0x50dc
cmpl $0x49, %r15d
jne 0x4d45
pushq $0x31
popq %rsi
pushq $0x35
popq %rdx
pushq $-0x1
popq %rbx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x3585
testb %al, %al
jne 0x4d54
movq (%r14), %rax
movslq 0x28(%rax), %rdi
leaq 0x66c7(%rip), %rsi      # 0xb3fd
xorl %eax, %eax
callq 0x3bdc
movl %ebx, %r12d
jmp 0x50dc
callq 0x306d
cmpq $0x34, %rax
jne 0x4f52
movq (%r14), %rdi
callq 0x3559
movl %eax, %ebx
movb $0x1, %al
cmpl $0x1, %ebx
je 0x4d7b
cmpl $0x2d, %ebx
je 0x4d7b
cmpl $0x46, %ebx
je 0x4d7b
cmpl $0x49, %r15d
jne 0x4fcf
xorl %eax, %eax
movq $0x0, -0x38(%rbp)
xorl %r12d, %r12d
movq $0x0, -0x40(%rbp)
cmpl $0x49, %r15d
setne %cl
xorb $0x1, %al
testb %al, %cl
jne 0x4f91
cmpl $0x49, %r15d
je 0x4dad
movq (%r14), %rdi
callq 0x306d
cmpl $0x46, %ebx
je 0x4dc1
cmpl $0x1, %ebx
movq -0x30(%rbp), %rax
jne 0x4dc9
incq -0x38(%rbp)
jmp 0x4dc9
incq -0x40(%rbp)
movq -0x30(%rbp), %rax
cmpq $0x0, -0x38(%rbp)
je 0x4ded
cmpq $0x1, -0x38(%rbp)
jne 0x5004
testq %r12, %r12
je 0x5017
cmpl $0x1, %ebx
jne 0x502a
movq %r12, -0x48(%rbp)
movq 0x38(%rax), %rdi
movq %r13, %r12
movq (%r13), %rsi
callq 0x38a9
movq %rax, %r13
testq %rax, %rax
jne 0x4e1e
cmpq $0x0, (%r12)
je 0x4e1e
movq -0x30(%rbp), %rax
cmpb $0x0, 0x34(%rax)
je 0x50c3
cmpl $0x49, %r15d
setne %al
cmpl $0x2d, %ebx
setne %cl
testb %cl, %al
jne 0x4eb9
movl $0x0, %eax
movl $0x0, %ecx
cmpl $0x49, %r15d
je 0x4e68
movq %r14, %rdi
pushq $0x43
popq %rsi
callq 0x4b33
testq %rax, %rax
je 0x50d8
movq -0x30(%rbp), %rcx
cmpb $0x0, 0x18(%rcx)
jne 0x4e65
movb $0x1, 0x21(%rax)
movq (%rax), %rcx
movl %ecx, %esi
orl $0x1000, %esi           # imm = 0x1000
movl %ecx, %edx
orl $0x2000, %edx           # imm = 0x2000
cmpl $0x48, %r15d
movl $0x4000, %edi           # imm = 0x4000
cmovnel %edi, %edx
cmpl $0x47, %r15d
cmovel %esi, %edx
testq %r13, %r13
je 0x4ef0
movl %edx, 0xc(%r13)
cmpl $0x47, %r15d
jne 0x4ef7
movslq 0x8(%r13), %rdx
cmpq %rdx, %rcx
jne 0x4ef7
movq 0x10(%rax), %rdx
xorl %edi, %edi
leaq 0x6689(%rip), %rsi      # 0xb539
xorl %eax, %eax
callq 0x3d40
jmp 0x4ef7
cmpq $0x0, -0x38(%rbp)
je 0x4ef7
movq -0x30(%rbp), %rax
cmpb $0x1, 0x34(%rax)
je 0x50b3
movq 0x38(%rax), %rdi
movq (%r12), %rsi
decq %rsi
callq 0x38a9
movl 0xc(%rax), %eax
testq %r13, %r13
je 0x4ef7
movl %eax, 0xc(%r13)
movq (%r13), %r13
jmp 0x4ee1
movq -0x30(%rbp), %rax
movl %edx, 0x30(%rax)
incq (%r12)
cmpl $0x49, %r15d
je 0x50ae
movq %r12, %r13
movq (%r14), %rdi
callq 0x3559
cmpl $0x31, %eax
jne 0x4f1d
movq (%r14), %rdi
callq 0x306d
movq -0x48(%rbp), %r12
incq %r12
movq (%r14), %rdi
callq 0x3559
movl %eax, %ebx
movb $0x1, %al
cmpl $0x1, %ebx
je 0x4d8e
cmpl $0x2d, %ebx
je 0x4d8e
cmpl $0x46, %ebx
je 0x4d8e
xorl %eax, %eax
jmp 0x4d8e
movq (%r14), %rax
movslq 0x28(%rax), %rdi
cmpl $0x48, %r15d
leaq 0x51e4(%rip), %rax      # 0xa148
leaq 0x51e3(%rip), %rcx      # 0xa14e
cmoveq %rax, %rcx
cmpl $0x47, %r15d
leaq 0x51c7(%rip), %rdx      # 0xa141
cmovneq %rcx, %rdx
leaq 0x64af(%rip), %rsi      # 0xb434
xorl %eax, %eax
callq 0x3bdc
jmp 0x50dc
movq (%r14), %rdi
callq 0x306d
cmpq $0x35, %rax
jne 0x503d
movq -0x30(%rbp), %rax
cmpb $0x1, 0x34(%rax)
jne 0x5079
cmpq $0x2, (%r13)
jb 0x5079
movq (%r14), %rax
movslq 0x28(%rax), %rdi
leaq 0x661c(%rip), %rsi      # 0xb5e6
jmp 0x50d1
movq (%r14), %rax
movslq 0x28(%rax), %rdi
cmpl $0x48, %r15d
leaq 0x5167(%rip), %rax      # 0xa148
leaq 0x5166(%rip), %rcx      # 0xa14e
cmoveq %rax, %rcx
cmpl $0x47, %r15d
leaq 0x514a(%rip), %rdx      # 0xa141
cmovneq %rcx, %rdx
leaq 0x6460(%rip), %rsi      # 0xb462
jmp 0x4f85
movq (%r14), %rax
movslq 0x28(%rax), %rdi
leaq 0x6486(%rip), %rsi      # 0xb498
jmp 0x50d1
movq (%r14), %rax
movslq 0x28(%rax), %rdi
leaq 0x6493(%rip), %rsi      # 0xb4b8
jmp 0x50d1
movq (%r14), %rax
movslq 0x28(%rax), %rdi
leaq 0x64af(%rip), %rsi      # 0xb4e7
jmp 0x50d1
cmpl $0x48, %r15d
leaq 0x5100(%rip), %rax      # 0xa148
leaq 0x50ff(%rip), %rcx      # 0xa14e
cmoveq %rax, %rcx
cmpl $0x47, %r15d
movq (%r14), %rax
movslq 0x28(%rax), %rdi
leaq 0x50dc(%rip), %rdx      # 0xa141
cmovneq %rcx, %rdx
leaq 0x6548(%rip), %rsi      # 0xb5b8
xorl %eax, %eax
callq 0x3bdc
jmp 0x50d8
cmpl $0x47, %r15d
jne 0x50ae
cmpq $0x1, -0x38(%rbp)
adcq $-0x1, %r12
cmpq %r12, -0x40(%rbp)
movl $0x0, %r12d
jne 0x50dc
movq (%r14), %rax
movslq 0x28(%rax), %rdi
leaq 0x6582(%rip), %rsi      # 0xb624
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x3d40
jmp 0x50dc
xorl %r12d, %r12d
jmp 0x50dc
movq (%r14), %rax
movslq 0x28(%rax), %rdi
leaq 0x64be(%rip), %rsi      # 0xb57f
jmp 0x50d1
movq (%r14), %rax
movslq 0x28(%rax), %rdi
leaq 0x643d(%rip), %rsi      # 0xb50e
xorl %eax, %eax
callq 0x3bdc
pushq $-0x1
popq %r12
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq