#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 timeval {
  int tv_usec;
};

/* Variables and functions */
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ timersub(struct timeval *, struct timeval *,
                            struct timeval *);
int /*<<< orphan*/ usleep(int);
int main() {
  int x;
  struct timeval t1, t2, t3;
  int t;
  int max, min;
  int del;
  int underRuns, overRuns, exactRuns, bogusRuns, total;
  int descheds;
  gettimeofday(&t1, NULL);
  gettimeofday(&t2, NULL);
  t = t2.tv_usec - t1.tv_usec;
  printf("Baseline test: %d\n", t);
  for (del = 1; del < 200; ++del) {
    underRuns = overRuns = exactRuns = total = 0;
    descheds = 0;
    max = 0;
    min = 999;
    for (x = 0; x < 1000; ++x) {
      for (;;) {
        gettimeofday(&t1, NULL);
        usleep(del);
        gettimeofday(&t2, NULL);
        timersub(&t2, &t1, &t3);
        t = t3.tv_usec;
        if (t > 999) {
          ++descheds;
          continue;
        } else
          break;
      }
      if (t == del)
        ++exactRuns;
      else if (t < del)
        ++underRuns;
      else if (t > del)
        ++overRuns;
      if (t > max)
        max = t;
      else if (t < min)
        min = t;
      total += t;
    }
    printf("Delay: %3d. Min: %3d, Max: %3d, Unders: %3d, Overs: %3d, Exacts: "
           "%3d, Average: %3d,  Descheds: %2d\n",
           del, min, max, underRuns, overRuns, exactRuns, total / 1000,
           descheds);
    fflush(stdout);
    usleep(1000);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    -0x18(%rbp),%eax
sub    -0x10(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x30(%rbp)
cmpl   $0xc8,-0x30(%rbp)
jge    134f <main+0x1ff>
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x48(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x3e7,-0x2c(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x3e8,-0x8(%rbp)
jge    12e2 <main+0x192>
jmp    11f1 <main+0xa1>
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    -0x30(%rbp),%edi
call   1070 <usleep@plt>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
lea    -0x18(%rbp),%rdi
lea    -0x10(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1060 <timersub@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x3e7,-0x24(%rbp)
jle    1245 <main+0xf5>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    11f1 <main+0xa1>
jmp    124a <main+0xfa>
mov    -0x24(%rbp),%eax
cmp    -0x30(%rbp),%eax
jne    1264 <main+0x114>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    129d <main+0x14d>
mov    -0x24(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    127e <main+0x12e>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1298 <main+0x148>
mov    -0x24(%rbp),%eax
cmp    -0x30(%rbp),%eax
jle    1293 <main+0x143>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1298 <main+0x148>
jmp    129d <main+0x14d>
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jle    12b4 <main+0x164>
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    12cb <main+0x17b>
mov    -0x24(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    12c6 <main+0x176>
mov    -0x24(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    12cb <main+0x17b>
mov    -0x24(%rbp),%eax
add    -0x44(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11df <main+0x8f>
mov    -0x30(%rbp),%esi
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x28(%rbp),%ecx
mov    -0x34(%rbp),%r8d
mov    -0x38(%rbp),%r9d
mov    -0x3c(%rbp),%r11d
mov    -0x44(%rbp),%eax
mov    $0x3e8,%edi
cltd
idiv   %edi
mov    -0x4c(%rbp),%edx
mov    %eax,%r10d
mov    -0x48(%rbp),%eax
lea    0xcfe(%rip),%rdi
mov    %r11d,(%rsp)
mov    %r10d,0x8(%rsp)
mov    %eax,0x10(%rsp)
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2cb0(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
mov    $0x3e8,%edi
call   1070 <usleep@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    119a <main+0x4a>
xor    %eax,%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    $0x38,%rsp
lea    0x30(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
lea    0x28(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    0x28(%rsp),%esi
sub    0x30(%rsp),%esi
lea    0xe75(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%ebp
lea    0x20(%rsp),%r15
jmp    1209 <main+0xb9>
xchg   %ax,%ax
movslq %esi,%rax
imul   $0x10624dd3,%rax,%r10
mov    %r10,%rax
shr    $0x3f,%rax
sar    $0x26,%r10
add    %eax,%r10d
sub    $0x8,%rsp
lea    0xe50(%rip),%rdi
mov    %ebp,%esi
mov    0x18(%rsp),%r8d
mov    0x1c(%rsp),%r9d
xor    %eax,%eax
push   %r12
push   %r10
push   0x30(%rsp)
call   1030 <printf@plt>
add    $0x20,%rsp
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
mov    $0x3e8,%edi
call   1070 <usleep@plt>
add    $0x1,%ebp
cmp    $0xc8,%ebp
je     12ea <main+0x19a>
mov    $0x3e7,%edx
xor    %r12d,%r12d
xor    %esi,%esi
xor    %r13d,%r13d
xor    %eax,%eax
mov    %rax,0x18(%rsp)
movl   $0x0,0x14(%rsp)
movl   $0x0,0x10(%rsp)
xor    %ecx,%ecx
jmp    127b <main+0x12b>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x18(%rsp),%rcx
add    $0x1,%ecx
mov    %rcx,0x18(%rsp)
mov    0xc(%rsp),%ecx
mov    0x8(%rsp),%esi
mov    0x4(%rsp),%edi
cmp    %edi,%eax
mov    %edi,%edx
cmovl  %eax,%edx
cmp    %ecx,%eax
cmovg  %eax,%ecx
cmovg  %edi,%edx
add    %eax,%esi
add    $0x1,%r13d
cmp    $0x3e8,%r13d
je     11a0 <main+0x50>
mov    %esi,0x8(%rsp)
mov    %ecx,0xc(%rsp)
mov    %edx,0x4(%rsp)
add    $0xffffffff,%r12d
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    %ebp,%edi
call   1070 <usleep@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1060 <timersub@plt>
mov    0x20(%rsp),%eax
add    $0x1,%r12d
cmp    $0x3e7,%eax
jg     1290 <main+0x140>
cmp    %ebp,%eax
je     1240 <main+0xf0>
jge    12e0 <main+0x190>
addl   $0x1,0x10(%rsp)
jmp    124d <main+0xfd>
nopl   0x0(%rax)
addl   $0x1,0x14(%rsp)
jmp    124d <main+0xfd>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
@.str = [19 x i8] c"Baseline test: %d\0A\00"
@.str.1 = [100 x i8] c"Delay: %3d. Min: %3d, Max: %3d, Unders: %3d, Overs: %3d, Exacts: %3d, Average: %3d, Descheds: %2d\0A\00"
@stdout = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = alloca %struct.timeval
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %1
%16 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%17 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%21 = load i32, i32* %20
%22 = sub nsw i32 %19, %21
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %23)
store i32 1, i32* %9
br label %25
25: ; preds = %104, %0
%26 = load i32, i32* %9
%27 = icmp slt i32 %26, 200
br i1 %27, label %28, label %107
28: ; preds = %25
store i32 0, i32* %14
store i32 0, i32* %12
store i32 0, i32* %11
store i32 0, i32* %10
store i32 0, i32* %15
store i32 0, i32* %7
store i32 999, i32* %8
store i32 0, i32* %2
br label %29
29: ; preds = %87, %28
%30 = load i32, i32* %2
%31 = icmp slt i32 %30, 1000
br i1 %31, label %32, label %90
32: ; preds = %29
br label %33
33: ; preds = %43, %32
%34 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%35 = load i32, i32* %9
%36 = call i32 @usleep(i32 %35)
%37 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%38 = call i32 @timersub(%struct.timeval* %4, %struct.timeval* %3, %struct.timeval* %5)
%39 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%40 = load i32, i32* %39
store i32 %40, i32* %6
%41 = load i32, i32* %6
%42 = icmp sgt i32 %41, 999
br i1 %42, label %43, label %46
43: ; preds = %33
%44 = load i32, i32* %15
%45 = add nsw i32 %44, 1
store i32 %45, i32* %15
br label %33
46: ; preds = %33
br label %47
47: ; preds = %46
%48 = load i32, i32* %6
%49 = load i32, i32* %9
%50 = icmp eq i32 %48, %49
br i1 %50, label %51, label %54
51: ; preds = %47
%52 = load i32, i32* %12
%53 = add nsw i32 %52, 1
store i32 %53, i32* %12
br label %70
54: ; preds = %47
%55 = load i32, i32* %6
%56 = load i32, i32* %9
%57 = icmp slt i32 %55, %56
br i1 %57, label %58, label %61
58: ; preds = %54
%59 = load i32, i32* %10
%60 = add nsw i32 %59, 1
store i32 %60, i32* %10
br label %69
61: ; preds = %54
%62 = load i32, i32* %6
%63 = load i32, i32* %9
%64 = icmp sgt i32 %62, %63
br i1 %64, label %65, label %68
65: ; preds = %61
%66 = load i32, i32* %11
%67 = add nsw i32 %66, 1
store i32 %67, i32* %11
br label %68
68: ; preds = %65, %61
br label %69
69: ; preds = %68, %58
br label %70
70: ; preds = %69, %51
%71 = load i32, i32* %6
%72 = load i32, i32* %7
%73 = icmp sgt i32 %71, %72
br i1 %73, label %74, label %76
74: ; preds = %70
%75 = load i32, i32* %6
store i32 %75, i32* %7
br label %83
76: ; preds = %70
%77 = load i32, i32* %6
%78 = load i32, i32* %8
%79 = icmp slt i32 %77, %78
br i1 %79, label %80, label %82
80: ; preds = %76
%81 = load i32, i32* %6
store i32 %81, i32* %8
br label %82
82: ; preds = %80, %76
br label %83
83: ; preds = %82, %74
%84 = load i32, i32* %6
%85 = load i32, i32* %14
%86 = add nsw i32 %85, %84
store i32 %86, i32* %14
br label %87
87: ; preds = %83
%88 = load i32, i32* %2
%89 = add nsw i32 %88, 1
store i32 %89, i32* %2
br label %29
90: ; preds = %29
%91 = load i32, i32* %9
%92 = load i32, i32* %8
%93 = load i32, i32* %7
%94 = load i32, i32* %10
%95 = load i32, i32* %11
%96 = load i32, i32* %12
%97 = load i32, i32* %14
%98 = sdiv i32 %97, 1000
%99 = load i32, i32* %15
%100 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([100 x i8], [100 x i8]* @.str.1, i64 0, i64 0), i32 %91, i32 %92, i32 %93, i32 %94, i32 %95, i32 %96, i32 %98, i32 %99)
%101 = load i32, i32* @stdout
%102 = call i32 @fflush(i32 %101)
%103 = call i32 @usleep(i32 1000)
br label %104
104: ; preds = %90
%105 = load i32, i32* %9
%106 = add nsw i32 %105, 1
store i32 %106, i32* %9
br label %25
107: ; preds = %25
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@.str = [19 x i8] c"Baseline test: %d\0A\00"
@.str.1 = [100 x i8] c"Delay: %3d. Min: %3d, Max: %3d, Unders: %3d, Overs: %3d, Exacts: %3d, Average: %3d, Descheds: %2d\0A\00"
@stdout = global i32 0
define i32 @main() {
%1 = alloca %struct.timeval
%2 = alloca %struct.timeval
%3 = alloca %struct.timeval
%4 = bitcast %struct.timeval* %1 to i8*
%5 = bitcast %struct.timeval* %2 to i8*
%6 = bitcast %struct.timeval* %3 to i8*
%7 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%8 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%10 = load i32, i32* %9
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%12 = load i32, i32* %11
%13 = sub nsw i32 %10, %12
%14 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
br label %16
16: ; preds = %0, %58
%17 = phi i32 [ 1, %0 ], [ %64, %58 ]
br label %18
18: ; preds = %16, %46
%19 = phi i32 [ 0, %16 ], [ %28, %46 ]
%20 = phi i32 [ 0, %16 ], [ %55, %46 ]
%21 = phi i32 [ 0, %16 ], [ %56, %46 ]
%22 = phi i32 [ 0, %16 ], [ %49, %46 ]
%23 = phi i32 [ 0, %16 ], [ %48, %46 ]
%24 = phi i32 [ 0, %16 ], [ %47, %46 ]
%25 = phi i32 [ 999, %16 ], [ %54, %46 ]
%26 = phi i32 [ 0, %16 ], [ %53, %46 ]
br label %27
27: ; preds = %27, %18
%28 = phi i32 [ %35, %27 ], [ %19, %18 ]
%29 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%30 = call i32 @usleep(i32 %17)
%31 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%32 = call i32 @timersub(%struct.timeval* %2, %struct.timeval* %1, %struct.timeval* %3)
%33 = load i32, i32* %15
%34 = icmp sgt i32 %33, 999
%35 = add nsw i32 %28, 1
br i1 %34, label %27, label %36
36: ; preds = %27
%37 = icmp eq i32 %33, %17
br i1 %37, label %38, label %40
38: ; preds = %36
%39 = add nsw i32 %22, 1
br label %46
40: ; preds = %36
%41 = icmp slt i32 %33, %17
br i1 %41, label %42, label %44
42: ; preds = %40
%43 = add nsw i32 %24, 1
br label %46
44: ; preds = %40
%45 = add nsw i32 %23, 1
br label %46
46: ; preds = %44, %42, %38
%47 = phi i32 [ %24, %38 ], [ %43, %42 ], [ %24, %44 ]
%48 = phi i32 [ %23, %38 ], [ %23, %42 ], [ %45, %44 ]
%49 = phi i32 [ %39, %38 ], [ %22, %42 ], [ %22, %44 ]
%50 = icmp sgt i32 %33, %26
%51 = icmp slt i32 %33, %25
%52 = select i1 %51, i32 %33, i32 %25
%53 = select i1 %50, i32 %33, i32 %26
%54 = select i1 %50, i32 %25, i32 %52
%55 = add nsw i32 %33, %20
%56 = add nuw nsw i32 %21, 1
%57 = icmp eq i32 %56, 1000
br i1 %57, label %58, label %18
58: ; preds = %46
%59 = sdiv i32 %55, 1000
%60 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([100 x i8], [100 x i8]* @.str.1, i64 0, i64 0), i32 %17, i32 %54, i32 %53, i32 %47, i32 %48, i32 %49, i32 %59, i32 %28)
%61 = load i32, i32* @stdout
%62 = call i32 @fflush(i32 %61)
%63 = call i32 @usleep(i32 1000)
%64 = add nuw nsw i32 %17, 1
%65 = icmp eq i32 %64, 200
br i1 %65, label %66, label %16
66: ; preds = %58
ret i32 0
}