// File: /workspace/github_c_makefile_star2_O1/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 0x2d00a
movq %rax, %rbp
movq %r15, %rdi
callq 0x2d00a
movq %rax, %rbx
movq 0x98(%r15), %r12
movq 0xa8(%r15), %r13
movl $0x4, %edx
movl $0x1, %ecx
movq %r15, %rdi
movl $0x2d, %esi
movq %r14, %r8
callq 0x2c00f
cmpl $0x0, (%r14)
jne 0x316e5
movq %r12, 0x18(%rsp)
movq %r13, 0x10(%rsp)
movq %r15, %rdi
movl $0x43, %esi
movq %rax, %rdx
movq %rbp, %rcx
movq %rbx, %r8
movq %r14, %r9
callq 0x2c0c2
cmpl $0x0, (%r14)
je 0x3170c
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 0x2b4c3
movq %rbx, %r12
movq %r15, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x2d017
cmpl $0x0, (%r14)
movq 0x8(%rsp), %rbx
je 0x3172d
xorl %r13d, %r13d
jmp 0x316ed
movq %rbx, %rdi
movq %r14, %rsi
callq 0x31020
movq %rax, %r13
cmpl $0x0, (%r14)
jne 0x316ed
movq 0x148(%r13), %rcx
movq %r15, %rdi
movl $0x1e, %esi
movq 0x10(%rsp), %rdx
movq %r14, %r8
callq 0x2c1ac
cmpl $0x0, (%r14)
jne 0x316ed
movq %r13, %rdi
callq 0x2b4c3
movq %r15, %rdi
movl $0x45, %esi
movq 0x18(%rsp), %rbp
movq %rbp, %rdx
movq %r14, %rcx
callq 0x2c19f
cmpl $0x0, (%r14)
jne 0x31728
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x2d017
cmpl $0x0, (%r14)
jne 0x31728
movq %rbx, %rdi
movq %r14, %rsi
callq 0x30f49
movq %rax, %r13
cmpl $0x0, (%r14)
jne 0x316ed
movq 0x148(%r13), %rcx
movq %r15, %rdi
movl $0x1e, %esi
movq 0x10(%rsp), %r12
movq %r12, %rdx
movq %r14, %r8
callq 0x2c1ac
cmpl $0x0, (%r14)
jne 0x316ed
movq %r15, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x2d017
cmpl $0x0, (%r14)
jne 0x316ed
movq %r15, %rdi
movl $0x49, %esi
movq %r12, %rdx
movq %r14, %rcx
callq 0x2c19f
jmp 0x316ed