// File: /workspace/github_c_makefile_star2_O2/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, -0x38(%rbp)
testq %rsi, %rsi
je 0x18296
cmpl $0xc00, (%rsi)          # imm = 0xC00
jne 0x182b5
movl 0x44(%rsi), %eax
testl %eax, %eax
jle 0x182d4
movl 0x70(%rsi), %ecx
movl %ecx, -0x2c(%rbp)
leaq 0x2822c(%rip), %r12     # 0x3ff15
leaq -0x50(%rbp), %r13
xorl %edx, %edx
movq %rsi, -0x68(%rbp)
cltq
cmpq %rax, %rdx
jge 0x1819c
movq 0x48(%rsi), %rax
movq (%rax,%rdx,8), %rdi
cmpl $0xc01, (%rdi)          # imm = 0xC01
jne 0x181d2
movl 0x44(%rdi), %ecx
movl %ecx, %eax
andl $-0x2, %eax
cmpl $0x2, %eax
jne 0x181f1
movq 0x48(%rdi), %rax
movq (%rax), %rbx
cmpl $0x200, (%rbx)          # imm = 0x200
jne 0x18210
cmpl $0x3, %ecx
jne 0x17d41
movq 0x10(%rax), %r15
jmp 0x17d44
xorl %r15d, %r15d
movq 0x8(%rax), %r14
movzbl 0x60(%rsi), %eax
cmpl $0x1, %eax
movq %rdx, -0x70(%rbp)
movq %rdi, -0x40(%rbp)
je 0x17fb3
movq %r15, -0x60(%rbp)
cmpl $0x2, %eax
jne 0x181ae
movq %r12, %r15
movq %r13, %rdi
callq 0x6e1c
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rax
movl 0x54(%rax), %r13d
leal 0x1(%r13), %ecx
movl %ecx, 0x54(%rax)
movq 0x10(%rbx), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
leaq -0x90(%rbp), %r12
movq %r12, %rdi
callq 0x6fa2
movq %r12, %rdi
leaq 0x29e5e(%rip), %rsi     # 0x41c0a
movq %r13, %rdx
leaq -0x50(%rbp), %rbx
xorl %eax, %eax
callq 0x739a
movq %rbx, %rdi
leaq 0x29e65(%rip), %rsi     # 0x41c29
callq 0x6e99
movq %rbx, %rdi
movq %r12, %rsi
callq 0x712e
movq %rbx, %rdi
leaq 0x29e31(%rip), %rsi     # 0x41c0f
callq 0x6e99
leaq -0x80(%rbp), %r12
movq %r12, %rdi
callq 0x6e1c
movq %r12, %rdi
movq %r14, %r12
movq %r14, %rsi
callq 0x18312
movq -0x78(%rbp), %rsi
testq %rsi, %rsi
leaq 0x2810b(%rip), %rax     # 0x3ff16
cmoveq %rax, %rsi
movq %rbx, %rdi
callq 0x6e99
movl -0x2c(%rbp), %edx
testl %edx, %edx
jle 0x17e2f
movq %rbx, %rdi
leaq 0x29dd7(%rip), %rsi     # 0x41bff
xorl %eax, %eax
callq 0x739a
movq %rbx, %rdi
movq %r15, %rsi
callq 0x6e99
movq -0x60(%rbp), %r15
testq %r15, %r15
movq -0x38(%rbp), %rbx
leaq -0x120(%rbp), %r14
je 0x17f30
leaq -0xa0(%rbp), %r13
movq %r13, %rdi
callq 0x6e1c
movq %r14, %rdi
callq 0x188c9
movq %r12, -0x58(%rbp)
leaq -0x1a0(%rbp), %r12
movq %r12, %rdi
callq 0x188c9
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x18941
movq 0x68(%r15), %r8
movq %rbx, %rdi
movq %r12, %rsi
movq -0x58(%rbp), %rdx
movq %r14, %rcx
callq 0x1896e
movq %r13, %rdi
leaq -0x190(%rbp), %rsi
callq 0x712e
movq %r13, %rdi
movq %r12, %rsi
callq 0x712e
movq -0x178(%rbp), %rdx
testq %rdx, %rdx
leaq 0x28048(%rip), %rax     # 0x3ff16
cmoveq %rax, %rdx
movq -0x78(%rbp), %rcx
testq %rcx, %rcx
cmoveq %rax, %rcx
movq %r13, %rdi
leaq 0x29d33(%rip), %rsi     # 0x41c1a
xorl %eax, %eax
callq 0x739a
movq %r13, %rdi
leaq -0x90(%rbp), %rsi
callq 0x712e
movq %r13, %rdi
leaq 0x2800e(%rip), %rsi     # 0x3ff15
callq 0x6e99
movq 0x20(%rbx), %rdi
movq %r13, %rsi
callq 0x712e
movq %r13, %rdi
callq 0x6e1c
movq %r14, %rdi
callq 0x191df
movq %r12, %rdi
callq 0x191df
movq 0x30(%rbx), %rdi
testq %rdi, %rdi
je 0x18239
movq -0x48(%rbp), %rsi
testq %rsi, %rsi
leaq 0x27fcb(%rip), %rax     # 0x3ff16
cmoveq %rax, %rsi
callq 0x6e99
movq -0x40(%rbp), %rax
movq 0x60(%rax), %r15
movb $0x1, 0x48(%r15)
addq $0x50, %r15
movq %r15, %rdi
callq 0x6e1c
movq %r15, %rdi
pushq $0x25
popq %rsi
callq 0x6f81
movq %r15, %rdi
leaq -0x90(%rbp), %rbx
movq %rbx, %rsi
callq 0x712e
leaq -0x80(%rbp), %rdi
callq 0x72b9
leaq -0x50(%rbp), %r13
movq %r13, %rdi
callq 0x72b9
movq %rbx, %rdi
callq 0x72b9
leaq 0x27f67(%rip), %r12     # 0x3ff15
jmp 0x18141
leaq -0x120(%rbp), %r12
movq %r12, %rdi
callq 0x6e1c
movq %r12, %rdi
leaq 0x29c14(%rip), %rsi     # 0x41be0
callq 0x6e99
movq 0x10(%rbx), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
movq %r12, %rdi
callq 0x700f
testq %r15, %r15
leaq 0x29c06(%rip), %rsi     # 0x41bf4
leaq 0x29bed(%rip), %rax     # 0x41be2
cmoveq %rax, %rsi
movq %r12, %rdi
callq 0x6e99
leaq -0x1a0(%rbp), %r13
movq %r13, %rdi
callq 0x6e1c
movq %r13, %rdi
movq %r14, %rsi
callq 0x18312
movq -0x198(%rbp), %rsi
testq %rsi, %rsi
leaq 0x27eea(%rip), %rax     # 0x3ff16
cmoveq %rax, %rsi
movq %r12, %rdi
callq 0x6e99
movq %r13, %rdi
callq 0x72b9
movq %r12, %rdi
leaq 0x25318(%rip), %rsi     # 0x3d362
callq 0x6e99
testq %r15, %r15
je 0x1807a
cmpb $0x0, 0x70(%r15)
je 0x18258
movq -0x38(%rbp), %rdi
xorl %esi, %esi
leaq -0x120(%rbp), %rdx
movq %r15, %rcx
callq 0x19259
leaq -0x50(%rbp), %r13
jmp 0x180ac
testq %r14, %r14
je 0x18277
movl (%r14), %eax
leal -0x400(%rax), %ecx
leaq 0x242cd(%rip), %rsi     # 0x3c360
cmpl $0x9, %ecx
leaq -0x50(%rbp), %r13
jae 0x18154
leaq -0x120(%rbp), %rdi
callq 0x6e99
movl -0x2c(%rbp), %edx
testl %edx, %edx
leaq -0x120(%rbp), %r15
jle 0x180cb
movq %r15, %rdi
leaq 0x29b3b(%rip), %rsi     # 0x41bff
xorl %eax, %eax
callq 0x739a
movq -0x38(%rbp), %r12
movq 0x20(%r12), %rdi
movq -0x118(%rbp), %rsi
testq %rsi, %rsi
leaq 0x27e31(%rip), %rax     # 0x3ff16
cmoveq %rax, %rsi
callq 0x6e99
movq %r15, %rdi
callq 0x72b9
movq -0x40(%rbp), %rax
movq 0x60(%rax), %r15
movb $0x1, 0x48(%r15)
addq $0x50, %r15
movq %r15, %rdi
callq 0x6e1c
movq %r15, %rdi
pushq $0x40
popq %rsi
callq 0x6f81
movq 0x10(%rbx), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
movq %r15, %rdi
callq 0x700f
movq 0x20(%r12), %rdi
leaq 0x27ddc(%rip), %r12     # 0x3ff15
movq %r12, %rsi
callq 0x6e99
movq -0x70(%rbp), %rdx
incq %rdx
movq -0x68(%rbp), %rsi
movl 0x44(%rsi), %eax
jmp 0x17cf3
leal -0x409(%rax), %ecx
cmpl $0x2, %ecx
jb 0x18190
cmpl $0x410, %eax            # imm = 0x410
je 0x18184
cmpl $0x420, %eax            # imm = 0x420
je 0x18184
cmpl $0x440, %eax            # imm = 0x440
jne 0x182f3
leaq 0x29c07(%rip), %rsi     # 0x41d86
jmp 0x180a0
leaq 0x29beb(%rip), %rsi     # 0x41d76
jmp 0x180a0
leaq 0x29bdb(%rip), %rsi     # 0x41d72
jmp 0x180a0
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x241ab(%rip), %rdi     # 0x3c360
leaq 0x297cc(%rip), %rsi     # 0x41988
leaq 0x299e3(%rip), %rcx     # 0x41ba6
cmpl $0x3, %eax
jne 0x1822f
movl $0x49b, %edx            # imm = 0x49B
callq 0x30a0
leaq 0x263ce(%rip), %rdi     # 0x3e5a7
leaq 0x297a8(%rip), %rsi     # 0x41988
leaq 0x299bf(%rip), %rcx     # 0x41ba6
movl $0x401, %edx            # imm = 0x401
callq 0x30a0
leaq 0x263d7(%rip), %rdi     # 0x3e5cf
leaq 0x29789(%rip), %rsi     # 0x41988
leaq 0x299a0(%rip), %rcx     # 0x41ba6
movl $0x402, %edx            # imm = 0x402
callq 0x30a0
leaq 0x263f3(%rip), %rdi     # 0x3e60a
leaq 0x2976a(%rip), %rsi     # 0x41988
leaq 0x29981(%rip), %rcx     # 0x41ba6
movl $0x405, %edx            # imm = 0x405
callq 0x30a0
movl $0x49f, %edx            # imm = 0x49F
callq 0x30a0
leaq 0x299eb(%rip), %rdi     # 0x41c2b
leaq 0x29741(%rip), %rsi     # 0x41988
leaq 0x29958(%rip), %rcx     # 0x41ba6
movl $0x488, %edx            # imm = 0x488
callq 0x30a0
leaq 0x2681a(%rip), %rdi     # 0x3ea79
leaq 0x29722(%rip), %rsi     # 0x41988
leaq 0x29b1e(%rip), %rcx     # 0x41d8b
movl $0x3ef, %edx            # imm = 0x3EF
callq 0x30a0
leaq 0x26495(%rip), %rdi     # 0x3e713
leaq 0x29703(%rip), %rsi     # 0x41988
leaq 0x29a8c(%rip), %rcx     # 0x41d18
movl $0x2fc, %edx            # imm = 0x2FC
callq 0x30a0
leaq 0x27b55(%rip), %rdi     # 0x3fdf2
leaq 0x296e4(%rip), %rsi     # 0x41988
leaq 0x298fb(%rip), %rcx     # 0x41ba6
movl $0x3f9, %edx            # imm = 0x3F9
callq 0x30a0
leaq 0x2fbdb(%rip), %rdi     # 0x47e97
leaq 0x296c5(%rip), %rsi     # 0x41988
leaq 0x298dc(%rip), %rcx     # 0x41ba6
movl $0x3fa, %edx            # imm = 0x3FA
callq 0x30a0
leaq 0x262ba(%rip), %rdi     # 0x3e595
leaq 0x296a6(%rip), %rsi     # 0x41988
leaq 0x298bd(%rip), %rcx     # 0x41ba6
movl $0x3fb, %edx            # imm = 0x3FB
callq 0x30a0
leaq 0x24066(%rip), %rdi     # 0x3c360
leaq 0x29687(%rip), %rsi     # 0x41988
leaq 0x29a10(%rip), %rcx     # 0x41d18
movl $0x31a, %edx            # imm = 0x31A
callq 0x30a0