// File: /workspace/github_c_makefile_star2_O1/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, -0x38(%rbp)
movq %rsi, %r15
movq %rdi, %rbx
movq (%rdi), %rdi
callq 0x331a
movq %rax, %r14
addl $-0x4a, %eax
cmpl $-0x4, %eax
ja 0x511b
movq (%rbx), %rax
movslq 0x28(%rax), %rdi
leaq 0x62dc(%rip), %rsi      # 0xb3eb
xorl %eax, %eax
callq 0x4060
jmp 0x520b
movq (%rbx), %rdi
cmpl $0x49, %r14d
jne 0x515f
movl $0x31, %esi
movl $0x35, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %eax, %eax
callq 0x39e3
testb %al, %al
jne 0x516a
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
movq (%rbx), %rax
movslq 0x28(%rax), %rdi
leaq 0x62c3(%rip), %rsi      # 0xb416
xorl %eax, %eax
callq 0x4060
jmp 0x5212
callq 0x331a
cmpq $0x34, %rax
jne 0x51d1
movq (%rbx), %rdi
callq 0x39b4
movl %eax, %r12d
xorl %ecx, %ecx
cmpl $0x1, %eax
je 0x518a
cmpl $0x2d, %r12d
je 0x518a
cmpl $0x46, %r12d
je 0x518a
movb $0x1, %cl
cmpl $0x49, %r14d
sete %al
xorb $0x1, %cl
orb %cl, %al
jne 0x5224
movq (%rbx), %rax
movslq 0x28(%rax), %rdi
cmpl $0x48, %r14d
leaq 0x5b0e(%rip), %rax      # 0xacbc
leaq 0x5b0d(%rip), %rcx      # 0xacc2
cmoveq %rax, %rcx
cmpl $0x47, %r14d
leaq 0x5af1(%rip), %rdx      # 0xacb5
cmovneq %rcx, %rdx
leaq 0x62ac(%rip), %rsi      # 0xb47b
jmp 0x5204
movq (%rbx), %rax
movslq 0x28(%rax), %rdi
cmpl $0x48, %r14d
leaq 0x5ad9(%rip), %rax      # 0xacbc
leaq 0x5ad8(%rip), %rcx      # 0xacc2
cmoveq %rax, %rcx
cmpl $0x47, %r14d
leaq 0x5abc(%rip), %rdx      # 0xacb5
cmovneq %rcx, %rdx
leaq 0x6249(%rip), %rsi      # 0xb44d
xorl %eax, %eax
callq 0x4060
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
movl -0x2c(%rbp), %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ecx, -0x30(%rbp)
xorl %r13d, %r13d
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
cmpl $0x49, %r14d
je 0x524a
testb $0x1, -0x30(%rbp)
je 0x548c
cmpl $0x49, %r14d
je 0x5258
movq (%rbx), %rdi
callq 0x331a
cmpl $0x46, %r12d
je 0x5269
cmpl $0x1, %r12d
jne 0x526d
incq %r13
jmp 0x526d
incq -0x48(%rbp)
testq %r13, %r13
je 0x5291
cmpq $0x1, %r13
jne 0x54c6
cmpq $0x0, -0x40(%rbp)
je 0x54d9
cmpl $0x1, %r12d
jne 0x54ec
movq %r13, -0x50(%rbp)
movq %r15, %r13
movq 0x38(%r15), %rdi
movq -0x38(%rbp), %rax
movq (%rax), %rsi
callq 0x3cd8
movq %rax, %r15
testq %rax, %rax
je 0x5362
cmpl $0x49, %r14d
setne %al
cmpl $0x2d, %r12d
setne %cl
testb %cl, %al
jne 0x533d
movl $0x0, %eax
movl $0x0, %ecx
cmpl $0x49, %r14d
je 0x52fa
movq %rbx, %rdi
movl $0x43, %esi
callq 0x4e60
testq %rax, %rax
je 0x5404
cmpb $0x0, 0x18(%r13)
jne 0x52f7
movb $0x1, 0x21(%rax)
movq (%rax), %rcx
movq %rcx, %rdx
orq $0x1000, %rdx           # imm = 0x1000
movq %rcx, %rsi
orq $0x2000, %rsi           # imm = 0x2000
testq %r15, %r15
je 0x53c8
cmpl $0x48, %r14d
movl $0x4000, %edi           # imm = 0x4000
cmovnel %edi, %esi
cmpl $0x47, %r14d
cmovnel %esi, %edx
movl %edx, 0xc(%r15)
movslq 0x8(%r15), %rdx
cmpq %rdx, %rcx
sete %cl
jmp 0x53e1
cmpq $0x0, -0x50(%rbp)
je 0x5413
cmpb $0x1, 0x34(%r13)
jne 0x539f
movq (%rbx), %rax
movslq 0x28(%rax), %rdi
xorl %r15d, %r15d
leaq 0x6238(%rip), %rsi      # 0xb598
jmp 0x538c
movq -0x38(%rbp), %rax
cmpq $0x0, (%rax)
je 0x52b4
cmpb $0x0, 0x34(%r13)
jne 0x52b4
movq (%rbx), %rax
movslq 0x28(%rax), %rdi
xorl %r15d, %r15d
leaq 0x619b(%rip), %rsi      # 0xb527
xorl %eax, %eax
callq 0x4060
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
jmp 0x5477
movq 0x38(%r13), %rdi
movq -0x38(%rbp), %rax
movq (%rax), %rsi
decq %rsi
callq 0x3cd8
testq %r15, %r15
je 0x5413
movl 0xc(%rax), %eax
movl %eax, 0xc(%r15)
movq (%r15), %r15
testq %r15, %r15
jne 0x53ba
jmp 0x5413
cmpl $0x48, %r14d
movl $0x4000, %ecx           # imm = 0x4000
cmovnel %ecx, %esi
cmpl $0x47, %r14d
cmovnel %esi, %edx
movl %edx, 0x30(%r13)
xorl %ecx, %ecx
movb $0x1, %r15b
cmpl $0x47, %r14d
jne 0x540e
testb %cl, %cl
je 0x540e
movq 0x10(%rax), %rdx
xorl %edi, %edi
leaq 0x6157(%rip), %rsi      # 0xb552
xorl %eax, %eax
callq 0x22f9
jmp 0x540e
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
xorl %r15d, %r15d
testb %r15b, %r15b
je 0x5465
movq -0x38(%rbp), %rax
incq (%rax)
incq -0x40(%rbp)
xorl %r15d, %r15d
cmpl $0x49, %r14d
jne 0x5430
movl $0x0, -0x2c(%rbp)
jmp 0x5477
movq (%rbx), %rdi
callq 0x39b4
cmpl $0x31, %eax
jne 0x5445
movq (%rbx), %rdi
callq 0x331a
movq (%rbx), %rdi
callq 0x39b4
movl %eax, %r12d
movb $0x1, %r15b
cmpl $0x1, %eax
je 0x545e
cmpl $0x2d, %r12d
jne 0x546a
movb $0x1, %al
movl %eax, -0x30(%rbp)
jmp 0x5477
xorl %r15d, %r15d
jmp 0x5477
cmpl $0x46, %r12d
sete %al
movl %eax, -0x30(%rbp)
movb $0x1, %r15b
testb %r15b, %r15b
movq %r13, %r15
movq -0x50(%rbp), %r13
jne 0x523a
jmp 0x5212
movq (%rbx), %rdi
callq 0x331a
cmpq $0x35, %rax
jne 0x54ff
cmpb $0x1, 0x34(%r15)
jne 0x5537
movq -0x38(%rbp), %rax
cmpq $0x2, (%rax)
jb 0x5537
movq (%rbx), %rax
movslq 0x28(%rax), %rdi
leaq 0x613e(%rip), %rsi      # 0xb5ff
jmp 0x510f
movq (%rbx), %rax
movslq 0x28(%rax), %rdi
leaq 0x5fdd(%rip), %rsi      # 0xb4b1
jmp 0x510f
movq (%rbx), %rax
movslq 0x28(%rax), %rdi
leaq 0x5fea(%rip), %rsi      # 0xb4d1
jmp 0x510f
movq (%rbx), %rax
movslq 0x28(%rax), %rdi
leaq 0x6006(%rip), %rsi      # 0xb500
jmp 0x510f
movq (%rbx), %rax
movslq 0x28(%rax), %rdi
cmpl $0x48, %r14d
leaq 0x57ab(%rip), %rax      # 0xacbc
leaq 0x57aa(%rip), %rcx      # 0xacc2
cmoveq %rax, %rcx
cmpl $0x47, %r14d
leaq 0x578e(%rip), %rdx      # 0xacb5
cmovneq %rcx, %rdx
leaq 0x609f(%rip), %rsi      # 0xb5d1
jmp 0x5204
movl $0x0, -0x2c(%rbp)
cmpl $0x47, %r14d
jne 0x5212
cmpq $0x1, %r13
movq -0x40(%rbp), %rax
adcq $-0x1, %rax
cmpq %rax, -0x48(%rbp)
jne 0x5212
movq (%rbx), %rax
movslq 0x28(%rax), %rdi
leaq 0x60d1(%rip), %rsi      # 0xb63d
movl $0x0, -0x2c(%rbp)
xorl %eax, %eax
callq 0x22f9
jmp 0x5212