#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;
  scalar_t__ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ nanosleep(struct timespec *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  struct timespec interval, start, stop;
  const int N = 3;
  int elapsed[2 * N];
  int i;
  interval.tv_sec = 0;
  interval.tv_nsec = 1;
  for (i = 0; i < N; ++i) {
    clock_gettime(CLOCK_REALTIME, &start);
    nanosleep(&interval, 0);
    clock_gettime(CLOCK_REALTIME, &stop);
    elapsed[i] = stop.tv_nsec - start.tv_nsec;
  }
  for (i = 0; i < N; ++i) {
    clock_gettime(CLOCK_REALTIME, &start);
    do {
    } while (0);
    clock_gettime(CLOCK_REALTIME, &stop);
    elapsed[i + N] = stop.tv_nsec - start.tv_nsec;
  }
  printf("sleep elapsed nanos:\n");
  for (i = 0; i < N; ++i) {
    printf("%d\n", elapsed[i]);
  }
  printf("loop elapsed nanos:\n");
  for (i = 0; i < N; ++i) {
    printf("%d\n", elapsed[i + N]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x3,-0x44(%rbp)
mov    %rsp,%rax
mov    %rax,-0x50(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
movl   $0x0,-0x54(%rbp)
cmpl   $0x3,-0x54(%rbp)
jge    11bf <main+0x8f>
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1030 <clock_gettime@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
call   1050 <nanosleep@plt>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x40(%rbp),%ecx
sub    -0x30(%rbp),%ecx
movslq -0x54(%rbp),%rax
mov    %ecx,-0x70(%rbp,%rax,4)
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    116a <main+0x3a>
movl   $0x0,-0x54(%rbp)
cmpl   $0x3,-0x54(%rbp)
jge    121e <main+0xee>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1030 <clock_gettime@plt>
jmp    11e7 <main+0xb7>
jmp    11ec <main+0xbc>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x40(%rbp),%ecx
sub    -0x30(%rbp),%ecx
mov    -0x54(%rbp),%eax
add    $0x3,%eax
cltq
mov    %ecx,-0x70(%rbp,%rax,4)
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    11c6 <main+0x96>
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x54(%rbp)
cmpl   $0x3,-0x54(%rbp)
jge    1261 <main+0x131>
movslq -0x54(%rbp),%rax
mov    -0x70(%rbp,%rax,4),%esi
lea    0xdca(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    1233 <main+0x103>
lea    0xdb2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x54(%rbp)
cmpl   $0x3,-0x54(%rbp)
jge    12a8 <main+0x178>
mov    -0x54(%rbp),%eax
add    $0x3,%eax
cltq
mov    -0x70(%rbp,%rax,4),%esi
lea    0xd83(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    1276 <main+0x146>
movl   $0x0,-0x4(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rsp,0x30(%rsp)
movq   $0x0,0x40(%rsp)
movl   $0x1,0x38(%rsp)
mov    0x2e75(%rip),%rbx
mov    (%rbx),%edi
lea    0x8(%rsp),%rbp
mov    %rbp,%rsi
call   1040 <clock_gettime@plt>
lea    0x38(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
call   1060 <nanosleep@plt>
mov    (%rbx),%edi
lea    0x20(%rsp),%r12
mov    %r12,%rsi
call   1040 <clock_gettime@plt>
mov    0x20(%rsp),%eax
sub    0x8(%rsp),%eax
mov    %eax,0x1c(%rsp)
mov    (%rbx),%edi
mov    %rbp,%rsi
call   1040 <clock_gettime@plt>
mov    %r14,%rdi
xor    %esi,%esi
call   1060 <nanosleep@plt>
mov    (%rbx),%edi
mov    %r12,%rsi
call   1040 <clock_gettime@plt>
mov    0x20(%rsp),%r13d
sub    0x8(%rsp),%r13d
mov    (%rbx),%edi
mov    %rbp,%rsi
call   1040 <clock_gettime@plt>
mov    %r14,%rdi
xor    %esi,%esi
call   1060 <nanosleep@plt>
mov    (%rbx),%edi
mov    %r12,%rsi
call   1040 <clock_gettime@plt>
mov    0x20(%rsp),%r15d
sub    0x8(%rsp),%r15d
mov    (%rbx),%edi
mov    %rbp,%rsi
mov    %rbp,%r14
call   1040 <clock_gettime@plt>
mov    (%rbx),%edi
mov    %r12,%rsi
call   1040 <clock_gettime@plt>
mov    0x20(%rsp),%ebp
sub    0x8(%rsp),%ebp
mov    (%rbx),%edi
mov    %r14,%rsi
call   1040 <clock_gettime@plt>
mov    (%rbx),%edi
mov    %r12,%rsi
call   1040 <clock_gettime@plt>
mov    0x20(%rsp),%r14d
sub    0x8(%rsp),%r14d
mov    (%rbx),%edi
lea    0x8(%rsp),%rsi
call   1040 <clock_gettime@plt>
mov    (%rbx),%edi
mov    %r12,%rsi
call   1040 <clock_gettime@plt>
mov    0x20(%rsp),%ebx
sub    0x8(%rsp),%ebx
lea    0xdae(%rip),%rdi
call   1030 <puts@plt>
lea    0xd9e(%rip),%r12
mov    %r12,%rdi
mov    0x1c(%rsp),%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12,%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12,%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xd88(%rip),%rdi
call   1030 <puts@plt>
mov    %r12,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12,%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x30(%rsp),%rsp
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@CLOCK_REALTIME = global i32 0
@.str = [22 x i8] c"sleep elapsed nanos:\0A\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@.str.2 = [21 x i8] c"loop elapsed nanos:\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.timespec
%7 = alloca %struct.timespec
%8 = alloca %struct.timespec
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 3, i32* %9
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %10
%13 = alloca i32, i64 6
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
store i64 0, i64* %14
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
store i32 1, i32* %15
store i32 0, i32* %11
br label %16
16: ; preds = %33, %2
%17 = load i32, i32* %11
%18 = icmp slt i32 %17, 3
br i1 %18, label %19, label %36
19: ; preds = %16
%20 = load i32, i32* @CLOCK_REALTIME
%21 = call i32 @clock_gettime(i32 %20, %struct.timespec* %7)
%22 = call i32 @nanosleep(%struct.timespec* %6, i32 0)
%23 = load i32, i32* @CLOCK_REALTIME
%24 = call i32 @clock_gettime(i32 %23, %struct.timespec* %8)
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
%28 = load i32, i32* %27
%29 = sub nsw i32 %26, %28
%30 = load i32, i32* %11
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %13, i64 %31
store i32 %29, i32* %32
br label %33
33: ; preds = %19
%34 = load i32, i32* %11
%35 = add nsw i32 %34, 1
store i32 %35, i32* %11
br label %16
36: ; preds = %16
store i32 0, i32* %11
br label %37
37: ; preds = %57, %36
%38 = load i32, i32* %11
%39 = icmp slt i32 %38, 3
br i1 %39, label %40, label %60
40: ; preds = %37
%41 = load i32, i32* @CLOCK_REALTIME
%42 = call i32 @clock_gettime(i32 %41, %struct.timespec* %7)
br label %43
43: ; preds = %40
br label %44
44: ; preds = %43
br label %45
45: ; preds = %44
%46 = load i32, i32* @CLOCK_REALTIME
%47 = call i32 @clock_gettime(i32 %46, %struct.timespec* %8)
%48 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
%49 = load i32, i32* %48
%50 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
%51 = load i32, i32* %50
%52 = sub nsw i32 %49, %51
%53 = load i32, i32* %11
%54 = add nsw i32 %53, 3
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i32, i32* %13, i64 %55
store i32 %52, i32* %56
br label %57
57: ; preds = %45
%58 = load i32, i32* %11
%59 = add nsw i32 %58, 1
store i32 %59, i32* %11
br label %37
60: ; preds = %37
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %11
br label %62
62: ; preds = %71, %60
%63 = load i32, i32* %11
%64 = icmp slt i32 %63, 3
br i1 %64, label %65, label %74
65: ; preds = %62
%66 = load i32, i32* %11
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i32, i32* %13, i64 %67
%69 = load i32, i32* %68
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %69)
br label %71
71: ; preds = %65
%72 = load i32, i32* %11
%73 = add nsw i32 %72, 1
store i32 %73, i32* %11
br label %62
74: ; preds = %62
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %11
br label %76
76: ; preds = %86, %74
%77 = load i32, i32* %11
%78 = icmp slt i32 %77, 3
br i1 %78, label %79, label %89
79: ; preds = %76
%80 = load i32, i32* %11
%81 = add nsw i32 %80, 3
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds i32, i32* %13, i64 %82
%84 = load i32, i32* %83
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %84)
br label %86
86: ; preds = %79
%87 = load i32, i32* %11
%88 = add nsw i32 %87, 1
store i32 %88, i32* %11
br label %76
89: ; preds = %76
store i32 0, i32* %3
%90 = load i8*, i8** %10
%91 = load i32, i32* %3
ret i32 %91
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@CLOCK_REALTIME = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
@str = [21 x i8] c"sleep elapsed nanos:\00"
@str.3 = [20 x i8] c"loop elapsed nanos:\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
%6 = bitcast %struct.timespec* %3 to i8*
%7 = bitcast %struct.timespec* %4 to i8*
%8 = bitcast %struct.timespec* %5 to i8*
%9 = tail call i8* @llvm.stacksave()
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
store i64 0, i64* %10
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
store i32 1, i32* %11
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 0
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
%14 = load i32, i32* @CLOCK_REALTIME
%15 = call i32 @clock_gettime(i32 %14, %struct.timespec* %4)
%16 = call i32 @nanosleep(%struct.timespec* %3, i32 0)
%17 = load i32, i32* @CLOCK_REALTIME
%18 = call i32 @clock_gettime(i32 %17, %struct.timespec* %5)
%19 = load i32, i32* %12
%20 = load i32, i32* %13
%21 = sub nsw i32 %19, %20
%22 = load i32, i32* @CLOCK_REALTIME
%23 = call i32 @clock_gettime(i32 %22, %struct.timespec* %4)
%24 = call i32 @nanosleep(%struct.timespec* %3, i32 0)
%25 = load i32, i32* @CLOCK_REALTIME
%26 = call i32 @clock_gettime(i32 %25, %struct.timespec* %5)
%27 = load i32, i32* %12
%28 = load i32, i32* %13
%29 = sub nsw i32 %27, %28
%30 = load i32, i32* @CLOCK_REALTIME
%31 = call i32 @clock_gettime(i32 %30, %struct.timespec* %4)
%32 = call i32 @nanosleep(%struct.timespec* %3, i32 0)
%33 = load i32, i32* @CLOCK_REALTIME
%34 = call i32 @clock_gettime(i32 %33, %struct.timespec* %5)
%35 = load i32, i32* %12
%36 = load i32, i32* %13
%37 = sub nsw i32 %35, %36
%38 = load i32, i32* @CLOCK_REALTIME
%39 = call i32 @clock_gettime(i32 %38, %struct.timespec* %4)
%40 = load i32, i32* @CLOCK_REALTIME
%41 = call i32 @clock_gettime(i32 %40, %struct.timespec* %5)
%42 = load i32, i32* %12
%43 = load i32, i32* %13
%44 = sub nsw i32 %42, %43
%45 = load i32, i32* @CLOCK_REALTIME
%46 = call i32 @clock_gettime(i32 %45, %struct.timespec* %4)
%47 = load i32, i32* @CLOCK_REALTIME
%48 = call i32 @clock_gettime(i32 %47, %struct.timespec* %5)
%49 = load i32, i32* %12
%50 = load i32, i32* %13
%51 = sub nsw i32 %49, %50
%52 = load i32, i32* @CLOCK_REALTIME
%53 = call i32 @clock_gettime(i32 %52, %struct.timespec* %4)
%54 = load i32, i32* @CLOCK_REALTIME
%55 = call i32 @clock_gettime(i32 %54, %struct.timespec* %5)
%56 = load i32, i32* %12
%57 = load i32, i32* %13
%58 = sub nsw i32 %56, %57
%59 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%60 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%61 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%62 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %37)
%63 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.3, i64 0, i64 0))
%64 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %44)
%65 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %51)
%66 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %58)
ret i32 0
}