#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 atoi(char *);
int /*<<< orphan*/ close_rand();
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ init_rand();
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ rand_range(unsigned int, unsigned int);
int /*<<< orphan*/ strcasecmp(char *, char *);
int main(int argc, char **argv) {
  unsigned int min, max;
  if (argc < 2) {
    printf("Usage:\n"
           "       `rand_range [min] max' returns a random\n"
           "        number between `min' (or 0) and `max'\n"
           "        (inclusive)\n"
           "\n"
           "       `rand_range rand' returns a random "
           "        number between 0 and 2^32-1\n");
    exit(1);
  } else if (argc == 2) {
    min = 0;
    if (!strcasecmp(argv[1], "rand"))
      max = 1 << 32 - 1;
    else
      max = atoi(argv[1]);
  } else {
    min = atoi(argv[1]);
    max = atoi(argv[2]);
  }
  init_rand();
  printf("%ld\n", (unsigned int)rand_range(min, max));
  close_rand();
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11a8 <main+0x38>
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpl   $0x2,-0x8(%rbp)
jne    11f7 <main+0x87>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xf00(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%eax
jne    11e2 <main+0x72>
movl   $0x80000000,-0x18(%rbp)
jmp    11f2 <main+0x82>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x18(%rbp)
jmp    1217 <main+0xa7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x18(%rbp)
jmp    121c <main+0xac>
mov    $0x0,%al
call   1060 <init_rand@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1090 <rand_range@plt>
mov    %eax,%esi
lea    0xe96(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <close_rand@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x1,%edi
jg     119f <main+0x1f>
lea    0xe7a(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    %rsi,%rbp
mov    0x8(%rsi),%rbx
cmp    $0x2,%edi
jne    11c8 <main+0x48>
lea    0xe4e(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcasecmp@plt>
mov    $0x80000000,%ebp
xor    %r14d,%r14d
test   %eax,%eax
jne    11d7 <main+0x57>
jmp    11e1 <main+0x61>
mov    %rbx,%rdi
call   1080 <atoi@plt>
mov    %eax,%r14d
mov    0x10(%rbp),%rbx
mov    %rbx,%rdi
call   1080 <atoi@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <init_rand@plt>
mov    %r14d,%edi
mov    %ebp,%esi
call   10a0 <rand_range@plt>
lea    0xe0c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1060 <close_rand@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [200 x i8] c"Usage:\0A `rand_range [min] max' returns a random\0A number between `min' (or 0) and `max'\0A (inclusive)\0A\0A `rand_range rand' returns a random number between 0 and 2^32-1\0A\00"
@.str.1 = [5 x i8] c"rand\00"
@.str.2 = [5 x i8] c"%ld\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 2
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([200 x i8], [200 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = load i32, i32* %4
%15 = icmp eq i32 %14, 2
br i1 %15, label %16, label %29
16: ; preds = %13
store i32 0, i32* %6
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @strcasecmp(i8* %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp ne i32 %20, 0
br i1 %21, label %23, label %22
22: ; preds = %16
store i32 -2147483648, i32* %7
br label %28
23: ; preds = %16
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 @atoi(i8* %26)
store i32 %27, i32* %7
br label %28
28: ; preds = %23, %22
br label %38
29: ; preds = %13
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i32 @atoi(i8* %32)
store i32 %33, i32* %6
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 2
%36 = load i8*, i8** %35
%37 = call i32 @atoi(i8* %36)
store i32 %37, i32* %7
br label %38
38: ; preds = %29, %28
br label %39
39: ; preds = %38
%40 = call i32 (...) @init_rand()
%41 = load i32, i32* %6
%42 = load i32, i32* %7
%43 = call i64 @rand_range(i32 %41, i32 %42)
%44 = trunc i64 %43 to i32
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %44)
%46 = call i32 (...) @close_rand()
%47 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.1 = [5 x i8] c"rand\00"
@.str.2 = [5 x i8] c"%ld\0A\00"
@str = [199 x i8] c"Usage:\0A `rand_range [min] max' returns a random\0A number between `min' (or 0) and `max'\0A (inclusive)\0A\0A `rand_range rand' returns a random number between 0 and 2^32-1\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([199 x i8], [199 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %2
%8 = icmp eq i32 %0, 2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
br i1 %8, label %11, label %14
11: ; preds = %7
%12 = tail call i32 @strcasecmp(i8* %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32 %12, 0
br i1 %13, label %22, label %18
14: ; preds = %7
%15 = tail call i32 @atoi(i8* %10)
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
br label %18
18: ; preds = %11, %14
%19 = phi i8* [ %17, %14 ], [ %10, %11 ]
%20 = phi i32 [ %15, %14 ], [ 0, %11 ]
%21 = tail call i32 @atoi(i8* %19)
br label %22
22: ; preds = %18, %11
%23 = phi i32 [ 0, %11 ], [ %20, %18 ]
%24 = phi i32 [ -2147483648, %11 ], [ %21, %18 ]
%25 = tail call i32 (...) @init_rand()
%26 = tail call i64 @rand_range(i32 %23, i32 %24)
%27 = trunc i64 %26 to i32
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %27)
%29 = tail call i32 (...) @close_rand()
%30 = tail call i32 @exit(i32 0)
unreachable
}