#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 */
typedef int int32_t;

/* Variables and functions */
int RAND_MAX;
int rand();
int32_t getRandom(int32_t highBoundary) {
  int partSize = (highBoundary == RAND_MAX
                      ? 1
                      : 1 + (RAND_MAX - highBoundary) / (highBoundary + 1));
  int maxUsefull = partSize * highBoundary + (partSize - 1);
  int draw;
  do {
    draw = rand();
  } while (draw > maxUsefull);
  return draw / partSize;
}


x86 assembly (clang, -O0):
<getRandom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    0x2ec3(%rip),%rcx
cmp    (%rcx),%eax
jne    113a <getRandom+0x2a>
mov    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1155 <getRandom+0x45>
mov    0x2ea7(%rip),%rax
mov    (%rax),%eax
sub    -0x4(%rbp),%eax
mov    -0x4(%rbp),%ecx
add    $0x1,%ecx
cltd
idiv   %ecx
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
imul   -0x4(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
add    %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1030 <rand@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jg     116d <getRandom+0x5d>
mov    -0x10(%rbp),%eax
cltd
idivl  -0x8(%rbp)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getRandom>:
push   %rbp
push   %rbx
push   %rax
mov    0x2ece(%rip),%rax
mov    (%rax),%eax
mov    $0x1,%ebx
sub    %edi,%eax
je     1130 <getRandom+0x20>
lea    0x1(%rdi),%ecx
cltd
idiv   %ecx
mov    %eax,%ebx
add    $0x1,%ebx
imul   %ebx,%edi
lea    (%rbx,%rdi,1),%ebp
add    $0xffffffff,%ebp
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <rand@plt>
cmp    %ebp,%eax
jg     1140 <getRandom+0x30>
cltd
idiv   %ebx
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@RAND_MAX = global i32 0
define i32 @getRandom(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 = load i32, i32* @RAND_MAX
%8 = icmp eq i32 %6, %7
br i1 %8, label %9, label %10
9: ; preds = %1
br label %18
10: ; preds = %1
%11 = load i32, i32* @RAND_MAX
%12 = load i32, i32* %2
%13 = sub nsw i32 %11, %12
%14 = load i32, i32* %2
%15 = add nsw i32 %14, 1
%16 = sdiv i32 %13, %15
%17 = add nsw i32 1, %16
br label %18
18: ; preds = %10, %9
%19 = phi i32 [ 1, %9 ], [ %17, %10 ]
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = load i32, i32* %2
%22 = mul nsw i32 %20, %21
%23 = load i32, i32* %3
%24 = sub nsw i32 %23, 1
%25 = add nsw i32 %22, %24
store i32 %25, i32* %4
br label %26
26: ; preds = %28, %18
%27 = call i32 (...) @rand()
store i32 %27, i32* %5
br label %28
28: ; preds = %26
%29 = load i32, i32* %5
%30 = load i32, i32* %4
%31 = icmp sgt i32 %29, %30
br i1 %31, label %26, label %32
32: ; preds = %28
%33 = load i32, i32* %5
%34 = load i32, i32* %3
%35 = sdiv i32 %33, %34
ret i32 %35
}

LLVM IR (-O2):
@RAND_MAX = global i32 0
define i32 @getRandom(i32 %0) {
%2 = load i32, i32* @RAND_MAX
%3 = icmp eq i32 %2, %0
br i1 %3, label %9, label %4
4: ; preds = %1
%5 = sub nsw i32 %2, %0
%6 = add nsw i32 %0, 1
%7 = sdiv i32 %5, %6
%8 = add nsw i32 %7, 1
br label %9
9: ; preds = %1, %4
%10 = phi i32 [ %8, %4 ], [ 1, %1 ]
%11 = mul nsw i32 %10, %0
%12 = add nsw i32 %10, -1
%13 = add nsw i32 %12, %11
br label %14
14: ; preds = %14, %9
%15 = tail call i32 (...) @rand()
%16 = icmp sgt i32 %15, %13
br i1 %16, label %14, label %17
17: ; preds = %14
%18 = sdiv i32 %15, %10
ret i32 %18
}