// File: /workspace/github_c_makefile_star2_O1/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 0x3eae
movq %rsi, %r15
movq %rdi, %r14
movl $0x10, %edi
callq 0x10e0
movq %rax, %rbx
movq %r14, %rdi
callq 0x305e
movq %rax, %r12
movl (%rax), %eax
leal -0x24(%rax), %ecx
cmpl $0x6, %ecx
ja 0x3bad
leaq 0x2d7c(%rip), %rax      # 0x6880
movslq (%rax,%rcx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movq %r14, %rdi
movl $0x24, %esi
callq 0x3a74
movl $0x18, %edi
callq 0x10e0
movq %rax, %r12
movq %r14, %rdi
movl $0xe, %esi
callq 0x3a74
movq %r14, %rdi
movq %r15, %rsi
callq 0x401c
movq %rax, (%r12)
movq %r14, %rdi
movl $0xf, %esi
callq 0x3a74
movl $0x10, %edi
callq 0x10e0
movq %rax, 0x8(%r12)
movq %r14, %rdi
movq %r15, %rsi
callq 0x40c3
movq %rax, 0x8(%r12)
movq $0x0, 0x10(%r12)
movq %r14, %rdi
callq 0x305e
testq %rax, %rax
je 0x3ba2
cmpl $0x25, (%rax)
jne 0x3ba2
movq %r14, %rdi
callq 0x2ffb
movq %r14, %rdi
movq %r15, %rsi
callq 0x40c3
movq %rax, 0x10(%r12)
movl $0x2, (%rbx)
jmp 0x3d7c
cmpl $0x4, %eax
je 0x3d40
cmpl $0xc, %eax
jne 0x3bd5
movq %r14, %rdi
movq %r15, %rsi
callq 0x3eff
movq %rax, 0x8(%rbx)
movl $0x3, (%rbx)
jmp 0x3eb0
movq %r14, %rdi
movl $0x2b, %esi
callq 0x39ab
testb %al, %al
je 0x3d85
movq %r14, %rdi
movl $0x12, %esi
callq 0x3a74
movl $0x9, (%rbx)
jmp 0x3eb0
movq %r14, %rdi
movl $0x2a, %esi
callq 0x3a74
movl $0x10, %edi
callq 0x10e0
movq %rax, %r12
movq %r14, %rdi
movq %r15, %rsi
callq 0x40c3
movq %rax, 0x8(%r12)
movq %r14, %rdi
movl $0x29, %esi
callq 0x3a74
movq %r14, %rdi
movl $0xe, %esi
callq 0x3a74
movq %r14, %rdi
movq %r15, %rsi
callq 0x401c
movq %rax, (%r12)
movq %r14, %rdi
movl $0xf, %esi
callq 0x3a74
movq %r14, %rdi
movl $0x12, %esi
callq 0x3a74
movq %r12, 0x8(%rbx)
movl $0x8, (%rbx)
jmp 0x3eb0
movq %r14, %rdi
movl $0x28, %esi
callq 0x3a74
movl $0x28, %edi
callq 0x10e0
movq %rax, %r12
movq %r15, %rdi
callq 0x2e04
movq %rax, %r15
movq %rax, 0x20(%r12)
movq %r14, %rdi
movl $0xe, %esi
callq 0x3a74
movq %r14, %rdi
callq 0x305e
movl (%rax), %eax
addl $-0x5, %eax
cmpl $0x1, %eax
ja 0x3db2
movq %r14, %rdi
movq %r15, %rsi
callq 0x3ab2
movq %rax, %r13
jmp 0x3de6
movq %r14, %rdi
movl $0x29, %esi
callq 0x3a74
movl $0x10, %edi
callq 0x10e0
movq %rax, %r12
movq %r14, %rdi
movl $0xe, %esi
callq 0x3a74
movq %r14, %rdi
movq %r15, %rsi
callq 0x401c
movq %rax, (%r12)
movq %r14, %rdi
movl $0xf, %esi
callq 0x3a74
movq %r14, %rdi
movq %r15, %rsi
callq 0x40c3
movq %rax, 0x8(%r12)
movq %r12, 0x8(%rbx)
movl $0x7, (%rbx)
jmp 0x3eb0
movl $0x8, %edi
callq 0x10e0
movq %rax, %r12
movq %r14, %rdi
movl $0x4, %esi
callq 0x3a74
movq %r14, %rdi
movq %r15, %rsi
callq 0x401c
movq %rax, (%r12)
movq %r14, %rdi
movl $0x12, %esi
callq 0x3a74
movl $0x0, (%rbx)
movq %r12, 0x8(%rbx)
jmp 0x3eb0
movq %r14, %rdi
movl $0x2c, %esi
callq 0x39ab
testb %al, %al
je 0x3e86
movq %r14, %rdi
movl $0x12, %esi
callq 0x3a74
movl $0xa, (%rbx)
jmp 0x3eb0
movl $0x10, %edi
callq 0x10e0
movq %rax, %r13
movl $0x4, (%rax)
movq %r14, %rdi
movq %r15, %rsi
movl $0x12, %edx
callq 0x4152
movq %rax, 0x8(%r13)
movq %r14, %rdi
movl $0x12, %esi
callq 0x3a74
movq %r13, (%r12)
movq %r14, %rdi
movq %r15, %rsi
movl $0x12, %edx
callq 0x4152
movq %rax, 0x8(%r12)
cmpl $0x5, (%rax)
jne 0x3e36
movl $0x10, %edi
callq 0x10e0
movq %rax, %r15
xorl %ebp, %ebp
movl %ebp, (%rax)
movl $0x10, %edi
callq 0x10e0
movq %rax, 0x8(%r15)
movl %ebp, (%rax)
movl $0x1, %ecx
movl %ecx, 0x8(%rax)
movl %ecx, 0x4(%r15)
movq %r15, 0x8(%r12)
movq %r14, %rdi
movl $0x12, %esi
callq 0x3a74
movq 0x20(%r12), %r15
movq %r14, %rdi
movq %r15, %rsi
movl $0xf, %edx
callq 0x4152
movq %rax, 0x10(%r12)
movq %r14, %rdi
movl $0xf, %esi
callq 0x3a74
movq %r14, %rdi
movq %r15, %rsi
callq 0x40c3
movq %rax, 0x18(%r12)
movq %r12, 0x8(%rbx)
movl $0x6, (%rbx)
jmp 0x3eb0
movl (%r12), %eax
addl $-0x5, %eax
cmpl $0x1, %eax
ja 0x3ec2
movq %r14, %rdi
movq %r15, %rsi
callq 0x3f3a
testq %rax, %rax
je 0x3eae
movl $0x1, (%rbx)
movq %rax, 0x8(%rbx)
jmp 0x3eb0
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 0x39ab
testb %al, %al
je 0x3edb
movl $0x5, (%rbx)
jmp 0x3eb0
movq %r14, %rdi
movq %r15, %rsi
callq 0x401c
movl $0x4, (%rbx)
movq %rax, 0x8(%rbx)
movq %r14, %rdi
movl $0x12, %esi
callq 0x3a74
jmp 0x3eb0