// File: /workspace/github_c_makefile_star2_O3/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, -0x30(%rbp)
movq %rsi, %r15
movq %rdi, %rbx
movq (%rdi), %rdi
callq 0x330e
movq %rax, %r14
addl $-0x4a, %eax
movq (%rbx), %rdi
cmpl $-0x4, %eax
ja 0x5005
movslq 0x28(%rdi), %rdi
leaq 0x63f2(%rip), %rsi      # 0xb3eb
xorl %eax, %eax
callq 0x3f64
jmp 0x52d8
cmpl $0x49, %r14d
jne 0x5045
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl $0x31, %esi
movl $0x35, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %eax, %eax
callq 0x38e8
testb %al, %al
jne 0x5054
movq (%rbx), %rax
movslq 0x28(%rax), %rdi
leaq 0x63dd(%rip), %rsi      # 0xb416
xorl %eax, %eax
callq 0x3f64
jmp 0x52de
callq 0x330e
cmpq $0x34, %rax
jne 0x529e
movq %r15, -0x38(%rbp)
movq (%rbx), %rdi
callq 0x38ba
movl %eax, %r13d
cmpl $0x1, %eax
je 0x507e
cmpl $0x2d, %r13d
je 0x507e
cmpl $0x46, %r13d
je 0x507e
cmpl $0x49, %r14d
jne 0x5329
movq $0x0, -0x40(%rbp)
xorl %r15d, %r15d
xorl %r12d, %r12d
cmpl $0x49, %r14d
je 0x509a
movq (%rbx), %rdi
callq 0x330e
cmpl $0x46, %r13d
je 0x50ab
cmpl $0x1, %r13d
jne 0x50af
incq %r12
jmp 0x50af
incq -0x40(%rbp)
testq %r12, %r12
je 0x50d1
cmpq $0x1, %r12
jne 0x52f0
testq %r15, %r15
je 0x5303
cmpl $0x1, %r13d
jne 0x5316
movq %r12, -0x48(%rbp)
movq -0x38(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x30(%rbp), %rax
movq (%rax), %rsi
callq 0x3bdc
movq %rax, %r12
testq %rax, %rax
jne 0x5109
movq -0x30(%rbp), %rax
cmpq $0x0, (%rax)
je 0x5109
movq -0x38(%rbp), %rax
cmpb $0x0, 0x34(%rax)
je 0x53eb
cmpl $0x49, %r14d
setne %al
cmpl $0x2d, %r13d
setne %cl
testb %cl, %al
jne 0x51aa
movl $0x0, %eax
movl $0x0, %ecx
cmpl $0x49, %r14d
movq -0x38(%rbp), %r13
je 0x5157
movq %rbx, %rdi
movl $0x43, %esi
callq 0x4d60
testq %rax, %rax
je 0x52d8
cmpb $0x0, 0x18(%r13)
jne 0x5154
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, %r14d
movl $0x4000, %edi           # imm = 0x4000
cmovnel %edi, %edx
cmpl $0x47, %r14d
cmovel %esi, %edx
testq %r12, %r12
je 0x51f3
movl %edx, 0xc(%r12)
cmpl $0x47, %r14d
jne 0x51f7
movslq 0x8(%r12), %rdx
cmpq %rdx, %rcx
jne 0x51f7
movq 0x10(%rax), %rdx
xorl %edi, %edi
leaq 0x63b1(%rip), %rsi      # 0xb552
xorl %eax, %eax
callq 0x22f9
jmp 0x51ea
cmpq $0x0, -0x48(%rbp)
movq -0x38(%rbp), %rax
je 0x51ea
cmpb $0x1, 0x34(%rax)
je 0x53d8
movq 0x38(%rax), %rdi
movq -0x30(%rbp), %rax
movq (%rax), %rsi
decq %rsi
callq 0x3bdc
testq %r12, %r12
je 0x51ea
movl 0xc(%rax), %eax
movl %eax, 0xc(%r12)
movq (%r12), %r12
testq %r12, %r12
jne 0x51da
jmp 0x51f7
movq -0x30(%rbp), %rax
incq (%rax)
jmp 0x5208
movl %edx, 0x30(%r13)
movq -0x30(%rbp), %rax
incq (%rax)
cmpl $0x49, %r14d
je 0x5361
movq (%rbx), %rdi
callq 0x38ba
cmpl $0x31, %eax
jne 0x521d
movq (%rbx), %rdi
callq 0x330e
movq (%rbx), %rdi
callq 0x38ba
movl %eax, %r13d
movb $0x1, %al
cmpl $0x1, %r13d
movq -0x48(%rbp), %r12
je 0x5242
cmpl $0x2d, %r13d
je 0x5242
cmpl $0x46, %r13d
je 0x5242
xorl %eax, %eax
incq %r15
cmpl $0x49, %r14d
sete %cl
orb %al, %cl
jne 0x508c
xorl %r13d, %r13d
negq %r12
sbbq %r13, %r13
movq (%rbx), %rdi
callq 0x330e
cmpq $0x35, %rax
jne 0x5369
movq -0x38(%rbp), %rax
cmpb $0x1, 0x34(%rax)
jne 0x53a1
movq -0x30(%rbp), %rax
cmpq $0x2, (%rax)
jb 0x53a1
movq (%rbx), %rax
movslq 0x28(%rax), %rdi
leaq 0x6366(%rip), %rsi      # 0xb5ff
jmp 0x4ff9
movq (%rbx), %rax
movslq 0x28(%rax), %rdi
cmpl $0x48, %r14d
leaq 0x5a0c(%rip), %rax      # 0xacbc
leaq 0x5a0b(%rip), %rcx      # 0xacc2
cmoveq %rax, %rcx
cmpl $0x47, %r14d
leaq 0x59ef(%rip), %rdx      # 0xacb5
cmovneq %rcx, %rdx
leaq 0x617c(%rip), %rsi      # 0xb44d
xorl %eax, %eax
callq 0x3f64
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rbx), %rax
movslq 0x28(%rax), %rdi
leaq 0x61b3(%rip), %rsi      # 0xb4b1
jmp 0x4ff9
movq (%rbx), %rax
movslq 0x28(%rax), %rdi
leaq 0x61c0(%rip), %rsi      # 0xb4d1
jmp 0x4ff9
movq (%rbx), %rax
movslq 0x28(%rax), %rdi
leaq 0x61dc(%rip), %rsi      # 0xb500
jmp 0x4ff9
movq (%rbx), %rax
movslq 0x28(%rax), %rdi
cmpl $0x48, %r14d
leaq 0x5981(%rip), %rax      # 0xacbc
leaq 0x5980(%rip), %rcx      # 0xacc2
cmoveq %rax, %rcx
cmpl $0x47, %r14d
leaq 0x5964(%rip), %rdx      # 0xacb5
cmovneq %rcx, %rdx
leaq 0x611f(%rip), %rsi      # 0xb47b
jmp 0x52d1
xorl %r12d, %r12d
jmp 0x52de
movq (%rbx), %rax
movslq 0x28(%rax), %rdi
cmpl $0x48, %r14d
leaq 0x5941(%rip), %rax      # 0xacbc
leaq 0x5940(%rip), %rcx      # 0xacc2
cmoveq %rax, %rcx
cmpl $0x47, %r14d
leaq 0x5924(%rip), %rdx      # 0xacb5
cmovneq %rcx, %rdx
leaq 0x6235(%rip), %rsi      # 0xb5d1
jmp 0x52d1
xorl %r12d, %r12d
cmpl $0x47, %r14d
jne 0x52de
addq %r13, %r15
cmpq %r15, -0x40(%rbp)
jne 0x52de
movq (%rbx), %rax
movslq 0x28(%rax), %rdi
leaq 0x6274(%rip), %rsi      # 0xb63d
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x22f9
jmp 0x52de
movq (%rbx), %rax
movslq 0x28(%rax), %rdi
leaq 0x61b2(%rip), %rsi      # 0xb598
jmp 0x4ff9
movq (%rbx), %rax
movslq 0x28(%rax), %rdi
leaq 0x612e(%rip), %rsi      # 0xb527
jmp 0x4ff9