#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_mult_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 - 1) {
    threshold = 0xFFFFFFFF / range * range - 1;
    while (leftover > threshold) {
      random32bit = pcg32_random();
      multiresult = random32bit * range;
      leftover = (uint32_t)multiresult;
    }
  }
  return multiresult >> 32;
}


x86 assembly (clang, -O0):
<ranged_random_mult_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_mult_lazycheckpower2+0x3c>
mov    -0xc(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
and    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11f8 <ranged_random_mult_lazycheckpower2+0xe8>
cmpl   $0x80000000,-0x8(%rbp)
jbe    1184 <ranged_random_mult_lazycheckpower2+0x74>
jmp    115e <ranged_random_mult_lazycheckpower2+0x4e>
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jl     1179 <ranged_random_mult_lazycheckpower2+0x69>
mov    $0x0,%al
call   1030 <pcg32_random@plt>
mov    %eax,-0xc(%rbp)
jmp    115e <ranged_random_mult_lazycheckpower2+0x4e>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f8 <ranged_random_mult_lazycheckpower2+0xe8>
mov    -0xc(%rbp),%eax
imul   -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
xor    %ecx,%ecx
sub    -0x8(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    11eb <ranged_random_mult_lazycheckpower2+0xdb>
mov    $0xffffffff,%eax
xor    %edx,%edx
divl   -0x8(%rbp)
imul   -0x8(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jle    11e6 <ranged_random_mult_lazycheckpower2+0xd6>
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    11bb <ranged_random_mult_lazycheckpower2+0xab>
jmp    11eb <ranged_random_mult_lazycheckpower2+0xdb>
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_mult_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_mult_lazycheckpower2+0x19>
and    %eax,%ecx
jmp    117e <ranged_random_mult_lazycheckpower2+0x6e>
cmp    $0x80000001,%ebx
jb     114f <ranged_random_mult_lazycheckpower2+0x3f>
cmp    %ebx,%ecx
jl     117e <ranged_random_mult_lazycheckpower2+0x6e>
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1030 <pcg32_random@plt>
cmp    %ebx,%eax
jge    1140 <ranged_random_mult_lazycheckpower2+0x30>
mov    %eax,%ecx
jmp    117e <ranged_random_mult_lazycheckpower2+0x6e>
imul   %ebx,%ecx
mov    %ebx,%eax
not    %eax
cmp    %eax,%ecx
jle    117e <ranged_random_mult_lazycheckpower2+0x6e>
mov    $0xffffffff,%eax
xor    %edx,%edx
div    %ebx
mov    $0xfffffffe,%ebp
sub    %edx,%ebp
cmp    %ebp,%ecx
jle    117e <ranged_random_mult_lazycheckpower2+0x6e>
xchg   %ax,%ax
xor    %eax,%eax
call   1030 <pcg32_random@plt>
imul   %ebx,%eax
cmp    %ebp,%eax
jg     1170 <ranged_random_mult_lazycheckpower2+0x60>
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @ranged_random_mult_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 %61
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 %61
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 = sub nsw i32 0, %37
%39 = sub nsw i32 %38, 1
%40 = icmp sgt i32 %36, %39
br i1 %40, label %41, label %58
41: ; preds = %31
%42 = load i32, i32* %3
%43 = udiv i32 -1, %42
%44 = load i32, i32* %3
%45 = mul i32 %43, %44
%46 = sub i32 %45, 1
store i32 %46, i32* %7
br label %47
47: ; preds = %51, %41
%48 = load i32, i32* %6
%49 = load i32, i32* %7
%50 = icmp sgt i32 %48, %49
br i1 %50, label %51, label %57
51: ; preds = %47
%52 = call i32 (...) @pcg32_random()
store i32 %52, i32* %4
%53 = load i32, i32* %4
%54 = load i32, i32* %3
%55 = mul nsw i32 %53, %54
store i32 %55, i32* %5
%56 = load i32, i32* %5
store i32 %56, i32* %6
br label %47
57: ; preds = %47
br label %58
58: ; preds = %57, %31
%59 = load i32, i32* %5
%60 = ashr i32 %59, 32
store i32 %60, i32* %2
br label %61
61: ; preds = %58, %29, %14
%62 = load i32, i32* %2
ret i32 %62
}

LLVM IR (-O2):
define i32 @ranged_random_mult_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 %27
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 %27, label %12
12: ; preds = %10, %12
%13 = tail call i32 (...) @pcg32_random()
%14 = icmp slt i32 %13, %0
br i1 %14, label %27, label %12
15: ; preds = %8
%16 = mul nsw i32 %2, %0
%17 = xor i32 %0, -1
%18 = icmp sgt i32 %16, %17
br i1 %18, label %19, label %27
19: ; preds = %15
%20 = urem i32 -1, %0
%21 = sub nuw i32 -2, %20
%22 = icmp sgt i32 %16, %21
br i1 %22, label %23, label %27
23: ; preds = %19, %23
%24 = tail call i32 (...) @pcg32_random()
%25 = mul nsw i32 %24, %0
%26 = icmp sgt i32 %25, %21
br i1 %26, label %23, label %27
27: ; preds = %23, %12, %19, %10, %15, %5
%28 = phi i32 [ %7, %5 ], [ poison, %15 ], [ %2, %10 ], [ poison, %19 ], [ %13, %12 ], [ poison, %23 ]
ret i32 %28
}