#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 */
long RAND_MAX;
long random();
long int random_to(long int n) {
  long int result = 0;
  if (n == 0)
    return (0);
  result = random();
  while (result > RAND_MAX - (RAND_MAX % n))
    result = random();
  return (result);
}


x86 assembly (clang, -O0):
<random_to>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    113c <random_to+0x2c>
movq   $0x0,-0x8(%rbp)
jmp    1191 <random_to+0x81>
mov    $0x0,%al
call   1030 <random@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%rcx
mov    0x2e88(%rip),%rax
mov    (%rax),%rax
cqto
idivq  -0x10(%rbp)
mov    -0x20(%rbp),%rax
sub    %rdx,%rcx
cmp    %rcx,%rax
jle    1189 <random_to+0x79>
mov    $0x0,%al
call   1030 <random@plt>
mov    %rax,-0x18(%rbp)
jmp    1147 <random_to+0x37>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_to>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     116a <random_to+0x5a>
mov    %rdi,%rbx
xor    %eax,%eax
call   1030 <random@plt>
mov    %rax,%rcx
mov    0x2ebb(%rip),%r14
nopl   (%rax)
mov    (%r14),%rsi
mov    %rsi,%rax
or     %rbx,%rax
shr    $0x20,%rax
je     1150 <random_to+0x40>
mov    %rsi,%rax
cqto
idiv   %rbx
jmp    1156 <random_to+0x46>
nopl   0x0(%rax)
mov    %esi,%eax
xor    %edx,%edx
div    %ebx
sub    %rdx,%rsi
cmp    %rsi,%rcx
jle    116c <random_to+0x5c>
xor    %eax,%eax
call   1030 <random@plt>
mov    %rax,%rcx
jmp    1130 <random_to+0x20>
xor    %ecx,%ecx
mov    %rcx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@RAND_MAX = global i64 0
define i64 @random_to(i64 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
store i64 %0, i64* %3
store i64 0, i64* %4
%5 = load i64, i64* %3
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %8
7: ; preds = %1
store i64 0, i64* %2
br label %22
8: ; preds = %1
%9 = call i64 (...) @random()
store i64 %9, i64* %4
br label %10
10: ; preds = %18, %8
%11 = load i64, i64* %4
%12 = load i64, i64* @RAND_MAX
%13 = load i64, i64* @RAND_MAX
%14 = load i64, i64* %3
%15 = srem i64 %13, %14
%16 = sub nsw i64 %12, %15
%17 = icmp sgt i64 %11, %16
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = call i64 (...) @random()
store i64 %19, i64* %4
br label %10
20: ; preds = %10
%21 = load i64, i64* %4
store i64 %21, i64* %2
br label %22
22: ; preds = %20, %7
%23 = load i64, i64* %2
ret i64 %23
}

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