#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 uint64_t;

/* Variables and functions */
int rand64();
uint64_t randn(uint64_t n) {
  uint64_t r, m;
  if (n == 0)
    return 0;
  n++;
  if (n == 0)
    return rand64();
  for (;;) {
    r = rand64();
    m = r % n;
    if (r - m <= -n)
      return m;
  }
}


x86 assembly (clang, -O0):
<randn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1131 <randn+0x21>
movl   $0x0,-0x4(%rbp)
jmp    118f <randn+0x7f>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1153 <randn+0x43>
mov    $0x0,%al
call   1030 <rand64@plt>
mov    %eax,-0x4(%rbp)
jmp    118f <randn+0x7f>
jmp    1158 <randn+0x48>
mov    $0x0,%al
call   1030 <rand64@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cltd
idivl  -0x8(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%eax
sub    -0x10(%rbp),%eax
xor    %ecx,%ecx
sub    -0x8(%rbp),%ecx
cmp    %ecx,%eax
jg     118a <randn+0x7a>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    118f <randn+0x7f>
jmp    1158 <randn+0x48>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<randn>:
push   %rbp
push   %rbx
push   %rax
test   %edi,%edi
je     112c <randn+0x1c>
mov    %edi,%ebx
mov    %edi,%ebp
inc    %ebp
jne    1130 <randn+0x20>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <rand64@plt>
xor    %edx,%edx
jmp    1152 <randn+0x42>
not    %ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <rand64@plt>
mov    %eax,%ecx
cltd
idiv   %ebp
sub    %edx,%ecx
cmp    %ebx,%ecx
jg     1140 <randn+0x30>
mov    %edx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @randn(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 0, i32* %2
br label %31
9: ; preds = %1
%10 = load i32, i32* %3
%11 = add nsw i32 %10, 1
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = call i32 (...) @rand64()
store i32 %15, i32* %2
br label %31
16: ; preds = %9
br label %17
17: ; preds = %30, %16
%18 = call i32 (...) @rand64()
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = load i32, i32* %3
%21 = srem i32 %19, %20
store i32 %21, i32* %5
%22 = load i32, i32* %4
%23 = load i32, i32* %5
%24 = sub nsw i32 %22, %23
%25 = load i32, i32* %3
%26 = sub nsw i32 0, %25
%27 = icmp sle i32 %24, %26
br i1 %27, label %28, label %30
28: ; preds = %17
%29 = load i32, i32* %5
store i32 %29, i32* %2
br label %31
30: ; preds = %17
br label %17
31: ; preds = %28, %14, %8
%32 = load i32, i32* %2
ret i32 %32
}

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