// File: /workspace/github_c_makefile_star2_O2/markdryan[P]subtilis/frontend/builtins_ir.c
// Function: subtilis_builtins_ir_rnd_real
void subtilis_builtins_ir_rnd_real(subtilis_parser_t *p,
				   subtilis_ir_section_t *current,
				   subtilis_error_t *err)
{
	subtilis_ir_section_t *old_current;
	subtilis_ir_operand_t one_label;
	subtilis_ir_operand_t zero_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 *e = 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;

	one_label.label = subtilis_ir_section_new_label(current);
	zero_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 = 1;
	cond.reg = subtilis_ir_section_add_instr(
	    current, SUBTILIS_OP_INSTR_EQI_I32, op1, op2, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

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

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

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

	subtilis_ir_section_add_instr_no_reg2(current, SUBTILIS_OP_INSTR_MOVFP,
					      result, e->exp.ir_op, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;
	subtilis_exp_delete(e);
	e = NULL;

	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_label.reg, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

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

	subtilis_ir_section_add_instr_no_reg2(current, SUBTILIS_OP_INSTR_MOVFP,
					      result, e->exp.ir_op, err);
	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_REAL, result, err);

cleanup:

	p->current = old_current;

	subtilis_exp_delete(e);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r14
movq %rsi, %r15
movq 0x48(%rdi), %rax
movq %rax, 0x20(%rsp)
movq %rdi, 0x8(%rsp)
movq %rsi, 0x48(%rdi)
movq %rsi, %rdi
callq 0x2b774
movq %rax, %rbp
movq %r15, %rdi
callq 0x2b774
movq %rax, %rbx
movq 0x98(%r15), %r12
movq 0xa8(%r15), %r13
pushq $0x2d
popq %rsi
pushq $0x4
popq %rdx
pushq $0x1
popq %rcx
movq %r15, %rdi
movq %r14, %r8
callq 0x2a819
cmpl $0x0, (%r14)
jne 0x2f9af
movq %r12, 0x18(%rsp)
movq %r13, 0x10(%rsp)
pushq $0x43
popq %rsi
movq %r15, %rdi
movq %rax, %rdx
movq %rbp, %rcx
movq %rbx, %r8
movq %r14, %r9
callq 0x2a8c1
cmpl $0x0, (%r14)
je 0x2f9d6
xorl %r13d, %r13d
movq 0x8(%rsp), %rbx
movq 0x20(%rsp), %rax
movq %rax, 0x48(%rbx)
movq %r13, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x29d88
movq %rbx, %r12
movq %r15, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x2b781
cmpl $0x0, (%r14)
movq 0x8(%rsp), %rbx
je 0x2f9f7
xorl %r13d, %r13d
jmp 0x2f9b7
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2f308
movq %rax, %r13
cmpl $0x0, (%r14)
jne 0x2f9b7
movq 0x148(%r13), %rcx
pushq $0x1e
popq %rsi
movq %r15, %rdi
movq 0x10(%rsp), %rdx
movq %r14, %r8
callq 0x2a9a4
cmpl $0x0, (%r14)
jne 0x2f9b7
movq %r13, %rdi
callq 0x29d88
pushq $0x45
popq %rsi
movq %r15, %rdi
movq 0x18(%rsp), %rbp
movq %rbp, %rdx
movq %r14, %rcx
callq 0x2a997
cmpl $0x0, (%r14)
jne 0x2f9f2
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x2b781
cmpl $0x0, (%r14)
jne 0x2f9f2
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2f231
movq %rax, %r13
cmpl $0x0, (%r14)
jne 0x2f9b7
movq 0x148(%r13), %rcx
pushq $0x1e
popq %rsi
movq %r15, %rdi
movq 0x10(%rsp), %r12
movq %r12, %rdx
movq %r14, %r8
callq 0x2a9a4
cmpl $0x0, (%r14)
jne 0x2f9b7
movq %r15, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x2b781
cmpl $0x0, (%r14)
jne 0x2f9b7
pushq $0x49
popq %rsi
movq %r15, %rdi
movq %r12, %rdx
movq %r14, %rcx
callq 0x2a997
jmp 0x2f9b7