#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 dummy;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ discrete_elapsed_hr(struct timespec *, struct timespec *,
                                       unsigned int *, char *);
int /*<<< orphan*/ free(struct timespec *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(struct timespec *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sleep(int);
int sleep_sleep(unsigned int duration, unsigned int iter) {
  struct timespec start, end;
  int i = 0;
  size_t sz = sizeof(struct timespec);
  struct timespec *overheads = NULL;
  overheads = (struct timespec *)malloc(2 * 1000 * sz);
  memset(overheads, 0, 2 * 1000 * sz);
  if (overheads == NULL) {
    perror("Sleep::sleep overheads allocation failure.");
  }
  clock_gettime(CLOCK_MONOTONIC, &start);
  for (i = 0; i < 1000; i++) {
    clock_gettime(CLOCK_MONOTONIC, &overheads[2 * i]);
    clock_gettime(CLOCK_MONOTONIC, &overheads[2 * i + 1]);
  }
  struct timespec *results = NULL;
  results = (struct timespec *)malloc(2 * iter * sz);
  memset(results, 0, 2 * iter * sz);
  if (results == NULL) {
    perror("Sleep::sleep results allocation failure.");
  }
  for (i = 0; i < iter; i++) {
    clock_gettime(CLOCK_MONOTONIC, &results[2 * i]);
    sleep(1 * duration);
    clock_gettime(CLOCK_MONOTONIC, &results[2 * i + 1]);
  }
  discrete_elapsed_hr(overheads, results, &iter, "Sleep::sleep");
  free(results);
  free(overheads);
  return 0;
}


x86 assembly (clang, -O0):
<sleep_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x4,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
imul   $0x7d0,-0x28(%rbp),%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
imul   $0x7d0,-0x28(%rbp),%rax
mov    %eax,%edx
xor    %esi,%esi
call   1050 <memset@plt>
cmpq   $0x0,-0x30(%rbp)
jne    11d4 <sleep_sleep+0x64>
lea    0xe31(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1040 <clock_gettime@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x3e8,-0x1c(%rbp)
jge    124d <sleep_sleep+0xdd>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rsi
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
call   1040 <clock_gettime@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rsi
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
add    $0x1,%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
call   1040 <clock_gettime@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ed <sleep_sleep+0x7d>
movq   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%eax
shl    $0x1,%eax
mov    %eax,%eax
imul   -0x28(%rbp),%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%eax
shl    $0x1,%eax
mov    %eax,%eax
imul   -0x28(%rbp),%rax
mov    %eax,%edx
xor    %esi,%esi
call   1050 <memset@plt>
cmpq   $0x0,-0x38(%rbp)
jne    129e <sleep_sleep+0x12e>
lea    0xd92(%rip),%rdi
call   1070 <perror@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jae    130f <sleep_sleep+0x19f>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rsi
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
call   1040 <clock_gettime@plt>
mov    -0x4(%rbp),%edi
shl    $0x0,%edi
call   1090 <sleep@plt>
mov    0x2d04(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rsi
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
add    $0x1,%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
call   1040 <clock_gettime@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    12a5 <sleep_sleep+0x135>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
lea    -0x8(%rbp),%rdx
lea    0xd32(%rip),%rcx
call   1080 <discrete_elapsed_hr@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sleep_sleep>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %edi,%r15d
mov    %esi,0x4(%rsp)
mov    $0x1f40,%edi
call   1060 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x1f40,%edx
call   1050 <memset@plt>
test   %r13,%r13
je     12aa <sleep_sleep+0x13a>
mov    0x2e34(%rip),%r12
mov    (%r12),%edi
lea    0x10(%rsp),%rsi
call   1040 <clock_gettime@plt>
lea    0x4(%r13),%rbp
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    (%r12),%edi
lea    (%rbx,%r13,1),%rsi
call   1040 <clock_gettime@plt>
mov    (%r12),%edi
lea    (%rbx,%rbp,1),%rsi
call   1040 <clock_gettime@plt>
add    $0x8,%rbx
cmp    $0x1f40,%rbx
jne    11d0 <sleep_sleep+0x60>
lea    0x0(,%r14,8),%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1050 <memset@plt>
mov    %rbx,0x8(%rsp)
test   %rbx,%rbx
je     12bb <sleep_sleep+0x14b>
test   %r14d,%r14d
je     126d <sleep_sleep+0xfd>
mov    %r15d,%ebx
mov    %r14d,%r14d
xor    %r15d,%r15d
mov    0x8(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
mov    (%r12),%edi
mov    %rbp,%rsi
call   1040 <clock_gettime@plt>
mov    %ebx,%edi
call   1090 <sleep@plt>
mov    (%r12),%edi
lea    0x4(%rbp),%rsi
call   1040 <clock_gettime@plt>
add    $0x1,%r15
add    $0x8,%rbp
cmp    %r14,%r15
jb     1240 <sleep_sleep+0xd0>
lea    0xde0(%rip),%rcx
lea    0x4(%rsp),%rdx
mov    %r13,%rdi
mov    0x8(%rsp),%rbx
mov    %rbx,%rsi
call   1080 <discrete_elapsed_hr@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd4f(%rip),%rdi
call   1070 <perror@plt>
jmp    11ad <sleep_sleep+0x3d>
lea    0xd69(%rip),%rdi
call   1070 <perror@plt>
test   %r14d,%r14d
jne    1228 <sleep_sleep+0xb8>
jmp    126d <sleep_sleep+0xfd>

LLVM IR (-O0):
%struct.timespec = type { i32 }
@.str = [43 x i8] c"Sleep::sleep overheads allocation failure.\00"
@CLOCK_MONOTONIC = global i32 0
@.str.1 = [41 x i8] c"Sleep::sleep results allocation failure.\00"
@.str.2 = [13 x i8] c"Sleep::sleep\00"
define i32 @sleep_sleep(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timespec
%6 = alloca %struct.timespec
%7 = alloca i32
%8 = alloca i64
%9 = alloca %struct.timespec*
%10 = alloca %struct.timespec*
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %7
store i64 4, i64* %8
store %struct.timespec* null, %struct.timespec** %9
%11 = load i64, i64* %8
%12 = mul i64 2000, %11
%13 = trunc i64 %12 to i32
%14 = call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to %struct.timespec*
store %struct.timespec* %15, %struct.timespec** %9
%16 = load %struct.timespec*, %struct.timespec** %9
%17 = load i64, i64* %8
%18 = mul i64 2000, %17
%19 = trunc i64 %18 to i32
%20 = call i32 @memset(%struct.timespec* %16, i32 0, i32 %19)
%21 = load %struct.timespec*, %struct.timespec** %9
%22 = icmp eq %struct.timespec* %21, null
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %23, %2
%26 = load i32, i32* @CLOCK_MONOTONIC
%27 = call i32 @clock_gettime(i32 %26, %struct.timespec* %5)
store i32 0, i32* %7
br label %28
28: ; preds = %47, %25
%29 = load i32, i32* %7
%30 = icmp slt i32 %29, 1000
br i1 %30, label %31, label %50
31: ; preds = %28
%32 = load i32, i32* @CLOCK_MONOTONIC
%33 = load %struct.timespec*, %struct.timespec** %9
%34 = load i32, i32* %7
%35 = mul nsw i32 2, %34
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds %struct.timespec, %struct.timespec* %33, i64 %36
%38 = call i32 @clock_gettime(i32 %32, %struct.timespec* %37)
%39 = load i32, i32* @CLOCK_MONOTONIC
%40 = load %struct.timespec*, %struct.timespec** %9
%41 = load i32, i32* %7
%42 = mul nsw i32 2, %41
%43 = add nsw i32 %42, 1
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds %struct.timespec, %struct.timespec* %40, i64 %44
%46 = call i32 @clock_gettime(i32 %39, %struct.timespec* %45)
br label %47
47: ; preds = %31
%48 = load i32, i32* %7
%49 = add nsw i32 %48, 1
store i32 %49, i32* %7
br label %28
50: ; preds = %28
store %struct.timespec* null, %struct.timespec** %10
%51 = load i32, i32* %4
%52 = mul i32 2, %51
%53 = zext i32 %52 to i64
%54 = load i64, i64* %8
%55 = mul i64 %53, %54
%56 = trunc i64 %55 to i32
%57 = call i64 @malloc(i32 %56)
%58 = inttoptr i64 %57 to %struct.timespec*
store %struct.timespec* %58, %struct.timespec** %10
%59 = load %struct.timespec*, %struct.timespec** %10
%60 = load i32, i32* %4
%61 = mul i32 2, %60
%62 = zext i32 %61 to i64
%63 = load i64, i64* %8
%64 = mul i64 %62, %63
%65 = trunc i64 %64 to i32
%66 = call i32 @memset(%struct.timespec* %59, i32 0, i32 %65)
%67 = load %struct.timespec*, %struct.timespec** %10
%68 = icmp eq %struct.timespec* %67, null
br i1 %68, label %69, label %71
69: ; preds = %50
%70 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
br label %71
71: ; preds = %69, %50
store i32 0, i32* %7
br label %72
72: ; preds = %95, %71
%73 = load i32, i32* %7
%74 = load i32, i32* %4
%75 = icmp ult i32 %73, %74
br i1 %75, label %76, label %98
76: ; preds = %72
%77 = load i32, i32* @CLOCK_MONOTONIC
%78 = load %struct.timespec*, %struct.timespec** %10
%79 = load i32, i32* %7
%80 = mul nsw i32 2, %79
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds %struct.timespec, %struct.timespec* %78, i64 %81
%83 = call i32 @clock_gettime(i32 %77, %struct.timespec* %82)
%84 = load i32, i32* %3
%85 = mul i32 1, %84
%86 = call i32 @sleep(i32 %85)
%87 = load i32, i32* @CLOCK_MONOTONIC
%88 = load %struct.timespec*, %struct.timespec** %10
%89 = load i32, i32* %7
%90 = mul nsw i32 2, %89
%91 = add nsw i32 %90, 1
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds %struct.timespec, %struct.timespec* %88, i64 %92
%94 = call i32 @clock_gettime(i32 %87, %struct.timespec* %93)
br label %95
95: ; preds = %76
%96 = load i32, i32* %7
%97 = add nsw i32 %96, 1
store i32 %97, i32* %7
br label %72
98: ; preds = %72
%99 = load %struct.timespec*, %struct.timespec** %9
%100 = load %struct.timespec*, %struct.timespec** %10
%101 = call i32 @discrete_elapsed_hr(%struct.timespec* %99, %struct.timespec* %100, i32* %4, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%102 = load %struct.timespec*, %struct.timespec** %10
%103 = call i32 @free(%struct.timespec* %102)
%104 = load %struct.timespec*, %struct.timespec** %9
%105 = call i32 @free(%struct.timespec* %104)
ret i32 0
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@.str = [43 x i8] c"Sleep::sleep overheads allocation failure.\00"
@CLOCK_MONOTONIC = global i32 0
@.str.1 = [41 x i8] c"Sleep::sleep results allocation failure.\00"
@.str.2 = [13 x i8] c"Sleep::sleep\00"
define i32 @sleep_sleep(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.timespec
store i32 %1, i32* %3
%5 = bitcast %struct.timespec* %4 to i8*
%6 = tail call i64 @malloc(i32 8000)
%7 = inttoptr i64 %6 to %struct.timespec*
%8 = tail call i32 @memset(%struct.timespec* %7, i32 0, i32 8000)
%9 = icmp eq i64 %6, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
%13 = load i32, i32* @CLOCK_MONOTONIC
%14 = call i32 @clock_gettime(i32 %13, %struct.timespec* %4)
br label %15
15: ; preds = %12, %15
%16 = phi i64 [ 0, %12 ], [ %25, %15 ]
%17 = load i32, i32* @CLOCK_MONOTONIC
%18 = shl nuw nsw i64 %16, 1
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i64 %18
%20 = call i32 @clock_gettime(i32 %17, %struct.timespec* %19)
%21 = load i32, i32* @CLOCK_MONOTONIC
%22 = or i64 %18, 1
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i64 %22
%24 = call i32 @clock_gettime(i32 %21, %struct.timespec* %23)
%25 = add nuw nsw i64 %16, 1
%26 = icmp eq i64 %25, 1000
br i1 %26, label %27, label %15
27: ; preds = %15
%28 = shl i32 %1, 3
%29 = call i64 @malloc(i32 %28)
%30 = inttoptr i64 %29 to %struct.timespec*
%31 = call i32 @memset(%struct.timespec* %30, i32 0, i32 %28)
%32 = icmp eq i64 %29, 0
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %27
%36 = icmp eq i32 %1, 0
br i1 %36, label %52, label %37
37: ; preds = %35
%38 = zext i32 %1 to i64
br label %39
39: ; preds = %37, %39
%40 = phi i64 [ 0, %37 ], [ %50, %39 ]
%41 = load i32, i32* @CLOCK_MONOTONIC
%42 = shl nuw nsw i64 %40, 1
%43 = getelementptr inbounds %struct.timespec, %struct.timespec* %30, i64 %42
%44 = call i32 @clock_gettime(i32 %41, %struct.timespec* %43)
%45 = call i32 @sleep(i32 %0)
%46 = load i32, i32* @CLOCK_MONOTONIC
%47 = or i64 %42, 1
%48 = getelementptr inbounds %struct.timespec, %struct.timespec* %30, i64 %47
%49 = call i32 @clock_gettime(i32 %46, %struct.timespec* %48)
%50 = add nuw nsw i64 %40, 1
%51 = icmp ult i64 %50, %38
br i1 %51, label %39, label %52
52: ; preds = %39, %35
%53 = call i32 @discrete_elapsed_hr(%struct.timespec* %7, %struct.timespec* %30, i32* %3, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%54 = call i32 @free(%struct.timespec* %30)
%55 = call i32 @free(%struct.timespec* %7)
ret i32 0
}