// File: /workspace/github_c_makefile_star2_O1/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 0xcb22
cmpl $0xc00, (%rsi)          # imm = 0xC00
jne 0xcb41
cmpl $0x0, 0x44(%rsi)
jle 0xcb60
cmpl $0x0, 0x44(%rsi)
jle 0xca56
movl 0x70(%rsi), %eax
movl %eax, -0x34(%rbp)
leaq -0x118(%rbp), %r13
xorl %r14d, %r14d
movq %rsi, -0x68(%rbp)
movq %rdi, -0x30(%rbp)
movq 0x48(%rsi), %rax
movq (%rax,%r14,8), %r12
cmpl $0xc01, (%r12)          # imm = 0xC01
jne 0xcaa6
movl 0x44(%r12), %ecx
movl %ecx, %eax
andl $-0x2, %eax
cmpl $0x2, %eax
jne 0xca87
movq 0x48(%r12), %rax
movq (%rax), %rbx
cmpl $0x200, (%rbx)          # imm = 0x200
jne 0xca68
cmpl $0x3, %ecx
jne 0xc57e
movq 0x10(%rax), %rcx
jmp 0xc580
xorl %ecx, %ecx
movq 0x8(%rax), %r15
movzbl 0x60(%rsi), %eax
cmpl $0x3, %eax
je 0xc959
movq %r14, -0x60(%rbp)
cmpl $0x2, %eax
movq %rcx, -0x50(%rbp)
movq %r15, -0x58(%rbp)
je 0xc716
leaq 0x36038(%rip), %r15     # 0x425e5
cmpl $0x1, %eax
jne 0xcac5
movq %r13, %r14
leaq -0xa8(%rbp), %r13
movq %r13, %rdi
callq 0x7548
movq %r13, %rdi
movl $0x5f, %esi
callq 0x76cb
movq 0x10(%rbx), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
movq %r13, %rdi
callq 0x7768
movq %r14, %r13
movq %r14, %rdi
callq 0x7548
movl -0x34(%rbp), %edx
testl %edx, %edx
jle 0xc60b
movq %r13, %rdi
leaq 0x35fd6(%rip), %rsi     # 0x425da
xorl %eax, %eax
callq 0x7b31
movq %r13, %rdi
movq %r15, %rsi
callq 0x75cf
movq -0xa0(%rbp), %rsi
testq %rsi, %rsi
leaq 0x37893(%rip), %r14     # 0x43eba
cmoveq %r14, %rsi
movq %r13, %rdi
callq 0x75cf
movq %r13, %rdi
movl $0x3a, %esi
callq 0x76cb
leaq -0x48(%rbp), %rbx
movq %rbx, %rdi
callq 0x7548
movq %rbx, %rdi
movq -0x58(%rbp), %rsi
callq 0xcbf5
movq -0x40(%rbp), %rsi
testq %rsi, %rsi
cmoveq %r14, %rsi
movq %r13, %rdi
callq 0x75cf
movq %rbx, %rdi
callq 0x7a38
cmpq $0x0, -0x50(%rbp)
movq -0x30(%rbp), %rbx
je 0xc6a3
movq %r13, %rdi
leaq 0x39bbf(%rip), %rsi     # 0x46247
callq 0x75cf
movq -0x50(%rbp), %rsi
cmpb $0x0, 0x70(%rsi)
je 0xcb03
movq %r13, %rdi
callq 0xdaad
movq %r13, %rdi
leaq 0x37f74(%rip), %rsi     # 0x44621
callq 0x75cf
movq 0x20(%rbx), %rdi
movq -0x110(%rbp), %rsi
testq %rsi, %rsi
cmoveq %r14, %rsi
callq 0x75cf
movq %r13, %rdi
callq 0x7a38
movq 0x60(%r12), %r15
movb $0x1, 0x48(%r15)
addq $0x50, %r15
movq %r15, %rdi
callq 0x7548
movq -0xa0(%rbp), %rsi
testq %rsi, %rsi
leaq 0x377c2(%rip), %rax     # 0x43eba
cmoveq %rax, %rsi
movl -0xa4(%rbp), %edx
movq %r15, %rdi
callq 0x7768
leaq -0xa8(%rbp), %rdi
jmp 0xc94b
leaq -0x48(%rbp), %r14
movq %r14, %rdi
callq 0x7548
movq %r14, %rdi
movl $0x5f, %esi
callq 0x76cb
movq 0x10(%rbx), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
movq %r14, %rdi
callq 0x7768
movq -0x30(%rbp), %rax
movq 0x40(%rax), %rax
movl 0x54(%rax), %edx
leal 0x1(%rdx), %ecx
movl %ecx, 0x54(%rax)
movq %r14, %rdi
leaq 0x3667d(%rip), %rsi     # 0x42dda
xorl %eax, %eax
callq 0x7b31
leaq -0xd8(%rbp), %r14
movq %r14, %rdi
callq 0x7548
movl -0x34(%rbp), %edx
testl %edx, %edx
jle 0xc78b
movq %r14, %rdi
leaq 0x35e56(%rip), %rsi     # 0x425da
xorl %eax, %eax
callq 0x7b31
movq %r14, %rdi
leaq 0x35e50(%rip), %rsi     # 0x425e5
callq 0x75cf
movq -0x40(%rbp), %rsi
testq %rsi, %rsi
leaq 0x37712(%rip), %rbx     # 0x43eba
cmoveq %rbx, %rsi
movq %r14, %rdi
callq 0x75cf
movq %r14, %rdi
movl $0x3a, %esi
callq 0x76cb
leaq -0xc8(%rbp), %r15
movq %r15, %rdi
callq 0x7548
movq %r15, %rdi
movq -0x58(%rbp), %rsi
callq 0xcbf5
movq -0xc0(%rbp), %rsi
testq %rsi, %rsi
cmoveq %rbx, %rsi
movq %r14, %rdi
callq 0x75cf
movq %r15, %rdi
callq 0x7a38
movq %r14, %rdi
leaq 0x37e1d(%rip), %rsi     # 0x44621
callq 0x75cf
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0xd0(%rbp), %rsi
testq %rsi, %rsi
cmoveq %rbx, %rsi
callq 0x75cf
movq %r14, %rdi
callq 0x7a38
movq -0x50(%rbp), %r14
testq %r14, %r14
je 0xc91b
leaq -0xb8(%rbp), %rbx
movq %rbx, %rdi
callq 0x7548
leaq -0xa8(%rbp), %rdi
callq 0xd03d
movq %r13, %rdi
callq 0xd03d
movq -0x30(%rbp), %r15
movq %r15, %rdi
movq %rbx, %rsi
leaq -0xa8(%rbp), %rdx
movq %r14, %rcx
callq 0xddbd
movq %rbx, %rdi
leaq 0x3763b(%rip), %rbx     # 0x43eba
leaq -0xa8(%rbp), %rsi
callq 0x788c
movq 0x68(%r14), %r8
movq %r15, %rdi
movq %r15, %r14
movq %r13, %rsi
movq -0x58(%rbp), %rdx
leaq -0xa8(%rbp), %rcx
callq 0xd093
leaq -0xb8(%rbp), %r15
movq %r15, %rdi
leaq -0x108(%rbp), %rsi
callq 0x788c
movq %r15, %rdi
movq %r13, %rsi
callq 0x788c
movq -0x40(%rbp), %rdx
testq %rdx, %rdx
cmoveq %rbx, %rdx
movq -0xf0(%rbp), %rcx
testq %rcx, %rcx
cmoveq %rbx, %rcx
movq %r15, %rdi
leaq 0x35cfe(%rip), %rsi     # 0x425ea
xorl %eax, %eax
callq 0x7b31
movq 0x20(%r14), %rdi
movq %r15, %rsi
callq 0x788c
movq %r15, %rdi
callq 0x7548
leaq -0xa8(%rbp), %rdi
callq 0xd787
movq %r13, %rdi
callq 0xd787
movq 0x60(%r12), %r15
movb $0x1, 0x48(%r15)
addq $0x50, %r15
movq %r15, %rdi
callq 0x7548
movq -0x40(%rbp), %rsi
testq %rsi, %rsi
cmoveq %rbx, %rsi
movl -0x44(%rbp), %edx
movq %r15, %rdi
callq 0x7768
leaq -0x48(%rbp), %rdi
callq 0x7a38
movq -0x60(%rbp), %r14
jmp 0xca42
testq %rcx, %rcx
jne 0xcae4
leaq -0xa8(%rbp), %r12
movq %r12, %rdi
callq 0x7548
movq %r12, %rdi
movl $0x5f, %esi
callq 0x76cb
movq 0x10(%rbx), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
movq %r12, %rdi
callq 0x7768
movq %r13, %rdi
callq 0x7548
movq %r13, %rdi
leaq 0x35c42(%rip), %rsi     # 0x425e5
callq 0x75cf
movq -0xa0(%rbp), %rsi
testq %rsi, %rsi
leaq 0x37501(%rip), %r12     # 0x43eba
cmoveq %r12, %rsi
movq %r13, %rdi
callq 0x75cf
movq %r13, %rdi
movl $0x3a, %esi
callq 0x76cb
leaq -0x48(%rbp), %rbx
movq %rbx, %rdi
callq 0x7548
movq %rbx, %rdi
movq %r15, %rsi
callq 0xcbf5
movq -0x40(%rbp), %rsi
testq %rsi, %rsi
cmoveq %r12, %rsi
movq %r13, %rdi
callq 0x75cf
movq %rbx, %rdi
callq 0x7a38
movq %r13, %rdi
leaq 0x37c13(%rip), %rsi     # 0x44621
callq 0x75cf
movq -0x30(%rbp), %rbx
movq 0x20(%rbx), %rdi
movq -0x110(%rbp), %rsi
testq %rsi, %rsi
cmoveq %r12, %rsi
callq 0x75cf
movq %r13, %rdi
callq 0x7a38
leaq -0xa8(%rbp), %rdi
callq 0x7a38
incq %r14
movq -0x68(%rbp), %rsi
movslq 0x44(%rsi), %rax
cmpq %rax, %r14
jl 0xc536
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x35b3f(%rip), %rdi     # 0x425ae
leaq 0x358a2(%rip), %rsi     # 0x42318
leaq 0x35a88(%rip), %rcx     # 0x42505
movl $0xe93, %edx            # imm = 0xE93
callq 0x30a0
leaq 0x35ae5(%rip), %rdi     # 0x42573
leaq 0x35883(%rip), %rsi     # 0x42318
leaq 0x35a69(%rip), %rcx     # 0x42505
movl $0xe90, %edx            # imm = 0xE90
callq 0x30a0
leaq 0x35a9e(%rip), %rdi     # 0x4254b
leaq 0x35864(%rip), %rsi     # 0x42318
leaq 0x35a4a(%rip), %rcx     # 0x42505
movl $0xe8f, %edx            # imm = 0xE8F
callq 0x30a0
leaq 0x338b8(%rip), %rdi     # 0x40384
leaq 0x35845(%rip), %rsi     # 0x42318
leaq 0x35a2b(%rip), %rcx     # 0x42505
movl $0xf35, %edx            # imm = 0xF35
callq 0x30a0
leaq 0x35b0f(%rip), %rdi     # 0x425fa
leaq 0x35826(%rip), %rsi     # 0x42318
leaq 0x35a0c(%rip), %rcx     # 0x42505
movl $0xf19, %edx            # imm = 0xF19
callq 0x30a0
leaq 0x35f13(%rip), %rdi     # 0x42a1d
leaq 0x35807(%rip), %rsi     # 0x42318
leaq 0x35eac(%rip), %rcx     # 0x429c4
movl $0xe7d, %edx            # imm = 0xE7D
callq 0x30a0
leaq 0x3726d(%rip), %rdi     # 0x43d96
leaq 0x357e8(%rip), %rsi     # 0x42318
leaq 0x359ce(%rip), %rcx     # 0x42505
movl $0xe87, %edx            # imm = 0xE87
callq 0x30a0
leaq 0x3f26f(%rip), %rdi     # 0x4bdb7
leaq 0x357c9(%rip), %rsi     # 0x42318
leaq 0x359af(%rip), %rcx     # 0x42505
movl $0xe88, %edx            # imm = 0xE88
callq 0x30a0
leaq 0x359d2(%rip), %rdi     # 0x42539
leaq 0x357aa(%rip), %rsi     # 0x42318
leaq 0x35990(%rip), %rcx     # 0x42505
movl $0xe89, %edx            # imm = 0xE89
callq 0x30a0