// File: /workspace/github_c_makefile_star2_O1/Ihsoh[P]ilang/compiler/frontend/irgen_llvmir.c
// Function: ir_var
static void _ir_var(
	IRGeneratorLlvmirContext *ctx,
	ParserASTNode *node
) {
	assert(ctx);
	assert(node);
	assert(node->type == FE_NODE_VAR);
	assert(node->nchilds > 0);

	int align = FE_VAR_AST_NODE_GET_ALIGN(node);

	for (int i = 0; i < node->nchilds; i++) {
		ParserASTNode *node_var_item = node->childs[i];
		assert(node_var_item->type == FE_NODE_VAR_ITEM);
		assert(node_var_item->nchilds == 2 || node_var_item->nchilds == 3);

		ParserASTNode *node_identifier = node_var_item->childs[0];
		assert(node_identifier->type == FE_NODE_IDENTIFIER);
		ParserASTNode *node_type = node_var_item->childs[1];
		ParserASTNode *node_expr = NULL;

		if (node_var_item->nchilds == 3) {
			node_expr = node_var_item->childs[2];
		}

		switch (FE_VAR_AST_NODE_GET_TYPE(node)) {
			case FE_VAR_TYPE_GLOBAL: {
				// @c = common global i8 zeroinitializer, align 1
				ResizableString rstr;
				rstr_init(&rstr);

				rstr_append_with_cstr(&rstr, "@");
				rstr_append_with_raw(&rstr, node_identifier->token->content, node_identifier->token->len);
				
				if (node_expr == NULL) {
					rstr_append_with_cstr(&rstr, " = common global ");
				} else {
					rstr_append_with_cstr(&rstr, " = global ");
				}

				ResizableString rstr_type;
				rstr_init(&rstr_type);
				_ir_type(ctx, &rstr_type, node_type);
				rstr_append_with_cstr(&rstr, RSTR_CSTR(&rstr_type));
				rstr_free(&rstr_type);

				rstr_append_with_cstr(&rstr, " ");

				if (node_expr == NULL) {
					_ir_zero_initializer(ctx, &rstr, node_type);
				} else {
					_ir_constexpr_initializer(ctx, &rstr, node_expr);
				}

				if (align > 0) {
					rstr_appendf(&rstr, ", align %d", align);
				}

				rstr_append_with_cstr(ctx->body, RSTR_CSTR(&rstr));

				rstr_free(&rstr);

				// 更新全局变量符号在代码生成时的名字。
				ParserSymbol *symbol = FE_VAR_ITEM_AST_NODE_GET_SYMBOL(node_var_item);
				FE_VAR_SYMBOL_SET_HAS_CODE_GEN_NAME(symbol, true);
				rstr_init(FE_VAR_SYMBOL_GET_CODE_GEN_NAME(symbol));
				rstr_append_with_char(FE_VAR_SYMBOL_GET_CODE_GEN_NAME(symbol), '@');
				rstr_append_with_raw(
					FE_VAR_SYMBOL_GET_CODE_GEN_NAME(symbol),
					node_identifier->token->content,
					node_identifier->token->len
				);

				rstr_append_with_cstr(ctx->body, "\n");
				break;
			}
			case FE_VAR_TYPE_LOCAL: {
				// %3 = alloca i32, align 4
				ResizableString rstr;
				rstr_init(&rstr);

				size_t no = FE_FUNC_SYMBOL_NEXT_NO(ctx->func_symbol);
				ResizableString rstr_local_name;
				rstr_init_with_raw(
					&rstr_local_name,
					node_identifier->token->content,
					node_identifier->token->len
				);
				rstr_appendf(
					&rstr_local_name,
					"_%zu",
					no
				);

				rstr_append_with_cstr(&rstr, "%");
				rstr_append_with_rstr(&rstr, &rstr_local_name);
				rstr_append_with_cstr(&rstr, " = alloca ");

				ResizableString rstr_type;
				rstr_init(&rstr_type);
				_ir_type(ctx, &rstr_type, node_type);
				rstr_append_with_cstr(&rstr, RSTR_CSTR(&rstr_type));

				if (align > 0) {
					rstr_appendf(&rstr, ", align %d", align);
				}

				rstr_append_with_cstr(&rstr, "\n");

				if (node_expr != NULL) {
					ResizableString rstr_expr;
					rstr_init(&rstr_expr);

					_ExprResult result;
					_expr_result_init(&result);

					_ExprResult casted_result;
					_expr_result_init(&casted_result);

					_ir_expr_wrapper_val(ctx, &rstr_expr, &result, node_expr);
					_expr_result_cast(
						ctx,
						&casted_result,
						node_type,
						&result,
						FE_EXPR_AST_NODE_GET_TYPE_NODE(node_expr)
					);
					rstr_append_with_rstr(&rstr_expr, &(casted_result.rstr_for_result_ptr));
					rstr_append_with_rstr(&rstr_expr, &(casted_result.rstr_for_result));
					rstr_appendf(
						&rstr_expr,
						"store %s, %s* %%",
						RSTR_CSTR(&(casted_result.rstr_result)),
						RSTR_CSTR(&rstr_type)
					);
					rstr_append_with_rstr(
						&rstr_expr,
						&rstr_local_name	
					);
					rstr_append_with_cstr(&rstr_expr, "\n");

					rstr_append_with_rstr(ctx->body, &rstr_expr);

					rstr_init(&rstr_expr);
					_expr_result_free(&result);
					_expr_result_free(&casted_result);
				}

				assert(ctx->local_var_defs);
				rstr_append_with_cstr(ctx->local_var_defs, RSTR_CSTR(&rstr));

				// 更新局部变量符号在代码生成时的名字。
				ParserSymbol *symbol = FE_VAR_ITEM_AST_NODE_GET_SYMBOL(node_var_item);
				FE_VAR_SYMBOL_SET_HAS_CODE_GEN_NAME(symbol, true);
				rstr_init(FE_VAR_SYMBOL_GET_CODE_GEN_NAME(symbol));
				rstr_append_with_char(FE_VAR_SYMBOL_GET_CODE_GEN_NAME(symbol), '%');
				rstr_append_with_rstr(
					FE_VAR_SYMBOL_GET_CODE_GEN_NAME(symbol),
					&rstr_local_name
				);

				rstr_free(&rstr_type);
				rstr_free(&rstr);
				rstr_free(&rstr_local_name);
				break;
			}
			case FE_VAR_TYPE_STRUCT_MEMBER: {
				assert(0);
				break;
			}
			default: {
				assert(0);
			}
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %rdi, -0x48(%rbp)
testq %rsi, %rsi
je 0x1986e
cmpl $0xc00, (%rsi)          # imm = 0xC00
jne 0x1988d
cmpl $0x0, 0x44(%rsi)
jle 0x198ac
cmpl $0x0, 0x44(%rsi)
jle 0x19770
movl 0x70(%rsi), %eax
movl %eax, -0x3c(%rbp)
leaq 0x2ac38(%rip), %rbx     # 0x43eba
leaq -0x68(%rbp), %r13
xorl %r14d, %r14d
movq %rsi, -0x70(%rbp)
movq 0x48(%rsi), %rax
movq (%rax,%r14,8), %rdx
cmpl $0xc01, (%rdx)          # imm = 0xC01
jne 0x197e8
movl 0x44(%rdx), %ecx
movl %ecx, %eax
andl $-0x2, %eax
cmpl $0x2, %eax
jne 0x197c9
movq 0x48(%rdx), %rax
movq (%rax), %r12
cmpl $0x200, (%r12)          # imm = 0x200
jne 0x197aa
cmpl $0x3, %ecx
jne 0x192d6
movq 0x10(%rax), %rcx
movq %rcx, -0x50(%rbp)
jmp 0x192de
movq $0x0, -0x50(%rbp)
movq 0x8(%rax), %r15
movzbl 0x60(%rsi), %eax
cmpl $0x1, %eax
movq %rdx, -0x58(%rbp)
je 0x19548
movq %r15, -0x30(%rbp)
cmpl $0x2, %eax
jne 0x19782
movq %r13, %rdi
callq 0x7548
movq -0x48(%rbp), %rax
movq 0x40(%rax), %rax
movl 0x54(%rax), %r13d
leal 0x1(%r13), %ecx
movl %ecx, 0x54(%rax)
movq 0x10(%r12), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
leaq -0x90(%rbp), %r12
movq %r12, %rdi
callq 0x76ed
movq %r12, %rdi
leaq 0x2c7b6(%rip), %rsi     # 0x45af6
movq %r13, %rdx
leaq -0x68(%rbp), %r13
xorl %eax, %eax
callq 0x7b31
movq %r13, %rdi
leaq 0x2c7bd(%rip), %rsi     # 0x45b15
callq 0x75cf
movq %r13, %rdi
movq %r12, %rsi
callq 0x788c
movq %r13, %rdi
leaq 0x2c789(%rip), %rsi     # 0x45afb
callq 0x75cf
leaq -0x80(%rbp), %r12
movq %r12, %rdi
callq 0x7548
movq %r12, %rdi
movq -0x30(%rbp), %rsi
callq 0x198ea
movq -0x78(%rbp), %rsi
testq %rsi, %rsi
cmoveq %rbx, %rsi
movq %r13, %rdi
callq 0x75cf
movl -0x3c(%rbp), %ebx
testl %ebx, %ebx
movq -0x50(%rbp), %r15
jle 0x193c0
movq %r13, %rdi
leaq 0x2c734(%rip), %rsi     # 0x45aeb
movl %ebx, %edx
xorl %eax, %eax
callq 0x7b31
movq %r13, %rdi
leaq 0x2aaef(%rip), %rbx     # 0x43eb9
movq %rbx, %rsi
callq 0x75cf
testq %r15, %r15
movq -0x48(%rbp), %r12
je 0x194cd
movq %r14, -0x38(%rbp)
leaq -0xa0(%rbp), %r14
movq %r14, %rdi
callq 0x7548
leaq -0x1a0(%rbp), %r13
movq %r13, %rdi
callq 0x19eca
leaq -0x120(%rbp), %rdi
callq 0x19eca
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x19f44
movq 0x68(%r15), %r8
movq %r12, %rdi
leaq -0x120(%rbp), %r15
movq %r15, %rsi
movq -0x30(%rbp), %rdx
movq %r13, %rcx
callq 0x19f71
movq %r14, %rdi
leaq -0x110(%rbp), %rsi
callq 0x788c
movq %r14, %rdi
movq %r15, %rsi
callq 0x788c
movq -0xf8(%rbp), %rdx
testq %rdx, %rdx
leaq 0x2aa54(%rip), %rax     # 0x43eba
cmoveq %rax, %rdx
movq -0x78(%rbp), %rcx
testq %rcx, %rcx
cmoveq %rax, %rcx
movq %r14, %rdi
leaq 0x2c687(%rip), %rsi     # 0x45b06
xorl %eax, %eax
callq 0x7b31
movq %r14, %rdi
leaq -0x90(%rbp), %rsi
callq 0x788c
movq %r14, %rdi
movq %rbx, %rsi
callq 0x75cf
movq 0x20(%r12), %rdi
movq %r14, %rsi
callq 0x788c
movq %r14, %rdi
movq -0x38(%rbp), %r14
callq 0x7548
movq %r13, %rdi
leaq -0x68(%rbp), %r13
callq 0x1a7ab
movq %r15, %rdi
callq 0x1a7ab
movq 0x30(%r12), %rdi
testq %rdi, %rdi
je 0x19807
movq -0x60(%rbp), %rsi
testq %rsi, %rsi
leaq 0x2a9d1(%rip), %rbx     # 0x43eba
cmoveq %rbx, %rsi
callq 0x75cf
movq -0x58(%rbp), %rax
movq 0x60(%rax), %r15
movb $0x1, 0x48(%r15)
addq $0x50, %r15
movq %r15, %rdi
callq 0x7548
movq %r15, %rdi
movl $0x25, %esi
callq 0x76cb
movq %r15, %rdi
leaq -0x90(%rbp), %r15
movq %r15, %rsi
callq 0x788c
leaq -0x80(%rbp), %rdi
callq 0x7a38
movq %r13, %rdi
callq 0x7a38
movq %r15, %rdi
callq 0x7a38
jmp 0x196fa
movq %r14, -0x38(%rbp)
leaq -0x1a0(%rbp), %r13
movq %r13, %rdi
callq 0x7548
movq %r13, %rdi
leaq 0x2c567(%rip), %rsi     # 0x45acc
callq 0x75cf
movq %r12, -0x30(%rbp)
movq 0x10(%r12), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
movq %r13, %rdi
callq 0x7768
movq -0x50(%rbp), %r12
testq %r12, %r12
leaq 0x2c550(%rip), %rsi     # 0x45ae0
leaq 0x2c537(%rip), %rax     # 0x45ace
cmoveq %rax, %rsi
movq %r13, %rdi
callq 0x75cf
movq %rbx, %r14
leaq -0x120(%rbp), %rbx
movq %rbx, %rdi
callq 0x7548
movq %rbx, %rdi
movq %r15, %rsi
callq 0x198ea
movq -0x118(%rbp), %rsi
testq %rsi, %rsi
cmoveq %r14, %rsi
movq %r13, %rdi
callq 0x75cf
movq %rbx, %rdi
callq 0x7a38
movq %r13, %rdi
leaq 0x27dae(%rip), %rsi     # 0x41396
callq 0x75cf
testq %r12, %r12
je 0x19627
cmpb $0x0, 0x70(%r12)
je 0x19830
movq -0x48(%rbp), %rdi
xorl %esi, %esi
leaq -0x1a0(%rbp), %rdx
movq %r12, %rcx
movq %rdx, %r15
callq 0x1a825
leaq 0x2a89d(%rip), %rbx     # 0x43eba
movq -0x38(%rbp), %r14
movq -0x30(%rbp), %r12
jmp 0x19671
testq %r15, %r15
je 0x1984f
movl (%r15), %eax
cmpl $0x408, %eax            # imm = 0x408
leaq -0x1a0(%rbp), %r15
leaq 0x2a874(%rip), %rbx     # 0x43eba
movq -0x30(%rbp), %r12
jg 0x19710
addl $0xfffffc00, %eax       # imm = 0xFFFFFC00
cmpl $0x9, %eax
movq -0x38(%rbp), %r14
jae 0x198cb
movq %r15, %rdi
leaq 0x26d18(%rip), %rsi     # 0x40384
callq 0x75cf
movl -0x3c(%rbp), %edx
testl %edx, %edx
jle 0x19689
movq %r15, %rdi
leaq 0x2c469(%rip), %rsi     # 0x45aeb
xorl %eax, %eax
callq 0x7b31
movq -0x48(%rbp), %r13
movq 0x20(%r13), %rdi
movq -0x198(%rbp), %rsi
testq %rsi, %rsi
cmoveq %rbx, %rsi
callq 0x75cf
movq %r15, %rdi
callq 0x7a38
movq -0x58(%rbp), %rax
movq 0x60(%rax), %r15
movb $0x1, 0x48(%r15)
addq $0x50, %r15
movq %r15, %rdi
callq 0x7548
movq %r15, %rdi
movl $0x40, %esi
callq 0x76cb
movq 0x10(%r12), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
movq %r15, %rdi
callq 0x7768
movq 0x20(%r13), %rdi
leaq 0x2a7c8(%rip), %rsi     # 0x43eb9
callq 0x75cf
leaq -0x68(%rbp), %r13
incq %r14
movq -0x70(%rbp), %rsi
movslq 0x44(%rsi), %rax
cmpq %rax, %r14
jl 0x1928d
jmp 0x19770
cmpl $0x41f, %eax            # imm = 0x41F
movq -0x38(%rbp), %r14
jg 0x19735
leal -0x409(%rax), %ecx
cmpl $0x2, %ecx
jae 0x19756
movq %r15, %rdi
leaq 0x2c52e(%rip), %rsi     # 0x45c5e
jmp 0x1966c
cmpl $0x420, %eax            # imm = 0x420
je 0x19761
cmpl $0x440, %eax            # imm = 0x440
jne 0x198cb
movq %r15, %rdi
leaq 0x2c521(%rip), %rsi     # 0x45c72
jmp 0x1966c
cmpl $0x410, %eax            # imm = 0x410
jne 0x198cb
movq %r15, %rdi
leaq 0x2c4f7(%rip), %rsi     # 0x45c62
jmp 0x1966c
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x26bfb(%rip), %rdi     # 0x40384
leaq 0x2c0e4(%rip), %rsi     # 0x45874
leaq 0x2c2fb(%rip), %rcx     # 0x45a92
cmpl $0x3, %eax
jne 0x19826
movl $0x49b, %edx            # imm = 0x49B
callq 0x30a0
leaq 0x28dfd(%rip), %rdi     # 0x425ae
leaq 0x2c0bc(%rip), %rsi     # 0x45874
leaq 0x2c2d3(%rip), %rcx     # 0x45a92
movl $0x405, %edx            # imm = 0x405
callq 0x30a0
leaq 0x28da3(%rip), %rdi     # 0x42573
leaq 0x2c09d(%rip), %rsi     # 0x45874
leaq 0x2c2b4(%rip), %rcx     # 0x45a92
movl $0x402, %edx            # imm = 0x402
callq 0x30a0
leaq 0x28d5c(%rip), %rdi     # 0x4254b
leaq 0x2c07e(%rip), %rsi     # 0x45874
leaq 0x2c295(%rip), %rcx     # 0x45a92
movl $0x401, %edx            # imm = 0x401
callq 0x30a0
leaq 0x2c309(%rip), %rdi     # 0x45b17
leaq 0x2c05f(%rip), %rsi     # 0x45874
leaq 0x2c276(%rip), %rcx     # 0x45a92
movl $0x488, %edx            # imm = 0x488
callq 0x30a0
movl $0x49f, %edx            # imm = 0x49F
callq 0x30a0
leaq 0x291e6(%rip), %rdi     # 0x42a1d
leaq 0x2c036(%rip), %rsi     # 0x45874
leaq 0x2c432(%rip), %rcx     # 0x45c77
movl $0x3ef, %edx            # imm = 0x3EF
callq 0x30a0
leaq 0x28e61(%rip), %rdi     # 0x426b7
leaq 0x2c017(%rip), %rsi     # 0x45874
leaq 0x2c3a0(%rip), %rcx     # 0x45c04
movl $0x2fc, %edx            # imm = 0x2FC
callq 0x30a0
leaq 0x2a521(%rip), %rdi     # 0x43d96
leaq 0x2bff8(%rip), %rsi     # 0x45874
leaq 0x2c20f(%rip), %rcx     # 0x45a92
movl $0x3f9, %edx            # imm = 0x3F9
callq 0x30a0
leaq 0x32523(%rip), %rdi     # 0x4bdb7
leaq 0x2bfd9(%rip), %rsi     # 0x45874
leaq 0x2c1f0(%rip), %rcx     # 0x45a92
movl $0x3fa, %edx            # imm = 0x3FA
callq 0x30a0
leaq 0x28c86(%rip), %rdi     # 0x42539
leaq 0x2bfba(%rip), %rsi     # 0x45874
leaq 0x2c1d1(%rip), %rcx     # 0x45a92
movl $0x3fb, %edx            # imm = 0x3FB
callq 0x30a0
leaq 0x26ab2(%rip), %rdi     # 0x40384
leaq 0x2bf9b(%rip), %rsi     # 0x45874
leaq 0x2c324(%rip), %rcx     # 0x45c04
movl $0x31a, %edx            # imm = 0x31A
callq 0x30a0