// File: /workspace/github_c_makefile_star2_O2/johnvtan[P]ccccc/tokenize.c
// Function: tokenize
list_t *tokenize(string_t *input) {
    if (!input || input->len == 0)
        return NULL;

    char *buf = string_get(input);
    list_t *token_list = list_new();
    token_t *curr_token;

    int advance;
    while (*buf) {
        if (is_whitespace(*buf)) {
            buf++;
            continue;
        }

        advance = 0;
        
        // freeing stuff is for squares
        curr_token = malloc(sizeof(token_t));

        advance = string_literal(buf, curr_token);
        if (advance > 0)
            goto next;

        advance = number_literal(buf, curr_token);
        if (advance > 0)
            goto next;

        advance = char_literal(buf, curr_token);
        if (advance > 0)
            goto next;

        advance = keyword(buf, curr_token);
        if (advance > 0) {
            char next_char = *(buf + advance);
            // TODO how to make this less hacky?
            if (curr_token->type == TOK_RETURN 
                && !is_whitespace(next_char)
                && next_char != ';') {
                printf("Bad input program: malformed keyword\n");
                exit(-1);
            }
            goto next;
        }

        advance = special_char(buf, curr_token);
        if (advance > 0)
            goto next;

        advance = identifier(buf, curr_token);
        if (advance > 0)
            goto next;

        unrecognized_token(buf);

next:
        list_push(token_list, curr_token);
        buf += advance;
    }

    return token_list;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
testq %rdi, %rdi
je 0x4bc1
cmpl $0x0, 0x8(%rdi)
je 0x4bc1
callq 0x4958
movq %rax, %rbx
callq 0x2c34
movq %rax, %r14
pushq $0x1
popq %rsi
leaq 0x4275(%rip), %r15      # 0x8c90
movq %rax, 0x10(%rsp)
movzbl (%rbx), %eax
leal -0x9(%rax), %edx
movq %rsi, %rcx
cmpl $0x2, %edx
jb 0x4bb9
movq %rsi, %rcx
cmpl $0x20, %eax
je 0x4bb9
testl %eax, %eax
je 0x4bc4
movsbq %al, %r14
pushq $0x10
popq %rdi
callq 0x10e0
movq %rax, %r13
callq 0x1120
movq %rax, %r12
movq (%rax), %rax
testb $0x8, 0x1(%rax,%r14,2)
je 0x4a97
movq %rbx, 0x8(%rsp)
movl $0x1, (%r13)
movq %rbx, %rdi
leaq 0x8(%rsp), %rsi
pushq $0xa
popq %rdx
callq 0x10d0
movl %eax, 0x8(%r13)
movl 0x8(%rsp), %ebp
subl %ebx, %ebp
testl %ebp, %ebp
jg 0x4ba4
xorl %ecx, %ecx
leaq 0x10(%rcx), %r14
cmpq $0x160, %r14            # imm = 0x160
je 0x4b17
movq (%rcx,%r15), %rbp
movq %rbp, %rdi
callq 0x1070
movq %rbx, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x1040
movq %r14, %rcx
testl %eax, %eax
jne 0x4a99
movl -0x8(%r14,%r15), %r14d
movl %r14d, (%r13)
movq %rbp, %rdi
callq 0x1070
movq %rax, %rbp
testl %ebp, %ebp
jle 0x4b17
cmpl $0x4, %r14d
jne 0x4ba4
movl %ebp, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movzbl (%rbx,%rax), %eax
cmpq $0x3b, %rax
ja 0x4bf9
movabsq $0x800000100000600, %rcx # imm = 0x800000100000600
btq %rax, %rcx
jb 0x4ba4
jmp 0x4bf9
movsbq (%rbx), %rax
xorl %ecx, %ecx
leaq 0x128c(%rip), %rsi      # 0x5db0
cmpq $0x15, %rcx
je 0x4b45
leaq 0x1(%rcx), %rdx
cmpb (%rsi,%rcx,8), %al
movq %rdx, %rcx
jne 0x4b24
movl -0x4(%rsi,%rdx,8), %eax
movl %eax, (%r13)
pushq $0x1
popq %rax
movl %eax, %ebp
jmp 0x4ba4
cmpb $0x5f, %al
je 0x4b58
movq (%r12), %rcx
movzwl (%rcx,%rax,2), %eax
andl $0x400, %eax            # imm = 0x400
je 0x4bd6
movl $0x3, (%r13)
callq 0x487f
movq %rax, 0x8(%r13)
movq %rbx, %rbp
movsbq (%rbp), %rsi
testq %rsi, %rsi
je 0x4b99
movq 0x8(%r13), %rdi
cmpb $0x5f, %sil
je 0x4b8f
movq (%r12), %rax
movzwl (%rax,%rsi,2), %eax
andl $0xc00, %eax            # imm = 0xC00
je 0x4b9d
incq %rbp
callq 0x4916
jmp 0x4b6c
movq 0x8(%r13), %rdi
movl 0x8(%rdi), %ebp
testl %ebp, %ebp
jle 0x4bd6
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x2c7e
movl %ebp, %ecx
pushq $0x1
popq %rsi
addq %rcx, %rbx
jmp 0x4a20
xorl %r14d, %r14d
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x441b(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x1124(%rip), %rsi      # 0x5d0b
movq %rbx, %rdx
xorl %eax, %eax
callq 0x10c0
pushq $-0x1
popq %rdi
callq 0x1110
leaq 0x1171(%rip), %rdi      # 0x5d71
callq 0x1050
jmp 0x4bf1