// File: /workspace/github_c_makefile_star2_O2/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 0x62d3
cmpl $0x9, %eax
jne 0x62d9
incq 0x40(%rbx)
jmp 0x62c0
movq 0x8(%rbx), %rsi
movl %eax, %edi
callq 0x3100
cmpl $0x3, %ebp
je 0x638e
cmpl $0x4, %ebp
je 0x6304
cmpl $0xa, %ebp
jne 0x63bd
movq 0x10(%rbx), %r14
jmp 0x63b7
movq 0x10(%rbx), %rdi
movzbl (%rdi), %eax
cmpl $0x2d, %eax
je 0x63c4
cmpl $0x27, %eax
jne 0x63de
movsbl 0x1(%rdi), %eax
cmpl $0x5c, %eax
jne 0x6416
movzbl 0x2(%rdi), %eax
leal -0x6e(%rax), %ecx
cmpl $0x8, %ecx
jbe 0x642e
cmpl $0x22, %eax
je 0x6478
cmpl $0x27, %eax
je 0x64b1
cmpl $0x30, %eax
je 0x648a
cmpl $0x3f, %eax
je 0x64a5
cmpl $0x5c, %eax
je 0x6481
cmpl $0x66, %eax
je 0x6490
cmpl $0x62, %eax
je 0x6499
cmpl $0x61, %eax
jne 0x6447
movl $0x7, 0x20(%rbx)
jmp 0x6419
movq 0x10(%rbx), %r14
movq %r14, %rdi
callq 0x5ccc
testq %rax, %rax
je 0x63b7
movl 0x8(%rax), %ecx
cmpl $0xc, %ecx
je 0x63b1
cmpl $0x9, %ecx
je 0x641e
cmpl $0x8, %ecx
jne 0x6424
movq %rax, 0x20(%rbx)
jmp 0x6424
movq %r14, 0x20(%rbx)
jmp 0x6427
andq $0x0, 0x20(%rbx)
jmp 0x6427
incq %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x30c0
movl %eax, %ecx
negl %ecx
movl %ecx, 0x20(%rbx)
movl $0x80000001, %ecx       # imm = 0x80000001
jmp 0x63f4
xorl %esi, %esi
xorl %edx, %edx
callq 0x30c0
movl %eax, 0x20(%rbx)
movabsq $0x100000000, %rcx      # imm = 0x100000000
cmpq %rcx, %rax
jl 0x6419
movq 0x10(%rbx), %rdx
movq 0x38(%rbx), %rcx
leaq 0x1f5d(%rip), %rsi      # 0x8365
movq %rbx, %rdi
xorl %eax, %eax
callq 0x5dea
pushq $0xb
jmp 0x641b
movl %eax, 0x20(%rbx)
pushq $0x4
popq %rbp
jmp 0x6427
movl 0x18(%rax), %ecx
movl %ecx, 0x20(%rbx)
movl 0x8(%rax), %ebp
movl %ebp, %eax
popq %rbx
popq %r14
popq %rbp
retq
leaq 0x19e7(%rip), %rdx      # 0x7e1c
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movl $0xa, 0x20(%rbx)
jmp 0x6419
movsbl %al, %edx
leaq 0x1ef3(%rip), %rsi      # 0x8344
movq %rbx, %rdi
xorl %eax, %eax
callq 0x5dea
jmp 0x6412
movl $0xd, 0x20(%rbx)
jmp 0x6419
movl $0xb, 0x20(%rbx)
jmp 0x6419
movl $0x9, 0x20(%rbx)
jmp 0x6419
movl $0x22, 0x20(%rbx)
jmp 0x6419
movl $0x5c, 0x20(%rbx)
jmp 0x6419
andl $0x0, 0x20(%rbx)
jmp 0x6419
movl $0xc, 0x20(%rbx)
jmp 0x6419
movl $0x8, 0x20(%rbx)
jmp 0x6419
movl $0x3f, 0x20(%rbx)
jmp 0x6419
movl $0x27, 0x20(%rbx)
jmp 0x6419