#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 */
int /*<<< orphan*/ ERROR_APPLICATION_FAILED(char *, char *, char *, int);
int RAND_MAX;
int random();
int rand_lim(int limit) {
  int divisor;
  if (limit < RAND_MAX) {
    divisor = RAND_MAX / (limit + 1);
  } else if (limit == RAND_MAX) {
    divisor = 1;
  } else {
    ERROR_APPLICATION_FAILED("random", "rand_lim", "number", limit);
  }
  int retval;
  do {
    retval = random() / divisor;
  } while (retval > limit);
  return retval;
}


x86 assembly (clang, -O0):
<rand_lim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    0x2eb3(%rip),%rcx
cmp    (%rcx),%eax
jge    1157 <rand_lim+0x37>
mov    0x2ea4(%rip),%rax
mov    (%rax),%eax
mov    -0x4(%rbp),%ecx
add    $0x1,%ecx
cltd
idiv   %ecx
mov    %eax,-0x8(%rbp)
jmp    1197 <rand_lim+0x77>
mov    -0x4(%rbp),%eax
mov    0x2e87(%rip),%rcx
cmp    (%rcx),%eax
jne    1175 <rand_lim+0x55>
movl   $0x1,-0x8(%rbp)
jmp    1192 <rand_lim+0x72>
mov    -0x4(%rbp),%ecx
lea    0xe81(%rip),%rdi
lea    0xe81(%rip),%rsi
lea    0xe83(%rip),%rdx
call   1030 <ERROR_APPLICATION_FAILED@plt>
jmp    1197 <rand_lim+0x77>
jmp    119c <rand_lim+0x7c>
mov    $0x0,%al
call   1040 <random@plt>
cltd
idivl  -0x8(%rbp)
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x4(%rbp),%eax
jg     119c <rand_lim+0x7c>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_lim>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2ebc(%rip),%rax
mov    (%rax),%eax
cmp    %edi,%eax
jle    113c <rand_lim+0x1c>
lea    0x1(%rbx),%ecx
cltd
idiv   %ecx
mov    %eax,%ebp
jmp    1160 <rand_lim+0x40>
mov    $0x1,%ebp
je     1160 <rand_lim+0x40>
lea    0xeb6(%rip),%rdi
lea    0xeb6(%rip),%rsi
lea    0xeb8(%rip),%rdx
mov    %ebx,%ecx
call   1030 <ERROR_APPLICATION_FAILED@plt>
nop
xor    %eax,%eax
call   1040 <random@plt>
cltd
idiv   %ebp
cmp    %ebx,%eax
jg     1160 <rand_lim+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@RAND_MAX = global i32 0
@.str = [7 x i8] c"random\00"
@.str.1 = [9 x i8] c"rand_lim\00"
@.str.2 = [7 x i8] c"number\00"
define i32 @rand_lim(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = load i32, i32* @RAND_MAX
%7 = icmp slt i32 %5, %6
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = load i32, i32* @RAND_MAX
%10 = load i32, i32* %2
%11 = add nsw i32 %10, 1
%12 = sdiv i32 %9, %11
store i32 %12, i32* %3
br label %22
13: ; preds = %1
%14 = load i32, i32* %2
%15 = load i32, i32* @RAND_MAX
%16 = icmp eq i32 %14, %15
br i1 %16, label %17, label %18
17: ; preds = %13
store i32 1, i32* %3
br label %21
18: ; preds = %13
%19 = load i32, i32* %2
%20 = call i32 @ERROR_APPLICATION_FAILED(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %18, %17
br label %22
22: ; preds = %21, %8
br label %23
23: ; preds = %27, %22
%24 = call i32 (...) @random()
%25 = load i32, i32* %3
%26 = sdiv i32 %24, %25
store i32 %26, i32* %4
br label %27
27: ; preds = %23
%28 = load i32, i32* %4
%29 = load i32, i32* %2
%30 = icmp sgt i32 %28, %29
br i1 %30, label %23, label %31
31: ; preds = %27
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
@RAND_MAX = global i32 0
@.str = [7 x i8] c"random\00"
@.str.1 = [9 x i8] c"rand_lim\00"
@.str.2 = [7 x i8] c"number\00"
define i32 @rand_lim(i32 %0) {
%2 = load i32, i32* @RAND_MAX
%3 = icmp sgt i32 %2, %0
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = add nsw i32 %0, 1
%6 = sdiv i32 %2, %5
br label %11
7: ; preds = %1
%8 = icmp eq i32 %2, %0
br i1 %8, label %11, label %9
9: ; preds = %7
%10 = tail call i32 @ERROR_APPLICATION_FAILED(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %0)
br label %11
11: ; preds = %7, %9, %4
%12 = phi i32 [ %6, %4 ], [ undef, %9 ], [ 1, %7 ]
br label %13
13: ; preds = %13, %11
%14 = tail call i32 (...) @random()
%15 = sdiv i32 %14, %12
%16 = icmp sgt i32 %15, %0
br i1 %16, label %13, label %17
17: ; preds = %13
ret i32 %15
}