#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
scalar_t__ RAND_MAX;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
long random();
int /*<<< orphan*/ stderr;
int rand_int(int min, int max) {
  unsigned long num_baldes = (unsigned long)max - min + 1;
  if (num_baldes < 1) {
    fprintf(stderr, "Intervalo invalido\n");
    exit(-1);
  }
  unsigned long num_rand = (unsigned long)RAND_MAX + 1;
  unsigned long tam_balde = num_rand / num_baldes;
  unsigned long defeito = num_rand % num_baldes;
  long x;
  do
    x = random();
  while (num_rand - defeito <= (unsigned long)x);
  return x / tam_balde + min;
}


x86 assembly (clang, -O0):
<rand_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movslq -0x8(%rbp),%rax
movslq -0x4(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x1,-0x10(%rbp)
jae    117b <rand_int+0x4b>
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
lea    0xe94(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
mov    0x2e66(%rip),%rax
mov    (%rax),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x10(%rbp)
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x10(%rbp)
mov    %rdx,-0x28(%rbp)
mov    $0x0,%al
call   1040 <random@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
sub    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jbe    11ad <rand_int+0x7d>
mov    -0x30(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x20(%rbp)
movslq -0x4(%rbp),%rcx
add    %rcx,%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_int>:
push   %rbp
push   %r14
push   %rbx
movslq %esi,%rcx
movslq %edi,%rax
sub    %rax,%rcx
inc    %rcx
je     11ab <rand_int+0x7b>
mov    %edi,%r14d
mov    0x2e9c(%rip),%rax
mov    (%rax),%rbx
add    $0x1,%rbx
mov    %rbx,%rax
or     %rcx,%rax
shr    $0x20,%rax
je     116c <rand_int+0x3c>
mov    %rbx,%rax
xor    %edx,%edx
div    %rcx
mov    %rax,%rbp
jmp    1174 <rand_int+0x44>
mov    %ebx,%eax
xor    %edx,%edx
div    %ecx
mov    %eax,%ebp
sub    %rdx,%rbx
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <random@plt>
cmp    %rax,%rbx
jbe    1180 <rand_int+0x50>
mov    %rax,%rcx
or     %rbp,%rcx
shr    $0x20,%rcx
je     119f <rand_int+0x6f>
xor    %edx,%edx
div    %rbp
jmp    11a3 <rand_int+0x73>
xor    %edx,%edx
div    %ebp
add    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
lea    0xe45(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [20 x i8] c"Intervalo invalido\0A\00"
@RAND_MAX = global i64 0
define i32 @rand_int(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %3
store i32 %1, i32* %4
%10 = load i32, i32* %4
%11 = sext i32 %10 to i64
%12 = load i32, i32* %3
%13 = sext i32 %12 to i64
%14 = sub i64 %11, %13
%15 = add i64 %14, 1
store i64 %15, i64* %5
%16 = load i64, i64* %5
%17 = icmp ult i64 %16, 1
br i1 %17, label %18, label %22
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %2
%23 = load i64, i64* @RAND_MAX
%24 = add i64 %23, 1
store i64 %24, i64* %6
%25 = load i64, i64* %6
%26 = load i64, i64* %5
%27 = udiv i64 %25, %26
store i64 %27, i64* %7
%28 = load i64, i64* %6
%29 = load i64, i64* %5
%30 = urem i64 %28, %29
store i64 %30, i64* %8
br label %31
31: ; preds = %33, %22
%32 = call i64 (...) @random()
store i64 %32, i64* %9
br label %33
33: ; preds = %31
%34 = load i64, i64* %6
%35 = load i64, i64* %8
%36 = sub i64 %34, %35
%37 = load i64, i64* %9
%38 = icmp ule i64 %36, %37
br i1 %38, label %31, label %39
39: ; preds = %33
%40 = load i64, i64* %9
%41 = load i64, i64* %7
%42 = udiv i64 %40, %41
%43 = load i32, i32* %3
%44 = sext i32 %43 to i64
%45 = add i64 %42, %44
%46 = trunc i64 %45 to i32
ret i32 %46
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [20 x i8] c"Intervalo invalido\0A\00"
@RAND_MAX = global i64 0
define i32 @rand_int(i32 %0, i32 %1) {
%3 = sext i32 %1 to i64
%4 = sext i32 %0 to i64
%5 = sub nsw i64 %3, %4
%6 = add nsw i64 %5, 1
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %2
%13 = load i64, i64* @RAND_MAX
%14 = add i64 %13, 1
%15 = urem i64 %14, %6
%16 = udiv i64 %14, %6
%17 = sub i64 %14, %15
br label %18
18: ; preds = %18, %12
%19 = tail call i64 (...) @random()
%20 = icmp ugt i64 %17, %19
br i1 %20, label %21, label %18
21: ; preds = %18
%22 = udiv i64 %19, %16
%23 = trunc i64 %22 to i32
%24 = add i32 %23, %0
ret i32 %24
}