// File: /workspace/github_c_makefile_star2_O3/tstword[P]MIPSAssemblerC/src/tokenizer.c
// Function: return_token
token_t return_token(token_t token, struct tokenizer *tokenizer) {
    int ch;
    struct reserved_entry *entry;

    /* Set NULL terminator */
    tokenizer->lexbuf[tokenizer->bufpos] = '\0';

    /* Skip whiespace */
    while((ch = fgetc(tokenizer->fstream)) == ' ' || ch == '\t') { 
        tokenizer->colno++; 
    }
    ungetc(ch, tokenizer->fstream);

    /* Set attributes */
    switch(token) {
        case TOK_IDENTIFIER:
            /* Perform lookup on reserved keyword table */
            if((entry = get_reserved_table(tokenizer->lexbuf)) != NULL) {
                if(entry->token == TOK_MNEMONIC || entry->token == TOK_DIRECTIVE)
                    tokenizer->attrptr = entry;
                else if(entry->token == TOK_REGISTER)
                    tokenizer->attrval = entry->attrval;
                return entry->token;
            }
            /* Intentional fall-through here, if not a reserved identifier set attrbuf to lexical buf */
        case TOK_STRING:
            /* Set attribute to lexbuffer */
            tokenizer->attrbuf = tokenizer->lexbuf;
            return token;
        case TOK_INTEGER: {
            if(*tokenizer->lexbuf == '\'') {
                if(*(tokenizer->lexbuf + 1) == '\\') {
                    switch(*(tokenizer->lexbuf + 2)) {
                        case 'a':
                            tokenizer->attrval = '\a';
                            break;
                        case 'b':
                            tokenizer->attrval = '\b';
                            break;
                        case 'f':
                            tokenizer->attrval = '\f';
                            break;
                        case 'n':
                            tokenizer->attrval = '\n';
                            break;
                        case 'r':
                            tokenizer->attrval = '\r';
                            break;
                        case 't':
                            tokenizer->attrval = '\t';
                            break;
                        case 'v':
                            tokenizer->attrval = '\v';
                            break;
                        case '\\':
                            tokenizer->attrval = '\\';
                            break;
                        case '\'':
                            tokenizer->attrval = '\'';
                            break;
                        case '"':
                            tokenizer->attrval = '\"';
                            break;
                        case '?':
                            tokenizer->attrval = '\?';
                            break;
                        case '0':
                            tokenizer->attrval = '\0';
                            break;  
                        default:
                            report_fsm(tokenizer, "Unrecognized escape character %c", *(tokenizer->lexbuf + 2));
                            return TOK_INVALID;
                    }
                }
                else {
                    tokenizer->attrval = *(tokenizer->lexbuf + 1);
                }
            }
            else {
                long long value;
                if(*tokenizer->lexbuf == '-') {
                    value = strtoll(tokenizer->lexbuf + 1, NULL, 0);
                    tokenizer->attrval = (int)value * -1;
                    if(value > 0x80000000) {
                        report_fsm(tokenizer, "Integer literal '%s' cannot be represented with 32-bits on line %ld", tokenizer->lexbuf, tokenizer->lineno);
                        return TOK_INVALID;
                    }
                }
                else {
                    value = strtoll(tokenizer->lexbuf, NULL, 0);
                    tokenizer->attrval = (int)value;
                    if(value > 0xFFFFFFFF) {
                        report_fsm(tokenizer, "Integer literal '%s' cannot be represented with 32-bits on line %ld", tokenizer->lexbuf, tokenizer->lineno);
                        return TOK_INVALID;
                    }
                }
            }
            return token;
        }
        default:
            /* Set attribute to NULL */
            tokenizer->attrptr = NULL;
            return token;
    }

    return token;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
movq %rsi, %rbx
movl %edi, %ebp
movq 0x10(%rsi), %rax
movq 0x28(%rsi), %rcx
movb $0x0, (%rax,%rcx)
movq 0x8(%rbx), %rdi
callq 0x3090
cmpl $0x20, %eax
je 0x6951
cmpl $0x9, %eax
jne 0x6957
incq 0x40(%rbx)
jmp 0x693e
movq 0x8(%rbx), %rsi
movl %eax, %edi
callq 0x3100
cmpl $0x3, %ebp
je 0x69e1
cmpl $0x4, %ebp
je 0x697e
cmpl $0xa, %ebp
jne 0x6a18
movq 0x10(%rbx), %r14
jmp 0x6a12
movq 0x10(%rbx), %rdi
movzbl (%rdi), %eax
cmpl $0x2d, %eax
je 0x6a22
cmpl $0x27, %eax
jne 0x6a41
movsbl 0x1(%rdi), %eax
cmpl $0x5c, %eax
jne 0x6a81
movzbl 0x2(%rdi), %eax
cmpl $0x61, %eax
jg 0x6a9b
cmpl $0x3e, %eax
jg 0x6abc
cmpl $0x22, %eax
je 0x6b28
cmpl $0x27, %eax
je 0x6b40
cmpl $0x30, %eax
jne 0x6aeb
movl $0x0, 0x20(%rbx)
jmp 0x6a84
movq 0x10(%rbx), %r14
movq %r14, %rdi
callq 0x625c
testq %rax, %rax
je 0x6a12
movl 0x8(%rax), %ecx
cmpl $0xc, %ecx
je 0x6a0c
cmpl $0x9, %ecx
je 0x6a8b
cmpl $0x8, %ecx
jne 0x6a91
movq %rax, 0x20(%rbx)
jmp 0x6a91
movq %r14, 0x20(%rbx)
jmp 0x6a94
movq $0x0, 0x20(%rbx)
jmp 0x6a94
incq %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x30c0
movl %eax, %ecx
negl %ecx
movl %ecx, 0x20(%rbx)
movl $0x4, %ebp
movl $0x80000001, %ecx       # imm = 0x80000001
jmp 0x6a5c
xorl %esi, %esi
xorl %edx, %edx
callq 0x30c0
movl %eax, 0x20(%rbx)
movl $0x4, %ebp
movabsq $0x100000000, %rcx      # imm = 0x100000000
cmpq %rcx, %rax
jl 0x6a94
movq 0x10(%rbx), %rdx
movq 0x38(%rbx), %rcx
leaq 0x18f5(%rip), %rsi      # 0x8365
movq %rbx, %rdi
xorl %eax, %eax
callq 0x636f
movl $0xb, %ebp
jmp 0x6a94
movl %eax, 0x20(%rbx)
movl $0x4, %ebp
jmp 0x6a94
movl 0x18(%rax), %ecx
movl %ecx, 0x20(%rbx)
movl 0x8(%rax), %ebp
movl %ebp, %eax
popq %rbx
popq %r14
popq %rbp
retq
leal -0x6e(%rax), %ecx
cmpl $0x8, %ecx
ja 0x6ad8
leaq 0x1372(%rip), %rdx      # 0x7e1c
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movl $0xa, 0x20(%rbx)
jmp 0x6a84
cmpl $0x3f, %eax
je 0x6b34
cmpl $0x5c, %eax
je 0x6b4c
cmpl $0x61, %eax
jne 0x6aeb
movl $0x7, 0x20(%rbx)
jmp 0x6a84
cmpl $0x62, %eax
je 0x6b58
cmpl $0x66, %eax
jne 0x6aeb
movl $0xc, 0x20(%rbx)
jmp 0x6a84
movsbl %al, %edx
leaq 0x184f(%rip), %rsi      # 0x8344
movq %rbx, %rdi
xorl %eax, %eax
callq 0x636f
jmp 0x6a7a
movl $0xb, 0x20(%rbx)
jmp 0x6a84
movl $0xd, 0x20(%rbx)
jmp 0x6a84
movl $0x9, 0x20(%rbx)
jmp 0x6a84
movl $0x22, 0x20(%rbx)
jmp 0x6a84
movl $0x3f, 0x20(%rbx)
jmp 0x6a84
movl $0x27, 0x20(%rbx)
jmp 0x6a84
movl $0x5c, 0x20(%rbx)
jmp 0x6a84
movl $0x8, 0x20(%rbx)
jmp 0x6a84