#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_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 - 1) {
    if ((range & (range - 1)) == 0) {
      return pcg32_random() & (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_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_mult_lazynopower2+0x4d>
jmp    1137 <ranged_random_mult_lazynopower2+0x27>
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jl     1152 <ranged_random_mult_lazynopower2+0x42>
mov    $0x0,%al
call   1030 <pcg32_random@plt>
mov    %eax,-0xc(%rbp)
jmp    1137 <ranged_random_mult_lazynopower2+0x27>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fc <ranged_random_mult_lazynopower2+0xec>
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    11ef <ranged_random_mult_lazynopower2+0xdf>
mov    -0x8(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
jne    11ab <ranged_random_mult_lazynopower2+0x9b>
mov    $0x0,%al
call   1030 <pcg32_random@plt>
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
and    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11fc <ranged_random_mult_lazynopower2+0xec>
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    11ea <ranged_random_mult_lazynopower2+0xda>
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    11bf <ranged_random_mult_lazynopower2+0xaf>
jmp    11ef <ranged_random_mult_lazynopower2+0xdf>
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_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_mult_lazynopower2+0x2f>
cmp    %ebx,%ecx
jl     118e <ranged_random_mult_lazynopower2+0x7e>
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <pcg32_random@plt>
cmp    %ebx,%eax
jge    1130 <ranged_random_mult_lazynopower2+0x20>
mov    %eax,%ecx
jmp    118e <ranged_random_mult_lazynopower2+0x7e>
imul   %ebx,%ecx
mov    %ebx,%eax
not    %eax
cmp    %eax,%ecx
jle    118e <ranged_random_mult_lazynopower2+0x7e>
lea    -0x1(%rbx),%ebp
test   %ebp,%ebx
jne    115e <ranged_random_mult_lazynopower2+0x4e>
xor    %eax,%eax
call   1030 <pcg32_random@plt>
mov    %eax,%ecx
and    %ebp,%ecx
jmp    118e <ranged_random_mult_lazynopower2+0x7e>
mov    $0xffffffff,%eax
xor    %edx,%edx
div    %ebx
mov    $0xfffffffe,%ebp
sub    %edx,%ebp
cmp    %ebp,%ecx
jle    118e <ranged_random_mult_lazynopower2+0x7e>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <pcg32_random@plt>
imul   %ebx,%eax
cmp    %ebp,%eax
jg     1180 <ranged_random_mult_lazynopower2+0x70>
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @ranged_random_mult_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 %61
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 = sub nsw i32 0, %26
%28 = sub nsw i32 %27, 1
%29 = icmp sgt i32 %25, %28
br i1 %29, label %30, label %58
30: ; preds = %20
%31 = load i32, i32* %3
%32 = load i32, i32* %3
%33 = sub nsw i32 %32, 1
%34 = and i32 %31, %33
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %30
%37 = call i32 (...) @pcg32_random()
%38 = load i32, i32* %3
%39 = sub nsw i32 %38, 1
%40 = and i32 %37, %39
store i32 %40, i32* %2
br label %61
41: ; preds = %30
%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, %20
%59 = load i32, i32* %5
%60 = ashr i32 %59, 32
store i32 %60, i32* %2
br label %61
61: ; preds = %58, %36, %18
%62 = load i32, i32* %2
ret i32 %62
}

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