#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 lrand48();
unsigned long int gsl_rng_uniform_int(unsigned long int n) {
  unsigned long int range = 1U << 30U;
  unsigned long int scale;
  unsigned long int k;
  if (n > range || n == 0) {
    return 0;
  }
  scale = range / n;
  do {
    k = lrand48() / scale;
  } while (k >= n);
  return k;
}


x86 assembly (clang, -O0):
<gsl_rng_uniform_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x40000000,-0x18(%rbp)
40
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
ja     113d <gsl_rng_uniform_int+0x2d>
cmpq   $0x0,-0x10(%rbp)
jne    114a <gsl_rng_uniform_int+0x3a>
movq   $0x0,-0x8(%rbp)
jmp    1183 <gsl_rng_uniform_int+0x73>
mov    -0x18(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x10(%rbp)
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <lrand48@plt>
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x20(%rbp)
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    115a <gsl_rng_uniform_int+0x4a>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gsl_rng_uniform_int>:
push   %r14
push   %rbx
push   %rax
lea    -0x40000001(%rdi),%rax
cmp    $0xffffffffc0000000,%rax
jae    1127 <gsl_rng_uniform_int+0x17>
xor    %eax,%eax
jmp    1163 <gsl_rng_uniform_int+0x53>
mov    %rdi,%r14
mov    $0x40000000,%eax
xor    %edx,%edx
div    %r14d
mov    %eax,%ebx
jmp    114a <gsl_rng_uniform_int+0x3a>
nopl   0x0(%rax,%rax,1)
xor    %edx,%edx
div    %rbx
cmp    %r14,%rax
jb     1163 <gsl_rng_uniform_int+0x53>
xor    %eax,%eax
call   1030 <lrand48@plt>
mov    %rax,%rcx
shr    $0x20,%rcx
jne    1140 <gsl_rng_uniform_int+0x30>
xor    %edx,%edx
div    %ebx
cmp    %r14,%rax
jae    114a <gsl_rng_uniform_int+0x3a>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i64 @gsl_rng_uniform_int(i64 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
store i64 %0, i64* %3
store i64 1073741824, i64* %4
%7 = load i64, i64* %3
%8 = load i64, i64* %4
%9 = icmp ugt i64 %7, %8
br i1 %9, label %13, label %10
10: ; preds = %1
%11 = load i64, i64* %3
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %10, %1
store i64 0, i64* %2
br label %28
14: ; preds = %10
%15 = load i64, i64* %4
%16 = load i64, i64* %3
%17 = udiv i64 %15, %16
store i64 %17, i64* %5
br label %18
18: ; preds = %22, %14
%19 = call i64 (...) @lrand48()
%20 = load i64, i64* %5
%21 = udiv i64 %19, %20
store i64 %21, i64* %6
br label %22
22: ; preds = %18
%23 = load i64, i64* %6
%24 = load i64, i64* %3
%25 = icmp uge i64 %23, %24
br i1 %25, label %18, label %26
26: ; preds = %22
%27 = load i64, i64* %6
store i64 %27, i64* %2
br label %28
28: ; preds = %26, %13
%29 = load i64, i64* %2
ret i64 %29
}

LLVM IR (-O2):
define i64 @gsl_rng_uniform_int(i64 %0) {
%2 = add i64 %0, -1073741825
%3 = icmp ult i64 %2, -1073741824
br i1 %3, label %12, label %4
4: ; preds = %1
%5 = trunc i64 %0 to i32
%6 = udiv i32 1073741824, %5
%7 = zext i32 %6 to i64
br label %8
8: ; preds = %8, %4
%9 = tail call i64 (...) @lrand48()
%10 = udiv i64 %9, %7
%11 = icmp ult i64 %10, %0
br i1 %11, label %12, label %8
12: ; preds = %8, %1
%13 = phi i64 [ 0, %1 ], [ %10, %8 ]
ret i64 %13
}