// File: /workspace/github_c_makefile_star2_O0/yutopp[P]cc/analyzer.c
// Function: analyze
void analyze(Analyzer* a, Node* node, Env* env) {
    switch(node->kind) {
    case NODE_TRANS_UNIT:
    {
        printf("LOG: translation unit\n");
        Env* t_env = env_new(NULL, env); // TODO: Set a name of the translation unit
        t_env->kind = ENV_KIND_TRANS;

        Vector* decls = node->value.trans_unit.decls;
        for(size_t i=0; i<vector_len(decls); ++i) {
            Node** n = (Node**)vector_at(decls, i);
            analyze(a, *n, t_env);
        }

        env_drop(t_env);
        break;
    }

    case NODE_FUNC_DEF:
    {
        printf("LOG: function def\n");

        node_fprint(stdout, node->value.func_def.decl);
        assert(node->value.func_def.decl->kind == NODE_DECLARATOR);
        Token* id_tok = node_declarator_extract_id_token(node->value.func_def.decl);
        assert(id_tok); // TODO: error handling

        // TODO: Lookup a decl

        Env* f_env = env_new(id_tok, env);
        f_env->kind = ENV_KIND_FUNC;

        // fprint_impl(fp, node->value.func_def.decl_spec, 0);
        // fprint_impl(fp, node->value.func_def.decl, 0);
        analyze(a, node->value.func_def.block, f_env);

        env_insert(env, f_env);

        break;
    }

    case NODE_STMT_COMPOUND:
    {
        printf("LOG: statement compound\n");

        Env* b_env = env_new(NULL, env);
        b_env->kind = ENV_KIND_BLOCK;

        Vector* stmts = node->value.stmt_compound.stmts;
        for(size_t i=0; i<vector_len(stmts); ++i) {
            Node** n = (Node**)vector_at(stmts, i);
            analyze(a, *n, b_env);
        }

        env_drop(b_env);
        break;
    }

    case NODE_STMT_EXPR:
        if (node->value.stmt_expr.expr) {
            analyze_expr(a, node->value.stmt_expr.expr, env);
        }
        break;

    case NODE_STMT_IF:
        // TODO: Dig a scope
        analyze_expr(a, node->value.stmt_if.cond, env);

        analyze(a, node->value.stmt_if.then_b, env);

        if (node->value.stmt_if.else_b) {
            analyze(a, node->value.stmt_if.else_b, env);
        }
        break;

    case NODE_STMT_JUMP:
        printf("LOG: statement jump\n");

        switch(node->value.stmt_jump.kind) {
        case TOK_KIND_RETURN:
            analyze_expr(a, node->value.stmt_jump.expr, env);
            break;

        default:
            // TODO: error handling...
            assert(0);
            break;
        }
        break;

    default:
        // TODO: error handling...
        fprintf(stderr, "Unknown kind: %d\n", node->kind);
        assert(0);
        break;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movq %rax, -0x70(%rbp)
subq $0x5, %rax
ja 0x6cce
movq -0x70(%rbp), %rax
leaq 0x4f02(%rip), %rcx      # 0xb8f8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x4f62(%rip), %rdi      # 0xb968
movb $0x0, %al
callq 0x1090
movq -0x18(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edi
callq 0x6730
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movl $0x0, 0x18(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x28(%rbp), %rdi
callq 0x4580
movq %rax, %rcx
movq -0x78(%rbp), %rax
cmpq %rcx, %rax
jae 0x6a8d
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x4530
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movq (%rax), %rsi
movq -0x20(%rbp), %rdx
callq 0x69c0
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x6a3d
movq -0x20(%rbp), %rdi
callq 0x67e0
jmp 0x6d0b
leaq 0x4c9c(%rip), %rdi      # 0xb73e
movb $0x0, %al
callq 0x1090
movq 0x8528(%rip), %rax      # 0xefd8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x4650
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
cmpl $0xc, (%rax)
jne 0x6acf
jmp 0x6aee
leaq 0x4ea9(%rip), %rdi      # 0xb97f
leaq 0x4e57(%rip), %rsi      # 0xb934
movl $0x7c, %edx
leaq 0x4ec9(%rip), %rcx      # 0xb9b2
callq 0x10b0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x4cf0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x6b08
jmp 0x6b27
leaq 0x4c42(%rip), %rdi      # 0xb751
leaq 0x4e1e(%rip), %rsi      # 0xb934
movl $0x7e, %edx
leaq 0x4e90(%rip), %rcx      # 0xb9b2
callq 0x10b0
movq -0x40(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x6730
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movl $0x1, 0x18(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x48(%rbp), %rdx
callq 0x69c0
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x6820
jmp 0x6d0b
leaq 0x4c1d(%rip), %rdi      # 0xb78e
movb $0x0, %al
callq 0x1090
movq -0x18(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edi
callq 0x6730
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movl $0x2, 0x18(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x58(%rbp), %rdi
callq 0x4580
movq %rax, %rcx
movq -0x80(%rbp), %rax
cmpq %rcx, %rax
jae 0x6bf8
movq -0x58(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x4530
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rax
movq (%rax), %rsi
movq -0x50(%rbp), %rdx
callq 0x69c0
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x6ba8
movq -0x50(%rbp), %rdi
callq 0x67e0
jmp 0x6d0b
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x6c26
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x18(%rbp), %rdx
callq 0x6d30
jmp 0x6d0b
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x18(%rbp), %rdx
callq 0x6d30
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x18(%rbp), %rdx
callq 0x69c0
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x6c75
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x18(%rbp), %rdx
callq 0x69c0
jmp 0x6d0b
leaq 0x4b4e(%rip), %rdi      # 0xb7cf
xorl %eax, %eax
callq 0x1090
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
subl $0x19, %eax
jne 0x6cad
jmp 0x6c96
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x18(%rbp), %rdx
callq 0x6d30
jmp 0x6ccc
leaq 0x4cb2(%rip), %rdi      # 0xb966
leaq 0x4c79(%rip), %rsi      # 0xb934
movl $0xba, %edx
leaq 0x4ceb(%rip), %rcx      # 0xb9b2
callq 0x10b0
jmp 0x6d0b
movq 0x8323(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movl (%rax), %edx
leaq 0x48a7(%rip), %rsi      # 0xb58c
movb $0x0, %al
callq 0x10e0
leaq 0x4c73(%rip), %rdi      # 0xb966
leaq 0x4c3a(%rip), %rsi      # 0xb934
movl $0xc2, %edx
leaq 0x4cac(%rip), %rcx      # 0xb9b2
callq 0x10b0
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)