#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 */
struct timespec {
  int tv_nsec;
  long tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ nanosleep(struct timespec *, int /*<<< orphan*/ *);
int rand();
int /*<<< orphan*/ srand(int);
int /*<<< orphan*/ stderr;
int strtol(char *, char **, int);
int main(int argc, char *argv[]) {
  char *end;
  long number;
  struct timespec t;
  errno = 0;
  number = (argc == 2) ? strtol(argv[1], &end, 10) : -1;
  if (errno != 0 || end == argv[1] || number < 1 || number > 3600) {
    fprintf(stderr, "Usage: sleep-rand [1-3600]\n");
    return EXIT_FAILURE;
  }
  clock_gettime(CLOCK_REALTIME, &t);
  srand(t.tv_nsec / 1000);
  t.tv_sec = rand() % number;
  t.tv_nsec = rand() % 1000 * 1000000L;
  nanosleep(&t, NULL);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e5b(%rip),%rax
movq   $0x0,(%rax)
cmpl   $0x2,-0x8(%rbp)
jne    11ac <main+0x4c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x18(%rbp),%rsi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %eax,-0x34(%rbp)
jmp    11b9 <main+0x59>
mov    $0xffffffff,%eax
mov    %eax,-0x34(%rbp)
jmp    11b9 <main+0x59>
mov    -0x34(%rbp),%eax
cltq
mov    %rax,-0x20(%rbp)
mov    0x2e0f(%rip),%rax
cmpq   $0x0,(%rax)
jne    11fe <main+0x9e>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
cmp    0x8(%rcx),%rax
je     11fe <main+0x9e>
cmpq   $0x1,-0x20(%rbp)
jl     11fe <main+0x9e>
cmpq   $0xe10,-0x20(%rbp)
jle    1224 <main+0xc4>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
lea    0xdf2(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1290 <main+0x130>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x30(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   1050 <srand@plt>
mov    $0x0,%al
call   1080 <rand@plt>
cltq
cqto
idivq  -0x20(%rbp)
mov    %rdx,-0x28(%rbp)
mov    $0x0,%al
call   1080 <rand@plt>
mov    $0x3e8,%ecx
cltd
idiv   %ecx
movslq %edx,%rax
imul   $0xf4240,%rax,%rax
mov    %eax,-0x30(%rbp)
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <nanosleep@plt>
mov    0x2d35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2e69(%rip),%rbp
movq   $0x0,0x0(%rbp)
cmp    $0x2,%edi
jne    1199 <main+0x39>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0x18(%rsp),%rsi
mov    $0xa,%edx
call   1070 <strtol@plt>
cmpq   $0x0,0x0(%rbp)
je     11c0 <main+0x60>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
lea    0xe57(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %eax,%r14d
mov    0x18(%rsp),%rax
cmp    0x8(%rbx),%rax
je     1199 <main+0x39>
test   %r14d,%r14d
jle    1199 <main+0x39>
cmp    $0xe11,%r14d
jge    1199 <main+0x39>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rbx
mov    %rbx,%rsi
call   1030 <clock_gettime@plt>
movslq 0x8(%rsp),%rax
imul   $0x10624dd3,%rax,%rdi
mov    %rdi,%rax
shr    $0x3f,%rax
sar    $0x26,%rdi
add    %eax,%edi
call   1050 <srand@plt>
xor    %eax,%eax
call   1080 <rand@plt>
cltd
idiv   %r14d
movslq %edx,%rax
mov    %rax,0x10(%rsp)
xor    %eax,%eax
call   1080 <rand@plt>
cltq
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
imul   $0x3e8,%ecx,%ecx
sub    %ecx,%eax
imul   $0xf4240,%eax,%eax
mov    %eax,0x8(%rsp)
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <nanosleep@plt>
mov    0x2d5d(%rip),%rax
jmp    11b5 <main+0x55>

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@errno = global i64 0
@stderr = global i32 0
@.str = [28 x i8] c"Usage: sleep-rand [1-3600]\0A\00"
@EXIT_FAILURE = global i32 0
@CLOCK_REALTIME = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %struct.timespec
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* @errno
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, 2
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i32 @strtol(i8* %14, i8** %6, i32 10)
br label %17
16: ; preds = %2
br label %17
17: ; preds = %16, %11
%18 = phi i32 [ %15, %11 ], [ -1, %16 ]
%19 = sext i32 %18 to i64
store i64 %19, i64* %7
%20 = load i64, i64* @errno
%21 = icmp ne i64 %20, 0
br i1 %21, label %34, label %22
22: ; preds = %17
%23 = load i8*, i8** %6
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = icmp eq i8* %23, %26
br i1 %27, label %34, label %28
28: ; preds = %22
%29 = load i64, i64* %7
%30 = icmp slt i64 %29, 1
br i1 %30, label %34, label %31
31: ; preds = %28
%32 = load i64, i64* %7
%33 = icmp sgt i64 %32, 3600
br i1 %33, label %34, label %38
34: ; preds = %31, %28, %22, %17
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
store i32 %37, i32* %3
br label %58
38: ; preds = %31
%39 = load i32, i32* @CLOCK_REALTIME
%40 = call i32 @clock_gettime(i32 %39, %struct.timespec* %8)
%41 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
%42 = load i32, i32* %41
%43 = sdiv i32 %42, 1000
%44 = call i32 @srand(i32 %43)
%45 = call i32 (...) @rand()
%46 = sext i32 %45 to i64
%47 = load i64, i64* %7
%48 = srem i64 %46, %47
%49 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 1
store i64 %48, i64* %49
%50 = call i32 (...) @rand()
%51 = srem i32 %50, 1000
%52 = sext i32 %51 to i64
%53 = mul nsw i64 %52, 1000000
%54 = trunc i64 %53 to i32
%55 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
store i32 %54, i32* %55
%56 = call i32 @nanosleep(%struct.timespec* %8, i32* null)
%57 = load i32, i32* @EXIT_SUCCESS
store i32 %57, i32* %3
br label %58
58: ; preds = %38, %34
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@errno = global i64 0
@stderr = global i32 0
@.str = [28 x i8] c"Usage: sleep-rand [1-3600]\0A\00"
@EXIT_FAILURE = global i32 0
@CLOCK_REALTIME = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca %struct.timespec
%5 = bitcast i8** %3 to i8*
%6 = bitcast %struct.timespec* %4 to i8*
store i64 0, i64* @errno
%7 = icmp eq i32 %0, 2
br i1 %7, label %8, label %23
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = call i32 @strtol(i8* %10, i8** %3, i32 10)
%12 = load i64, i64* @errno
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %23
14: ; preds = %8
%15 = load i8*, i8** %3
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = icmp eq i8* %15, %17
%19 = icmp slt i32 %11, 1
%20 = select i1 %18, i1 true, i1 %19
%21 = icmp sgt i32 %11, 3600
%22 = select i1 %20, i1 true, i1 %21
br i1 %22, label %23, label %26
23: ; preds = %2, %14, %8
%24 = load i32, i32* @stderr
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %41
26: ; preds = %14
%27 = load i32, i32* @CLOCK_REALTIME
%28 = call i32 @clock_gettime(i32 %27, %struct.timespec* %4)
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
%30 = load i32, i32* %29
%31 = sdiv i32 %30, 1000
%32 = call i32 @srand(i32 %31)
%33 = call i32 (...) @rand()
%34 = srem i32 %33, %11
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
store i64 %35, i64* %36
%37 = call i32 (...) @rand()
%38 = srem i32 %37, 1000
%39 = mul nsw i32 %38, 1000000
store i32 %39, i32* %29
%40 = call i32 @nanosleep(%struct.timespec* %4, i32* null)
br label %41
41: ; preds = %26, %23
%42 = phi i32* [ @EXIT_FAILURE, %23 ], [ @EXIT_SUCCESS, %26 ]
%43 = load i32, i32* %42
ret i32 %43
}