// File: /workspace/github_c_makefile_star2_O1/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 0x2d00a
movq %rax, 0x28(%rsp)
movq %r15, %rdi
callq 0x2d00a
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 0x2c00f
movq %rbx, 0x20(%rsp)
cmpl $0x0, (%rbx)
movq %rax, %rdx
movl $0x0, %eax
movl $0x0, %r8d
movl $0x0, %ecx
jne 0x3139c
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 0x2c0c2
cmpl $0x0, (%r14)
jne 0x31382
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x2d017
cmpl $0x0, (%r14)
je 0x313d6
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 0x2b4c3
movq %r14, %rdi
callq 0x2b4c3
movq %rbx, %rdi
callq 0x2b4c3
movq 0x18(%rsp), %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2b4c3
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 0x2c0c2
cmpl $0x0, (%r14)
movl $0x0, %eax
movl $0x0, %r8d
movl $0x0, %ecx
movq 0x10(%rsp), %r13
movq 0x8(%rsp), %rbp
jne 0x3139c
movl %ebx, %edi
movq 0x20(%rsp), %r14
movq %r14, %rsi
callq 0x2ba99
movq %rax, %r8
cmpl $0x0, (%r14)
je 0x3144e
movq $0x0, 0x18(%rsp)
xorl %eax, %eax
jmp 0x31390
leaq 0x74d1b(%rip), %rbx     # 0xa6170
movq (%rbx), %rsi
leaq 0x43499(%rip), %rdx     # 0x748f8
movq 0x10(%rsp), %r13
movq %r13, %rdi
movq %r8, %rcx
movq %r14, %r8
callq 0x35cc8
cmpl $0x0, (%r14)
jne 0x314a7
movq %r15, %rdi
movl $0x45, %esi
movq 0x38(%rsp), %rdx
movq %r14, %rcx
callq 0x2c19f
cmpl $0x0, (%r14)
jne 0x314a7
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x2d017
cmpl $0x0, (%r14)
je 0x314bc
movq $0x0, 0x18(%rsp)
xorl %eax, %eax
xorl %r8d, %r8d
xorl %ecx, %ecx
jmp 0x31397
movq %r13, %rdi
movq %r14, %rsi
callq 0x30e97
cmpl $0x0, (%r14)
je 0x314d8
movq $0x0, 0x18(%rsp)
jmp 0x314b2
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
callq 0x362d2
movq %rax, %r8
cmpl $0x0, (%r14)
je 0x3150c
movq $0x0, 0x18(%rsp)
xorl %ecx, %ecx
movq 0x10(%rsp), %r13
movq 0x8(%rsp), %rbp
movq %r12, %rax
jmp 0x3139c
movq %r8, 0x28(%rsp)
movl $0x4, %edi
movq %r14, %rsi
callq 0x2ba99
movq %rax, 0x18(%rsp)
cmpl $0x0, (%r14)
je 0x3152d
xorl %ecx, %ecx
jmp 0x31548
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movq 0x20(%rsp), %r14
movq %r14, %rsi
callq 0x2bb29
movq %rax, %rcx
cmpl $0x0, (%r14)
je 0x3155f
movq 0x10(%rsp), %r13
movq 0x8(%rsp), %rbp
movq %r12, %rax
movq 0x28(%rsp), %r8
jmp 0x3139c
movq 0x10(%rsp), %rdi
movq %r12, %rsi
movq %rcx, %rdx
movq 0x20(%rsp), %r14
movq %r14, %rcx
callq 0x36db8
cmpl $0x0, (%r14)
je 0x3158b
xorl %ecx, %ecx
movq 0x10(%rsp), %r13
movq 0x8(%rsp), %rbp
jmp 0x31555
movq 0x10(%rsp), %rdi
movq %rax, %rsi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %r14
movq %r14, %rcx
callq 0x36fed
movq %rax, %r12
cmpl $0x0, (%r14)
jne 0x315dc
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 0x2c0c2
cmpl $0x0, (%r14)
je 0x315ea
movq $0x0, 0x18(%rsp)
jmp 0x31529
movq (%rbx), %rsi
leaq 0x43304(%rip), %rdx     # 0x748f8
movq 0x10(%rsp), %rdi
movq 0x28(%rsp), %rcx
movq 0x20(%rsp), %rbx
movq %rbx, %r8
callq 0x35cc8
cmpl $0x0, (%rbx)
jne 0x31641
movq %r15, %rdi
movq 0x38(%rsp), %rsi
movq 0x20(%rsp), %rbx
movq %rbx, %rdx
callq 0x2d017
cmpl $0x0, (%rbx)
jne 0x31641
movq %r15, %rdi
movl $0x47, %esi
movq 0x30(%rsp), %rdx
movq 0x20(%rsp), %rcx
callq 0x2c19f
movq $0x0, 0x18(%rsp)
xorl %r8d, %r8d
jmp 0x314f8