#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 randint(int n) {
  if ((n - 1) == RAND_MAX) {
    return rand();
  } else {
    long end = RAND_MAX / n;
    assert(end > 0L);
    end *= n;
    int r;
    while ((r = rand()) >= end)
      ;
    return r % n;
  }
}


x86 assembly (clang, -O0):
<randint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
mov    0x2eb0(%rip),%rcx
cmp    (%rcx),%eax
jne    114f <randint+0x2f>
mov    $0x0,%al
call   1040 <rand@plt>
mov    %eax,-0x4(%rbp)
jmp    11a6 <randint+0x86>
mov    0x2e92(%rip),%rax
mov    (%rax),%eax
cltd
idivl  -0x8(%rbp)
cltq
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
movslq -0x8(%rbp),%rax
imul   -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
mov    %eax,-0x14(%rbp)
cltq
cmp    -0x10(%rbp),%rax
jl     119c <randint+0x7c>
jmp    1181 <randint+0x61>
mov    -0x14(%rbp),%eax
cltd
idivl  -0x8(%rbp)
mov    %edx,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<randint>:
push   %r15
push   %r14
push   %rbx
mov    %edi,%r14d
lea    -0x1(%r14),%ecx
mov    0x2eb5(%rip),%rax
mov    (%rax),%eax
cmp    %eax,%ecx
jne    1145 <randint+0x25>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <rand@plt>
cltd
idiv   %r14d
movslq %eax,%r15
xor    %edi,%edi
test   %r15d,%r15d
setg   %dil
call   1030 <assert@plt>
movslq %r14d,%rbx
imul   %r15,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <rand@plt>
movslq %eax,%rcx
cmp    %rcx,%rbx
jle    1170 <randint+0x50>
cltd
idiv   %r14d
mov    %edx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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