#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 */

/* Variables and functions */
int rand();
int rand32();
int rand_range32(int min, int max) {
  int range = (max - min);
  if (range < 2)
    return min;
  if (range < (1 << 15))
    return min + (rand() % range);
  return min + (rand32() % range);
}


x86 assembly (clang, -O0):
<rand_range32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
sub    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x2,-0x10(%rbp)
jge    114c <rand_range32+0x2c>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1190 <rand_range32+0x70>
cmpl   $0x8000,-0x10(%rbp)
jge    1177 <rand_range32+0x57>
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
cltd
idivl  -0x10(%rbp)
mov    -0x14(%rbp),%eax
add    %edx,%eax
mov    %eax,-0x4(%rbp)
jmp    1190 <rand_range32+0x70>
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <rand32@plt>
cltd
idivl  -0x10(%rbp)
mov    -0x18(%rbp),%eax
add    %edx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_range32>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%ebx
sub    %edi,%ebp
cmp    $0x2,%ebp
jl     1149 <rand_range32+0x29>
xor    %eax,%eax
cmp    $0x7fff,%ebp
ja     113f <rand_range32+0x1f>
call   1040 <rand@plt>
jmp    1144 <rand_range32+0x24>
call   1030 <rand32@plt>
cltd
idiv   %ebp
add    %edx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @rand_range32(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = load i32, i32* %5
%8 = load i32, i32* %4
%9 = sub nsw i32 %7, %8
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = load i32, i32* %4
store i32 %13, i32* %3
br label %29
14: ; preds = %2
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 32768
br i1 %16, label %17, label %23
17: ; preds = %14
%18 = load i32, i32* %4
%19 = call i32 (...) @rand()
%20 = load i32, i32* %6
%21 = srem i32 %19, %20
%22 = add nsw i32 %18, %21
store i32 %22, i32* %3
br label %29
23: ; preds = %14
%24 = load i32, i32* %4
%25 = call i32 (...) @rand32()
%26 = load i32, i32* %6
%27 = srem i32 %25, %26
%28 = add nsw i32 %24, %27
store i32 %28, i32* %3
br label %29
29: ; preds = %23, %17, %12
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
define i32 @rand_range32(i32 %0, i32 %1) {
%3 = sub nsw i32 %1, %0
%4 = icmp slt i32 %3, 2
br i1 %4, label %15, label %5
5: ; preds = %2
%6 = icmp ult i32 %3, 32768
br i1 %6, label %7, label %9
7: ; preds = %5
%8 = tail call i32 (...) @rand()
br label %11
9: ; preds = %5
%10 = tail call i32 (...) @rand32()
br label %11
11: ; preds = %7, %9
%12 = phi i32 [ %10, %9 ], [ %8, %7 ]
%13 = srem i32 %12, %3
%14 = add nsw i32 %13, %0
br label %15
15: ; preds = %11, %2
%16 = phi i32 [ %0, %2 ], [ %14, %11 ]
ret i32 %16
}