// File: /workspace/github_c_makefile_star2_O2/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 0x2b774
movq %rax, 0x28(%rsp)
movq %r15, %rdi
callq 0x2b774
movq %rax, 0x40(%rsp)
movq 0x98(%r15), %r14
movq 0xa8(%r15), %r12
movq $0x0, 0x18(%rsp)
pushq $0x3d
popq %rsi
pushq $0x4
popq %rdx
movq %r15, %rdi
xorl %ecx, %ecx
movq %rbx, %r8
callq 0x2a819
movq %rbx, 0x20(%rsp)
cmpl $0x0, (%rbx)
movq %rax, %rdx
movl $0x0, %eax
movl $0x0, %r8d
movl $0x0, %ecx
jne 0x2f67c
movq %r14, 0x38(%rsp)
movq %r12, 0x30(%rsp)
movq %rbp, 0x8(%rsp)
movq %r13, 0x10(%rsp)
pushq $0x43
popq %rsi
movq %r15, %rdi
movq 0x28(%rsp), %r12
movq %r12, %rcx
movq 0x40(%rsp), %rbx
movq %rbx, %r8
movq 0x20(%rsp), %r14
movq %r14, %r9
callq 0x2a8c1
cmpl $0x0, (%r14)
jne 0x2f662
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x2b781
cmpl $0x0, (%r14)
je 0x2f6b6
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 0x29d88
movq %r14, %rdi
callq 0x29d88
movq %rbx, %rdi
callq 0x29d88
movq 0x18(%rsp), %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x29d88
movq $0x0, 0x18(%rsp)
pushq $0x21
popq %rsi
pushq $0x4
popq %rcx
movq %r15, %rdi
movq %rbx, %r12
movq 0x30(%rsp), %rbx
movq %rbx, %rdx
xorl %r8d, %r8d
movq %r14, %r9
callq 0x2a8c1
cmpl $0x0, (%r14)
movl $0x0, %eax
movl $0x0, %r8d
movl $0x0, %ecx
movq 0x10(%rsp), %r13
movq 0x8(%rsp), %rbp
jne 0x2f67c
movl %ebx, %edi
movq 0x20(%rsp), %r14
movq %r14, %rsi
callq 0x2a336
movq %rax, %r8
cmpl $0x0, (%r14)
je 0x2f72a
movq $0x0, 0x18(%rsp)
xorl %eax, %eax
jmp 0x2f670
leaq 0x76a3f(%rip), %rbx     # 0xa6170
movq (%rbx), %rsi
leaq 0x3f18d(%rip), %rdx     # 0x6e8c8
movq 0x10(%rsp), %r13
movq %r13, %rdi
movq %r8, %rcx
movq %r14, %r8
callq 0x33be8
cmpl $0x0, (%r14)
jne 0x2f781
pushq $0x45
popq %rsi
movq %r15, %rdi
movq 0x38(%rsp), %rdx
movq %r14, %rcx
callq 0x2a997
cmpl $0x0, (%r14)
jne 0x2f781
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x2b781
cmpl $0x0, (%r14)
je 0x2f796
movq $0x0, 0x18(%rsp)
xorl %eax, %eax
xorl %r8d, %r8d
xorl %ecx, %ecx
jmp 0x2f677
movq %r13, %rdi
movq %r14, %rsi
callq 0x2f17f
cmpl $0x0, (%r14)
je 0x2f7b2
movq $0x0, 0x18(%rsp)
jmp 0x2f78c
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
callq 0x341c4
movq %rax, %r8
cmpl $0x0, (%r14)
je 0x2f7e6
movq $0x0, 0x18(%rsp)
xorl %ecx, %ecx
movq 0x10(%rsp), %r13
movq 0x8(%rsp), %rbp
movq %r12, %rax
jmp 0x2f67c
movq %r8, 0x28(%rsp)
pushq $0x4
popq %rdi
movq %r14, %rsi
callq 0x2a336
movq %rax, 0x18(%rsp)
cmpl $0x0, (%r14)
je 0x2f805
xorl %ecx, %ecx
jmp 0x2f820
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movq 0x20(%rsp), %r14
movq %r14, %rsi
callq 0x2a369
movq %rax, %rcx
cmpl $0x0, (%r14)
je 0x2f837
movq 0x10(%rsp), %r13
movq 0x8(%rsp), %rbp
movq %r12, %rax
movq 0x28(%rsp), %r8
jmp 0x2f67c
movq 0x10(%rsp), %rdi
movq %r12, %rsi
movq %rcx, %rdx
movq 0x20(%rsp), %r14
movq %r14, %rcx
callq 0x34ca6
cmpl $0x0, (%r14)
je 0x2f863
xorl %ecx, %ecx
movq 0x10(%rsp), %r13
movq 0x8(%rsp), %rbp
jmp 0x2f82d
movq 0x10(%rsp), %rdi
movq %rax, %rsi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %r14
movq %r14, %rcx
callq 0x34f7a
movq %rax, %r12
cmpl $0x0, (%r14)
jne 0x2f8b0
movq 0x148(%r12), %rcx
pushq $0x9
popq %rsi
pushq $0x1
popq %r8
movq %r15, %rdi
movq 0x30(%rsp), %rdx
movq 0x20(%rsp), %r14
movq %r14, %r9
callq 0x2a8c1
cmpl $0x0, (%r14)
je 0x2f8be
movq $0x0, 0x18(%rsp)
jmp 0x2f801
movq (%rbx), %rsi
leaq 0x3f000(%rip), %rdx     # 0x6e8c8
movq 0x10(%rsp), %rdi
movq 0x28(%rsp), %rcx
movq 0x20(%rsp), %rbx
movq %rbx, %r8
callq 0x33be8
cmpl $0x0, (%rbx)
jne 0x2f913
movq %r15, %rdi
movq 0x38(%rsp), %rsi
movq 0x20(%rsp), %rbx
movq %rbx, %rdx
callq 0x2b781
cmpl $0x0, (%rbx)
jne 0x2f913
pushq $0x47
popq %rsi
movq %r15, %rdi
movq 0x30(%rsp), %rdx
movq 0x20(%rsp), %rcx
callq 0x2a997
movq $0x0, 0x18(%rsp)
xorl %r8d, %r8d
jmp 0x2f7d2