// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x7d2d
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8(%rax)
jne 0x7d3a
movq $0x0, -0x8(%rbp)
jmp 0x7ea4
movq -0x10(%rbp), %rdi
callq 0x7bf0
movq %rax, -0x18(%rbp)
callq 0x45f0
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
cmpb $0x0, (%rax)
je 0x7e9c
movq -0x18(%rbp), %rax
movsbl (%rax), %edi
callq 0x7eb0
testb $0x1, %al
jne 0x7d6f
jmp 0x7d7d
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x7d50
movl $0x0, -0x2c(%rbp)
movl $0x10, %edi
callq 0x10e0
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x7ef0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jle 0x7dad
jmp 0x7e79
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x7f20
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jle 0x7dc8
jmp 0x7e79
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x7fa0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jle 0x7de3
jmp 0x7e79
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x7fd0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jle 0x7e40
movq -0x18(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x2d(%rbp)
movq -0x28(%rbp), %rax
cmpl $0x4, (%rax)
jne 0x7e3e
movsbl -0x2d(%rbp), %edi
callq 0x7eb0
testb $0x1, %al
jne 0x7e3e
movsbl -0x2d(%rbp), %eax
cmpl $0x3b, %eax
je 0x7e3e
leaq 0x1ded(%rip), %rdi      # 0x9c1a
movb $0x0, %al
callq 0x1070
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1110
jmp 0x7e79
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x8080
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jle 0x7e58
jmp 0x7e79
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x8100
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jle 0x7e70
jmp 0x7e79
movq -0x18(%rbp), %rdi
callq 0x8200
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x46a0
movl -0x2c(%rbp), %ecx
movq -0x18(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x18(%rbp)
jmp 0x7d50
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nop