// File: /workspace/github_c_makefile_star2_O3/markdryan[P]subtilis/frontend/builtins_ir.c
// Function: subtilis_builtins_ir_rnd_int
void subtilis_builtins_ir_rnd_int(subtilis_parser_t *p,
				  subtilis_ir_section_t *current,
				  subtilis_error_t *err)
{
	subtilis_ir_section_t *old_current;
	subtilis_ir_operand_t neg_label;
	subtilis_ir_operand_t zero_pos_label;
	subtilis_ir_operand_t end_label;
	subtilis_ir_operand_t result;
	subtilis_ir_operand_t op1;
	subtilis_ir_operand_t op2;
	subtilis_ir_operand_t cond;
	subtilis_exp_t *m = NULL;
	subtilis_exp_t *e = NULL;
	subtilis_exp_t *e_dup = NULL;
	subtilis_exp_t *top_bit = NULL;

	/*
	 * This is a bit nasty.  The expression functions take a parser
	 * and not a section.  As we don't want to add to the current section
	 * here we need to temporarily replace the current section.  We should
	 * probably update the expression functions to take an explicit section.
	 */

	old_current = p->current;
	p->current = current;

	neg_label.label = subtilis_ir_section_new_label(current);
	zero_pos_label.label = subtilis_ir_section_new_label(current);
	end_label.label = current->end_label;

	result.reg = current->ret_reg;

	op1.reg = SUBTILIS_IR_REG_TEMP_START;
	op2.integer = 0;
	cond.reg = subtilis_ir_section_add_instr(
	    current, SUBTILIS_OP_INSTR_LTI_I32, op1, op2, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	subtilis_ir_section_add_instr_reg(current, SUBTILIS_OP_INSTR_JMPC, cond,
					  neg_label, zero_pos_label, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	subtilis_ir_section_add_label(current, neg_label.reg, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	op2.integer = 0;
	subtilis_ir_section_add_instr_reg(current, SUBTILIS_OP_INSTR_RSUBI_I32,
					  result, op1, op2, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	e_dup = subtilis_exp_new_int32_var(result.reg, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	subtilis_var_assign_hidden(p, subtilis_rnd_hidden_var,
				   &subtilis_type_integer, e_dup, err);
	e_dup = NULL;
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	subtilis_ir_section_add_instr_no_reg(current, SUBTILIS_OP_INSTR_JMP,
					     end_label, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	subtilis_ir_section_add_label(current, zero_pos_label.reg, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	e = subtilis_builtins_ir_basic_rnd(p, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	e_dup = subtilis_type_if_dup(e, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	m = subtilis_exp_new_int32_var(SUBTILIS_IR_REG_TEMP_START, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	top_bit = subtilis_exp_new_int32((int32_t)0x7fffffff, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	e = subtilis_type_if_and(p, e, top_bit, err);
	top_bit = NULL;
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	e = subtilis_type_if_mod(p, e, m, err);
	m = NULL;
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	op2.integer = 1;
	subtilis_ir_section_add_instr_reg(current, SUBTILIS_OP_INSTR_ADDI_I32,
					  result, e->exp.ir_op, op2, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	subtilis_var_assign_hidden(p, subtilis_rnd_hidden_var,
				   &subtilis_type_integer, e_dup, err);
	e_dup = NULL;
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	subtilis_ir_section_add_label(current, end_label.label, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	subtilis_ir_section_add_instr_no_reg(current, SUBTILIS_OP_INSTR_RET_I32,
					     result, err);

cleanup:

	p->current = old_current;

	subtilis_exp_delete(top_bit);
	subtilis_exp_delete(e_dup);
	subtilis_exp_delete(e);
	subtilis_exp_delete(m);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r13
movq 0x48(%rdi), %rbp
movq %rsi, 0x48(%rdi)
movq %rsi, %rdi
callq 0x2cc99
movq %rax, 0x28(%rsp)
movq %r15, %rdi
callq 0x2cc99
movq %rax, 0x40(%rsp)
movq 0x98(%r15), %r14
movq 0xa8(%r15), %r12
movq $0x0, 0x18(%rsp)
movl $0x4, %edx
movq %r15, %rdi
movl $0x3d, %esi
xorl %ecx, %ecx
movq %rbx, %r8
callq 0x2bc8f
movq %rbx, 0x20(%rsp)
cmpl $0x0, (%rbx)
movq %rax, %rdx
movl $0x0, %eax
movl $0x0, %r8d
movl $0x0, %ecx
jne 0x311d0
movq %r14, 0x38(%rsp)
movq %r12, 0x30(%rsp)
movq %rbp, 0x8(%rsp)
movq %r13, 0x10(%rsp)
movq %r15, %rdi
movl $0x43, %esi
movq 0x28(%rsp), %r12
movq %r12, %rcx
movq 0x40(%rsp), %rbx
movq %rbx, %r8
movq 0x20(%rsp), %r14
movq %r14, %r9
callq 0x2bd42
cmpl $0x0, (%r14)
jne 0x311b6
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x2cca6
cmpl $0x0, (%r14)
je 0x3120a
movq $0x0, 0x18(%rsp)
xorl %eax, %eax
xorl %r8d, %r8d
xorl %ecx, %ecx
movq 0x10(%rsp), %r13
movq 0x8(%rsp), %rbp
movq %rbp, 0x48(%r13)
movq %rax, %rbx
movq %rcx, %rdi
movq %r8, %r14
callq 0x2b166
movq %r14, %rdi
callq 0x2b166
movq %rbx, %rdi
callq 0x2b166
movq 0x18(%rsp), %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2b166
movq $0x0, 0x18(%rsp)
movl $0x4, %ecx
movq %r15, %rdi
movl $0x21, %esi
movq %rbx, %r12
movq 0x30(%rsp), %rbx
movq %rbx, %rdx
xorl %r8d, %r8d
movq %r14, %r9
callq 0x2bd42
cmpl $0x0, (%r14)
movl $0x0, %eax
movl $0x0, %r8d
movl $0x0, %ecx
movq 0x10(%rsp), %r13
movq 0x8(%rsp), %rbp
jne 0x311d0
movl %ebx, %edi
movq 0x20(%rsp), %r14
movq %r14, %rsi
callq 0x2b71f
movq %rax, %r8
cmpl $0x0, (%r14)
je 0x31282
movq $0x0, 0x18(%rsp)
xorl %eax, %eax
jmp 0x311c4
leaq 0x74ee7(%rip), %rbx     # 0xa6170
movq (%rbx), %rsi
leaq 0x43685(%rip), %rdx     # 0x74918
movq 0x10(%rsp), %r13
movq %r13, %rdi
movq %r8, %rcx
movq %r14, %r8
callq 0x35b78
cmpl $0x0, (%r14)
jne 0x312db
movq %r15, %rdi
movl $0x45, %esi
movq 0x38(%rsp), %rdx
movq %r14, %rcx
callq 0x2be1f
cmpl $0x0, (%r14)
jne 0x312db
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x2cca6
cmpl $0x0, (%r14)
je 0x312f0
movq $0x0, 0x18(%rsp)
xorl %eax, %eax
xorl %r8d, %r8d
xorl %ecx, %ecx
jmp 0x311cb
movq %r13, %rdi
movq %r14, %rsi
callq 0x30ccb
cmpl $0x0, (%r14)
je 0x3130c
movq $0x0, 0x18(%rsp)
jmp 0x312e6
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
callq 0x36182
movq %rax, %r8
cmpl $0x0, (%r14)
je 0x31340
movq $0x0, 0x18(%rsp)
xorl %ecx, %ecx
movq 0x10(%rsp), %r13
movq 0x8(%rsp), %rbp
movq %r12, %rax
jmp 0x311d0
movq %r8, 0x28(%rsp)
movl $0x4, %edi
movq %r14, %rsi
callq 0x2b71f
movq %rax, 0x18(%rsp)
cmpl $0x0, (%r14)
je 0x31361
xorl %ecx, %ecx
jmp 0x3137c
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movq 0x20(%rsp), %r14
movq %r14, %rsi
callq 0x2b7af
movq %rax, %rcx
cmpl $0x0, (%r14)
je 0x31393
movq 0x10(%rsp), %r13
movq 0x8(%rsp), %rbp
movq %r12, %rax
movq 0x28(%rsp), %r8
jmp 0x311d0
movq 0x10(%rsp), %rdi
movq %r12, %rsi
movq %rcx, %rdx
movq 0x20(%rsp), %r14
movq %r14, %rcx
callq 0x36c60
cmpl $0x0, (%r14)
je 0x313bf
xorl %ecx, %ecx
movq 0x10(%rsp), %r13
movq 0x8(%rsp), %rbp
jmp 0x31389
movq 0x10(%rsp), %rdi
movq %rax, %rsi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %r14
movq %r14, %rcx
callq 0x36eae
movq %rax, %r12
cmpl $0x0, (%r14)
jne 0x31410
movq 0x148(%r12), %rcx
movl $0x1, %r8d
movq %r15, %rdi
movl $0x9, %esi
movq 0x30(%rsp), %rdx
movq 0x20(%rsp), %r14
movq %r14, %r9
callq 0x2bd42
cmpl $0x0, (%r14)
je 0x3141e
movq $0x0, 0x18(%rsp)
jmp 0x3135d
movq (%rbx), %rsi
leaq 0x434f0(%rip), %rdx     # 0x74918
movq 0x10(%rsp), %rdi
movq 0x28(%rsp), %rcx
movq 0x20(%rsp), %rbx
movq %rbx, %r8
callq 0x35b78
cmpl $0x0, (%rbx)
jne 0x31475
movq %r15, %rdi
movq 0x38(%rsp), %rsi
movq 0x20(%rsp), %rbx
movq %rbx, %rdx
callq 0x2cca6
cmpl $0x0, (%rbx)
jne 0x31475
movq %r15, %rdi
movl $0x47, %esi
movq 0x30(%rsp), %rdx
movq 0x20(%rsp), %rcx
callq 0x2be1f
movq $0x0, 0x18(%rsp)
xorl %r8d, %r8d
jmp 0x3132c