// File: /workspace/github_c_makefile_star2_O3/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 0xc8be
cmpl $0xc00, (%rsi)          # imm = 0xC00
jne 0xc8dd
cmpl $0x0, 0x44(%rsi)
jle 0xc8fc
movl 0x70(%rsi), %eax
movl %eax, -0x2c(%rbp)
leaq 0x36c6d(%rip), %r14     # 0x42f06
xorl %r15d, %r15d
movq %rsi, -0x68(%rbp)
movq %rdi, -0x38(%rbp)
movq 0x48(%rsi), %rax
movq (%rax,%r15,8), %rbx
cmpl $0xc01, (%rbx)          # imm = 0xC01
jne 0xc842
movl 0x44(%rbx), %ecx
movl %ecx, %eax
andl $-0x2, %eax
cmpl $0x2, %eax
jne 0xc823
movq 0x48(%rbx), %rax
movq (%rax), %r12
cmpl $0x200, (%r12)          # imm = 0x200
jne 0xc804
cmpl $0x3, %ecx
jne 0xc2e9
movq 0x10(%rax), %rcx
jmp 0xc2eb
xorl %ecx, %ecx
movq 0x8(%rax), %r13
movzbl 0x60(%rsi), %eax
cmpl $0x3, %eax
je 0xc6ed
cmpl $0x2, %eax
movq %rcx, -0x58(%rbp)
movq %r15, -0x60(%rbp)
je 0xc4a4
movq %r13, -0x50(%rbp)
leaq 0x35319(%rip), %r15     # 0x41631
leaq -0xa8(%rbp), %r14
cmpl $0x1, %eax
jne 0xc861
leaq -0xf8(%rbp), %r13
movq %r13, %rdi
callq 0x7354
movq %r13, %rdi
movl $0x5f, %esi
callq 0x74da
movq 0x10(%r12), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
movq %r13, %rdi
callq 0x7577
movq %r14, %rdi
callq 0x7354
movl -0x2c(%rbp), %edx
testl %edx, %edx
jle 0xc37c
leaq -0xa8(%rbp), %rdi
leaq 0x352b1(%rip), %rsi     # 0x41626
xorl %eax, %eax
callq 0x7945
leaq -0xa8(%rbp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x73db
movq -0xf0(%rbp), %rsi
testq %rsi, %rsi
leaq 0x36b67(%rip), %r14     # 0x42f06
cmoveq %r14, %rsi
movq %r12, %rdi
callq 0x73db
movq %r12, %rdi
movl $0x3a, %esi
callq 0x74da
leaq -0x48(%rbp), %r15
movq %r15, %rdi
callq 0x7354
movq %r15, %rdi
movq -0x50(%rbp), %rsi
callq 0xc991
movq -0x40(%rbp), %rsi
testq %rsi, %rsi
cmoveq %r14, %rsi
movq %r12, %rdi
callq 0x73db
movq %r15, %rdi
callq 0x7852
cmpq $0x0, -0x58(%rbp)
movq -0x38(%rbp), %r12
je 0xc423
leaq -0xa8(%rbp), %rdi
leaq 0x38f13(%rip), %rsi     # 0x45317
callq 0x73db
movq -0x58(%rbp), %rsi
cmpb $0x0, 0x70(%rsi)
je 0xc89f
leaq -0xa8(%rbp), %rdi
callq 0xd82e
leaq -0xa8(%rbp), %r15
movq %r15, %rdi
leaq 0x37239(%rip), %rsi     # 0x4366d
callq 0x73db
movq 0x20(%r12), %rdi
movq -0xa0(%rbp), %rsi
testq %rsi, %rsi
cmoveq %r14, %rsi
callq 0x73db
movq %r15, %rdi
callq 0x7852
movq 0x60(%rbx), %r15
movb $0x1, 0x48(%r15)
addq $0x50, %r15
movq %r15, %rdi
callq 0x7354
movq -0xf0(%rbp), %rsi
testq %rsi, %rsi
leaq 0x36a87(%rip), %rax     # 0x42f06
cmoveq %rax, %rsi
movl -0xf4(%rbp), %edx
movq %r15, %rdi
leaq 0x36a73(%rip), %r14     # 0x42f06
callq 0x7577
leaq -0xf8(%rbp), %rdi
jmp 0xc6df
leaq -0x48(%rbp), %r14
movq %r14, %rdi
callq 0x7354
movq %r14, %rdi
movl $0x5f, %esi
callq 0x74da
movq 0x10(%r12), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
movq %r14, %rdi
callq 0x7577
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rax
movl 0x54(%rax), %edx
leal 0x1(%rdx), %ecx
movl %ecx, 0x54(%rax)
movq %r14, %rdi
leaq 0x3593a(%rip), %rsi     # 0x41e26
xorl %eax, %eax
callq 0x7945
leaq -0x118(%rbp), %r12
movq %r12, %rdi
callq 0x7354
movl -0x2c(%rbp), %edx
testl %edx, %edx
jle 0xc51a
movq %r12, %rdi
leaq 0x35113(%rip), %rsi     # 0x41626
xorl %eax, %eax
callq 0x7945
movq %r12, %rdi
leaq 0x3510d(%rip), %rsi     # 0x41631
callq 0x73db
movq -0x40(%rbp), %rsi
testq %rsi, %rsi
leaq 0x369cf(%rip), %r14     # 0x42f06
cmoveq %r14, %rsi
movq %r12, %rdi
callq 0x73db
movq %r12, %rdi
movl $0x3a, %esi
callq 0x74da
leaq -0x108(%rbp), %r15
movq %r15, %rdi
callq 0x7354
movq %r15, %rdi
movq %r13, %rsi
callq 0xc991
movq -0x100(%rbp), %rsi
testq %rsi, %rsi
cmoveq %r14, %rsi
movq %r12, %rdi
callq 0x73db
movq %r15, %rdi
callq 0x7852
movq %r12, %rdi
leaq 0x370db(%rip), %rsi     # 0x4366d
callq 0x73db
movq -0x38(%rbp), %r15
movq 0x20(%r15), %rdi
movq -0x110(%rbp), %rsi
testq %rsi, %rsi
cmoveq %r14, %rsi
callq 0x73db
movq %r12, %rdi
movq %r15, %r12
callq 0x7852
movq -0x58(%rbp), %rcx
testq %rcx, %rcx
je 0xc6b0
leaq -0xb8(%rbp), %rdi
movq %rcx, %r14
callq 0x7354
leaq -0xf8(%rbp), %r15
movq %r15, %rdi
callq 0xcdbe
movq %r13, -0x50(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0xcdbe
movq %r12, %rdi
leaq -0xb8(%rbp), %r13
movq %r13, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0xdb3e
movq %r13, %rdi
movq %r15, %rsi
callq 0x769e
movq 0x68(%r14), %r8
leaq 0x368e0(%rip), %r14     # 0x42f06
movq %r12, %rdi
leaq -0xa8(%rbp), %r13
movq %r13, %rsi
movq -0x50(%rbp), %rdx
movq %r15, %rcx
callq 0xce14
leaq -0xb8(%rbp), %r15
movq %r15, %rdi
leaq -0x98(%rbp), %rsi
callq 0x769e
movq %r15, %rdi
movq %r13, %rsi
callq 0x769e
movq -0x40(%rbp), %rdx
testq %rdx, %rdx
cmoveq %r14, %rdx
movq -0x80(%rbp), %rcx
testq %rcx, %rcx
cmoveq %r14, %rcx
movq %r15, %rdi
leaq 0x34fb6(%rip), %rsi     # 0x41636
xorl %eax, %eax
callq 0x7945
movq 0x20(%r12), %rdi
movq %r15, %rsi
callq 0x769e
movq %r15, %rdi
callq 0x7354
leaq -0xf8(%rbp), %rdi
callq 0xd508
movq %r13, %rdi
callq 0xd508
movq 0x60(%rbx), %r15
movb $0x1, 0x48(%r15)
addq $0x50, %r15
movq %r15, %rdi
callq 0x7354
movq -0x40(%rbp), %rsi
testq %rsi, %rsi
cmoveq %r14, %rsi
movl -0x44(%rbp), %edx
movq %r15, %rdi
callq 0x7577
leaq -0x48(%rbp), %rdi
callq 0x7852
movq -0x60(%rbp), %r15
jmp 0xc7de
testq %rcx, %rcx
jne 0xc880
leaq -0xf8(%rbp), %rbx
movq %rbx, %rdi
callq 0x7354
movq %rbx, %rdi
movl $0x5f, %esi
callq 0x74da
movq 0x10(%r12), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
movq %rbx, %rdi
callq 0x7577
leaq -0xa8(%rbp), %rbx
movq %rbx, %rdi
callq 0x7354
movq %rbx, %rdi
leaq 0x34ef2(%rip), %rsi     # 0x41631
callq 0x73db
movq -0xf0(%rbp), %rsi
testq %rsi, %rsi
cmoveq %r14, %rsi
movq %rbx, %rdi
callq 0x73db
movq %rbx, %rdi
movl $0x3a, %esi
callq 0x74da
movq %r14, %r12
leaq -0x48(%rbp), %r14
movq %r14, %rdi
callq 0x7354
movq %r14, %rdi
movq %r13, %rsi
callq 0xc991
movq -0x40(%rbp), %rsi
testq %rsi, %rsi
cmoveq %r12, %rsi
movq %rbx, %rdi
callq 0x73db
movq %r14, %rdi
movq %r12, %r14
callq 0x7852
movq %rbx, %rdi
leaq 0x36ec4(%rip), %rsi     # 0x4366d
callq 0x73db
movq -0x38(%rbp), %r12
movq 0x20(%r12), %rdi
movq -0xa0(%rbp), %rsi
testq %rsi, %rsi
cmoveq %r14, %rsi
callq 0x73db
movq %rbx, %rdi
callq 0x7852
leaq -0xf8(%rbp), %rdi
callq 0x7852
incq %r15
movq -0x68(%rbp), %rsi
movslq 0x44(%rsi), %rax
cmpq %rax, %r15
jl 0xc2a4
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x34def(%rip), %rdi     # 0x415fa
leaq 0x34b52(%rip), %rsi     # 0x41364
leaq 0x34d38(%rip), %rcx     # 0x41551
movl $0xe93, %edx            # imm = 0xE93
callq 0x30a0
leaq 0x34d95(%rip), %rdi     # 0x415bf
leaq 0x34b33(%rip), %rsi     # 0x41364
leaq 0x34d19(%rip), %rcx     # 0x41551
movl $0xe90, %edx            # imm = 0xE90
callq 0x30a0
leaq 0x34d4e(%rip), %rdi     # 0x41597
leaq 0x34b14(%rip), %rsi     # 0x41364
leaq 0x34cfa(%rip), %rcx     # 0x41551
movl $0xe8f, %edx            # imm = 0xE8F
callq 0x30a0
leaq 0x32af8(%rip), %rdi     # 0x3f360
leaq 0x34af5(%rip), %rsi     # 0x41364
leaq 0x34cdb(%rip), %rcx     # 0x41551
movl $0xf35, %edx            # imm = 0xF35
callq 0x30a0
leaq 0x34dbf(%rip), %rdi     # 0x41646
leaq 0x34ad6(%rip), %rsi     # 0x41364
leaq 0x34cbc(%rip), %rcx     # 0x41551
movl $0xf19, %edx            # imm = 0xF19
callq 0x30a0
leaq 0x351c3(%rip), %rdi     # 0x41a69
leaq 0x34ab7(%rip), %rsi     # 0x41364
leaq 0x3515c(%rip), %rcx     # 0x41a10
movl $0xe7d, %edx            # imm = 0xE7D
callq 0x30a0
leaq 0x3651d(%rip), %rdi     # 0x42de2
leaq 0x34a98(%rip), %rsi     # 0x41364
leaq 0x34c7e(%rip), %rcx     # 0x41551
movl $0xe87, %edx            # imm = 0xE87
callq 0x30a0
leaq 0x3e573(%rip), %rdi     # 0x4ae57
leaq 0x34a79(%rip), %rsi     # 0x41364
leaq 0x34c5f(%rip), %rcx     # 0x41551
movl $0xe88, %edx            # imm = 0xE88
callq 0x30a0
leaq 0x34c82(%rip), %rdi     # 0x41585
leaq 0x34a5a(%rip), %rsi     # 0x41364
leaq 0x34c40(%rip), %rcx     # 0x41551
movl $0xe89, %edx            # imm = 0xE89
callq 0x30a0