// File: /workspace/github_c_makefile_star2_O1/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
pushq %rax
testq %rdi, %rdi
je 0x5210
cmpl $0x0, 0x8(%rdi)
je 0x5210
callq 0x4f2b
movq %rax, %r14
callq 0x2efc
movq %rax, %rbx
movzbl (%r14), %ecx
movl $0x1, %eax
leal -0x9(%rcx), %edx
cmpl $0x2, %edx
jb 0x5078
cmpl $0x20, %ecx
je 0x5078
testl %ecx, %ecx
je 0x5212
movsbq %cl, %r15
movl $0x10, %edi
callq 0x10e0
movq %rax, %r13
callq 0x1120
movq %rax, %rbp
movq (%rax), %rax
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testb $0x8, 0x1(%rax,%r15,2)
je 0x5065
movq %r14, (%rsp)
movl $0x1, (%r13)
movq %r14, %rdi
movq %rsp, %rsi
movl $0xa, %edx
callq 0x10d0
movl %eax, 0x8(%r13)
movl (%rsp), %r12d
subl %r14d, %r12d
testl %r12d, %r12d
jle 0x5080
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2f67
movslq %r12d, %rax
addq %rax, %r14
jmp 0x4ff6
movl $0x6, %edx
movq %r14, %rdi
leaq 0x1cbd(%rip), %r12      # 0x6d4c
movq %r12, %rsi
callq 0x1040
leaq 0x4bf2(%rip), %r15      # 0x9c90
testl %eax, %eax
je 0x50e4
xorl %ecx, %ecx
leaq 0x10(%rcx), %r15
cmpq $0x150, %r15            # imm = 0x150
je 0x50f9
leaq 0x4bd8(%rip), %rax      # 0x9c90
movq 0x10(%rcx,%rax), %r12
movq %r12, %rdi
callq 0x1070
movq %r14, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x1040
movq %r15, %rcx
testl %eax, %eax
jne 0x50a4
leaq 0x4baf(%rip), %rax      # 0x9c90
addq %rax, %r15
movl 0x8(%r15), %eax
movl %eax, (%r13)
movq %r12, %rdi
callq 0x1070
movq %rax, %r12
jmp 0x50ff
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testl %r12d, %r12d
leaq 0x1cc7(%rip), %rsi      # 0x6dd0
jle 0x5146
cmpl $0x4, (%r13)
jne 0x506a
movl %r12d, %eax
movb (%r14,%rax), %cl
cmpb $0x21, %cl
setb %al
movabsq $0x100000600, %rdx      # imm = 0x100000600
shrq %cl, %rdx
cmpb $0x3b, %cl
je 0x506a
andb %dl, %al
jne 0x506a
jmp 0x5224
movb (%r14), %dl
movq %rsi, %rcx
cmpb $0x2b, %dl
sete %al
je 0x5179
movl $0x8, %ecx
cmpb (%rcx,%rsi), %dl
sete %al
je 0x5176
addq $0x8, %rcx
cmpq $0xa8, %rcx
jne 0x5159
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x5186
addq %rsi, %rcx
movl 0x4(%rcx), %ecx
movl %ecx, (%r13)
movl $0x1, %r12d
testb %al, %al
jne 0x506a
movsbq (%r14), %rax
cmpq $0x5f, %rax
je 0x51ad
movq (%rbp), %rcx
movzwl (%rcx,%rax,2), %eax
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
andl $0x400, %eax            # imm = 0x400
je 0x51ff
movl $0x3, (%r13)
callq 0x4e20
movq %rax, 0x8(%r13)
movb (%r14), %al
testb %al, %al
je 0x51f7
leaq 0x1(%r14), %r15
cmpb $0x5f, %al
je 0x51e1
movq (%rbp), %rcx
movsbq %al, %rdx
movzwl (%rcx,%rdx,2), %ecx
andl $0xc00, %ecx            # imm = 0xC00
je 0x51f7
movq 0x8(%r13), %rdi
movsbl %al, %esi
callq 0x4eee
movb (%r15), %al
incq %r15
testb %al, %al
jne 0x51c9
movq 0x8(%r13), %rax
movl 0x8(%rax), %r12d
testl %r12d, %r12d
jg 0x506a
movq %r14, %rdi
callq 0x1140
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1b66(%rip), %rdi      # 0x6d91
callq 0x1050
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1110