// File: /workspace/github_c_makefile_star2_O2/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 0x186ef
testq %rsi, %rsi
je 0x1870e
movq %rdi, %rbx
movl (%rsi), %eax
leal -0x400(%rax), %ecx
cmpl $0x20, %ecx
ja 0x18361
leaq 0x294f0(%rip), %rax     # 0x4183c
movslq (%rax,%rcx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
leaq 0x29931(%rip), %rsi     # 0x41c8d
jmp 0x18534
cmpl $0x430, %eax            # imm = 0x430
je 0x184d8
cmpl $0x440, %eax            # imm = 0x440
je 0x18514
cmpl $0x4ff, %eax            # imm = 0x4FF
jne 0x18755
movq 0x30(%rsi), %rax
testq %rax, %rax
je 0x1854a
cmpl $0x440, (%rax)          # imm = 0x440
jne 0x1854a
leaq 0x298eb(%rip), %rsi     # 0x41c8d
jmp 0x18551
leaq 0x298ea(%rip), %rsi     # 0x41c98
jmp 0x18534
leaq 0x298d6(%rip), %rsi     # 0x41c90
jmp 0x18534
leaq 0x298ce(%rip), %rsi     # 0x41c94
jmp 0x18534
cmpl $0x2, 0x44(%rsi)
jne 0x18774
movq 0x48(%rsi), %rax
movq (%rax), %r13
cmpl $0x421, (%r13)          # imm = 0x421
jne 0x187f0
cmpl $0x0, 0x44(%r13)
jle 0x1884d
movq 0x8(%rax), %rsi
movq %rbx, %rdi
callq 0x18312
movl 0x44(%r13), %r14d
leaq 0x303f1(%rip), %r12     # 0x487fd
testl %r14d, %r14d
jle 0x186a2
movq 0x48(%r13), %rax
movq -0x8(%rax,%r14,8), %r15
cmpl $0x300, (%r15)          # imm = 0x300
jne 0x186b1
movq %rbx, %rdi
leaq 0x29880(%rip), %rsi     # 0x41cb5
callq 0x7808
movq 0x10(%r15), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rax), %edx
movq %rbx, %rdi
callq 0x7835
movq %rbx, %rdi
leaq 0x263b4(%rip), %rsi     # 0x3e80b
callq 0x7808
movq %rbx, %rdi
movq %r12, %rsi
callq 0x6e99
decq %r14
jmp 0x1840c
leaq 0x29829(%rip), %rsi     # 0x41c9c
jmp 0x18534
cmpl $0x1, 0x44(%rsi)
jne 0x18793
movq 0x48(%rsi), %rax
movq (%rax), %r14
cmpl $0x200, (%r14)          # imm = 0x200
jne 0x1880f
leaq 0x2980f(%rip), %rsi     # 0x41cac
movq %rbx, %rdi
callq 0x6e99
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 0x700f
leaq 0x26276(%rip), %rsi     # 0x3e743
jmp 0x18534
leaq 0x26267(%rip), %rsi     # 0x3e73d
jmp 0x18534
movl 0x44(%rsi), %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0x187b2
movq 0x48(%rsi), %rcx
movq (%rcx), %r12
cmpl $0xa01, (%r12)          # imm = 0xA01
jne 0x1882e
cmpl $0x2, %eax
jne 0x18567
movq 0x8(%rcx), %rsi
testq %rsi, %rsi
je 0x18567
movq %rbx, %rdi
callq 0x18312
jmp 0x18576
cmpl $0x1, 0x44(%rsi)
jne 0x187d1
movq 0x48(%rsi), %rax
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x18312
leaq 0x2859b(%rip), %rsi     # 0x40acf
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6e99
leaq 0x29768(%rip), %rsi     # 0x41cb9
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7808
leaq 0x2974b(%rip), %rsi     # 0x41cb9
movq %rbx, %rdi
callq 0x6e99
leaq 0x28ef3(%rip), %rsi     # 0x41470
movq %rbx, %rdi
callq 0x6e99
cmpl $0xa01, (%r12)          # imm = 0xA01
jne 0x1886c
cmpl $0x0, 0x44(%r12)
jle 0x18693
movq 0x48(%r12), %rax
movq (%rax), %rax
cmpl $0xa02, (%rax)          # imm = 0xA02
jne 0x1888b
cmpl $0x2, 0x44(%rax)
jne 0x188aa
movq 0x48(%rax), %rax
movq 0x8(%rax), %r15
leaq -0x38(%rbp), %r14
movq %r14, %rdi
callq 0x6e1c
movq %r14, %rdi
movq %r15, %rsi
callq 0x18312
movq %rbx, %rdi
movq %r14, %rsi
callq 0x712e
movq %r14, %rdi
callq 0x72b9
pushq $0x1
popq %r15
movslq 0x44(%r12), %rax
cmpq %rax, %r15
jge 0x18693
movq 0x48(%r12), %rax
movq (%rax,%r15,8), %r13
movl (%r13), %eax
andl $-0x2, %eax
cmpl $0xa02, %eax            # imm = 0xA02
jne 0x186d0
movq %rbx, %rdi
leaq 0x26a2c(%rip), %rsi     # 0x3f052
callq 0x6e99
movl (%r13), %eax
cmpl $0xa02, %eax            # imm = 0xA02
je 0x18652
cmpl $0xa03, %eax            # imm = 0xA03
jne 0x1872d
movq %rbx, %rdi
leaq 0x262f1(%rip), %rsi     # 0x3e93c
callq 0x6e99
jmp 0x1868b
cmpl $0x2, 0x44(%r13)
jne 0x18736
movq 0x48(%r13), %rax
movq 0x8(%rax), %r13
movq %r14, %rdi
callq 0x6e1c
movq %r14, %rdi
movq %r13, %rsi
callq 0x18312
movq %rbx, %rdi
movq %r14, %rsi
callq 0x712e
movq %r14, %rdi
callq 0x72b9
incq %r15
jmp 0x185f3
leaq 0x28398(%rip), %rsi     # 0x40a32
movq %rbx, %rdi
callq 0x6e99
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x26126(%rip), %rdi     # 0x3e7de
leaq 0x292c9(%rip), %rsi     # 0x41988
leaq 0x29579(%rip), %rcx     # 0x41c3f
movl $0x2b5, %edx            # imm = 0x2B5
callq 0x30a0
leaq 0x26282(%rip), %rdi     # 0x3e959
leaq 0x292aa(%rip), %rsi     # 0x41988
leaq 0x295d9(%rip), %rcx     # 0x41cbe
movl $0x259, %edx            # imm = 0x259
callq 0x30a0
leaq 0x2433a(%rip), %rdi     # 0x3ca30
leaq 0x2928b(%rip), %rsi     # 0x41988
leaq 0x2953b(%rip), %rcx     # 0x41c3f
movl $0x26a, %edx            # imm = 0x26A
callq 0x30a0
leaq 0x25ffe(%rip), %rdi     # 0x3e713
leaq 0x2926c(%rip), %rsi     # 0x41988
leaq 0x2951c(%rip), %rcx     # 0x41c3f
movl $0x26b, %edx            # imm = 0x26B
callq 0x30a0
leaq 0x23c2c(%rip), %rdi     # 0x3c360
jmp 0x1873d
leaq 0x26203(%rip), %rdi     # 0x3e940
leaq 0x29244(%rip), %rsi     # 0x41988
leaq 0x29573(%rip), %rcx     # 0x41cbe
movl $0x25d, %edx            # imm = 0x25D
callq 0x30a0
leaq 0x23c04(%rip), %rdi     # 0x3c360
leaq 0x29225(%rip), %rsi     # 0x41988
leaq 0x294d5(%rip), %rcx     # 0x41c3f
movl $0x2ec, %edx            # imm = 0x2EC
callq 0x30a0
leaq 0x260ad(%rip), %rdi     # 0x3e828
leaq 0x29206(%rip), %rsi     # 0x41988
leaq 0x294b6(%rip), %rcx     # 0x41c3f
movl $0x2a9, %edx            # imm = 0x2A9
callq 0x30a0
leaq 0x25fba(%rip), %rdi     # 0x3e754
leaq 0x291e7(%rip), %rsi     # 0x41988
leaq 0x29497(%rip), %rcx     # 0x41c3f
movl $0x29f, %edx            # imm = 0x29F
callq 0x30a0
leaq 0x26054(%rip), %rdi     # 0x3e80d
leaq 0x291c8(%rip), %rsi     # 0x41988
leaq 0x29478(%rip), %rcx     # 0x41c3f
movl $0x2c1, %edx            # imm = 0x2C1
callq 0x30a0
leaq 0x25f7c(%rip), %rdi     # 0x3e754
leaq 0x291a9(%rip), %rsi     # 0x41988
leaq 0x29459(%rip), %rcx     # 0x41c3f
movl $0x2d8, %edx            # imm = 0x2D8
callq 0x30a0
leaq 0x25f99(%rip), %rdi     # 0x3e790
leaq 0x2918a(%rip), %rsi     # 0x41988
leaq 0x2943a(%rip), %rcx     # 0x41c3f
movl $0x2ac, %edx            # imm = 0x2AC
callq 0x30a0
leaq 0x25f56(%rip), %rdi     # 0x3e76c
leaq 0x2916b(%rip), %rsi     # 0x41988
leaq 0x2941b(%rip), %rcx     # 0x41c3f
movl $0x2a2, %edx            # imm = 0x2A2
callq 0x30a0
leaq 0x2600b(%rip), %rdi     # 0x3e840
leaq 0x2914c(%rip), %rsi     # 0x41988
leaq 0x293fc(%rip), %rcx     # 0x41c3f
movl $0x2c6, %edx            # imm = 0x2C6
callq 0x30a0
leaq 0x25f6d(%rip), %rdi     # 0x3e7c1
leaq 0x2912d(%rip), %rsi     # 0x41988
leaq 0x293dd(%rip), %rcx     # 0x41c3f
movl $0x2ad, %edx            # imm = 0x2AD
callq 0x30a0
leaq 0x26069(%rip), %rdi     # 0x3e8dc
leaq 0x2910e(%rip), %rsi     # 0x41988
leaq 0x2943d(%rip), %rcx     # 0x41cbe
movl $0x237, %edx            # imm = 0x237
callq 0x30a0
leaq 0x2607d(%rip), %rdi     # 0x3e90f
leaq 0x290ef(%rip), %rsi     # 0x41988
leaq 0x2941e(%rip), %rcx     # 0x41cbe
movl $0x23b, %edx            # imm = 0x23B
callq 0x30a0
leaq 0x2608f(%rip), %rdi     # 0x3e940
leaq 0x290d0(%rip), %rsi     # 0x41988
leaq 0x293ff(%rip), %rcx     # 0x41cbe
movl $0x254, %edx            # imm = 0x254
callq 0x30a0