#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 */
unsigned int RAND_MAX;
unsigned int rand();
int rand_within(int first, int last) {
  unsigned int num;
  int rand_val;
  first = first < 0 ? 0 : first;
  last = RAND_MAX < (unsigned int)last ? RAND_MAX : last;
  num = last - first + 1U;
  rand_val = rand() / ((RAND_MAX + 1U) / num) + first;
  return rand_val;
}


x86 assembly (clang, -O0):
<rand_within>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    1132 <rand_within+0x22>
xor    %eax,%eax
mov    %eax,-0x14(%rbp)
jmp    1138 <rand_within+0x28>
mov    -0x4(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    0x2ea3(%rip),%rax
mov    (%rax),%eax
cmp    -0x8(%rbp),%eax
jae    1161 <rand_within+0x51>
mov    0x2e91(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
jmp    1167 <rand_within+0x57>
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
sub    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1030 <rand@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
xor    %edx,%edx
divl   -0xc(%rbp)
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
xor    %edx,%edx
div    %ecx
add    -0x4(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_within>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebx
xor    %eax,%eax
test   %edi,%edi
cmovs  %eax,%ebx
mov    %esi,%ebp
mov    0x2ec2(%rip),%r14
mov    (%r14),%eax
cmp    %esi,%eax
cmovb  %eax,%ebp
sub    %ebx,%ebp
add    $0x1,%ebp
xor    %eax,%eax
call   1030 <rand@plt>
mov    %eax,%ecx
mov    (%r14),%eax
add    $0x1,%eax
xor    %edx,%edx
div    %ebp
mov    %eax,%esi
mov    %ecx,%eax
xor    %edx,%edx
div    %esi
add    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@RAND_MAX = global i32 0
define i32 @rand_within(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %2
br label %12
10: ; preds = %2
%11 = load i32, i32* %3
br label %12
12: ; preds = %10, %9
%13 = phi i32 [ 0, %9 ], [ %11, %10 ]
store i32 %13, i32* %3
%14 = load i32, i32* @RAND_MAX
%15 = load i32, i32* %4
%16 = icmp ult i32 %14, %15
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = load i32, i32* @RAND_MAX
br label %21
19: ; preds = %12
%20 = load i32, i32* %4
br label %21
21: ; preds = %19, %17
%22 = phi i32 [ %18, %17 ], [ %20, %19 ]
store i32 %22, i32* %4
%23 = load i32, i32* %4
%24 = load i32, i32* %3
%25 = sub nsw i32 %23, %24
%26 = add i32 %25, 1
store i32 %26, i32* %5
%27 = call i32 (...) @rand()
%28 = load i32, i32* @RAND_MAX
%29 = add i32 %28, 1
%30 = load i32, i32* %5
%31 = udiv i32 %29, %30
%32 = udiv i32 %27, %31
%33 = load i32, i32* %3
%34 = add i32 %32, %33
store i32 %34, i32* %6
%35 = load i32, i32* %6
ret i32 %35
}

LLVM IR (-O2):
@RAND_MAX = global i32 0
define i32 @rand_within(i32 %0, i32 %1) {
%3 = icmp sgt i32 %0, 0
%4 = select i1 %3, i32 %0, i32 0
%5 = load i32, i32* @RAND_MAX
%6 = icmp ult i32 %5, %1
%7 = select i1 %6, i32 %5, i32 %1
%8 = sub nsw i32 1, %4
%9 = add i32 %8, %7
%10 = tail call i32 (...) @rand()
%11 = load i32, i32* @RAND_MAX
%12 = add i32 %11, 1
%13 = udiv i32 %12, %9
%14 = udiv i32 %10, %13
%15 = add i32 %14, %4
ret i32 %15
}