// File: /workspace/github_c_makefile_star2_O3/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 0x194c4
cmpl $0xc00, (%rsi)          # imm = 0xC00
jne 0x194e3
cmpl $0x0, 0x44(%rsi)
jle 0x19502
movl 0x70(%rsi), %eax
movl %eax, -0x3c(%rbp)
leaq 0x2a025(%rip), %rbx     # 0x42f06
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 0x1943e
movl 0x44(%rdx), %ecx
movl %ecx, %eax
andl $-0x2, %eax
cmpl $0x2, %eax
jne 0x1941f
movq 0x48(%rdx), %rax
movq (%rax), %r12
cmpl $0x200, (%r12)          # imm = 0x200
jne 0x19400
cmpl $0x3, %ecx
jne 0x18f35
movq 0x10(%rax), %rcx
movq %rcx, -0x50(%rbp)
jmp 0x18f3d
movq $0x0, -0x50(%rbp)
movq 0x8(%rax), %r15
movzbl 0x60(%rsi), %eax
cmpl $0x1, %eax
movq %rdx, -0x58(%rbp)
je 0x191a7
movq %r15, -0x30(%rbp)
cmpl $0x2, %eax
jne 0x193d8
movq %r13, %rdi
callq 0x7354
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 0x74fc
movq %r12, %rdi
leaq 0x2bc27(%rip), %rsi     # 0x44bc6
movq %r13, %rdx
leaq -0x68(%rbp), %r13
xorl %eax, %eax
callq 0x7945
movq %r13, %rdi
leaq 0x2bc2e(%rip), %rsi     # 0x44be5
callq 0x73db
movq %r13, %rdi
movq %r12, %rsi
callq 0x769e
movq %r13, %rdi
leaq 0x2bbfa(%rip), %rsi     # 0x44bcb
callq 0x73db
leaq -0x80(%rbp), %r12
movq %r12, %rdi
callq 0x7354
movq %r12, %rdi
movq -0x30(%rbp), %rsi
callq 0x19540
movq -0x78(%rbp), %rsi
testq %rsi, %rsi
cmoveq %rbx, %rsi
movq %r13, %rdi
callq 0x73db
movl -0x3c(%rbp), %ebx
testl %ebx, %ebx
movq -0x50(%rbp), %r15
jle 0x1901f
movq %r13, %rdi
leaq 0x2bba5(%rip), %rsi     # 0x44bbb
movl %ebx, %edx
xorl %eax, %eax
callq 0x7945
movq %r13, %rdi
leaq 0x29edc(%rip), %rbx     # 0x42f05
movq %rbx, %rsi
callq 0x73db
testq %r15, %r15
movq -0x48(%rbp), %r12
je 0x1912c
movq %r14, -0x38(%rbp)
leaq -0xa0(%rbp), %r14
movq %r14, %rdi
callq 0x7354
leaq -0x1a0(%rbp), %r13
movq %r13, %rdi
callq 0x19b1c
leaq -0x120(%rbp), %rdi
callq 0x19b1c
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x19b96
movq 0x68(%r15), %r8
movq %r12, %rdi
leaq -0x120(%rbp), %r15
movq %r15, %rsi
movq -0x30(%rbp), %rdx
movq %r13, %rcx
callq 0x19bc3
movq %r14, %rdi
leaq -0x110(%rbp), %rsi
callq 0x769e
movq %r14, %rdi
movq %r15, %rsi
callq 0x769e
movq -0xf8(%rbp), %rdx
testq %rdx, %rdx
leaq 0x29e41(%rip), %rax     # 0x42f06
cmoveq %rax, %rdx
movq -0x78(%rbp), %rcx
testq %rcx, %rcx
cmoveq %rax, %rcx
movq %r14, %rdi
leaq 0x2baf8(%rip), %rsi     # 0x44bd6
xorl %eax, %eax
callq 0x7945
movq %r14, %rdi
leaq -0x90(%rbp), %rsi
callq 0x769e
movq %r14, %rdi
movq %rbx, %rsi
callq 0x73db
movq 0x20(%r12), %rdi
movq %r14, %rsi
callq 0x769e
movq %r14, %rdi
movq -0x38(%rbp), %r14
callq 0x7354
movq %r13, %rdi
leaq -0x68(%rbp), %r13
callq 0x1a3fd
movq %r15, %rdi
callq 0x1a3fd
movq 0x30(%r12), %rdi
testq %rdi, %rdi
je 0x1945d
movq -0x60(%rbp), %rsi
testq %rsi, %rsi
leaq 0x29dbe(%rip), %rbx     # 0x42f06
cmoveq %rbx, %rsi
callq 0x73db
movq -0x58(%rbp), %rax
movq 0x60(%rax), %r15
movb $0x1, 0x48(%r15)
addq $0x50, %r15
movq %r15, %rdi
callq 0x7354
movq %r15, %rdi
movl $0x25, %esi
callq 0x74da
movq %r15, %rdi
leaq -0x90(%rbp), %r15
movq %r15, %rsi
callq 0x769e
leaq -0x80(%rbp), %rdi
callq 0x7852
movq %r13, %rdi
callq 0x7852
movq %r15, %rdi
callq 0x7852
jmp 0x19359
movq %r14, -0x38(%rbp)
leaq -0x1a0(%rbp), %r13
movq %r13, %rdi
callq 0x7354
movq %r13, %rdi
leaq 0x2b9d8(%rip), %rsi     # 0x44b9c
callq 0x73db
movq %r12, -0x30(%rbp)
movq 0x10(%r12), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
movq %r13, %rdi
callq 0x7577
movq -0x50(%rbp), %r12
testq %r12, %r12
leaq 0x2b9c1(%rip), %rsi     # 0x44bb0
leaq 0x2b9a8(%rip), %rax     # 0x44b9e
cmoveq %rax, %rsi
movq %r13, %rdi
callq 0x73db
movq %rbx, %r14
leaq -0x120(%rbp), %rbx
movq %rbx, %rdi
callq 0x7354
movq %rbx, %rdi
movq %r15, %rsi
callq 0x19540
movq -0x118(%rbp), %rsi
testq %rsi, %rsi
cmoveq %r14, %rsi
movq %r13, %rdi
callq 0x73db
movq %rbx, %rdi
callq 0x7852
movq %r13, %rdi
leaq 0x27107(%rip), %rsi     # 0x4034e
callq 0x73db
testq %r12, %r12
je 0x19286
cmpb $0x0, 0x70(%r12)
je 0x19486
movq -0x48(%rbp), %rdi
xorl %esi, %esi
leaq -0x1a0(%rbp), %rdx
movq %r12, %rcx
movq %rdx, %r15
callq 0x1a477
leaq 0x29c8a(%rip), %rbx     # 0x42f06
movq -0x38(%rbp), %r14
movq -0x30(%rbp), %r12
jmp 0x192d0
testq %r15, %r15
je 0x194a5
movl (%r15), %eax
cmpl $0x408, %eax            # imm = 0x408
leaq -0x1a0(%rbp), %r15
leaq 0x29c61(%rip), %rbx     # 0x42f06
movq -0x30(%rbp), %r12
jg 0x1936f
addl $0xfffffc00, %eax       # imm = 0xFFFFFC00
leaq 0x260a5(%rip), %rsi     # 0x3f360
cmpl $0x9, %eax
movq -0x38(%rbp), %r14
jae 0x19521
movq %r15, %rdi
callq 0x73db
movl -0x3c(%rbp), %edx
testl %edx, %edx
jle 0x192e8
movq %r15, %rdi
leaq 0x2b8da(%rip), %rsi     # 0x44bbb
xorl %eax, %eax
callq 0x7945
movq -0x48(%rbp), %r13
movq 0x20(%r13), %rdi
movq -0x198(%rbp), %rsi
testq %rsi, %rsi
cmoveq %rbx, %rsi
callq 0x73db
movq %r15, %rdi
callq 0x7852
movq -0x58(%rbp), %rax
movq 0x60(%rax), %r15
movb $0x1, 0x48(%r15)
addq $0x50, %r15
movq %r15, %rdi
callq 0x7354
movq %r15, %rdi
movl $0x40, %esi
callq 0x74da
movq 0x10(%r12), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
movq %r15, %rdi
callq 0x7577
movq 0x20(%r13), %rdi
leaq 0x29bb5(%rip), %rsi     # 0x42f05
callq 0x73db
leaq -0x68(%rbp), %r13
incq %r14
movq -0x70(%rbp), %rsi
movslq 0x44(%rsi), %rax
cmpq %rax, %r14
jl 0x18eec
jmp 0x193c6
cmpl $0x41f, %eax            # imm = 0x41F
movq -0x38(%rbp), %r14
jg 0x19391
leal -0x409(%rax), %ecx
cmpl $0x2, %ecx
jae 0x193af
leaq 0x2b9a2(%rip), %rsi     # 0x44d2e
jmp 0x192c8
cmpl $0x420, %eax            # imm = 0x420
je 0x193ba
cmpl $0x440, %eax            # imm = 0x440
jne 0x19521
leaq 0x2b998(%rip), %rsi     # 0x44d42
jmp 0x192c8
cmpl $0x410, %eax            # imm = 0x410
jne 0x19521
leaq 0x2b971(%rip), %rsi     # 0x44d32
jmp 0x192c8
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x25f81(%rip), %rdi     # 0x3f360
leaq 0x2b55e(%rip), %rsi     # 0x44944
leaq 0x2b775(%rip), %rcx     # 0x44b62
cmpl $0x3, %eax
jne 0x1947c
movl $0x49b, %edx            # imm = 0x49B
callq 0x30a0
leaq 0x281f3(%rip), %rdi     # 0x415fa
leaq 0x2b536(%rip), %rsi     # 0x44944
leaq 0x2b74d(%rip), %rcx     # 0x44b62
movl $0x405, %edx            # imm = 0x405
callq 0x30a0
leaq 0x28199(%rip), %rdi     # 0x415bf
leaq 0x2b517(%rip), %rsi     # 0x44944
leaq 0x2b72e(%rip), %rcx     # 0x44b62
movl $0x402, %edx            # imm = 0x402
callq 0x30a0
leaq 0x28152(%rip), %rdi     # 0x41597
leaq 0x2b4f8(%rip), %rsi     # 0x44944
leaq 0x2b70f(%rip), %rcx     # 0x44b62
movl $0x401, %edx            # imm = 0x401
callq 0x30a0
leaq 0x2b783(%rip), %rdi     # 0x44be7
leaq 0x2b4d9(%rip), %rsi     # 0x44944
leaq 0x2b6f0(%rip), %rcx     # 0x44b62
movl $0x488, %edx            # imm = 0x488
callq 0x30a0
movl $0x49f, %edx            # imm = 0x49F
callq 0x30a0
leaq 0x285dc(%rip), %rdi     # 0x41a69
leaq 0x2b4b0(%rip), %rsi     # 0x44944
leaq 0x2b8ac(%rip), %rcx     # 0x44d47
movl $0x3ef, %edx            # imm = 0x3EF
callq 0x30a0
leaq 0x28257(%rip), %rdi     # 0x41703
leaq 0x2b491(%rip), %rsi     # 0x44944
leaq 0x2b81a(%rip), %rcx     # 0x44cd4
movl $0x2fc, %edx            # imm = 0x2FC
callq 0x30a0
leaq 0x29917(%rip), %rdi     # 0x42de2
leaq 0x2b472(%rip), %rsi     # 0x44944
leaq 0x2b689(%rip), %rcx     # 0x44b62
movl $0x3f9, %edx            # imm = 0x3F9
callq 0x30a0
leaq 0x3196d(%rip), %rdi     # 0x4ae57
leaq 0x2b453(%rip), %rsi     # 0x44944
leaq 0x2b66a(%rip), %rcx     # 0x44b62
movl $0x3fa, %edx            # imm = 0x3FA
callq 0x30a0
leaq 0x2807c(%rip), %rdi     # 0x41585
leaq 0x2b434(%rip), %rsi     # 0x44944
leaq 0x2b64b(%rip), %rcx     # 0x44b62
movl $0x3fb, %edx            # imm = 0x3FB
callq 0x30a0
leaq 0x25e38(%rip), %rdi     # 0x3f360
leaq 0x2b415(%rip), %rsi     # 0x44944
leaq 0x2b79e(%rip), %rcx     # 0x44cd4
movl $0x31a, %edx            # imm = 0x31A
callq 0x30a0