#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 randn(int n) {
  assert(n <= RAND_MAX && n > 0);
  int v;
  int max = RAND_MAX / n * n - 1;
  while ((v = rand()) > max) {
  }
  return v % n;
}


x86 assembly (clang, -O0):
<randn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%ecx
xor    %eax,%eax
mov    0x2eb1(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0xd(%rbp)
jg     114c <randn+0x2c>
cmpl   $0x0,-0x4(%rbp)
setg   %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    0x2e88(%rip),%rax
mov    (%rax),%eax
cltd
idivl  -0x4(%rbp)
imul   -0x4(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
mov    %eax,-0x8(%rbp)
cmp    -0xc(%rbp),%eax
jle    1188 <randn+0x68>
jmp    1170 <randn+0x50>
mov    -0x8(%rbp),%eax
cltd
idivl  -0x4(%rbp)
mov    %edx,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<randn>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2ebc(%rip),%rbp
cmp    %edi,0x0(%rbp)
setge  %al
test   %edi,%edi
setg   %cl
and    %al,%cl
movzbl %cl,%edi
call   1030 <assert@plt>
mov    0x0(%rbp),%ecx
mov    %ecx,%eax
cltd
idiv   %ebx
mov    %edx,%ebp
not    %ebp
add    %ecx,%ebp
nop
xor    %eax,%eax
call   1040 <rand@plt>
cmp    %ebp,%eax
jg     1150 <randn+0x30>
cltd
idiv   %ebx
mov    %edx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@RAND_MAX = global i32 0
define i32 @randn(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = load i32, i32* @RAND_MAX
%7 = icmp sle i32 %5, %6
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i32, i32* %2
%10 = icmp sgt i32 %9, 0
br label %11
11: ; preds = %8, %1
%12 = phi i1 [ false, %1 ], [ %10, %8 ]
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i32, i32* @RAND_MAX
%16 = load i32, i32* %2
%17 = sdiv i32 %15, %16
%18 = load i32, i32* %2
%19 = mul nsw i32 %17, %18
%20 = sub nsw i32 %19, 1
store i32 %20, i32* %4
br label %21
21: ; preds = %25, %11
%22 = call i32 (...) @rand()
store i32 %22, i32* %3
%23 = load i32, i32* %4
%24 = icmp sgt i32 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %21
br label %21
26: ; preds = %21
%27 = load i32, i32* %3
%28 = load i32, i32* %2
%29 = srem i32 %27, %28
ret i32 %29
}

LLVM IR (-O2):
@RAND_MAX = global i32 0
define i32 @randn(i32 %0) {
%2 = load i32, i32* @RAND_MAX
%3 = icmp sge i32 %2, %0
%4 = icmp sgt i32 %0, 0
%5 = and i1 %4, %3
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = load i32, i32* @RAND_MAX
%9 = srem i32 %8, %0
%10 = xor i32 %9, -1
%11 = add i32 %8, %10
br label %12
12: ; preds = %12, %1
%13 = tail call i32 (...) @rand()
%14 = icmp sgt i32 %13, %11
br i1 %14, label %12, label %15
15: ; preds = %12
%16 = srem i32 %13, %0
ret i32 %16
}