#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 tm {
  int dummy;
};
struct timeval {
  int /*<<< orphan*/ tv_sec;
};
struct timespec {
  long long tv_sec;
  int tv_nsec;
};
typedef int /*<<< orphan*/ s;
typedef int int64_t;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int);
int get_diff(struct timespec *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ localtime_r(int /*<<< orphan*/ *, struct tm *);
int /*<<< orphan*/ printf(char *, char *, long long);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strftime(char *, int, char *, struct tm *);
int /*<<< orphan*/ usleep(int);
int main(int argc, char **argv) {
  int64_t now, diff0, diff, newdiff, prevprint = 0;
  int64_t nextloop;
  struct timespec ts;
  int msecs = 10;
  if (argc > 1) {
    msecs = atoi(argv[1]);
    if (!msecs)
      msecs = 10;
  }
  fprintf(stderr, "%s: looping every %i millisecs\n", argv[0], msecs);
  diff0 = get_diff(&ts);
  nextloop = ts.tv_sec * 1000LL * 1000 + ts.tv_nsec / 1000;
  while (1) {
    diff = get_diff(&ts);
    newdiff = diff - diff0;
    if (newdiff > prevprint + 500 * 1000 || newdiff < prevprint - 500 * 1000) {
      struct timeval tv;
      struct tm tm;
      char s[32];
      gettimeofday(&tv, NULL);
      localtime_r(&tv.tv_sec, &tm);
      strftime(s, sizeof(s), "%y-%m-%d-%H:%M:%S", &tm);
      printf("%s: %10lli us\n", s, (long long)newdiff / 1000);
      prevprint = newdiff;
      fflush(stdout);
    }
    nextloop += msecs * 1000;
    now = ts.tv_sec * 1000LL * 1000 + ts.tv_nsec / 1000;
    if (now < nextloop)
      usleep(nextloop - now);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0xa,-0x3c(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11e7 <main+0x57>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jne    11e2 <main+0x52>
movl   $0xa,-0x3c(%rbp)
jmp    11e7 <main+0x57>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x3c(%rbp),%ecx
lea    0xdff(%rip),%rsi
call   1070 <fprintf@plt>
lea    -0x38(%rbp),%rdi
call   1030 <get_diff@plt>
mov    %eax,-0x18(%rbp)
imul   $0x3e8,-0x38(%rbp),%rax
imul   $0x3e8,%rax,%rax
mov    %rax,-0x78(%rbp)
mov    -0x30(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x78(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %eax,-0x28(%rbp)
lea    -0x38(%rbp),%rdi
call   1030 <get_diff@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
sub    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    -0x24(%rbp),%ecx
add    $0x7a120,%ecx
cmp    %ecx,%eax
jg     127c <main+0xec>
mov    -0x20(%rbp),%eax
mov    -0x24(%rbp),%ecx
sub    $0x7a120,%ecx
cmp    %ecx,%eax
jge    12e4 <main+0x154>
lea    -0x40(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
lea    -0x40(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   1040 <localtime_r@plt>
lea    -0x70(%rbp),%rdi
mov    $0x20,%esi
lea    0xd7a(%rip),%rdx
lea    -0x48(%rbp),%rcx
call   1090 <strftime@plt>
lea    -0x70(%rbp),%rsi
movslq -0x20(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,%rdx
lea    0xd67(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
imul   $0x3e8,-0x3c(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
imul   $0x3e8,-0x38(%rbp),%rax
imul   $0x3e8,%rax,%rax
mov    %rax,-0x80(%rbp)
mov    -0x30(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x80(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    1335 <main+0x1a5>
mov    -0x28(%rbp),%edi
sub    -0x14(%rbp),%edi
call   10b0 <usleep@plt>
jmp    123f <main+0xaf>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rsi,%rbp
mov    $0xa,%ebx
cmp    $0x2,%edi
jl     11be <main+0x2e>
mov    0x8(%rbp),%rdi
call   10a0 <atoi@plt>
test   %eax,%eax
mov    $0xa,%ebx
cmovne %eax,%ebx
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rdx
lea    0xe2e(%rip),%rsi
mov    %ebx,%ecx
call   1070 <fprintf@plt>
mov    %rsp,%rdi
call   1030 <get_diff@plt>
mov    %eax,%ebp
movslq 0x8(%rsp),%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rax
add    %ecx,%eax
imul   $0xf4240,(%rsp),%r14d
add    %eax,%r14d
imul   $0x3e8,%ebx,%r15d
add    %r15d,%r14d
xor    %r13d,%r13d
jmp    1223 <main+0x93>
cs nopw 0x0(%rax,%rax,1)
add    %r15d,%r14d
mov    %rsp,%rdi
call   1030 <get_diff@plt>
mov    %eax,%r12d
sub    %ebp,%r12d
lea    0x7a120(%r13),%ecx
lea    -0x7a120(%r13),%eax
cmp    %ecx,%r12d
jg     1249 <main+0xb9>
cmp    %eax,%r12d
jge    12c4 <main+0x134>
lea    0x18(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
mov    %rbx,%rdi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
call   1040 <localtime_r@plt>
mov    %ebp,%r13d
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x20,%esi
lea    0xda1(%rip),%rdx
mov    %rbx,%rcx
call   1090 <strftime@plt>
movslq %r12d,%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rax
add    %ecx,%eax
movslq %eax,%rdx
lea    0xd8a(%rip),%rdi
mov    %rbp,%rsi
mov    %r13d,%ebp
call   1050 <printf@plt>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
mov    %r12d,%r13d
movslq 0x8(%rsp),%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rax
add    %ecx,%eax
imul   $0xf4240,(%rsp),%ecx
add    %eax,%ecx
mov    %r14d,%edi
sub    %ecx,%edi
jle    1220 <main+0x90>
call   10b0 <usleep@plt>
jmp    1220 <main+0x90>

LLVM IR (-O0):
%struct.timespec = type { i64, i32 }
%struct.timeval = type { i32 }
%struct.tm = type { i32 }
@stderr = global i32 0
@.str = [32 x i8] c"%s: looping every %i millisecs\0A\00"
@.str.1 = [18 x i8] c"%y-%m-%d-%H:%M:%S\00"
@.str.2 = [15 x i8] c"%s: %10lli us\0A\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.timespec
%13 = alloca i32
%14 = alloca %struct.timeval
%15 = alloca %struct.tm
%16 = alloca [32 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %10
store i32 10, i32* %13
%17 = load i32, i32* %4
%18 = icmp sgt i32 %17, 1
br i1 %18, label %19, label %28
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %13
%24 = load i32, i32* %13
%25 = icmp ne i32 %24, 0
br i1 %25, label %27, label %26
26: ; preds = %19
store i32 10, i32* %13
br label %27
27: ; preds = %26, %19
br label %28
28: ; preds = %27, %2
%29 = load i32, i32* @stderr
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 0
%32 = load i8*, i8** %31
%33 = load i32, i32* %13
%34 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %32, i32 %33)
%35 = call i32 @get_diff(%struct.timespec* %12)
store i32 %35, i32* %7
%36 = getelementptr inbounds %struct.timespec, %struct.timespec* %12, i32 0, i32 0
%37 = load i64, i64* %36
%38 = mul nsw i64 %37, 1000
%39 = mul nsw i64 %38, 1000
%40 = getelementptr inbounds %struct.timespec, %struct.timespec* %12, i32 0, i32 1
%41 = load i32, i32* %40
%42 = sdiv i32 %41, 1000
%43 = sext i32 %42 to i64
%44 = add nsw i64 %39, %43
%45 = trunc i64 %44 to i32
store i32 %45, i32* %11
br label %46
46: ; preds = %28, %97
%47 = call i32 @get_diff(%struct.timespec* %12)
store i32 %47, i32* %8
%48 = load i32, i32* %8
%49 = load i32, i32* %7
%50 = sub nsw i32 %48, %49
store i32 %50, i32* %9
%51 = load i32, i32* %9
%52 = load i32, i32* %10
%53 = add nsw i32 %52, 500000
%54 = icmp sgt i32 %51, %53
br i1 %54, label %60, label %55
55: ; preds = %46
%56 = load i32, i32* %9
%57 = load i32, i32* %10
%58 = sub nsw i32 %57, 500000
%59 = icmp slt i32 %56, %58
br i1 %59, label %60, label %74
60: ; preds = %55, %46
%61 = call i32 @gettimeofday(%struct.timeval* %14, i32* null)
%62 = getelementptr inbounds %struct.timeval, %struct.timeval* %14, i32 0, i32 0
%63 = call i32 @localtime_r(i32* %62, %struct.tm* %15)
%64 = getelementptr inbounds [32 x i8], [32 x i8]* %16, i64 0, i64 0
%65 = call i32 @strftime(i8* %64, i32 32, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), %struct.tm* %15)
%66 = getelementptr inbounds [32 x i8], [32 x i8]* %16, i64 0, i64 0
%67 = load i32, i32* %9
%68 = sext i32 %67 to i64
%69 = sdiv i64 %68, 1000
%70 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %66, i64 %69)
%71 = load i32, i32* %9
store i32 %71, i32* %10
%72 = load i32, i32* @stdout
%73 = call i32 @fflush(i32 %72)
br label %74
74: ; preds = %60, %55
%75 = load i32, i32* %13
%76 = mul nsw i32 %75, 1000
%77 = load i32, i32* %11
%78 = add nsw i32 %77, %76
store i32 %78, i32* %11
%79 = getelementptr inbounds %struct.timespec, %struct.timespec* %12, i32 0, i32 0
%80 = load i64, i64* %79
%81 = mul nsw i64 %80, 1000
%82 = mul nsw i64 %81, 1000
%83 = getelementptr inbounds %struct.timespec, %struct.timespec* %12, i32 0, i32 1
%84 = load i32, i32* %83
%85 = sdiv i32 %84, 1000
%86 = sext i32 %85 to i64
%87 = add nsw i64 %82, %86
%88 = trunc i64 %87 to i32
store i32 %88, i32* %6
%89 = load i32, i32* %6
%90 = load i32, i32* %11
%91 = icmp slt i32 %89, %90
br i1 %91, label %92, label %97
92: ; preds = %74
%93 = load i32, i32* %11
%94 = load i32, i32* %6
%95 = sub nsw i32 %93, %94
%96 = call i32 @usleep(i32 %95)
br label %97
97: ; preds = %92, %74
br label %46
}

LLVM IR (-O2):
%struct.timespec = type { i64, i32 }
%struct.timeval = type { i32 }
%struct.tm = type { i32 }
@stderr = global i32 0
@.str = [32 x i8] c"%s: looping every %i millisecs\0A\00"
@.str.1 = [18 x i8] c"%y-%m-%d-%H:%M:%S\00"
@.str.2 = [15 x i8] c"%s: %10lli us\0A\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.timespec
%4 = alloca %struct.timeval
%5 = alloca %struct.tm
%6 = alloca [32 x i8]
%7 = bitcast %struct.timespec* %3 to i8*
%8 = icmp sgt i32 %0, 1
br i1 %8, label %9, label %15
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = icmp eq i32 %12, 0
%14 = select i1 %13, i32 10, i32 %12
br label %15
15: ; preds = %9, %2
%16 = phi i32 [ %14, %9 ], [ 10, %2 ]
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %1
%19 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %18, i32 %16)
%20 = call i32 @get_diff(%struct.timespec* %3)
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%22 = load i64, i64* %21
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%24 = load i32, i32* %23
%25 = sdiv i32 %24, 1000
%26 = trunc i64 %22 to i32
%27 = mul i32 %26, 1000000
%28 = add i32 %27, %25
%29 = bitcast %struct.timeval* %4 to i8*
%30 = bitcast %struct.tm* %5 to i8*
%31 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%33 = mul nsw i32 %16, 1000
br label %34
34: ; preds = %66, %15
%35 = phi i32 [ 0, %15 ], [ %54, %66 ]
%36 = phi i32 [ %28, %15 ], [ %55, %66 ]
%37 = call i32 @get_diff(%struct.timespec* %3)
%38 = sub nsw i32 %37, %20
%39 = add nsw i32 %35, 500000
%40 = icmp sgt i32 %38, %39
%41 = add nsw i32 %35, -500000
%42 = icmp slt i32 %38, %41
%43 = select i1 %40, i1 true, i1 %42
br i1 %43, label %44, label %53
44: ; preds = %34
%45 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%46 = call i32 @localtime_r(i32* %32, %struct.tm* %5)
%47 = call i32 @strftime(i8* %31, i32 32, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), %struct.tm* %5)
%48 = sdiv i32 %38, 1000
%49 = sext i32 %48 to i64
%50 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %31, i64 %49)
%51 = load i32, i32* @stdout
%52 = call i32 @fflush(i32 %51)
br label %53
53: ; preds = %34, %44
%54 = phi i32 [ %38, %44 ], [ %35, %34 ]
%55 = add nsw i32 %36, %33
%56 = load i64, i64* %21
%57 = load i32, i32* %23
%58 = sdiv i32 %57, 1000
%59 = trunc i64 %56 to i32
%60 = mul i32 %59, 1000000
%61 = add i32 %60, %58
%62 = icmp sgt i32 %55, %61
br i1 %62, label %63, label %66
63: ; preds = %53
%64 = sub nsw i32 %55, %61
%65 = call i32 @usleep(i32 %64)
br label %66
66: ; preds = %63, %53
br label %34
}