// File: /workspace/github_c_makefile_star2_O1/markdryan[P]subtilis/frontend/builtins_ir.c
// Function: prv_compute_whole_int_part
static void prv_compute_whole_int_part(subtilis_parser_t *p,
				       subtilis_ir_operand_t str_data,
				       subtilis_ir_operand_t base,
				       subtilis_ir_operand_t ptr,
				       subtilis_ir_operand_t ret_val,
				       subtilis_ir_operand_t overflow_label,
				       subtilis_ir_operand_t end_label,
				       subtilis_ir_operand_t factor,
				       subtilis_error_t *err)
/* clang-format on */

{
	subtilis_ir_operand_t sign;
	subtilis_ir_operand_t op1;
	subtilis_ir_operand_t condee;
	subtilis_ir_operand_t byte;
	subtilis_ir_operand_t product;
	subtilis_ir_operand_t new_ptr;
	subtilis_ir_operand_t overflow;
	subtilis_ir_operand_t whole_start_label;
	subtilis_ir_operand_t process_whole_label;
	subtilis_ir_operand_t check_overflow_label;

	whole_start_label.label = subtilis_ir_section_new_label(p->current);
	process_whole_label.label = subtilis_ir_section_new_label(p->current);
	check_overflow_label.label = subtilis_ir_section_new_label(p->current);

	sign.reg = subtilis_ir_section_add_instr2(
	    p->current, SUBTILIS_OP_INSTR_MOV, factor, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	new_ptr.reg = subtilis_ir_section_add_instr2(
	    p->current, SUBTILIS_OP_INSTR_MOV, ptr, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	subtilis_ir_section_add_label(p->current, whole_start_label.label, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	op1.integer = 1;
	subtilis_ir_section_add_instr_reg(
	    p->current, SUBTILIS_OP_INSTR_SUBI_I32, new_ptr, new_ptr, op1, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	condee.reg = subtilis_ir_section_add_instr(
	    p->current, SUBTILIS_OP_INSTR_GTE_I32, new_ptr, str_data, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	subtilis_ir_section_add_instr_reg(p->current, SUBTILIS_OP_INSTR_JMPC,
					  condee, process_whole_label,
					  end_label, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	subtilis_ir_section_add_label(p->current, process_whole_label.label,
				      err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	op1.integer = 0;
	byte.reg = subtilis_ir_section_add_instr(
	    p->current, SUBTILIS_OP_INSTR_LOADO_I8, new_ptr, op1, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	op1.integer = '0';
	subtilis_ir_section_add_instr_reg(
	    p->current, SUBTILIS_OP_INSTR_SUBI_I32, byte, byte, op1, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	product.reg = subtilis_ir_section_add_instr(
	    p->current, SUBTILIS_OP_INSTR_MUL_I32, byte, factor, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	subtilis_ir_section_add_instr_reg(p->current, SUBTILIS_OP_INSTR_MUL_I32,
					  factor, factor, base, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	subtilis_ir_section_add_instr_reg(p->current, SUBTILIS_OP_INSTR_ADD_I32,
					  ret_val, ret_val, product, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	subtilis_ir_section_add_instr_reg(p->current, SUBTILIS_OP_INSTR_JMPC,
					  ret_val, check_overflow_label,
					  whole_start_label, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	subtilis_ir_section_add_label(p->current, check_overflow_label.label,
				      err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	/*
	 * Now check for overflow.  We EOR the sign and the sum
	 * together.  If the top bit is set we've overflowed.
	 */

	overflow.reg = subtilis_ir_section_add_instr(
	    p->current, SUBTILIS_OP_INSTR_EOR_I32, ret_val, sign, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	op1.integer = 0x80000000;
	overflow.reg = subtilis_ir_section_add_instr(
	    p->current, SUBTILIS_OP_INSTR_ANDI_I32, overflow, op1, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	subtilis_ir_section_add_instr_reg(p->current, SUBTILIS_OP_INSTR_JMPC,
					  overflow, overflow_label,
					  whole_start_label, err);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, 0x30(%rsp)
movq %r8, 0x20(%rsp)
movq %rcx, 0x8(%rsp)
movq %rdx, %r12
movq %rsi, %rbx
movq %rdi, %r15
movq 0x78(%rsp), %r14
movq 0x70(%rsp), %r13
movq 0x48(%rdi), %rdi
callq 0x2d00a
movq %rax, 0x10(%rsp)
movq 0x48(%r15), %rdi
callq 0x2d00a
movq %rax, %rbp
movq 0x48(%r15), %rdi
callq 0x2d00a
movq %rax, 0x18(%rsp)
movq 0x48(%r15), %rdi
movl $0x1d, %esi
movq %r13, %rdx
movq %r14, %rcx
callq 0x2c189
movq %rax, 0x28(%rsp)
cmpl $0x0, (%r14)
jne 0x35b1a
movq 0x48(%r15), %rdi
movl $0x1d, %esi
movq %r12, %rdx
movq %r14, %rcx
callq 0x2c189
cmpl $0x0, (%r14)
jne 0x35b1a
movq %rax, %r12
movq 0x48(%r15), %rdi
movq 0x10(%rsp), %rsi
movq %r14, %rdx
callq 0x2d017
cmpl $0x0, (%r14)
jne 0x35b1a
movq 0x48(%r15), %rdi
movl $0x1, %r8d
movl $0xb, %esi
movq %r12, %rdx
movq %r12, %rcx
movq %r14, %r9
callq 0x2c0c2
cmpl $0x0, (%r14)
jne 0x35b1a
movq 0x48(%r15), %rdi
movl $0x3f, %esi
movq %r12, %rdx
movq %rbx, %rcx
movq %r14, %r8
callq 0x2c00f
cmpl $0x0, (%r14)
jne 0x35b1a
movq 0x48(%r15), %rdi
movl $0x43, %esi
movq %rax, %rdx
movq %rbp, %rcx
movq 0x30(%rsp), %r8
movq %r14, %r9
callq 0x2c0c2
cmpl $0x0, (%r14)
jne 0x35b1a
movq 0x48(%r15), %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x2d017
cmpl $0x0, (%r14)
jne 0x35b1a
movq 0x48(%r15), %rdi
movl $0x11, %esi
movq %r12, %rdx
xorl %ecx, %ecx
movq %r14, %r8
callq 0x2c00f
cmpl $0x0, (%r14)
jne 0x35b1a
movq %rax, %rbx
movq 0x48(%r15), %rdi
movl $0x30, %r8d
movl $0xb, %esi
movq %rax, %rdx
movq %rax, %rcx
movq %r14, %r9
callq 0x2c0c2
cmpl $0x0, (%r14)
jne 0x35b1a
movq 0x48(%r15), %rdi
movl $0x4, %esi
movq %rbx, %rdx
movq %r13, %rcx
movq %r14, %r8
callq 0x2c00f
cmpl $0x0, (%r14)
jne 0x35b1a
movq %rax, %rbx
movq 0x48(%r15), %rdi
movl $0x5, %r8d
movl $0x4, %esi
movq %r13, %rdx
movq %r13, %rcx
movq %r14, %r9
callq 0x2c0c2
cmpl $0x0, (%r14)
jne 0x35b1a
movq 0x48(%r15), %rdi
xorl %esi, %esi
movq 0x8(%rsp), %rcx
movq %rcx, %rdx
movq %rbx, %r8
movq %r14, %r9
callq 0x2c0c2
cmpl $0x0, (%r14)
jne 0x35b1a
movq 0x48(%r15), %rdi
movl $0x43, %esi
movq 0x8(%rsp), %rdx
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %r8
movq %r14, %r9
callq 0x2c0c2
cmpl $0x0, (%r14)
jne 0x35b1a
movq 0x48(%r15), %rdi
movq 0x18(%rsp), %rsi
movq %r14, %rdx
callq 0x2d017
cmpl $0x0, (%r14)
jne 0x35b1a
movq 0x48(%r15), %rdi
movl $0x28, %esi
movq 0x8(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq %r14, %r8
callq 0x2c00f
cmpl $0x0, (%r14)
jne 0x35b1a
movq 0x48(%r15), %rdi
movl $0x80000000, %ecx       # imm = 0x80000000
movl $0x25, %esi
movq %rax, %rdx
movq %r14, %r8
callq 0x2c00f
cmpl $0x0, (%r14)
je 0x35b29
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x48(%r15), %rdi
movl $0x43, %esi
movq %rax, %rdx
movq 0x20(%rsp), %rcx
movq 0x10(%rsp), %r8
movq %r14, %r9
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2c0c2
nopl (%rax)