// File: /workspace/github_c_makefile_star2_O2/Ihsoh[P]ilang/compiler/frontend/irgen_llvmir.c
// Function: ir_constexpr
static void _ir_constexpr(
	IRGeneratorLlvmirContext *ctx,
	ResizableString *rstr_type,
	ResizableString *rstr_val,
	ParserASTNode *node_constexpr
) {
	assert(ctx);
	assert(rstr_val);
	assert(node_constexpr);
	assert(FE_EXPR_AST_NODE_GET_CONSTANT(node_constexpr));

	if (rstr_type != NULL) {
		_ir_type(ctx, rstr_type, FE_EXPR_AST_NODE_GET_TYPE_NODE(node_constexpr));
	}

	switch (FE_EXPR_AST_NODE_GET_CONSTANT_TYPE(node_constexpr)) {
		case FE_TYPE_CHAR: {
			char str[128];
			snprintf(
				str, sizeof(str), "%d",
				FE_EXPR_AST_NODE_GET_CONSTEXPR_RESULT_CHAR_VAL(node_constexpr)
			);
			rstr_append_with_cstr(rstr_val, str);
			break;
		}
		case FE_TYPE_INT8: {
			char str[128];
			snprintf(
				str, sizeof(str), "%d",
				FE_EXPR_AST_NODE_GET_CONSTEXPR_RESULT_INT8_VAL(node_constexpr)
			);
			rstr_append_with_cstr(rstr_val, str);
			break;
		}
		case FE_TYPE_INT16: {
			char str[128];
			snprintf(
				str, sizeof(str), "%d",
				FE_EXPR_AST_NODE_GET_CONSTEXPR_RESULT_INT16_VAL(node_constexpr)
			);
			rstr_append_with_cstr(rstr_val, str);
			break;
		}
		case FE_TYPE_INT32: {
			char str[128];
			snprintf(
				str, sizeof(str), "%d",
				FE_EXPR_AST_NODE_GET_CONSTEXPR_RESULT_INT32_VAL(node_constexpr)
			);
			rstr_append_with_cstr(rstr_val, str);
			break;
		}
		case FE_TYPE_INT64: {
			char str[128];
			snprintf(
				str, sizeof(str), "%lld",
				FE_EXPR_AST_NODE_GET_CONSTEXPR_RESULT_INT64_VAL(node_constexpr)
			);
			rstr_append_with_cstr(rstr_val, str);
			break;
		}
		case FE_TYPE_UINT8: {
			char str[128];
			snprintf(
				str, sizeof(str), "%u",
				FE_EXPR_AST_NODE_GET_CONSTEXPR_RESULT_UINT8_VAL(node_constexpr)
			);
			rstr_append_with_cstr(rstr_val, str);
			break;
		}
		case FE_TYPE_UINT16: {
			char str[128];
			snprintf(
				str, sizeof(str), "%u",
				FE_EXPR_AST_NODE_GET_CONSTEXPR_RESULT_UINT16_VAL(node_constexpr)
			);
			rstr_append_with_cstr(rstr_val, str);
			break;
		}
		case FE_TYPE_UINT32: {
			char str[128];
			snprintf(
				str, sizeof(str), "%u",
				FE_EXPR_AST_NODE_GET_CONSTEXPR_RESULT_UINT32_VAL(node_constexpr)
			);
			rstr_append_with_cstr(rstr_val, str);
			break;
		}
		case FE_TYPE_UINT64: {
			char str[128];
			snprintf(
				str, sizeof(str), "%llu",
				FE_EXPR_AST_NODE_GET_CONSTEXPR_RESULT_UINT64_VAL(node_constexpr)
			);
			rstr_append_with_cstr(rstr_val, str);
			break;
		}
		case FE_TYPE_FLOAT: {
			char str[128];
			double val = FE_EXPR_AST_NODE_GET_CONSTEXPR_RESULT_FLOAT_VAL(node_constexpr);
			snprintf(
				str, sizeof(str), "0x%llX",
				*(uint64_t *)&val
			);
			rstr_append_with_cstr(rstr_val, str);
			break;
		}
		case FE_TYPE_DOUBLE: {
			char str[128];
			double val = FE_EXPR_AST_NODE_GET_CONSTEXPR_RESULT_DOUBLE_VAL(node_constexpr);
			snprintf(
				str, sizeof(str), "0x%llX",
				*(uint64_t *)&val
			);
			rstr_append_with_cstr(rstr_val, str);
			break;
		}
		case FE_TYPE_POINTER: {
			if (FE_EXPR_AST_NODE_GET_HAS_CONSTSTR(node_constexpr)) {
				FeParserConstexprString *conststr = FE_EXPR_AST_NODE_GET_CONSTSTR(node_constexpr);
				uint64_t ptr_val = FE_EXPR_AST_NODE_GET_CONSTEXPR_RESULT_POINTER_VAL(node_constexpr);

				ResizableString conststr_name;
				rstr_init(&conststr_name);

				int len = _new_string(ctx, &conststr_name, conststr->str, conststr->len);

				ResizableString target_type;
				rstr_init(&target_type);

				_ir_type(ctx, &target_type, FE_EXPR_AST_NODE_GET_TYPE_NODE(node_constexpr));

				char *offset_type = "";
				if (ctx->psrctx->arch == FE_ARCH_32) {
					offset_type = "i32";
				} else if (ctx->psrctx->arch == FE_ARCH_64) {
					offset_type = "i64";
				} else {
					assert(0);
				}

				if (ptr_val == 0) {
					rstr_appendf(
						rstr_val,
						"bitcast (i8* getelementptr inbounds ([%zu x i8], [%zu x i8]* %s, i32 0, %s 0) to %s)",
						len,
						len,
						RSTR_CSTR(&conststr_name),
						offset_type,
						RSTR_CSTR(&target_type)
					);
				} else {
					rstr_appendf(
						rstr_val,
						"bitcast (i8* getelementptr inbounds ([%zu x i8], [%zu x i8]* %s, i32 0, %s %u) to %s)",
						len,
						len,
						RSTR_CSTR(&conststr_name),
						offset_type,
						ptr_val,
						RSTR_CSTR(&target_type)
					);
				}

				rstr_free(&conststr_name);
				rstr_free(&target_type);
			} else {
				char *source_type = "";
				if (ctx->psrctx->arch == FE_ARCH_32) {
					source_type = "i32";
				} else if (ctx->psrctx->arch == FE_ARCH_64) {
					source_type = "i64";
				} else {
					assert(0);
				}

				ResizableString target_type;
				rstr_init(&target_type);
				_ir_type(ctx, &target_type, FE_EXPR_AST_NODE_GET_TYPE_NODE(node_constexpr));

				char str[128];
				snprintf(
					str, sizeof(str), "inttoptr (%s %llu to %s)",
					source_type,
					FE_EXPR_AST_NODE_GET_CONSTEXPR_RESULT_POINTER_VAL(node_constexpr),
					RSTR_CSTR(&target_type)
				);
				rstr_append_with_cstr(rstr_val, str);

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
testq %rdx, %rdx
je 0x1960b
movq %rcx, %r14
cmpb $0x0, 0x70(%rcx)
je 0x1962a
movq %rdx, %rbx
movq %rdi, %r15
testq %rsi, %rsi
je 0x1929d
movq 0x68(%r14), %rax
movq %rsi, %rdi
movq %rax, %rsi
callq 0x18312
movzbl 0x71(%r14), %eax
decl %eax
cmpl $0xf, %eax
ja 0x19649
leaq 0x2860c(%rip), %rcx     # 0x418c0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movsbl 0x88(%r14), %ecx
jmp 0x1945e
cmpb $0x1, 0x72(%r14)
jne 0x194a9
movq %rbx, -0x40(%rbp)
movq 0x88(%r14), %rax
movq %rax, -0x38(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x6e1c
movq 0x78(%r14), %r12
testq %r12, %r12
je 0x19668
movq 0x80(%r14), %rax
movq %rax, -0x30(%rbp)
movq 0x38(%r15), %r13
leaq 0x1(%r13), %rax
movq %rax, 0x38(%r15)
leaq 0x28c58(%rip), %rsi     # 0x41f70
leaq -0x50(%rbp), %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x739a
leaq -0xd0(%rbp), %rbx
movq %rbx, %rdi
callq 0x6e1c
movq (%r15), %rax
movq (%rax), %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq -0x30(%rbp), %rcx
callq 0x20276
movl 0x4(%rbx), %r12d
movq 0x18(%r15), %rdi
leal 0x1(%r12), %ecx
leaq 0x28c1c(%rip), %rsi     # 0x41f7a
movq %r13, %rdx
movl %ecx, -0x30(%rbp)
xorl %eax, %eax
callq 0x739a
testl %r12d, %r12d
movl $0x0, %eax
cmovlel %eax, %r12d
xorl %r13d, %r13d
cmpq %r13, %r12
je 0x194c6
movslq -0xcc(%rbp), %rax
movb $-0x1, %bl
cmpq %rax, %r13
jge 0x193aa
movq -0xc8(%rbp), %rax
testq %rax, %rax
leaq 0x26b74(%rip), %rcx     # 0x3ff16
cmoveq %rcx, %rax
movb (%rax,%r13), %bl
callq 0x3180
movq (%rax), %rax
movsbq %bl, %rcx
testb $0x40, 0x1(%rax,%rcx,2)
movsbl %cl, %edx
jne 0x193d4
movq 0x18(%r15), %rdi
leaq 0x28be6(%rip), %rsi     # 0x41fb1
xorl %eax, %eax
callq 0x739a
jmp 0x193df
movq 0x18(%r15), %rdi
movl %edx, %esi
callq 0x6f81
incq %r13
jmp 0x1937a
cvtss2sd 0x88(%r14), %xmm0
movq %xmm0, %rcx
jmp 0x193fb
movq 0x88(%r14), %rcx
leaq 0x28a4e(%rip), %rdx     # 0x41e50
jmp 0x19422
movq 0x88(%r14), %rcx
leaq 0x277fb(%rip), %rdx     # 0x40c0d
jmp 0x19422
movq 0x88(%r14), %rcx
leaq 0x277e3(%rip), %rdx     # 0x40c05
leaq -0xd0(%rbp), %r14
movl $0x80, %esi
movq %r14, %rdi
xorl %eax, %eax
callq 0x3090
jmp 0x1948c
movzbl 0x88(%r14), %ecx
jmp 0x1946f
movswl 0x88(%r14), %ecx
jmp 0x1945e
movl 0x88(%r14), %ecx
jmp 0x1946f
movl 0x88(%r14), %ecx
leaq 0x287a2(%rip), %rdx     # 0x41c07
jmp 0x19476
movzwl 0x88(%r14), %ecx
leaq 0x27794(%rip), %rdx     # 0x40c0a
leaq -0xd0(%rbp), %r14
movl $0x80, %esi
movq %r14, %rdi
xorl %eax, %eax
callq 0x3090
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6e99
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r15), %rax
movl 0x30(%rax), %eax
cmpl $0x1, %eax
je 0x19526
cmpl $0x2, %eax
jne 0x19687
leaq 0x287d4(%rip), %r12     # 0x41c98
jmp 0x1952d
movq 0x18(%r15), %rdi
leaq 0x28ae6(%rip), %rsi     # 0x41fb7
callq 0x6e99
leaq -0xd0(%rbp), %rdi
callq 0x72b9
leaq -0xd0(%rbp), %r13
movq %r13, %rdi
callq 0x6e1c
movq 0x68(%r14), %rsi
movq %r13, %rdi
callq 0x18312
movq (%r15), %rax
movl 0x30(%rax), %eax
cmpl $0x1, %eax
je 0x1958e
cmpl $0x2, %eax
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %r11
jne 0x196a6
leaq 0x28774(%rip), %r9      # 0x41c98
jmp 0x1959d
leaq 0x28767(%rip), %r12     # 0x41c94
leaq -0x50(%rbp), %r15
movq %r15, %rdi
callq 0x6e1c
movq 0x68(%r14), %rsi
movq %r15, %rdi
callq 0x18312
movq 0x88(%r14), %r8
movq 0x8(%r15), %rax
testq %rax, %rax
leaq 0x269bc(%rip), %r9      # 0x3ff16
cmovneq %rax, %r9
leaq 0x2899d(%rip), %rdx     # 0x41f02
leaq -0xd0(%rbp), %r14
movl $0x80, %esi
movq %r14, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0x3090
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6e99
movq %r15, %rdi
jmp 0x19601
leaq 0x286ff(%rip), %r9      # 0x41c94
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %r11
movq -0x48(%rbp), %r8
testq %r8, %r8
leaq 0x2696b(%rip), %rax     # 0x3ff16
cmoveq %rax, %r8
movq -0xc8(%rbp), %r10
testq %r10, %r10
cmoveq %rax, %r10
testq %r11, %r11
je 0x195d6
leaq 0x288e3(%rip), %rsi     # 0x41eac
movl -0x30(%rbp), %ecx
movl %ecx, %edx
xorl %eax, %eax
pushq %r10
pushq %r11
jmp 0x195ea
subq $0x8, %rsp
leaq 0x28876(%rip), %rsi     # 0x41e57
movl -0x30(%rbp), %ecx
movl %ecx, %edx
xorl %eax, %eax
pushq %r10
callq 0x739a
popq %rax
popq %rcx
leaq -0x50(%rbp), %rdi
callq 0x72b9
leaq -0xd0(%rbp), %rdi
callq 0x72b9
jmp 0x19497
leaq 0x254e2(%rip), %rdi     # 0x3eaf4
leaq 0x2836f(%rip), %rsi     # 0x41988
leaq 0x287ca(%rip), %rcx     # 0x41dea
movl $0x327, %edx            # imm = 0x327
callq 0x30a0
leaq 0x25448(%rip), %rdi     # 0x3ea79
leaq 0x28350(%rip), %rsi     # 0x41988
leaq 0x287ab(%rip), %rcx     # 0x41dea
movl $0x329, %edx            # imm = 0x329
callq 0x30a0
leaq 0x22d10(%rip), %rdi     # 0x3c360
leaq 0x28331(%rip), %rsi     # 0x41988
leaq 0x2878c(%rip), %rcx     # 0x41dea
movl $0x3e2, %edx            # imm = 0x3E2
callq 0x30a0
leaq 0x2342c(%rip), %rdi     # 0x3ca9b
leaq 0x28312(%rip), %rsi     # 0x41988
leaq 0x2889e(%rip), %rcx     # 0x41f1b
movl $0x11d, %edx            # imm = 0x11D
callq 0x30a0
leaq 0x22cd2(%rip), %rdi     # 0x3c360
leaq 0x282f3(%rip), %rsi     # 0x41988
leaq 0x2874e(%rip), %rcx     # 0x41dea
movl $0x3cd, %edx            # imm = 0x3CD
callq 0x30a0
leaq 0x22cb3(%rip), %rdi     # 0x3c360
leaq 0x282d4(%rip), %rsi     # 0x41988
leaq 0x2872f(%rip), %rcx     # 0x41dea
movl $0x3aa, %edx            # imm = 0x3AA
callq 0x30a0