#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;
typedef int uint16_t;

/* Variables and functions */
int fast_random(int);
uint32_t fast_irandom(unsigned int max) {
  if (max <= 65536)
    return fast_random(max);
  int himax = (max - 1) / 65536;
  uint16_t hi = fast_random(himax + 1);
  return ((uint32_t)hi << 16) | fast_random(hi < himax ? 65536 : max % 65536);
}


x86 assembly (clang, -O0):
<fast_irandom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x10000,-0x8(%rbp)
ja     1138 <fast_irandom+0x28>
mov    -0x8(%rbp),%edi
call   1030 <fast_random@plt>
mov    %eax,-0x4(%rbp)
jmp    1191 <fast_irandom+0x81>
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
shr    $0x10,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
add    $0x1,%edi
call   1030 <fast_random@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
shl    $0x10,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1174 <fast_irandom+0x64>
mov    $0x10000,%eax
mov    %eax,-0x18(%rbp)
jmp    117f <fast_irandom+0x6f>
mov    -0x8(%rbp),%eax
and    $0xffff,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <fast_random@plt>
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
or     %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fast_irandom>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebx
cmp    $0x10000,%edi
ja     1129 <fast_irandom+0x19>
mov    %ebx,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <fast_random@plt>
lea    -0x1(%rbx),%r14d
shr    $0x10,%r14d
lea    0x1(%r14),%edi
call   1030 <fast_random@plt>
mov    %eax,%ebp
shl    $0x10,%ebp
cmp    %r14d,%eax
movzwl %bx,%eax
mov    $0x10000,%edi
cmovge %eax,%edi
call   1030 <fast_random@plt>
or     %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @fast_irandom(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 = icmp ule i32 %6, 65536
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i32, i32* %3
%10 = call i32 @fast_random(i32 %9)
store i32 %10, i32* %2
br label %31
11: ; preds = %1
%12 = load i32, i32* %3
%13 = sub i32 %12, 1
%14 = udiv i32 %13, 65536
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = add nsw i32 %15, 1
%17 = call i32 @fast_random(i32 %16)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = shl i32 %18, 16
%20 = load i32, i32* %5
%21 = load i32, i32* %4
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %11
br label %27
24: ; preds = %11
%25 = load i32, i32* %3
%26 = urem i32 %25, 65536
br label %27
27: ; preds = %24, %23
%28 = phi i32 [ 65536, %23 ], [ %26, %24 ]
%29 = call i32 @fast_random(i32 %28)
%30 = or i32 %19, %29
store i32 %30, i32* %2
br label %31
31: ; preds = %27, %8
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
define i32 @fast_irandom(i32 %0) {
%2 = icmp ult i32 %0, 65537
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = tail call i32 @fast_random(i32 %0)
br label %16
5: ; preds = %1
%6 = add i32 %0, -1
%7 = lshr i32 %6, 16
%8 = add nuw nsw i32 %7, 1
%9 = tail call i32 @fast_random(i32 %8)
%10 = shl i32 %9, 16
%11 = icmp slt i32 %9, %7
%12 = and i32 %0, 65535
%13 = select i1 %11, i32 65536, i32 %12
%14 = tail call i32 @fast_random(i32 %13)
%15 = or i32 %10, %14
br label %16
16: ; preds = %5, %3
%17 = phi i32 [ %4, %3 ], [ %15, %5 ]
ret i32 %17
}