// File: /workspace/github_c_makefile_star2_O3/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 0x5141
cmpl $0x0, 0x8(%rdi)
je 0x5141
callq 0x4e85
movq %rax, %r14
callq 0x2ea0
movq %rax, %r15
leaq 0x4d40(%rip), %rbx      # 0x9c90
movq %rax, 0x10(%rsp)
movzbl (%r14), %ecx
movl $0x1, %eax
leal -0x9(%rcx), %edx
cmpl $0x2, %edx
jb 0x5139
cmpl $0x20, %ecx
je 0x5139
testl %ecx, %ecx
je 0x5144
movsbq %cl, %r15
movl $0x10, %edi
callq 0x10e0
movq %rax, %r13
callq 0x1120
movq %rax, %rbp
movq (%rax), %rax
testb $0x8, 0x1(%rax,%r15,2)
je 0x4fd3
movq %r14, 0x8(%rsp)
movl $0x1, (%r13)
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
movl $0xa, %edx
callq 0x10d0
movl %eax, 0x8(%r13)
movl 0x8(%rsp), %r12d
subl %r14d, %r12d
testl %r12d, %r12d
jg 0x5126
movl $0x6, %edx
movq %r14, %rdi
leaq 0x1d1a(%rip), %r12      # 0x6cfc
movq %r12, %rsi
callq 0x1040
movq %rbx, %r15
testl %eax, %eax
je 0x5025
xorl %ecx, %ecx
leaq 0x10(%rcx), %r15
cmpq $0x150, %r15            # imm = 0x150
je 0x5077
movq 0x10(%rcx,%rbx), %r12
movq %r12, %rdi
callq 0x1070
movq %r14, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x1040
movq %r15, %rcx
testl %eax, %eax
jne 0x4ff3
addq %rbx, %r15
movl 0x8(%r15), %r15d
movl %r15d, (%r13)
movq %r12, %rdi
callq 0x1070
movq %rax, %r12
testl %r12d, %r12d
jle 0x5077
cmpl $0x4, %r15d
jne 0x5126
movl %r12d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movzbl (%r14,%rax), %eax
cmpq $0x3b, %rax
ja 0x515e
movabsq $0x800000100000600, %rcx # imm = 0x800000100000600
btq %rax, %rcx
jb 0x5126
jmp 0x515e
movsbq (%r14), %rax
leaq 0x1cfe(%rip), %rsi      # 0x6d80
movq %rsi, %rcx
cmpq $0x2b, %rax
je 0x50a6
xorl %edx, %edx
cmpq $0xa0, %rdx
je 0x50b5
leaq 0x8(%rdx), %rcx
cmpb 0x8(%rdx,%rsi), %al
movq %rcx, %rdx
jne 0x508d
addq %rsi, %rcx
movl 0x4(%rcx), %eax
movl %eax, (%r13)
movl $0x1, %r12d
jmp 0x5126
cmpb $0x5f, %al
je 0x50cc
movq (%rbp), %rcx
movzwl (%rcx,%rax,2), %eax
andl $0x400, %eax            # imm = 0x400
je 0x5156
movl $0x3, (%r13)
callq 0x4d75
movq %rax, %rdi
movq %rax, 0x8(%r13)
movb (%r14), %al
testb %al, %al
je 0x511d
leaq 0x1(%r14), %r15
movq 0x8(%r13), %rdi
cmpb $0x5f, %al
je 0x5107
movq (%rbp), %rcx
movsbq %al, %rdx
movzwl (%rcx,%rdx,2), %ecx
andl $0xc00, %ecx            # imm = 0xC00
je 0x511d
movsbl %al, %esi
callq 0x4e43
movb (%r15), %al
incq %r15
testb %al, %al
jne 0x50eb
movq 0x8(%r13), %rdi
movl 0x8(%rdi), %r12d
testl %r12d, %r12d
jle 0x5156
movq 0x10(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x2f0b
movl %r12d, %eax
addq %rax, %r14
jmp 0x4f55
xorl %r15d, %r15d
movq %r15, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x1140
leaq 0x1bdc(%rip), %rdi      # 0x6d41
callq 0x1050
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1110