// File: /workspace/github_c_makefile_star2_O3/johnvtan[P]ccccc/parse.c
// Function: parse_stmt
static stmt_t *parse_stmt(list_t *tokens, env_t *env) {
    if (!tokens || !env)
        return NULL;
    stmt_t *ret = malloc(sizeof(stmt_t));
    token_t *curr = list_peek(tokens);

    if (curr->type == TOK_OPEN_BRACE) {
        ret->block = parse_block(tokens, env);
        ret->type = STMT_BLOCK;
        return ret;
    }

    if (curr->type == TOK_RETURN) {
        debug("parse_stmt: Found return statement\n");
        return_stmt_t *ret_stmt = parse_return_stmt(tokens, env);
        if (!ret_stmt)
            return NULL;
        ret->type = STMT_RETURN;
        ret->ret = ret_stmt;
        return ret;
    }

    if (curr->type == TOK_IF) {
        if_stmt_t *if_stmt = parse_if_stmt(tokens, env);
        ret->type = STMT_IF;
        ret->if_stmt = if_stmt;
        return ret;
    }

    if (curr->type == TOK_FOR) {
        ret->for_stmt = parse_for_stmt(tokens, env);
        ret->type = STMT_FOR;
        return ret;
    }

    if (curr->type == TOK_WHILE) {
        ret->while_stmt = parse_while_stmt(tokens, env);
        ret->type = STMT_WHILE;
        return ret;
    }

    if (curr->type == TOK_DO) {
        debug("Found do stmt\n");
        ret->do_stmt = parse_do_stmt(tokens, env);
        ret->type = STMT_DO;
        return ret;
    }

    if (match(tokens, TOK_BREAK)) {
        debug("parse_stmt: Found break\n");
        expect_next(tokens, TOK_SEMICOLON);
        ret->type = STMT_BREAK;
        return ret;
    }

    if (match(tokens, TOK_CONTINUE)) {
        debug("parse_stmt: Found continue\n");
        expect_next(tokens, TOK_SEMICOLON);
        ret->type = STMT_CONTINUE;
        return ret;
    }

    // The first token in a declaration is a type
    if (is_type(curr->type)) {
        debug("parse_stmt: Found declaration statement\n");
        declare_stmt_t *declare = parse_declare_stmt(tokens, env);
        if (!declare)
            return NULL;
        ret->type = STMT_DECLARE;
        ret->declare = declare;
        return ret;
    }

    // Otherwise, try to parse an expression
    if (match(tokens, TOK_SEMICOLON)) {
        debug("parse_stmt: Found null expr stmt\n");
        ret->type = STMT_NULL;
        return ret;
    }

    debug("parse_stmt: Found nonnull expr stmt\n");
    expr_t *expr = parse_expr(tokens, env);
    ret->type = STMT_EXPR;
    ret->expr = expr; 
    expect_next(tokens, TOK_SEMICOLON);
    return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
orb %al, %cl
jne 0x3e0b
movq %rsi, %r15
movq %rdi, %r14
movl $0x10, %edi
callq 0x10e0
movq %rax, %rbx
movq %r14, %rdi
callq 0x3002
movq %rax, %r12
movl (%rax), %eax
leal -0x24(%rax), %ecx
cmpl $0x6, %ecx
ja 0x3b0f
leaq 0x2dbb(%rip), %rax      # 0x6830
movslq (%rax,%rcx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movq %r14, %rdi
movl $0x24, %esi
callq 0x39e5
movl $0x18, %edi
callq 0x10e0
movq %rax, %r12
movq %r14, %rdi
movl $0xe, %esi
callq 0x39e5
movq %r14, %rdi
movq %r15, %rsi
callq 0x3f79
movq %rax, (%r12)
movq %r14, %rdi
movl $0xf, %esi
callq 0x39e5
movq %r14, %rdi
movq %r15, %rsi
callq 0x4022
movq %rax, 0x8(%r12)
movq $0x0, 0x10(%r12)
movq %r14, %rdi
callq 0x3002
testq %rax, %rax
je 0x3b04
cmpl $0x25, (%rax)
jne 0x3b04
movq %r14, %rdi
callq 0x2f9f
movq %r14, %rdi
movq %r15, %rsi
callq 0x4022
movq %rax, 0x10(%r12)
movl $0x2, (%rbx)
jmp 0x3cde
cmpl $0x4, %eax
je 0x3ca2
cmpl $0xc, %eax
jne 0x3b37
movq %r14, %rdi
movq %r15, %rsi
callq 0x3e5c
movq %rax, 0x8(%rbx)
movl $0x3, (%rbx)
jmp 0x3e0d
movq %r14, %rdi
movl $0x2b, %esi
callq 0x391c
testb %al, %al
je 0x3ce7
movq %r14, %rdi
movl $0x12, %esi
callq 0x39e5
movl $0x9, (%rbx)
jmp 0x3e0d
movq %r14, %rdi
movl $0x2a, %esi
callq 0x39e5
movl $0x10, %edi
callq 0x10e0
movq %rax, %r12
movq %r14, %rdi
movq %r15, %rsi
callq 0x4022
movq %rax, 0x8(%r12)
movq %r14, %rdi
movl $0x29, %esi
callq 0x39e5
movq %r14, %rdi
movl $0xe, %esi
callq 0x39e5
movq %r14, %rdi
movq %r15, %rsi
callq 0x3f79
movq %rax, (%r12)
movq %r14, %rdi
movl $0xf, %esi
callq 0x39e5
movq %r14, %rdi
movl $0x12, %esi
callq 0x39e5
movq %r12, 0x8(%rbx)
movl $0x8, (%rbx)
jmp 0x3e0d
movq %r14, %rdi
movl $0x28, %esi
callq 0x39e5
movl $0x28, %edi
callq 0x10e0
movq %rax, %r12
movq %r15, %rdi
callq 0x2dac
movq %rax, %r15
movq %rax, 0x20(%r12)
movq %r14, %rdi
movl $0xe, %esi
callq 0x39e5
movq %r14, %rdi
callq 0x3002
movl (%rax), %eax
addl $-0x5, %eax
cmpl $0x1, %eax
ja 0x3d14
movq %r14, %rdi
movq %r15, %rsi
callq 0x3a23
movq %rax, %r13
jmp 0x3d48
movq %r14, %rdi
movl $0x29, %esi
callq 0x39e5
movl $0x10, %edi
callq 0x10e0
movq %rax, %r12
movq %r14, %rdi
movl $0xe, %esi
callq 0x39e5
movq %r14, %rdi
movq %r15, %rsi
callq 0x3f79
movq %rax, (%r12)
movq %r14, %rdi
movl $0xf, %esi
callq 0x39e5
movq %r14, %rdi
movq %r15, %rsi
callq 0x4022
movq %rax, 0x8(%r12)
movq %r12, 0x8(%rbx)
movl $0x7, (%rbx)
jmp 0x3e0d
movl $0x8, %edi
callq 0x10e0
movq %rax, %r12
movq %r14, %rdi
movl $0x4, %esi
callq 0x39e5
movq %r14, %rdi
movq %r15, %rsi
callq 0x3f79
movq %rax, (%r12)
movq %r14, %rdi
movl $0x12, %esi
callq 0x39e5
movl $0x0, (%rbx)
movq %r12, 0x8(%rbx)
jmp 0x3e0d
movq %r14, %rdi
movl $0x2c, %esi
callq 0x391c
testb %al, %al
je 0x3de3
movq %r14, %rdi
movl $0x12, %esi
callq 0x39e5
movl $0xa, (%rbx)
jmp 0x3e0d
movl $0x10, %edi
callq 0x10e0
movq %rax, %r13
movl $0x4, (%rax)
movq %r14, %rdi
movq %r15, %rsi
movl $0x12, %edx
callq 0x40b1
movq %rax, 0x8(%r13)
movq %r14, %rdi
movl $0x12, %esi
callq 0x39e5
movq %r13, (%r12)
movq %r14, %rdi
movq %r15, %rsi
movl $0x12, %edx
callq 0x40b1
movq %rax, 0x8(%r12)
cmpl $0x5, (%rax)
jne 0x3d98
movl $0x10, %edi
callq 0x10e0
movq %rax, %r13
xorl %ebp, %ebp
movl %ebp, (%rax)
movl $0x10, %edi
callq 0x10e0
movq %rax, 0x8(%r13)
movl %ebp, (%rax)
movl $0x1, %ecx
movl %ecx, 0x8(%rax)
movl %ecx, 0x4(%r13)
movq %r13, 0x8(%r12)
movq %r14, %rdi
movl $0x12, %esi
callq 0x39e5
movq %r14, %rdi
movq %r15, %rsi
movl $0xf, %edx
callq 0x40b1
movq %rax, 0x10(%r12)
movq %r14, %rdi
movl $0xf, %esi
callq 0x39e5
movq %r14, %rdi
movq %r15, %rsi
callq 0x4022
movq %rax, 0x18(%r12)
movq %r12, 0x8(%rbx)
movl $0x6, (%rbx)
jmp 0x3e0d
movl (%r12), %eax
addl $-0x5, %eax
cmpl $0x1, %eax
ja 0x3e1f
movq %r14, %rdi
movq %r15, %rsi
callq 0x3e97
testq %rax, %rax
je 0x3e0b
movl $0x1, (%rbx)
movq %rax, 0x8(%rbx)
jmp 0x3e0d
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
movl $0x12, %esi
callq 0x391c
testb %al, %al
je 0x3e38
movl $0x5, (%rbx)
jmp 0x3e0d
movq %r14, %rdi
movq %r15, %rsi
callq 0x3f79
movl $0x4, (%rbx)
movq %rax, 0x8(%rbx)
movq %r14, %rdi
movl $0x12, %esi
callq 0x39e5
jmp 0x3e0d