// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x28(%rcx), %rcx
movb $0x0, (%rax,%rcx)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x3080
movl %eax, %ecx
movl %ecx, -0x14(%rbp)
movb $0x1, %al
cmpl $0x20, %ecx
movb %al, -0x29(%rbp)
je 0x8b49
cmpl $0x9, -0x14(%rbp)
sete %al
movb %al, -0x29(%rbp)
movb -0x29(%rbp), %al
testb $0x1, %al
jne 0x8b52
jmp 0x8b64
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x40(%rax)
jmp 0x8b23
movl -0x14(%rbp), %edi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x30f0
movl -0x8(%rbp), %eax
movl %eax, -0x30(%rbp)
subl $0x3, %eax
je 0x8b9c
jmp 0x8b81
movl -0x30(%rbp), %eax
subl $0x4, %eax
je 0x8c1b
jmp 0x8b8f
movl -0x30(%rbp), %eax
subl $0xa, %eax
je 0x8c00
jmp 0x8e3c
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x8030
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
je 0x8bfe
movq -0x20(%rbp), %rax
cmpl $0x8, 0x8(%rax)
je 0x8bc7
movq -0x20(%rbp), %rax
cmpl $0xc, 0x8(%rax)
jne 0x8bd5
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
jmp 0x8bef
movq -0x20(%rbp), %rax
cmpl $0x9, 0x8(%rax)
jne 0x8bed
movq -0x20(%rbp), %rax
movl 0x18(%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x20(%rax)
jmp 0x8bef
movq -0x20(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0x8e4e
jmp 0x8c00
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movl -0x8(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x8e4e
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movsbl (%rax), %eax
cmpl $0x27, %eax
jne 0x8d65
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movsbl 0x1(%rax), %eax
cmpl $0x5c, %eax
jne 0x8d4d
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movsbl 0x2(%rax), %eax
addl $-0x22, %eax
movl %eax, %ecx
movq %rcx, -0x38(%rbp)
subl $0x54, %eax
ja 0x8d21
movq -0x38(%rbp), %rax
leaq 0x268f(%rip), %rcx      # 0xb2fc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x10(%rbp), %rax
movl $0x7, 0x20(%rax)
jmp 0x8d4b
movq -0x10(%rbp), %rax
movl $0x8, 0x20(%rax)
jmp 0x8d4b
movq -0x10(%rbp), %rax
movl $0xc, 0x20(%rax)
jmp 0x8d4b
movq -0x10(%rbp), %rax
movl $0xa, 0x20(%rax)
jmp 0x8d4b
movq -0x10(%rbp), %rax
movl $0xd, 0x20(%rax)
jmp 0x8d4b
movq -0x10(%rbp), %rax
movl $0x9, 0x20(%rax)
jmp 0x8d4b
movq -0x10(%rbp), %rax
movl $0xb, 0x20(%rax)
jmp 0x8d4b
movq -0x10(%rbp), %rax
movl $0x5c, 0x20(%rax)
jmp 0x8d4b
movq -0x10(%rbp), %rax
movl $0x27, 0x20(%rax)
jmp 0x8d4b
movq -0x10(%rbp), %rax
movl $0x22, 0x20(%rax)
jmp 0x8d4b
movq -0x10(%rbp), %rax
movl $0x3f, 0x20(%rax)
jmp 0x8d4b
movq -0x10(%rbp), %rax
movl $0x0, 0x20(%rax)
jmp 0x8d4b
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movsbl 0x2(%rax), %edx
leaq 0x2c24(%rip), %rsi      # 0xb95c
movb $0x0, %al
callq 0x8250
movl $0xb, -0x4(%rbp)
jmp 0x8e4e
jmp 0x8d60
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movsbl 0x1(%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x20(%rax)
jmp 0x8e34
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0x8dd6
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
addq $0x1, %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x30b0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
imull $-0x1, %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x20(%rax)
movl $0x80000000, %eax       # imm = 0x80000000
cmpq %rax, -0x28(%rbp)
jle 0x8dd4
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rcx
leaq 0x2bb9(%rip), %rsi      # 0xb97d
movb $0x0, %al
callq 0x8250
movl $0xb, -0x4(%rbp)
jmp 0x8e4e
jmp 0x8e32
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x30b0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x20(%rax)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, -0x28(%rbp)
jle 0x8e30
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rcx
leaq 0x2b5d(%rip), %rsi      # 0xb97d
movb $0x0, %al
callq 0x8250
movl $0xb, -0x4(%rbp)
jmp 0x8e4e
jmp 0x8e32
jmp 0x8e34
movl -0x8(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x8e4e
movq -0x10(%rbp), %rax
movq $0x0, 0x20(%rax)
movl -0x8(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw (%rax,%rax)