// File: /workspace/github_c_makefile_star2_O2/Ihsoh[P]ilang/compiler/frontend/irgen.c
// Function: ir_var
static void _ir_var(
	IRGeneratorContext *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: {
				ResizableString rstr_id;
				rstr_init(&rstr_id);
				_ir_identifier_name(ctx, &rstr_id, node_identifier);

				ResizableString rstr;
				rstr_init(&rstr);

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

				rstr_append_with_cstr(&rstr, "var ");
				rstr_append_with_cstr(&rstr, RSTR_CSTR(&rstr_id));
				rstr_append_with_char(&rstr, ':');

				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);

				if (node_expr != NULL) {
					rstr_append_with_cstr(&rstr, " = ");
					_ir_constexpr_initializer(ctx, &rstr, node_expr);
				}

				rstr_append_with_cstr(&rstr, ";\n");

				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_raw(
					FE_VAR_SYMBOL_GET_CODE_GEN_NAME(symbol),
					RSTR_CSTR(&rstr_id),
					RSTR_LEN(&rstr_id)
				);
				
				rstr_free(&rstr_id);
				break;
			}
			case FE_VAR_TYPE_LOCAL: {
				ResizableString rstr_id;
				rstr_init(&rstr_id);
				_ir_identifier_name(ctx, &rstr_id, node_identifier);
				size_t no = FE_FUNC_SYMBOL_NEXT_NO(ctx->func_symbol);
				rstr_appendf(&rstr_id, ".%zu__", no);

				ResizableString rstr;
				rstr_init(&rstr);

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

				rstr_append_with_cstr(&rstr, "var ");
				rstr_append_with_cstr(&rstr, RSTR_CSTR(&rstr_id));
				rstr_append_with_char(&rstr, ':');

				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, ";\n");

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

				rstr_free(&rstr);

				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,
						"assign %s, %s;\n",
						RSTR_CSTR(&rstr_id),
						RSTR_CSTR(&(casted_result.rstr_result))
					);

					rstr_append_with_rstr(ctx->body, &rstr_expr);

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

				// 更新全局变量符号在代码生成时的名字。
				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_raw(
					FE_VAR_SYMBOL_GET_CODE_GEN_NAME(symbol),
					RSTR_CSTR(&rstr_id),
					RSTR_LEN(&rstr_id)
				);
				
				rstr_free(&rstr_id);
				break;
			}
			case FE_VAR_TYPE_STRUCT_MEMBER: {
				assert(node_expr == NULL);
				
				ResizableString rstr_id;
				rstr_init(&rstr_id);
				_ir_identifier_name(ctx, &rstr_id, node_identifier);

				ResizableString rstr;
				rstr_init(&rstr);

				rstr_append_with_cstr(&rstr, "var ");
				rstr_append_with_cstr(&rstr, RSTR_CSTR(&rstr_id));
				rstr_append_with_char(&rstr, ':');

				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, ";\n");

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

				rstr_free(&rstr);
				rstr_free(&rstr_id);
				break;
			}
			default: {
				assert(0);
			}
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
testq %rsi, %rsi
je 0xbcc6
cmpl $0xc00, (%rsi)          # imm = 0xC00
jne 0xbce5
movq %rdi, -0x38(%rbp)
movl 0x44(%rsi), %eax
testl %eax, %eax
jle 0xbd04
movl 0x70(%rsi), %ebx
leaq -0x118(%rbp), %r13
leaq 0x347dc(%rip), %r15     # 0x3ff16
xorl %edx, %edx
movq %rsi, -0x60(%rbp)
movl %ebx, -0x2c(%rbp)
cltq
cmpq %rax, %rdx
jge 0xbbfa
movq 0x48(%rsi), %rax
movq (%rax,%rdx,8), %r12
cmpl $0xc01, (%r12)          # imm = 0xC01
jne 0xbc2b
movl 0x44(%r12), %ecx
movl %ecx, %eax
andl $-0x2, %eax
cmpl $0x2, %eax
jne 0xbc4a
movq 0x48(%r12), %rax
movq (%rax), %r14
cmpl $0x200, (%r14)          # imm = 0x200
jne 0xbc0c
cmpl $0x3, %ecx
jne 0xb797
movq 0x10(%rax), %rcx
jmp 0xb799
xorl %ecx, %ecx
movq 0x8(%rax), %rbx
movzbl 0x60(%rsi), %eax
cmpl $0x3, %eax
movq %rdx, -0x68(%rbp)
je 0xbb13
cmpl $0x2, %eax
movq %rcx, -0x50(%rbp)
je 0xb909
cmpl $0x1, %eax
jne 0xbc69
movq %r13, %r15
leaq -0xb8(%rbp), %r13
movq %r13, %rdi
callq 0x6e1c
movq %r13, %rdi
movq %r14, %rsi
callq 0xbd23
movq %r15, %r13
movq %r15, %rdi
callq 0x6e1c
movl -0x2c(%rbp), %edx
testl %edx, %edx
jle 0xb804
movq %r13, %rdi
leaq 0x32e39(%rip), %rsi     # 0x3e636
xorl %eax, %eax
callq 0x739a
movq %r13, %rdi
leaq 0x32e33(%rip), %rsi     # 0x3e641
callq 0x6e99
movq -0xb0(%rbp), %rsi
testq %rsi, %rsi
leaq 0x346f2(%rip), %r15     # 0x3ff16
cmoveq %r15, %rsi
movq %r13, %rdi
callq 0x6e99
movq %r13, %rdi
pushq $0x3a
popq %rsi
callq 0x6f81
leaq -0x48(%rbp), %r14
movq %r14, %rdi
callq 0x6e1c
movq %r14, %rdi
movq %rbx, %rsi
callq 0xbd8f
movq -0x40(%rbp), %rsi
testq %rsi, %rsi
cmoveq %r15, %rsi
movq %r13, %rdi
callq 0x6e99
movq %r14, %rdi
callq 0x72b9
cmpq $0x0, -0x50(%rbp)
je 0xb899
movq %r13, %rdi
leaq 0x36add(%rip), %rsi     # 0x4235b
callq 0x6e99
movq -0x50(%rbp), %rsi
cmpb $0x0, 0x70(%rsi)
je 0xbca7
movq %r13, %rdi
callq 0xcbf9
movq %r13, %rdi
leaq 0x34dda(%rip), %rsi     # 0x4067d
callq 0x6e99
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x110(%rbp), %rsi
testq %rsi, %rsi
cmoveq %r15, %rsi
callq 0x6e99
movq %r13, %rdi
callq 0x72b9
movq 0x60(%r12), %r14
movb $0x1, 0x48(%r14)
addq $0x50, %r14
movq %r14, %rdi
callq 0x6e1c
movq -0xb0(%rbp), %rsi
testq %rsi, %rsi
cmoveq %r15, %rsi
movl -0xb4(%rbp), %edx
movq %r14, %rdi
callq 0x700f
leaq -0xb8(%rbp), %rdi
jmp 0xbbe2
movq %rbx, -0x58(%rbp)
movq %r15, %rbx
leaq -0x48(%rbp), %r15
movq %r15, %rdi
callq 0x6e1c
movq %r15, %rdi
movq %r14, %rsi
callq 0xbd23
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rax
movl 0x54(%rax), %edx
leal 0x1(%rdx), %ecx
movl %ecx, 0x54(%rax)
movq %r15, %rdi
leaq 0x334f4(%rip), %rsi     # 0x3ee36
xorl %eax, %eax
callq 0x739a
leaq -0xd8(%rbp), %r15
movq %r15, %rdi
callq 0x6e1c
movl -0x2c(%rbp), %edx
testl %edx, %edx
jle 0xb970
movq %r15, %rdi
leaq 0x32ccd(%rip), %rsi     # 0x3e636
xorl %eax, %eax
callq 0x739a
movq %r15, %rdi
leaq 0x32cc7(%rip), %rsi     # 0x3e641
callq 0x6e99
movq -0x40(%rbp), %rsi
testq %rsi, %rsi
cmoveq %rbx, %rsi
movq %r15, %rdi
callq 0x6e99
movq %r15, %rdi
pushq $0x3a
popq %rsi
callq 0x6f81
leaq -0xc8(%rbp), %r14
movq %r14, %rdi
callq 0x6e1c
movq %r14, %rdi
movq -0x58(%rbp), %rsi
callq 0xbd8f
movq -0xc0(%rbp), %rsi
testq %rsi, %rsi
cmoveq %rbx, %rsi
movq %r15, %rdi
callq 0x6e99
movq %r14, %rdi
callq 0x72b9
movq %r15, %rdi
leaq 0x34c9d(%rip), %rsi     # 0x4067d
callq 0x6e99
movq -0x38(%rbp), %r14
movq 0x20(%r14), %rdi
movq -0xd0(%rbp), %rsi
testq %rsi, %rsi
cmoveq %rbx, %rsi
callq 0x6e99
movq %r15, %rdi
callq 0x72b9
movq -0x50(%rbp), %rax
testq %rax, %rax
je 0xbad6
leaq -0x78(%rbp), %rdi
movq %rax, %r15
callq 0x6e1c
leaq -0xb8(%rbp), %rdi
callq 0xc17d
movq %r13, %rdi
callq 0xc17d
movq %r14, %rdi
leaq -0x78(%rbp), %rsi
leaq -0xb8(%rbp), %rdx
movq %r15, %rcx
callq 0xc1d1
movq 0x68(%r15), %r8
movq %r14, %rdi
movq %r13, %rsi
movq -0x58(%rbp), %rdx
leaq -0xb8(%rbp), %rcx
callq 0xc1f2
leaq -0x78(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x712e
leaq -0x78(%rbp), %rdi
movq %rdi, %r15
movq %r13, %rsi
callq 0x712e
movq -0x40(%rbp), %rdx
testq %rdx, %rdx
cmoveq %rbx, %rdx
movq -0xf0(%rbp), %rcx
testq %rcx, %rcx
cmoveq %rbx, %rcx
movq %r15, %rdi
leaq 0x32b9f(%rip), %rsi     # 0x3e646
xorl %eax, %eax
callq 0x739a
movq 0x20(%r14), %rdi
movq %r15, %rsi
callq 0x712e
movq %r15, %rdi
callq 0x6e1c
leaq -0xb8(%rbp), %rdi
callq 0xc8e4
movq %r13, %rdi
callq 0xc8e4
movq 0x60(%r12), %r14
movb $0x1, 0x48(%r14)
addq $0x50, %r14
movq %r14, %rdi
callq 0x6e1c
movq -0x40(%rbp), %rsi
testq %rsi, %rsi
cmoveq %rbx, %rsi
movl -0x44(%rbp), %edx
movq %r14, %rdi
callq 0x700f
leaq -0x48(%rbp), %rdi
callq 0x72b9
movq %rbx, %r15
jmp 0xbbe7
testq %rcx, %rcx
jne 0xbc88
movq %rbx, %r12
leaq -0xb8(%rbp), %r15
movq %r15, %rdi
callq 0x6e1c
movq %r15, %rdi
movq %r14, %rsi
callq 0xbd23
movq %r13, %rdi
callq 0x6e1c
movq %r13, %rdi
leaq 0x32af6(%rip), %rsi     # 0x3e641
callq 0x6e99
movq -0xb0(%rbp), %rsi
testq %rsi, %rsi
leaq 0x343b5(%rip), %rbx     # 0x3ff16
cmoveq %rbx, %rsi
movq %r13, %rdi
callq 0x6e99
movq %r13, %rdi
pushq $0x3a
popq %rsi
callq 0x6f81
leaq -0x48(%rbp), %r14
movq %r14, %rdi
callq 0x6e1c
movq %r14, %rdi
movq %r12, %rsi
callq 0xbd8f
movq -0x40(%rbp), %rsi
testq %rsi, %rsi
cmoveq %rbx, %rsi
movq %r13, %rdi
callq 0x6e99
movq %r14, %rdi
callq 0x72b9
movq %r13, %rdi
leaq 0x34ac9(%rip), %rsi     # 0x4067d
callq 0x6e99
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x110(%rbp), %rsi
testq %rsi, %rsi
cmoveq %rbx, %rsi
callq 0x6e99
movq %r13, %rdi
callq 0x72b9
movq %r15, %rdi
movq %rbx, %r15
callq 0x72b9
movq -0x68(%rbp), %rdx
incq %rdx
movq -0x60(%rbp), %rsi
movl 0x44(%rsi), %eax
jmp 0xb743
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x329f7(%rip), %rdi     # 0x3e60a
leaq 0x3275a(%rip), %rsi     # 0x3e374
leaq 0x32940(%rip), %rcx     # 0x3e561
movl $0xe93, %edx            # imm = 0xE93
callq 0x30a0
leaq 0x32975(%rip), %rdi     # 0x3e5a7
leaq 0x3273b(%rip), %rsi     # 0x3e374
leaq 0x32921(%rip), %rcx     # 0x3e561
movl $0xe8f, %edx            # imm = 0xE8F
callq 0x30a0
leaq 0x3297e(%rip), %rdi     # 0x3e5cf
leaq 0x3271c(%rip), %rsi     # 0x3e374
leaq 0x32902(%rip), %rcx     # 0x3e561
movl $0xe90, %edx            # imm = 0xE90
callq 0x30a0
leaq 0x306f0(%rip), %rdi     # 0x3c360
leaq 0x326fd(%rip), %rsi     # 0x3e374
leaq 0x328e3(%rip), %rcx     # 0x3e561
movl $0xf35, %edx            # imm = 0xF35
callq 0x30a0
leaq 0x329c7(%rip), %rdi     # 0x3e656
leaq 0x326de(%rip), %rsi     # 0x3e374
leaq 0x328c4(%rip), %rcx     # 0x3e561
movl $0xf19, %edx            # imm = 0xF19
callq 0x30a0
leaq 0x32dcb(%rip), %rdi     # 0x3ea79
leaq 0x326bf(%rip), %rsi     # 0x3e374
leaq 0x32d64(%rip), %rcx     # 0x3ea20
movl $0xe7d, %edx            # imm = 0xE7D
callq 0x30a0
leaq 0x34125(%rip), %rdi     # 0x3fdf2
leaq 0x326a0(%rip), %rsi     # 0x3e374
leaq 0x32886(%rip), %rcx     # 0x3e561
movl $0xe87, %edx            # imm = 0xE87
callq 0x30a0
leaq 0x3c1ab(%rip), %rdi     # 0x47e97
leaq 0x32681(%rip), %rsi     # 0x3e374
leaq 0x32867(%rip), %rcx     # 0x3e561
movl $0xe88, %edx            # imm = 0xE88
callq 0x30a0
leaq 0x3288a(%rip), %rdi     # 0x3e595
leaq 0x32662(%rip), %rsi     # 0x3e374
leaq 0x32848(%rip), %rcx     # 0x3e561
movl $0xe89, %edx            # imm = 0xE89
callq 0x30a0