// File: /workspace/github_c_makefile_star2_O3/Ihsoh[P]ilang/compiler/frontend/irgen_llvmir.c
// Function: ir_type
static void _ir_type(
	IRGeneratorLlvmirContext *ctx,
	ResizableString *rstr,
	ParserASTNode *node_type
) {
	assert(ctx);
	assert(rstr);
	assert(node_type);

	switch (node_type->type) {
		case FE_NODE_TYPE_CHAR: {
			rstr_append_with_cstr(rstr, "i8");
			break;
		}
		case FE_NODE_TYPE_INT8: {
			rstr_append_with_cstr(rstr, "i8");
			break;
		}
		case FE_NODE_TYPE_UINT8: {
			rstr_append_with_cstr(rstr, "i8");
			break;
		}
		case FE_NODE_TYPE_INT16: {
			rstr_append_with_cstr(rstr, "i16");
			break;
		}
		case FE_NODE_TYPE_UINT16: {
			rstr_append_with_cstr(rstr, "i16");
			break;
		}
		case FE_NODE_TYPE_INT32: {
			rstr_append_with_cstr(rstr, "i32");
			break;
		}
		case FE_NODE_TYPE_UINT32: {
			rstr_append_with_cstr(rstr, "i32");
			break;
		}
		case FE_NODE_TYPE_INT64: {
			rstr_append_with_cstr(rstr, "i64");
			break;
		}
		case FE_NODE_TYPE_UINT64: {
			rstr_append_with_cstr(rstr, "i64");
			break;
		}
		case FE_NODE_TYPE_FLOAT: {
			rstr_append_with_cstr(rstr, "float");
			break;
		}
		case FE_NODE_TYPE_DOUBLE: {
			rstr_append_with_cstr(rstr, "double");
			break;
		}
		case FE_NODE_TYPE_VA_LIST: {
			rstr_append_with_cstr(rstr, "%struct.va_list");
			break;
		}
		case FE_NODE_TYPE_STRUCT: {
			assert(node_type->nchilds == 1);

			ParserASTNode *node_id = node_type->childs[0];
			assert(node_id->type == FE_NODE_IDENTIFIER);

			rstr_append_with_cstr(rstr, "%struct.");
			rstr_append_with_raw(rstr, node_id->token->content, node_id->token->len);
			break;
		}
		case FE_NODE_TYPE_ARRAY: {
			assert(node_type->nchilds == 2);

			ParserASTNode *node_array_dims = node_type->childs[0];
			assert(node_array_dims->type == FE_NODE_TYPE_ARRAY_DIMS);
			assert(node_array_dims->nchilds > 0);

			ParserASTNode *node_t = node_type->childs[1];

			_ir_type(ctx, rstr, node_t);

			for (int i = node_array_dims->nchilds - 1; i >= 0; i--) {
				ParserASTNode *node_array_dim = node_array_dims->childs[i];
				assert(node_array_dim->type == FE_NODE_LITERAL_UINT);

				rstr_prepend_with_cstr(rstr, " x ");
				rstr_prepend_with_raw(rstr, node_array_dim->token->content, node_array_dim->token->len);
				rstr_prepend_with_cstr(rstr, "[");

				rstr_append_with_cstr(rstr, "]");
			}

			break;
		}
		case FE_NODE_TYPE_FUNC: {
			assert(node_type->nchilds == 1 || node_type->nchilds == 2);
			ParserASTNode *node_params = NULL;
			ParserASTNode *node_t = NULL;

			node_params = node_type->childs[0];
			assert(node_params->type == FE_NODE_FUNC_PARAMS);

			if (node_type->nchilds == 2) {
				node_t = node_type->childs[1];
			}

			if (node_t != NULL) {
				_ir_type(ctx, rstr, node_t);
			} else {
				rstr_append_with_cstr(rstr, "void");
			}

			rstr_append_with_cstr(rstr, " (");
			_ir_func_type_params(ctx, rstr, node_params);
			rstr_append_with_cstr(rstr, ")");
			break;
		}
		case FE_NODE_TYPE_POINTER: {
			assert(node_type->nchilds == 1);
			ParserASTNode *node_t = node_type->childs[0];

			_ir_type(ctx, rstr, node_t);

			rstr_append_with_cstr(rstr, "*");
			break;
		}
		case FE_NODE_TYPE_VOID: {
			// LLVM IR不允许有void指针类型，需要把void指针类型转换为i8指针类型。
			// 但是在方法的返回值类型的地方，是允许为void类型。
			if (node_type->parent != NULL
					&& node_type->parent->type == FE_NODE_TYPE_POINTER) {
				rstr_prepend_with_cstr(rstr, "i8");
			} else {
				rstr_prepend_with_cstr(rstr, "void");
			}
			break;
		}
		default: {
			assert(0);
			break;
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
testq %rdi, %rdi
je 0x19923
testq %rsi, %rsi
je 0x19942
movq %rdi, %rbx
movl (%rsi), %eax
cmpl $0x42f, %eax            # imm = 0x42F
jg 0x19599
addl $0xfffffc00, %eax       # imm = 0xFFFFFC00
cmpl $0x20, %eax
ja 0x19abf
leaq 0x2b274(%rip), %rcx     # 0x447f8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x2b6b5(%rip), %rsi     # 0x44c49
jmp 0x19742
cmpl $0x430, %eax            # imm = 0x430
je 0x19758
cmpl $0x440, %eax            # imm = 0x440
je 0x19722
cmpl $0x4ff, %eax            # imm = 0x4FF
jne 0x19abf
movq 0x30(%rsi), %rax
testq %rax, %rax
je 0x19794
cmpl $0x440, (%rax)          # imm = 0x440
jne 0x19794
leaq 0x2b66f(%rip), %rsi     # 0x44c49
jmp 0x1979b
leaq 0x2b666(%rip), %rsi     # 0x44c4c
jmp 0x19742
leaq 0x2b662(%rip), %rsi     # 0x44c54
jmp 0x19742
leaq 0x2b652(%rip), %rsi     # 0x44c50
jmp 0x19742
leaq 0x28123(%rip), %rsi     # 0x4172d
jmp 0x19742
leaq 0x2b642(%rip), %rsi     # 0x44c58
jmp 0x19742
leaq 0x28111(%rip), %rsi     # 0x41733
jmp 0x19742
cmpl $0x2, 0x44(%rsi)
jne 0x199a8
movq 0x48(%rsi), %rax
movq (%rax), %r13
cmpl $0x421, (%r13)          # imm = 0x421
jne 0x19a24
cmpl $0x0, 0x44(%r13)
jle 0x19a81
movq 0x8(%rax), %rsi
movq %rbx, %rdi
callq 0x19540
movslq 0x44(%r13), %r14
testq %r14, %r14
jle 0x198f5
incq %r14
leaq 0x32149(%rip), %r12     # 0x4b7bd
movq 0x48(%r13), %rax
movq -0x10(%rax,%r14,8), %r15
cmpl $0x300, (%r15)          # imm = 0x300
jne 0x19904
movq %rbx, %rdi
leaq 0x2b5dd(%rip), %rsi     # 0x44c71
callq 0x7db4
movq 0x10(%r15), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
movq %rbx, %rdi
callq 0x7de4
movq %rbx, %rdi
leaq 0x28145(%rip), %rsi     # 0x417fb
callq 0x7db4
movq %rbx, %rdi
movq %r12, %rsi
callq 0x73db
decq %r14
cmpq $0x1, %r14
ja 0x19674
jmp 0x198f5
cmpl $0x1, 0x44(%rsi)
jne 0x199c7
movq 0x48(%rsi), %rax
movq (%rax), %r14
cmpl $0x200, (%r14)          # imm = 0x200
jne 0x19a43
leaq 0x2b56f(%rip), %rsi     # 0x44c68
movq %rbx, %rdi
callq 0x73db
movq 0x10(%r14), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7577
cmpl $0x1, 0x44(%rsi)
jne 0x199e6
movq 0x48(%rsi), %rax
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x19540
leaq 0x2a362(%rip), %rsi     # 0x43aa4
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x73db
movl 0x44(%rsi), %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0x19a05
movq 0x48(%rsi), %rcx
movq (%rcx), %r12
cmpl $0xa01, (%r12)          # imm = 0xA01
jne 0x19a62
cmpl $0x2, %eax
jne 0x197b1
movq 0x8(%rcx), %rsi
testq %rsi, %rsi
je 0x197b1
movq %rbx, %rdi
callq 0x19540
jmp 0x197c0
leaq 0x2b4da(%rip), %rsi     # 0x44c75
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7db4
leaq 0x2b4bd(%rip), %rsi     # 0x44c75
movq %rbx, %rdi
callq 0x73db
leaq 0x2ac7e(%rip), %rsi     # 0x44445
movq %rbx, %rdi
callq 0x73db
cmpl $0xa01, (%r12)          # imm = 0xA01
jne 0x19aa0
cmpl $0x0, 0x44(%r12)
jle 0x198e6
movq 0x48(%r12), %rax
movq (%rax), %rax
cmpl $0xa02, (%rax)          # imm = 0xA02
jne 0x19ade
cmpl $0x2, 0x44(%rax)
jne 0x19afd
movq 0x48(%rax), %rax
movq 0x8(%rax), %r14
leaq -0x38(%rbp), %r15
movq %r15, %rdi
callq 0x7354
movq %r15, %rdi
movq %r14, %rsi
callq 0x19540
movq %rbx, %rdi
movq %r15, %rsi
callq 0x769e
movq %r15, %rdi
callq 0x7852
cmpl $0x2, 0x44(%r12)
jl 0x198e6
movl $0x1, %r14d
movq 0x48(%r12), %rax
movq (%rax,%r14,8), %r13
movl (%r13), %eax
andl $-0x2, %eax
cmpl $0xa02, %eax            # imm = 0xA02
jne 0x19961
movq %rbx, %rdi
leaq 0x287d2(%rip), %rsi     # 0x42042
callq 0x73db
movl (%r13), %eax
cmpl $0xa02, %eax            # imm = 0xA02
je 0x1989c
cmpl $0xa03, %eax            # imm = 0xA03
jne 0x19989
movq %rbx, %rdi
leaq 0x28097(%rip), %rsi     # 0x4192c
callq 0x73db
jmp 0x198d5
cmpl $0x2, 0x44(%r13)
jne 0x19980
movq 0x48(%r13), %rax
movq 0x8(%rax), %r13
movq %r15, %rdi
callq 0x7354
movq %r15, %rdi
movq %r13, %rsi
callq 0x19540
movq %rbx, %rdi
movq %r15, %rsi
callq 0x769e
movq %r15, %rdi
callq 0x7852
incq %r14
movslq 0x44(%r12), %rax
cmpq %rax, %r14
jl 0x1984b
leaq 0x2a11d(%rip), %rsi     # 0x43a0a
movq %rbx, %rdi
callq 0x73db
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x27ec3(%rip), %rdi     # 0x417ce
leaq 0x2b032(%rip), %rsi     # 0x44944
leaq 0x2b2e2(%rip), %rcx     # 0x44bfb
movl $0x2b5, %edx            # imm = 0x2B5
callq 0x30a0
leaq 0x26106(%rip), %rdi     # 0x3fa30
leaq 0x2b013(%rip), %rsi     # 0x44944
leaq 0x2b2c3(%rip), %rcx     # 0x44bfb
movl $0x26a, %edx            # imm = 0x26A
callq 0x30a0
leaq 0x27dba(%rip), %rdi     # 0x41703
leaq 0x2aff4(%rip), %rsi     # 0x44944
leaq 0x2b2a4(%rip), %rcx     # 0x44bfb
movl $0x26b, %edx            # imm = 0x26B
callq 0x30a0
leaq 0x27fe1(%rip), %rdi     # 0x41949
leaq 0x2afd5(%rip), %rsi     # 0x44944
leaq 0x2b304(%rip), %rcx     # 0x44c7a
movl $0x259, %edx            # imm = 0x259
callq 0x30a0
leaq 0x27fa9(%rip), %rdi     # 0x41930
jmp 0x19990
leaq 0x259d0(%rip), %rdi     # 0x3f360
leaq 0x2afad(%rip), %rsi     # 0x44944
leaq 0x2b2dc(%rip), %rcx     # 0x44c7a
movl $0x25d, %edx            # imm = 0x25D
callq 0x30a0
leaq 0x27e69(%rip), %rdi     # 0x41818
leaq 0x2af8e(%rip), %rsi     # 0x44944
leaq 0x2b23e(%rip), %rcx     # 0x44bfb
movl $0x2a9, %edx            # imm = 0x2A9
callq 0x30a0
leaq 0x27d76(%rip), %rdi     # 0x41744
leaq 0x2af6f(%rip), %rsi     # 0x44944
leaq 0x2b21f(%rip), %rcx     # 0x44bfb
movl $0x29f, %edx            # imm = 0x29F
callq 0x30a0
leaq 0x27d57(%rip), %rdi     # 0x41744
leaq 0x2af50(%rip), %rsi     # 0x44944
leaq 0x2b200(%rip), %rcx     # 0x44bfb
movl $0x2d8, %edx            # imm = 0x2D8
callq 0x30a0
leaq 0x27df1(%rip), %rdi     # 0x417fd
leaq 0x2af31(%rip), %rsi     # 0x44944
leaq 0x2b1e1(%rip), %rcx     # 0x44bfb
movl $0x2c1, %edx            # imm = 0x2C1
callq 0x30a0
leaq 0x27d55(%rip), %rdi     # 0x41780
leaq 0x2af12(%rip), %rsi     # 0x44944
leaq 0x2b1c2(%rip), %rcx     # 0x44bfb
movl $0x2ac, %edx            # imm = 0x2AC
callq 0x30a0
leaq 0x27d12(%rip), %rdi     # 0x4175c
leaq 0x2aef3(%rip), %rsi     # 0x44944
leaq 0x2b1a3(%rip), %rcx     # 0x44bfb
movl $0x2a2, %edx            # imm = 0x2A2
callq 0x30a0
leaq 0x27dc7(%rip), %rdi     # 0x41830
leaq 0x2aed4(%rip), %rsi     # 0x44944
leaq 0x2b184(%rip), %rcx     # 0x44bfb
movl $0x2c6, %edx            # imm = 0x2C6
callq 0x30a0
leaq 0x27d29(%rip), %rdi     # 0x417b1
leaq 0x2aeb5(%rip), %rsi     # 0x44944
leaq 0x2b165(%rip), %rcx     # 0x44bfb
movl $0x2ad, %edx            # imm = 0x2AD
callq 0x30a0
leaq 0x27e25(%rip), %rdi     # 0x418cc
leaq 0x2ae96(%rip), %rsi     # 0x44944
leaq 0x2b1c5(%rip), %rcx     # 0x44c7a
movl $0x237, %edx            # imm = 0x237
callq 0x30a0
leaq 0x2589a(%rip), %rdi     # 0x3f360
leaq 0x2ae77(%rip), %rsi     # 0x44944
leaq 0x2b127(%rip), %rcx     # 0x44bfb
movl $0x2ec, %edx            # imm = 0x2EC
callq 0x30a0
leaq 0x27e1a(%rip), %rdi     # 0x418ff
leaq 0x2ae58(%rip), %rsi     # 0x44944
leaq 0x2b187(%rip), %rcx     # 0x44c7a
movl $0x23b, %edx            # imm = 0x23B
callq 0x30a0
leaq 0x27e2c(%rip), %rdi     # 0x41930
leaq 0x2ae39(%rip), %rsi     # 0x44944
leaq 0x2b168(%rip), %rcx     # 0x44c7a
movl $0x254, %edx            # imm = 0x254
callq 0x30a0