#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_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 > lsbset - 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_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_mult_lazy+0x4d>
jmp    1137 <ranged_random_mult_lazy+0x27>
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jl     1152 <ranged_random_mult_lazy+0x42>
mov    $0x0,%al
call   1030 <pcg32_random@plt>
mov    %eax,-0xc(%rbp)
jmp    1137 <ranged_random_mult_lazy+0x27>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e3 <ranged_random_mult_lazy+0xd3>
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    -0x1c(%rbp),%ecx
sub    -0x8(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    11d6 <ranged_random_mult_lazy+0xc6>
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    11d1 <ranged_random_mult_lazy+0xc1>
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    11a6 <ranged_random_mult_lazy+0x96>
jmp    11d6 <ranged_random_mult_lazy+0xc6>
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_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_mult_lazy+0x2f>
cmp    %ebx,%ecx
jl     117e <ranged_random_mult_lazy+0x6e>
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <pcg32_random@plt>
cmp    %ebx,%eax
jge    1130 <ranged_random_mult_lazy+0x20>
mov    %eax,%ecx
jmp    117e <ranged_random_mult_lazy+0x6e>
imul   %ebx,%ecx
lea    -0x1(%rbx),%eax
and    %ebx,%eax
not    %eax
cmp    %eax,%ecx
jle    117e <ranged_random_mult_lazy+0x6e>
mov    $0xffffffff,%eax
xor    %edx,%edx
div    %ebx
mov    $0xfffffffe,%ebp
sub    %edx,%ebp
cmp    %ebp,%ecx
jle    117e <ranged_random_mult_lazy+0x6e>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <pcg32_random@plt>
imul   %ebx,%eax
cmp    %ebp,%eax
jg     1170 <ranged_random_mult_lazy+0x60>
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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