#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_lazy(uint32_t range) {
  uint64_t random32bit, multiresult;
  uint32_t leftover;
  uint32_t threshold;
  uint32_t lsbset;
  random32bit = pcg32_random();
  if (range > 0x80000000) {
    while (random32bit >= range) {
      random32bit = pcg32_random();
    }
    return random32bit;
  }
  lsbset = range & (~(range - 1));
  multiresult = random32bit * range;
  leftover = (uint32_t)multiresult;
  if (leftover < range - lsbset) {
    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_lazy>:
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_lazy+0x4d>
jmp    1137 <ranged_random_revmult_lazy+0x27>
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jl     1152 <ranged_random_revmult_lazy+0x42>
mov    $0x0,%al
call   1030 <pcg32_random@plt>
mov    %eax,-0xc(%rbp)
jmp    1137 <ranged_random_revmult_lazy+0x27>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d9 <ranged_random_revmult_lazy+0xc9>
mov    -0x8(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    %eax,-0x1c(%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
mov    -0x8(%rbp),%ecx
sub    -0x1c(%rbp),%ecx
cmp    %ecx,%eax
jge    11cc <ranged_random_revmult_lazy+0xbc>
mov    $0xffffffff,%eax
xor    %edx,%edx
divl   -0x8(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jg     11c7 <ranged_random_revmult_lazy+0xb7>
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    119c <ranged_random_revmult_lazy+0x8c>
jmp    11cc <ranged_random_revmult_lazy+0xbc>
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_lazy>:
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_lazy+0x2f>
cmp    %ebx,%ecx
jl     116e <ranged_random_revmult_lazy+0x5e>
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <pcg32_random@plt>
cmp    %ebx,%eax
jge    1130 <ranged_random_revmult_lazy+0x20>
mov    %eax,%ecx
jmp    116e <ranged_random_revmult_lazy+0x5e>
imul   %ebx,%ecx
lea    -0x1(%rbx),%eax
and    %ebx,%eax
cmp    %eax,%ecx
jge    116e <ranged_random_revmult_lazy+0x5e>
mov    $0xffffffff,%eax
xor    %edx,%edx
div    %ebx
cmp    %edx,%ecx
jg     116e <ranged_random_revmult_lazy+0x5e>
mov    %edx,%ebp
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <pcg32_random@plt>
imul   %ebx,%eax
cmp    %ebp,%eax
jle    1160 <ranged_random_revmult_lazy+0x50>
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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