#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*/ usleep(int);
int sleep_usleep(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::usleep 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::usleep results allocation failure.");
  }
  for (i = 0; i < iter; i++) {
    clock_gettime(CLOCK_MONOTONIC, &results[2 * i]);
    usleep(1000 * 1000 * duration);
    clock_gettime(CLOCK_MONOTONIC, &results[2 * i + 1]);
  }
  discrete_elapsed_hr(overheads, results, &iter, "Sleep::usleep");
  free(results);
  free(overheads);
  return 0;
}


x86 assembly (clang, -O0):
<sleep_usleep>:
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_usleep+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_usleep+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_usleep+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_usleep+0x12e>
lea    0xd93(%rip),%rdi
call   1070 <perror@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jae    1310 <sleep_usleep+0x1a0>
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>
imul   $0xf4240,-0x4(%rbp),%edi
call   1090 <usleep@plt>
mov    0x2d03(%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_usleep+0x135>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
lea    -0x8(%rbp),%rdx
lea    0xd33(%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_usleep>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r12d
mov    %edi,%r14d
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     12a9 <sleep_usleep+0x139>
mov    0x2e34(%rip),%r15
mov    (%r15),%edi
lea    0x10(%rsp),%rsi
call   1040 <clock_gettime@plt>
lea    0x4(%r13),%rbx
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    (%r15),%edi
mov    %r13,%rsi
add    %rbp,%rsi
call   1040 <clock_gettime@plt>
mov    (%r15),%edi
lea    (%rbx,%rbp,1),%rsi
call   1040 <clock_gettime@plt>
add    $0x8,%rbp
cmp    $0x1f40,%rbp
jne    11d0 <sleep_usleep+0x60>
lea    0x0(,%r12,8),%ebx
mov    %ebx,%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1050 <memset@plt>
mov    %rbp,0x8(%rsp)
test   %rbp,%rbp
je     12ba <sleep_usleep+0x14a>
test   %r12d,%r12d
je     126c <sleep_usleep+0xfc>
imul   $0xf4240,%r14d,%r14d
mov    %r12d,%ebp
xor    %r12d,%r12d
mov    0x8(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    (%r15),%edi
mov    %rbx,%rsi
call   1040 <clock_gettime@plt>
mov    %r14d,%edi
call   1090 <usleep@plt>
mov    (%r15),%edi
lea    0x4(%rbx),%rsi
call   1040 <clock_gettime@plt>
add    $0x1,%r12
add    $0x8,%rbx
cmp    %rbp,%r12
jb     1240 <sleep_usleep+0xd0>
lea    0xde3(%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    0xd50(%rip),%rdi
call   1070 <perror@plt>
jmp    11ad <sleep_usleep+0x3d>
lea    0xd6b(%rip),%rdi
call   1070 <perror@plt>
test   %r12d,%r12d
jne    1228 <sleep_usleep+0xb8>
jmp    126c <sleep_usleep+0xfc>

LLVM IR (-O0):
%struct.timespec = type { i32 }
@.str = [44 x i8] c"Sleep::usleep overheads allocation failure.\00"
@CLOCK_MONOTONIC = global i32 0
@.str.1 = [42 x i8] c"Sleep::usleep results allocation failure.\00"
@.str.2 = [14 x i8] c"Sleep::usleep\00"
define i32 @sleep_usleep(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 ([44 x i8], [44 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 ([42 x i8], [42 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 1000000, %84
%86 = call i32 @usleep(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 ([14 x i8], [14 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 = [44 x i8] c"Sleep::usleep overheads allocation failure.\00"
@CLOCK_MONOTONIC = global i32 0
@.str.1 = [42 x i8] c"Sleep::usleep results allocation failure.\00"
@.str.2 = [14 x i8] c"Sleep::usleep\00"
define i32 @sleep_usleep(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 ([44 x i8], [44 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 ([42 x i8], [42 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 %53, label %37
37: ; preds = %35
%38 = mul i32 %0, 1000000
%39 = zext i32 %1 to i64
br label %40
40: ; preds = %37, %40
%41 = phi i64 [ 0, %37 ], [ %51, %40 ]
%42 = load i32, i32* @CLOCK_MONOTONIC
%43 = shl nuw nsw i64 %41, 1
%44 = getelementptr inbounds %struct.timespec, %struct.timespec* %30, i64 %43
%45 = call i32 @clock_gettime(i32 %42, %struct.timespec* %44)
%46 = call i32 @usleep(i32 %38)
%47 = load i32, i32* @CLOCK_MONOTONIC
%48 = or i64 %43, 1
%49 = getelementptr inbounds %struct.timespec, %struct.timespec* %30, i64 %48
%50 = call i32 @clock_gettime(i32 %47, %struct.timespec* %49)
%51 = add nuw nsw i64 %41, 1
%52 = icmp ult i64 %51, %39
br i1 %52, label %40, label %53
53: ; preds = %40, %35
%54 = call i32 @discrete_elapsed_hr(%struct.timespec* %7, %struct.timespec* %30, i32* %3, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%55 = call i32 @free(%struct.timespec* %30)
%56 = call i32 @free(%struct.timespec* %7)
ret i32 0
}