#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 */
unsigned long RAND_MAX;
int /*<<< orphan*/ assert(int);
scalar_t__ random();
unsigned long _dgs_randomm_libc(unsigned long n) {
  assert(n < RAND_MAX);
  unsigned long r;
  unsigned long k = RAND_MAX / n;
  do {
    r = (unsigned long)random();
  } while (r >= k * n);
  return r % n;
}


x86 assembly (clang, -O0):
<_dgs_randomm_libc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    0x2eb1(%rip),%rcx
cmp    (%rcx),%rax
setb   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    0x2e9a(%rip),%rax
mov    (%rax),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x8(%rbp)
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <random@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
imul   -0x8(%rbp),%rcx
cmp    %rcx,%rax
jae    115d <_dgs_randomm_libc+0x3d>
mov    -0x10(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x8(%rbp)
mov    %rdx,%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_dgs_randomm_libc>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    0x2eb9(%rip),%rbx
xor    %edi,%edi
cmp    %r14,(%rbx)
seta   %dil
call   1030 <assert@plt>
mov    (%rbx),%rbx
mov    %rbx,%r15
or     %r14,%r15
shr    $0x20,%r15
jmp    1163 <_dgs_randomm_libc+0x43>
nopl   0x0(%rax)
mov    %rbx,%rax
xor    %edx,%edx
div    %r14
mov    %rbx,%rax
sub    %rdx,%rax
cmp    %rax,%rcx
jb     117b <_dgs_randomm_libc+0x5b>
xor    %eax,%eax
call   1040 <random@plt>
mov    %rax,%rcx
test   %r15,%r15
jne    1150 <_dgs_randomm_libc+0x30>
mov    %ebx,%eax
xor    %edx,%edx
div    %r14d
jmp    1158 <_dgs_randomm_libc+0x38>
mov    %rcx,%rax
or     %r14,%rax
shr    $0x20,%rax
je     1194 <_dgs_randomm_libc+0x74>
mov    %rcx,%rax
xor    %edx,%edx
div    %r14
mov    %rdx,%rax
jmp    119d <_dgs_randomm_libc+0x7d>
mov    %ecx,%eax
xor    %edx,%edx
div    %r14d
mov    %edx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@RAND_MAX = global i64 0
define i64 @_dgs_randomm_libc(i64 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
store i64 %0, i64* %2
%5 = load i64, i64* %2
%6 = load i64, i64* @RAND_MAX
%7 = icmp ult i64 %5, %6
%8 = zext i1 %7 to i32
%9 = call i32 @assert(i32 %8)
%10 = load i64, i64* @RAND_MAX
%11 = load i64, i64* %2
%12 = udiv i64 %10, %11
store i64 %12, i64* %4
br label %13
13: ; preds = %15, %1
%14 = call i64 (...) @random()
store i64 %14, i64* %3
br label %15
15: ; preds = %13
%16 = load i64, i64* %3
%17 = load i64, i64* %4
%18 = load i64, i64* %2
%19 = mul i64 %17, %18
%20 = icmp uge i64 %16, %19
br i1 %20, label %13, label %21
21: ; preds = %15
%22 = load i64, i64* %3
%23 = load i64, i64* %2
%24 = urem i64 %22, %23
ret i64 %24
}

LLVM IR (-O2):
@RAND_MAX = global i64 0
define i64 @_dgs_randomm_libc(i64 %0) {
%2 = load i64, i64* @RAND_MAX
%3 = icmp ugt i64 %2, %0
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = load i64, i64* @RAND_MAX
br label %7
7: ; preds = %7, %1
%8 = tail call i64 (...) @random()
%9 = urem i64 %6, %0
%10 = sub i64 %6, %9
%11 = icmp ult i64 %8, %10
br i1 %11, label %12, label %7
12: ; preds = %7
%13 = urem i64 %8, %0
ret i64 %13
}