#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;
typedef scalar_t__ uint32_t;

/* Variables and functions */
scalar_t__ UINT32_MAX;
int pcg32_random();
uint32_t __attribute__((no)) ranged_random_recycle_mod(uint32_t range) {
  uint64_t rotations = 0;
  uint64_t random32bit = pcg32_random();
  while (1) {
    uint32_t candidate = (uint32_t)(random32bit) % range;
    if (random32bit - candidate > UINT32_MAX - range + 1) {
      if (rotations == 31) {
        rotations = 0;
        random32bit = pcg32_random();
      } else {
        rotations++;
        random32bit = (random32bit >> 1) | (random32bit << 31);
      }
    } else {
      return candidate;
    }
  }
}


x86 assembly (clang, -O0):
<ranged_random_recycle_mod>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0x0,%al
call   1030 <pcg32_random@plt>
mov    %eax,-0x10(%rbp)
movslq -0x10(%rbp),%rax
cqto
idivq  -0x8(%rbp)
mov    %rdx,-0x18(%rbp)
movslq -0x10(%rbp),%rax
sub    -0x18(%rbp),%rax
mov    0x2eae(%rip),%rcx
mov    (%rcx),%rcx
sub    -0x8(%rbp),%rcx
add    $0x1,%rcx
cmp    %rcx,%rax
jle    119d <ranged_random_recycle_mod+0x8d>
cmpl   $0x1f,-0xc(%rbp)
jne    117e <ranged_random_recycle_mod+0x6e>
movl   $0x0,-0xc(%rbp)
mov    $0x0,%al
call   1030 <pcg32_random@plt>
mov    %eax,-0x10(%rbp)
jmp    1198 <ranged_random_recycle_mod+0x88>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
sar    $0x1,%eax
mov    -0x10(%rbp),%ecx
shl    $0x1f,%ecx
or     %ecx,%eax
mov    %eax,-0x10(%rbp)
jmp    11a7 <ranged_random_recycle_mod+0x97>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret
jmp    112d <ranged_random_recycle_mod+0x1d>

x86 assembly (clang, -O2):
<ranged_random_recycle_mod>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   1030 <pcg32_random@plt>
mov    %eax,%ecx
mov    $0x1,%r14d
sub    %rbx,%r14
movslq %eax,%rsi
mov    %rsi,%rax
or     %rbx,%rax
shr    $0x20,%rax
je     1145 <ranged_random_recycle_mod+0x35>
mov    %rsi,%rax
cqto
idiv   %rbx
jmp    114b <ranged_random_recycle_mod+0x3b>
mov    %ecx,%eax
xor    %edx,%edx
div    %ebx
sub    %rdx,%rsi
mov    0x2ea3(%rip),%r15
mov    (%r15),%rdi
lea    (%r14,%rdi,1),%rax
cmp    %rax,%rsi
jle    11c5 <ranged_random_recycle_mod+0xb5>
xor    %ebp,%ebp
jmp    1184 <ranged_random_recycle_mod+0x74>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rsi,%rax
cqto
idiv   %rbx
sub    %rdx,%rsi
lea    (%r14,%rdi,1),%rax
cmp    %rax,%rsi
jle    11c5 <ranged_random_recycle_mod+0xb5>
cmp    $0x1f,%ebp
je     11a0 <ranged_random_recycle_mod+0x90>
add    $0x1,%ebp
mov    %ecx,%eax
sar    %eax
shl    $0x1f,%ecx
or     %eax,%ecx
jmp    11ae <ranged_random_recycle_mod+0x9e>
nopw   0x0(%rax,%rax,1)
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <pcg32_random@plt>
mov    %eax,%ecx
mov    (%r15),%rdi
movslq %ecx,%rsi
mov    %rsi,%rax
or     %rbx,%rax
shr    $0x20,%rax
jne    1170 <ranged_random_recycle_mod+0x60>
mov    %ecx,%eax
xor    %edx,%edx
div    %ebx
jmp    1178 <ranged_random_recycle_mod+0x68>
mov    %rdx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@UINT32_MAX = global i64 0
define i64 @ranged_random_recycle_mod(i64 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
store i64 %0, i64* %2
store i32 0, i32* %3
%6 = call i32 (...) @pcg32_random()
store i32 %6, i32* %4
br label %7
7: ; preds = %1, %37
%8 = load i32, i32* %4
%9 = sext i32 %8 to i64
%10 = load i64, i64* %2
%11 = srem i64 %9, %10
store i64 %11, i64* %5
%12 = load i32, i32* %4
%13 = sext i32 %12 to i64
%14 = load i64, i64* %5
%15 = sub nsw i64 %13, %14
%16 = load i64, i64* @UINT32_MAX
%17 = load i64, i64* %2
%18 = sub nsw i64 %16, %17
%19 = add nsw i64 %18, 1
%20 = icmp sgt i64 %15, %19
br i1 %20, label %21, label %35
21: ; preds = %7
%22 = load i32, i32* %3
%23 = icmp eq i32 %22, 31
br i1 %23, label %24, label %26
24: ; preds = %21
store i32 0, i32* %3
%25 = call i32 (...) @pcg32_random()
store i32 %25, i32* %4
br label %34
26: ; preds = %21
%27 = load i32, i32* %3
%28 = add nsw i32 %27, 1
store i32 %28, i32* %3
%29 = load i32, i32* %4
%30 = ashr i32 %29, 1
%31 = load i32, i32* %4
%32 = shl i32 %31, 31
%33 = or i32 %30, %32
store i32 %33, i32* %4
br label %34
34: ; preds = %26, %24
br label %37
35: ; preds = %7
%36 = load i64, i64* %5
ret i64 %36
37: ; preds = %34
br label %7
}

LLVM IR (-O2):
@UINT32_MAX = global i64 0
define i64 @ranged_random_recycle_mod(i64 %0) {
%2 = tail call i32 (...) @pcg32_random()
%3 = sub i64 1, %0
%4 = sext i32 %2 to i64
%5 = srem i64 %4, %0
%6 = sub nsw i64 %4, %5
%7 = load i64, i64* @UINT32_MAX
%8 = add i64 %3, %7
%9 = icmp sgt i64 %6, %8
br i1 %9, label %10, label %32
10: ; preds = %1, %23
%11 = phi i64 [ %24, %23 ], [ %7, %1 ]
%12 = phi i32 [ %26, %23 ], [ %2, %1 ]
%13 = phi i32 [ %25, %23 ], [ 0, %1 ]
%14 = icmp eq i32 %13, 31
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = tail call i32 (...) @pcg32_random()
%17 = load i64, i64* @UINT32_MAX
br label %23
18: ; preds = %10
%19 = add nsw i32 %13, 1
%20 = ashr i32 %12, 1
%21 = shl i32 %12, 31
%22 = or i32 %20, %21
br label %23
23: ; preds = %18, %15
%24 = phi i64 [ %17, %15 ], [ %11, %18 ]
%25 = phi i32 [ 0, %15 ], [ %19, %18 ]
%26 = phi i32 [ %16, %15 ], [ %22, %18 ]
%27 = sext i32 %26 to i64
%28 = srem i64 %27, %0
%29 = sub nsw i64 %27, %28
%30 = add i64 %3, %24
%31 = icmp sgt i64 %29, %30
br i1 %31, label %10, label %32
32: ; preds = %23, %1
%33 = phi i64 [ %5, %1 ], [ %28, %23 ]
ret i64 %33
}