#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 int uint32_t;

/* Variables and functions */
int pcg32_random();
uint32_t __attribute__((no)) ranged_random_revmult(uint32_t range) {
  uint64_t random32bit, multiresult;
  uint32_t leftover, threshold;
  if ((range & (range - 1)) == 0)
    return pcg32_random() & (range - 1);
  if (range > 0x80000000) {
    while (random32bit >= range) {
      random32bit = pcg32_random();
    }
    return random32bit;
  }
  threshold = 0xFFFFFFFF % range;
  do {
    random32bit = pcg32_random();
    multiresult = random32bit * range;
    leftover = (uint32_t)multiresult;
  } while (leftover <= threshold);
  return multiresult >> 32;
}


x86 assembly (clang, -O0):
<ranged_random_revmult>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
jne    1146 <ranged_random_revmult+0x36>
mov    $0x0,%al
call   1030 <pcg32_random@plt>
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
and    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11be <ranged_random_revmult+0xae>
cmpl   $0x80000000,-0x8(%rbp)
jbe    117e <ranged_random_revmult+0x6e>
jmp    1158 <ranged_random_revmult+0x48>
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jl     1173 <ranged_random_revmult+0x63>
mov    $0x0,%al
call   1030 <pcg32_random@plt>
mov    %eax,-0xc(%rbp)
jmp    1158 <ranged_random_revmult+0x48>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11be <ranged_random_revmult+0xae>
mov    $0xffffffff,%eax
xor    %edx,%edx
divl   -0x8(%rbp)
mov    %edx,-0x18(%rbp)
mov    $0x0,%al
call   1030 <pcg32_random@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
imul   -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jle    118b <ranged_random_revmult+0x7b>
mov    -0x10(%rbp),%eax
mov    $0x20,%ecx
sar    %cl,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ranged_random_revmult>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
lea    -0x1(%rbx),%ebp
test   %ebp,%edi
jne    1127 <ranged_random_revmult+0x17>
xor    %eax,%eax
call   1030 <pcg32_random@plt>
and    %ebp,%eax
jmp    1160 <ranged_random_revmult+0x50>
cmp    $0x80000001,%ebx
jb     113d <ranged_random_revmult+0x2d>
nop
xor    %eax,%eax
call   1030 <pcg32_random@plt>
cmp    %ebx,%eax
jge    1130 <ranged_random_revmult+0x20>
jmp    1160 <ranged_random_revmult+0x50>
mov    $0xffffffff,%eax
xor    %edx,%edx
div    %ebx
mov    %edx,%ebp
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <pcg32_random@plt>
mov    %eax,%ecx
imul   %ebx,%ecx
cmp    %ebp,%ecx
jle    1150 <ranged_random_revmult+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @ranged_random_revmult(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
%8 = load i32, i32* %3
%9 = load i32, i32* %3
%10 = sub nsw i32 %9, 1
%11 = and i32 %8, %10
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %1
%14 = call i32 (...) @pcg32_random()
%15 = load i32, i32* %3
%16 = sub nsw i32 %15, 1
%17 = and i32 %14, %16
store i32 %17, i32* %2
br label %46
18: ; preds = %1
%19 = load i32, i32* %3
%20 = icmp ugt i32 %19, -2147483648
br i1 %20, label %21, label %30
21: ; preds = %18
br label %22
22: ; preds = %26, %21
%23 = load i32, i32* %4
%24 = load i32, i32* %3
%25 = icmp sge i32 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 (...) @pcg32_random()
store i32 %27, i32* %4
br label %22
28: ; preds = %22
%29 = load i32, i32* %4
store i32 %29, i32* %2
br label %46
30: ; preds = %18
%31 = load i32, i32* %3
%32 = urem i32 -1, %31
store i32 %32, i32* %7
br label %33
33: ; preds = %39, %30
%34 = call i32 (...) @pcg32_random()
store i32 %34, i32* %4
%35 = load i32, i32* %4
%36 = load i32, i32* %3
%37 = mul nsw i32 %35, %36
store i32 %37, i32* %5
%38 = load i32, i32* %5
store i32 %38, i32* %6
br label %39
39: ; preds = %33
%40 = load i32, i32* %6
%41 = load i32, i32* %7
%42 = icmp sle i32 %40, %41
br i1 %42, label %33, label %43
43: ; preds = %39
%44 = load i32, i32* %5
%45 = ashr i32 %44, 32
store i32 %45, i32* %2
br label %46
46: ; preds = %43, %28, %13
%47 = load i32, i32* %2
ret i32 %47
}

LLVM IR (-O2):
define i32 @ranged_random_revmult(i32 %0) {
%2 = tail call i32 @llvm.ctpop.i32(i32 %0)
%3 = icmp ult i32 %2, 2
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = add nsw i32 %0, -1
%6 = tail call i32 (...) @pcg32_random()
%7 = and i32 %6, %5
br label %19
8: ; preds = %1
%9 = icmp ugt i32 %0, -2147483648
br i1 %9, label %10, label %13
10: ; preds = %8, %10
%11 = tail call i32 (...) @pcg32_random()
%12 = icmp slt i32 %11, %0
br i1 %12, label %19, label %10
13: ; preds = %8
%14 = urem i32 -1, %0
br label %15
15: ; preds = %15, %13
%16 = tail call i32 (...) @pcg32_random()
%17 = mul nsw i32 %16, %0
%18 = icmp sgt i32 %17, %14
br i1 %18, label %19, label %15
19: ; preds = %15, %10, %4
%20 = phi i32 [ %7, %4 ], [ %11, %10 ], [ poison, %15 ]
ret i32 %20
}