// File: /workspace/github_c_makefile_star2_O2/yutopp[P]cc/analyzer.c
// Function: analyze_expr
Type* analyze_expr(Analyzer* a, Node* node, Env* env) {
    switch(node->kind) {
    case NODE_EXPR_BIN:
    {
        printf("LOG: expr binary = ");
        token_fprint_buf(stdout, node->value.expr_bin.op);
        printf("\n");

        analyze_expr(a, node->value.expr_bin.lhs, env);
        analyze_expr(a, node->value.expr_bin.rhs, env);

        break;
    }

    case NODE_EXPR_POSTFIX:
    {
        fprintf(DEBUGOUT,"LOG: postfix = \n");

        analyze_expr(a, node->value.expr_postfix.lhs, env);

        switch(node->value.expr_postfix.kind) {
        case NODE_EXPR_POSTFIX_KIND_FUNC_CALL:
            if (node->value.expr_postfix.rhs) {
                analyze_expr(a, node->value.expr_postfix.rhs, env);
            }
            break;

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

        break;
    }

    case NODE_LIT_INT:
    {
        printf("LOG: lit int = %d\n", node->value.lit_int.v);

        Type* ty = type_arena_malloc(a->arena);
        ty->kind = TYPE_KIND_INT;
        ty->value.int_.bits = -1;

        return ty;
    }

    case NODE_LIT_STRING:
    {
        printf("LOG: lit string = %d\n", node->value.lit_string.v);

        Type* ty_inner = type_arena_malloc(a->arena);
        ty_inner->kind = TYPE_KIND_INT;
        ty_inner->value.int_.bits = 8;

        Type* ty = type_arena_malloc(a->arena);
        ty->kind = TYPE_KIND_PTR;
        ty->value.ptr.inner = ty_inner;

        node->ty = ty;

        return ty;
    }

    case NODE_ID:
    {
        fprintf(DEBUGOUT, "LOG: id =");
        token_fprint_buf(DEBUGOUT, node->value.id.tok);
        fprintf(DEBUGOUT, "\n");

        Env* found = env_lookup(env, node->value.id.tok);
        if (found == NULL) {
            fprintf(DEBUGOUT, "! NOT FOUND\n");
            return NULL;
        }
        fprintf(DEBUGOUT, "! FOUND\n");

        return NULL; // TODO: fix
    }

    case NODE_ARGS_LIST:
    {
        fprintf(DEBUGOUT, "LOG: args list\n");

        Vector* args = node->value.args_list.args;
        for(size_t i=0; i<vector_len(args); ++i) {
            Node** n = (Node**)vector_at(args, i);
            analyze_expr(a, *n, env); // TODO: error handling
        }

        return NULL; // TODO: fix
    }

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x272b(%rip), %r12      # 0x67e8
movq 0x6f14(%rip), %r13      # 0xafd8
pushq $0x1
popq %rbp
movl (%r15), %edx
leal -0x6(%rdx), %eax
cmpl $0x5, %eax
ja 0x42a5
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
leaq 0x27b5(%rip), %rdi      # 0x689b
xorl %eax, %eax
callq 0x10b0
movq (%r13), %rdi
movq 0x8(%r15), %rsi
callq 0x18e4
pushq $0xa
popq %rdi
callq 0x1050
movq 0x10(%r15), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x40a2
movq 0x18(%r15), %r15
jmp 0x40c7
movq (%r13), %rcx
leaq 0x278d(%rip), %rdi      # 0x68af
pushq $0x10
popq %rsi
movq %rbp, %rdx
callq 0x11c0
movq 0x10(%r15), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x40a2
cmpl $0x0, 0x8(%r15)
jne 0x4286
movq 0x18(%r15), %r15
testq %r15, %r15
jne 0x40c7
jmp 0x4257
movq (%r13), %rcx
leaq 0x27b3(%rip), %rdi      # 0x6917
pushq $0x9
popq %rsi
pushq $0x1
popq %rdx
callq 0x11c0
movq (%r13), %rdi
movq 0x8(%r15), %rsi
callq 0x18e4
movq (%r13), %rsi
pushq $0xa
popq %rdi
callq 0x10f0
movq 0x8(%r15), %rsi
movq %r14, %rdi
callq 0x3de4
movq (%r13), %rcx
testq %rax, %rax
je 0x4266
leaq 0x2786(%rip), %rdi      # 0x692e
pushq $0x8
jmp 0x426f
movq 0x8(%r15), %rsi
leaq 0x2747(%rip), %rdi      # 0x6901
xorl %eax, %eax
callq 0x10b0
movq (%rbx), %rdi
callq 0x5918
movq %rax, %r14
andl $0x0, (%rax)
movl $0x8, 0x8(%rax)
movq (%rbx), %rdi
callq 0x5918
movl $0x1, (%rax)
movq %r14, 0x8(%rax)
movq %rax, 0x28(%r15)
jmp 0x4257
movq (%r13), %rcx
leaq 0x273e(%rip), %rdi      # 0x6937
pushq $0xf
popq %rsi
pushq $0x1
popq %rdx
callq 0x11c0
movq 0x8(%r15), %r15
xorl %r12d, %r12d
movq %r15, %rdi
callq 0x29db
cmpq %rax, %r12
jae 0x4257
movq %r15, %rdi
movq %r12, %rsi
callq 0x29c6
movq (%rax), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x40a2
incq %r12
jmp 0x420b
movl 0x8(%r15), %esi
leaq 0x26ad(%rip), %rdi      # 0x68ee
xorl %eax, %eax
callq 0x10b0
movq (%rbx), %rdi
callq 0x5918
andl $0x0, (%rax)
orl $-0x1, 0x8(%rax)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x26b4(%rip), %rdi      # 0x6921
pushq $0xc
popq %rsi
pushq $0x1
popq %rdx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x11c0
leaq 0x25b1(%rip), %rdi      # 0x683e
leaq 0x2578(%rip), %rsi      # 0x680c
leaq 0x2625(%rip), %rcx      # 0x68c0
movl $0xe4, %edx
callq 0x10d0
movq 0x6d4c(%rip), %rax      # 0xaff8
movq (%rax), %rdi
leaq 0x2691(%rip), %rsi      # 0x6947
xorl %eax, %eax
callq 0x1120
leaq 0x257a(%rip), %rdi      # 0x683e
leaq 0x2541(%rip), %rsi      # 0x680c
leaq 0x25ee(%rip), %rcx      # 0x68c0
movl $0x126, %edx            # imm = 0x126
callq 0x10d0