#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 RAND_MAX;
int /*<<< orphan*/ assert(int);
int rand();
int random_integer(int m) {
  assert(m >= 0);
  assert(m <= RAND_MAX);
  const int r = (RAND_MAX - m) % (m + 1);
  int value;
  do {
    value = rand();
  } while (value > RAND_MAX - r);
  const int result = value % (m + 1);
  assert(result >= 0);
  assert(result <= m);
  return result;
}


x86 assembly (clang, -O0):
<random_integer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x4(%rbp),%eax
mov    0x2ea2(%rip),%rcx
cmp    (%rcx),%eax
setle  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    0x2e8c(%rip),%rax
mov    (%rax),%eax
sub    -0x4(%rbp),%eax
mov    -0x4(%rbp),%ecx
add    $0x1,%ecx
cltd
idiv   %ecx
mov    %edx,-0x8(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2e67(%rip),%rcx
mov    (%rcx),%ecx
sub    -0x8(%rbp),%ecx
cmp    %ecx,%eax
jg     116d <random_integer+0x4d>
mov    -0xc(%rbp),%eax
mov    -0x4(%rbp),%ecx
add    $0x1,%ecx
cltd
idiv   %ecx
mov    %edx,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x10(%rbp),%eax
cmp    -0x4(%rbp),%eax
setle  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_integer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
not    %edi
shr    $0x1f,%edi
call   1030 <assert@plt>
mov    0x2ead(%rip),%rbx
xor    %edi,%edi
cmp    %r14d,(%rbx)
setge  %dil
call   1030 <assert@plt>
mov    (%rbx),%eax
sub    %r14d,%eax
lea    0x1(%r14),%r15d
cltd
idiv   %r15d
mov    %edx,%ebp
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <rand@plt>
mov    (%rbx),%ecx
sub    %ebp,%ecx
cmp    %ecx,%eax
jg     1160 <random_integer+0x40>
cltd
idiv   %r15d
mov    %edx,%ebp
mov    %edx,%edi
not    %edi
shr    $0x1f,%edi
call   1030 <assert@plt>
xor    %edi,%edi
cmp    %r14d,%ebp
setle  %dil
call   1030 <assert@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@RAND_MAX = global i32 0
define i32 @random_integer(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = icmp sge i32 %6, 0
%8 = zext i1 %7 to i32
%9 = call i32 @assert(i32 %8)
%10 = load i32, i32* %2
%11 = load i32, i32* @RAND_MAX
%12 = icmp sle i32 %10, %11
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i32, i32* @RAND_MAX
%16 = load i32, i32* %2
%17 = sub nsw i32 %15, %16
%18 = load i32, i32* %2
%19 = add nsw i32 %18, 1
%20 = srem i32 %17, %19
store i32 %20, i32* %3
br label %21
21: ; preds = %23, %1
%22 = call i32 (...) @rand()
store i32 %22, i32* %4
br label %23
23: ; preds = %21
%24 = load i32, i32* %4
%25 = load i32, i32* @RAND_MAX
%26 = load i32, i32* %3
%27 = sub nsw i32 %25, %26
%28 = icmp sgt i32 %24, %27
br i1 %28, label %21, label %29
29: ; preds = %23
%30 = load i32, i32* %4
%31 = load i32, i32* %2
%32 = add nsw i32 %31, 1
%33 = srem i32 %30, %32
store i32 %33, i32* %5
%34 = load i32, i32* %5
%35 = icmp sge i32 %34, 0
%36 = zext i1 %35 to i32
%37 = call i32 @assert(i32 %36)
%38 = load i32, i32* %5
%39 = load i32, i32* %2
%40 = icmp sle i32 %38, %39
%41 = zext i1 %40 to i32
%42 = call i32 @assert(i32 %41)
%43 = load i32, i32* %5
ret i32 %43
}

LLVM IR (-O2):
@RAND_MAX = global i32 0
define i32 @random_integer(i32 %0) {
%2 = xor i32 %0, -1
%3 = lshr i32 %2, 31
%4 = tail call i32 @assert(i32 %3)
%5 = load i32, i32* @RAND_MAX
%6 = icmp sge i32 %5, %0
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = load i32, i32* @RAND_MAX
%10 = sub nsw i32 %9, %0
%11 = add nsw i32 %0, 1
%12 = srem i32 %10, %11
br label %13
13: ; preds = %13, %1
%14 = tail call i32 (...) @rand()
%15 = load i32, i32* @RAND_MAX
%16 = sub nsw i32 %15, %12
%17 = icmp sgt i32 %14, %16
br i1 %17, label %13, label %18
18: ; preds = %13
%19 = srem i32 %14, %11
%20 = xor i32 %19, -1
%21 = lshr i32 %20, 31
%22 = tail call i32 @assert(i32 %21)
%23 = icmp sle i32 %19, %0
%24 = zext i1 %23 to i32
%25 = tail call i32 @assert(i32 %24)
ret i32 %19
}