// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x8(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x10(%rbp), %rdi
callq 0x39940
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
callq 0x39940
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rax
movq %rax, -0x40(%rbp)
movq $0x4, -0x48(%rbp)
movl $0x0, -0x50(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %r8
movq -0x48(%rbp), %rdx
movq -0x50(%rbp), %rcx
movl $0x3d, %esi
callq 0x38230
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0x40a82
jmp 0x40d16
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %r9
movq -0x58(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
movl $0x43, %esi
callq 0x38360
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0x40aae
jmp 0x40d16
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x39960
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0x40acd
jmp 0x40d16
movl $0x0, -0x50(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %r9
movq -0x40(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x50(%rbp), %r8
movl $0x21, %esi
callq 0x38360
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0x40b00
jmp 0x40d16
movq -0x40(%rbp), %rax
movl %eax, %edi
movq -0x18(%rbp), %rsi
callq 0x37770
movq %rax, -0x70(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0x40b21
jmp 0x40d16
movq -0x8(%rbp), %rdi
leaq 0x93d04(%rip), %rax     # 0xd4830
movq (%rax), %rsi
movq -0x70(%rbp), %rcx
movq -0x18(%rbp), %r8
leaq 0x6ad92(%rip), %rdx     # 0xab8d0
callq 0x47150
movq $0x0, -0x70(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0x40b59
jmp 0x40d16
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rcx
movq -0x38(%rbp), %rdx
movl $0x45, %esi
callq 0x38540
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0x40b7d
jmp 0x40d16
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x39960
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0x40b9c
jmp 0x40d16
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x40390
movq %rax, -0x68(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0x40bbb
jmp 0x40d16
movq -0x68(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x47d20
movq %rax, -0x70(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0x40bda
jmp 0x40d16
movq -0x18(%rbp), %rsi
movl $0x4, %edi
callq 0x37770
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0x40bfa
jmp 0x40d16
movq -0x18(%rbp), %rsi
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
callq 0x37800
movq %rax, -0x78(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0x40c1a
jmp 0x40d16
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x78(%rbp), %rdx
movq -0x18(%rbp), %rcx
callq 0x49090
movq %rax, -0x68(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0x40c49
jmp 0x40d16
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq -0x18(%rbp), %rcx
callq 0x493f0
movq %rax, -0x68(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0x40c78
jmp 0x40d16
movl $0x1, -0x50(%rbp)
movq -0x10(%rbp), %rdi
movq -0x68(%rbp), %rax
movq -0x18(%rbp), %r9
movq -0x40(%rbp), %rdx
movq 0x148(%rax), %rcx
movq -0x50(%rbp), %r8
movl $0x9, %esi
callq 0x38360
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0x40caf
jmp 0x40d16
movq -0x8(%rbp), %rdi
leaq 0x93b76(%rip), %rax     # 0xd4830
movq (%rax), %rsi
movq -0x70(%rbp), %rcx
movq -0x18(%rbp), %r8
leaq 0x6ac04(%rip), %rdx     # 0xab8d0
callq 0x47150
movq $0x0, -0x70(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0x40ce4
jmp 0x40d16
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x39960
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0x40d00
jmp 0x40d16
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rcx
movq -0x40(%rbp), %rdx
movl $0x47, %esi
callq 0x38540
movq -0x20(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x78(%rbp), %rdi
callq 0x36ee0
movq -0x70(%rbp), %rdi
callq 0x36ee0
movq -0x68(%rbp), %rdi
callq 0x36ee0
movq -0x60(%rbp), %rdi
callq 0x36ee0
addq $0x80, %rsp
popq %rbp
retq
nop