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

/* 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*/ nanosleep(struct timespec *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int sleep_nanosleep(unsigned int duration, unsigned int iter) {
  struct timespec start, end;
  struct timespec slp;
  int i = 0;
  slp.tv_sec = (time_t)1 * duration;
  slp.tv_nsec = (long)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::nanosleep 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::nanosleep results allocation failure.");
  }
  for (i = 0; i < iter; i++) {
    clock_gettime(CLOCK_MONOTONIC, &results[2 * i]);
    nanosleep(&slp, NULL);
    clock_gettime(CLOCK_MONOTONIC, &results[2 * i + 1]);
  }
  discrete_elapsed_hr(overheads, results, &iter, "Sleep::nanosleep");
  free(results);
  free(overheads);
  return 0;
}


x86 assembly (clang, -O0):
<sleep_nanosleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x4(%rbp),%eax
shl    $0x0,%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x10,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
imul   $0x7d0,-0x48(%rbp),%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
imul   $0x7d0,-0x48(%rbp),%rax
mov    %eax,%edx
xor    %esi,%esi
call   1060 <memset@plt>
cmpq   $0x0,-0x50(%rbp)
jne    11e5 <sleep_nanosleep+0x75>
lea    0xe20(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1040 <clock_gettime@plt>
movl   $0x0,-0x3c(%rbp)
cmpl   $0x3e8,-0x3c(%rbp)
jge    125e <sleep_nanosleep+0xee>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
mov    -0x50(%rbp),%rsi
mov    -0x3c(%rbp),%eax
shl    $0x1,%eax
cltq
shl    $0x4,%rax
add    %rax,%rsi
call   1040 <clock_gettime@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
mov    -0x50(%rbp),%rsi
mov    -0x3c(%rbp),%eax
shl    $0x1,%eax
add    $0x1,%eax
cltq
shl    $0x4,%rax
add    %rax,%rsi
call   1040 <clock_gettime@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11fe <sleep_nanosleep+0x8e>
movq   $0x0,-0x58(%rbp)
mov    -0x8(%rbp),%eax
shl    $0x1,%eax
mov    %eax,%eax
imul   -0x48(%rbp),%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rdi
mov    -0x8(%rbp),%eax
shl    $0x1,%eax
mov    %eax,%eax
imul   -0x48(%rbp),%rax
mov    %eax,%edx
xor    %esi,%esi
call   1060 <memset@plt>
cmpq   $0x0,-0x58(%rbp)
jne    12af <sleep_nanosleep+0x13f>
lea    0xd85(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jae    1322 <sleep_nanosleep+0x1b2>
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
mov    -0x58(%rbp),%rsi
mov    -0x3c(%rbp),%eax
shl    $0x1,%eax
cltq
shl    $0x4,%rax
add    %rax,%rsi
call   1040 <clock_gettime@plt>
lea    -0x38(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <nanosleep@plt>
mov    0x2cf1(%rip),%rax
mov    (%rax),%edi
mov    -0x58(%rbp),%rsi
mov    -0x3c(%rbp),%eax
shl    $0x1,%eax
add    $0x1,%eax
cltq
shl    $0x4,%rax
add    %rax,%rsi
call   1040 <clock_gettime@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    12b6 <sleep_nanosleep+0x146>
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rsi
lea    -0x8(%rbp),%rdx
lea    0xd27(%rip),%rcx
call   1090 <discrete_elapsed_hr@plt>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sleep_nanosleep>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %esi,%r15d
mov    %esi,0xc(%rsp)
mov    %edi,0x18(%rsp)
movq   $0x0,0x20(%rsp)
mov    $0x7d00,%edi
call   1070 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x7d00,%edx
call   1060 <memset@plt>
test   %r12,%r12
je     12bb <sleep_nanosleep+0x14b>
mov    0x2e2a(%rip),%r14
mov    (%r14),%edi
lea    0x28(%rsp),%rsi
call   1040 <clock_gettime@plt>
lea    0x10(%r12),%rbp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r14),%edi
lea    (%r12,%rbx,1),%rsi
call   1040 <clock_gettime@plt>
mov    (%r14),%edi
lea    (%rbx,%rbp,1),%rsi
call   1040 <clock_gettime@plt>
add    $0x20,%rbx
cmp    $0x7d00,%rbx
jne    11e0 <sleep_nanosleep+0x70>
mov    %r15d,%ebp
shl    $0x5,%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1060 <memset@plt>
mov    %rbx,0x10(%rsp)
test   %rbx,%rbx
je     12cc <sleep_nanosleep+0x15c>
test   %r15d,%r15d
je     127e <sleep_nanosleep+0x10e>
mov    %r15d,%r13d
xor    %r15d,%r15d
lea    0x18(%rsp),%rbx
mov    0x10(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r14),%edi
mov    %rbp,%rsi
call   1040 <clock_gettime@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <nanosleep@plt>
mov    (%r14),%edi
lea    0x10(%rbp),%rsi
call   1040 <clock_gettime@plt>
add    $0x1,%r15
add    $0x20,%rbp
cmp    %r13,%r15
jb     1250 <sleep_nanosleep+0xe0>
lea    0xdd7(%rip),%rcx
lea    0xc(%rsp),%rdx
mov    %r12,%rdi
mov    0x10(%rsp),%rbx
mov    %rbx,%rsi
call   1090 <discrete_elapsed_hr@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd3e(%rip),%rdi
call   1080 <perror@plt>
jmp    11b7 <sleep_nanosleep+0x47>
lea    0xd5c(%rip),%rdi
call   1080 <perror@plt>
test   %r15d,%r15d
jne    1234 <sleep_nanosleep+0xc4>
jmp    127e <sleep_nanosleep+0x10e>

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

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