#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 uint32_t;

/* Variables and functions */
int /*<<< orphan*/ abort();
int pcg32_random();
uint32_t __attribute__((no)) ranged_random_modgolang(uint32_t range) {
  uint32_t random32bit, maxval;
  if ((range & (range - 1)) == 0) {
    return pcg32_random() & (range - 1);
  }
  if (range >= (1U << 31)) {
    abort();
  }
  maxval = (1U << 31) - 1 - ((1U << 31) % range);
  random32bit = pcg32_random() & 0x7FFFFFFF;
  while (random32bit > maxval) {
    random32bit = pcg32_random() & 0x7FFFFFFF;
  }
  return random32bit % range;
}


x86 assembly (clang, -O0):
<ranged_random_modgolang>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
jne    1156 <ranged_random_modgolang+0x36>
mov    $0x0,%al
call   1040 <pcg32_random@plt>
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
and    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11b7 <ranged_random_modgolang+0x97>
cmpl   $0x80000000,-0x8(%rbp)
jb     116a <ranged_random_modgolang+0x4a>
mov    $0x0,%al
call   1030 <abort@plt>
mov    $0x80000000,%eax
xor    %edx,%edx
divl   -0x8(%rbp)
mov    $0x7fffffff,%eax
sub    %edx,%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1040 <pcg32_random@plt>
and    $0x7fffffff,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jle    11ad <ranged_random_modgolang+0x8d>
mov    $0x0,%al
call   1040 <pcg32_random@plt>
and    $0x7fffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    118d <ranged_random_modgolang+0x6d>
mov    -0xc(%rbp),%eax
cltd
idivl  -0x8(%rbp)
mov    %edx,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ranged_random_modgolang>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
lea    -0x1(%rbx),%ebp
test   %ebp,%edi
jne    1137 <ranged_random_modgolang+0x17>
xor    %eax,%eax
call   1040 <pcg32_random@plt>
and    %ebp,%eax
jmp    1176 <ranged_random_modgolang+0x56>
test   %ebx,%ebx
js     117d <ranged_random_modgolang+0x5d>
mov    $0x80000000,%eax
xor    %edx,%edx
div    %ebx
mov    $0x7fffffff,%ebp
sub    %edx,%ebp
xor    %eax,%eax
call   1040 <pcg32_random@plt>
and    $0x7fffffff,%eax
cmp    %ebp,%eax
jbe    1170 <ranged_random_modgolang+0x50>
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <pcg32_random@plt>
and    $0x7fffffff,%eax
cmp    %ebp,%eax
jg     1160 <ranged_random_modgolang+0x40>
xor    %edx,%edx
idiv   %ebx
mov    %edx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
define i32 @ranged_random_modgolang(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = load i32, i32* %3
%8 = sub nsw i32 %7, 1
%9 = and i32 %6, %8
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = call i32 (...) @pcg32_random()
%13 = load i32, i32* %3
%14 = sub nsw i32 %13, 1
%15 = and i32 %12, %14
store i32 %15, i32* %2
br label %38
16: ; preds = %1
%17 = load i32, i32* %3
%18 = icmp uge i32 %17, -2147483648
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 (...) @abort()
unreachable
21: ; preds = %16
%22 = load i32, i32* %3
%23 = urem i32 -2147483648, %22
%24 = sub i32 2147483647, %23
store i32 %24, i32* %5
%25 = call i32 (...) @pcg32_random()
%26 = and i32 %25, 2147483647
store i32 %26, i32* %4
br label %27
27: ; preds = %31, %21
%28 = load i32, i32* %4
%29 = load i32, i32* %5
%30 = icmp sgt i32 %28, %29
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = call i32 (...) @pcg32_random()
%33 = and i32 %32, 2147483647
store i32 %33, i32* %4
br label %27
34: ; preds = %27
%35 = load i32, i32* %4
%36 = load i32, i32* %3
%37 = srem i32 %35, %36
store i32 %37, i32* %2
br label %38
38: ; preds = %34, %11
%39 = load i32, i32* %2
ret i32 %39
}

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