#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 ranged_random_revmult_lazynopower2(uint32_t range) {
  uint64_t random32bit, multiresult;
  uint32_t leftover;
  uint32_t threshold;
  random32bit = pcg32_random();
  if (range > 0x80000000) {
    while (random32bit >= range) {
      random32bit = pcg32_random();
    }
    return random32bit;
  }
  multiresult = random32bit * range;
  leftover = (uint32_t)multiresult;
  if (leftover < range) {
    if ((range & (range - 1)) == 0) {
      return pcg32_random() & (range - 1);
    }
    threshold = 0xFFFFFFFF % range;
    while (leftover <= threshold) {
      random32bit = pcg32_random();
      multiresult = random32bit * range;
      leftover = (uint32_t)multiresult;
    }
  }
  return multiresult >> 32;
}


x86 assembly (clang, -O0):
<ranged_random_revmult_lazynopower2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    $0x0,%al
call   1030 <pcg32_random@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x80000000,-0x8(%rbp)
jbe    115d <ranged_random_revmult_lazynopower2+0x4d>
jmp    1137 <ranged_random_revmult_lazynopower2+0x27>
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jl     1152 <ranged_random_revmult_lazynopower2+0x42>
mov    $0x0,%al
call   1030 <pcg32_random@plt>
mov    %eax,-0xc(%rbp)
jmp    1137 <ranged_random_revmult_lazynopower2+0x27>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ee <ranged_random_revmult_lazynopower2+0xde>
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    -0x8(%rbp),%eax
jge    11e1 <ranged_random_revmult_lazynopower2+0xd1>
mov    -0x8(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
jne    11a4 <ranged_random_revmult_lazynopower2+0x94>
mov    $0x0,%al
call   1030 <pcg32_random@plt>
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
and    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11ee <ranged_random_revmult_lazynopower2+0xde>
mov    $0xffffffff,%eax
xor    %edx,%edx
divl   -0x8(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jg     11dc <ranged_random_revmult_lazynopower2+0xcc>
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)
jmp    11b1 <ranged_random_revmult_lazynopower2+0xa1>
jmp    11e1 <ranged_random_revmult_lazynopower2+0xd1>
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_lazynopower2>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
xor    %eax,%eax
call   1030 <pcg32_random@plt>
mov    %eax,%ecx
cmp    $0x80000001,%ebx
jb     113f <ranged_random_revmult_lazynopower2+0x2f>
cmp    %ebx,%ecx
jl     117e <ranged_random_revmult_lazynopower2+0x6e>
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <pcg32_random@plt>
cmp    %ebx,%eax
jge    1130 <ranged_random_revmult_lazynopower2+0x20>
mov    %eax,%ecx
jmp    117e <ranged_random_revmult_lazynopower2+0x6e>
imul   %ebx,%ecx
cmp    %ebx,%ecx
jge    117e <ranged_random_revmult_lazynopower2+0x6e>
lea    -0x1(%rbx),%ebp
test   %ebp,%ebx
jne    115a <ranged_random_revmult_lazynopower2+0x4a>
xor    %eax,%eax
call   1030 <pcg32_random@plt>
mov    %eax,%ecx
and    %ebp,%ecx
jmp    117e <ranged_random_revmult_lazynopower2+0x6e>
mov    $0xffffffff,%eax
xor    %edx,%edx
div    %ebx
cmp    %edx,%ecx
jg     117e <ranged_random_revmult_lazynopower2+0x6e>
mov    %edx,%ebp
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <pcg32_random@plt>
imul   %ebx,%eax
cmp    %ebp,%eax
jle    1170 <ranged_random_revmult_lazynopower2+0x60>
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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