#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_lazycheckpower2(uint32_t range) {
  uint64_t random32bit, multiresult;
  uint32_t leftover;
  uint32_t threshold;
  random32bit = pcg32_random();
  if ((range & (range - 1)) == 0) {
    return random32bit & (range - 1);
  }
  if (range > 0x80000000) {
    while (random32bit >= range) {
      random32bit = pcg32_random();
    }
    return random32bit;
  }
  multiresult = random32bit * range;
  leftover = (uint32_t)multiresult;
  if (leftover < range) {
    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_lazycheckpower2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    $0x0,%al
call   1030 <pcg32_random@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
jne    114c <ranged_random_revmult_lazycheckpower2+0x3c>
mov    -0xc(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
and    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11ea <ranged_random_revmult_lazycheckpower2+0xda>
cmpl   $0x80000000,-0x8(%rbp)
jbe    1184 <ranged_random_revmult_lazycheckpower2+0x74>
jmp    115e <ranged_random_revmult_lazycheckpower2+0x4e>
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jl     1179 <ranged_random_revmult_lazycheckpower2+0x69>
mov    $0x0,%al
call   1030 <pcg32_random@plt>
mov    %eax,-0xc(%rbp)
jmp    115e <ranged_random_revmult_lazycheckpower2+0x4e>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ea <ranged_random_revmult_lazycheckpower2+0xda>
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    11dd <ranged_random_revmult_lazycheckpower2+0xcd>
mov    $0xffffffff,%eax
xor    %edx,%edx
divl   -0x8(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jg     11d8 <ranged_random_revmult_lazycheckpower2+0xc8>
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    11ad <ranged_random_revmult_lazycheckpower2+0x9d>
jmp    11dd <ranged_random_revmult_lazycheckpower2+0xcd>
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_lazycheckpower2>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
xor    %eax,%eax
call   1030 <pcg32_random@plt>
mov    %eax,%ecx
lea    -0x1(%rbx),%eax
test   %eax,%ebx
jne    1129 <ranged_random_revmult_lazycheckpower2+0x19>
and    %eax,%ecx
jmp    117e <ranged_random_revmult_lazycheckpower2+0x6e>
cmp    $0x80000001,%ebx
jb     114f <ranged_random_revmult_lazycheckpower2+0x3f>
cmp    %ebx,%ecx
jl     117e <ranged_random_revmult_lazycheckpower2+0x6e>
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1030 <pcg32_random@plt>
cmp    %ebx,%eax
jge    1140 <ranged_random_revmult_lazycheckpower2+0x30>
mov    %eax,%ecx
jmp    117e <ranged_random_revmult_lazycheckpower2+0x6e>
imul   %ebx,%ecx
cmp    %ebx,%ecx
jge    117e <ranged_random_revmult_lazycheckpower2+0x6e>
mov    $0xffffffff,%eax
xor    %edx,%edx
div    %ebx
cmp    %edx,%ecx
jg     117e <ranged_random_revmult_lazycheckpower2+0x6e>
mov    %edx,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1030 <pcg32_random@plt>
imul   %ebx,%eax
cmp    %ebp,%eax
jle    1170 <ranged_random_revmult_lazycheckpower2+0x60>
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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