#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_sec;
  long tv_usec;
};

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, long, long);
int /*<<< orphan*/ usleep(int);
int main(int argc, char **argv) {
  int i;
  struct timeval tv1;
  struct timeval tv2;
  long max = 0;
  long avg = 0;
  for (i = 1;; i++) {
    gettimeofday(&tv1, NULL);
    usleep(1000);
    gettimeofday(&tv2, NULL);
    long usec = (tv2.tv_sec - tv1.tv_sec) * 1000000 + tv2.tv_usec - tv1.tv_usec;
    avg += usec;
    if (usec > max)
      max = usec;
    if (!(i % 1000)) {
      avg /= 1000;
      printf("max %ld\tavg %ld\n", max, avg);
      max = 0;
      avg = 0;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
movl   $0x1,-0x14(%rbp)
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    $0x3e8,%edi
call   1050 <usleep@plt>
lea    -0x38(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    -0x38(%rbp),%eax
sub    -0x28(%rbp),%eax
imul   $0xf4240,%eax,%eax
cltq
add    -0x30(%rbp),%rax
sub    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
add    -0x48(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x50(%rbp),%rax
cmp    -0x40(%rbp),%rax
jle    11bd <main+0x8d>
mov    -0x50(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1207 <main+0xd7>
mov    -0x48(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%rdx
lea    0xe0e(%rip),%rdi
call   1030 <printf@plt>
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
jmp    120c <main+0xdc>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    115d <main+0x2d>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    $0x1,%r13d
mov    $0xffffffff,%ebp
xor    %r15d,%r15d
lea    0x8(%rsp),%r12
xor    %ebx,%ebx
jmp    116a <main+0x3a>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%r13d
add    $0xffffffff,%ebp
mov    %rsi,%r15
mov    %r13d,%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
imul   $0x3e8,%eax,%r14d
lea    0x18(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    $0x3e8,%edi
call   1050 <usleep@plt>
mov    %r12,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
movslq 0x8(%rsp),%rax
movslq 0x18(%rsp),%rcx
sub    %rcx,%rax
imul   $0xf4240,%rax,%rax
mov    0x10(%rsp),%rsi
sub    0x20(%rsp),%rsi
add    %rax,%rsi
add    %rsi,%rbx
cmp    %r15,%rsi
cmovle %r15,%rsi
add    %ebp,%r14d
jne    1160 <main+0x30>
mov    %rbx,%rax
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
imul   %rcx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x7,%rdx
add    %rax,%rdx
lea    0xe0c(%rip),%rdi
call   1030 <printf@plt>
xor    %esi,%esi
xor    %ebx,%ebx
jmp    1160 <main+0x30>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [17 x i8] c"max %ld\09avg %ld\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = alloca %struct.timeval
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* %9
store i64 0, i64* %10
store i32 1, i32* %6
br label %12
12: ; preds = %48, %2
%13 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
%14 = call i32 @usleep(i32 1000)
%15 = call i32 @gettimeofday(%struct.timeval* %8, i32* null)
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%19 = load i32, i32* %18
%20 = sub nsw i32 %17, %19
%21 = mul nsw i32 %20, 1000000
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
%24 = load i64, i64* %23
%25 = add nsw i64 %22, %24
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
%27 = load i64, i64* %26
%28 = sub nsw i64 %25, %27
store i64 %28, i64* %11
%29 = load i64, i64* %11
%30 = load i64, i64* %10
%31 = add nsw i64 %30, %29
store i64 %31, i64* %10
%32 = load i64, i64* %11
%33 = load i64, i64* %9
%34 = icmp sgt i64 %32, %33
br i1 %34, label %35, label %37
35: ; preds = %12
%36 = load i64, i64* %11
store i64 %36, i64* %9
br label %37
37: ; preds = %35, %12
%38 = load i32, i32* %6
%39 = srem i32 %38, 1000
%40 = icmp ne i32 %39, 0
br i1 %40, label %47, label %41
41: ; preds = %37
%42 = load i64, i64* %10
%43 = sdiv i64 %42, 1000
store i64 %43, i64* %10
%44 = load i64, i64* %9
%45 = load i64, i64* %10
%46 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i64 %44, i64 %45)
store i64 0, i64* %9
store i64 0, i64* %10
br label %47
47: ; preds = %41, %37
br label %48
48: ; preds = %47
%49 = load i32, i32* %6
%50 = add nsw i32 %49, 1
store i32 %50, i32* %6
br label %12
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [17 x i8] c"max %ld\09avg %ld\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = bitcast %struct.timeval* %3 to i8*
%6 = bitcast %struct.timeval* %4 to i8*
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
br label %11
11: ; preds = %35, %2
%12 = phi i64 [ 0, %2 ], [ %36, %35 ]
%13 = phi i64 [ 0, %2 ], [ %37, %35 ]
%14 = phi i32 [ 1, %2 ], [ %38, %35 ]
%15 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%16 = tail call i32 @usleep(i32 1000)
%17 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%18 = load i32, i32* %7
%19 = load i32, i32* %8
%20 = sub nsw i32 %18, %19
%21 = mul nsw i32 %20, 1000000
%22 = sext i32 %21 to i64
%23 = load i64, i64* %9
%24 = load i64, i64* %10
%25 = sub i64 %23, %24
%26 = add i64 %25, %22
%27 = add nsw i64 %26, %13
%28 = icmp sgt i64 %26, %12
%29 = select i1 %28, i64 %26, i64 %12
%30 = urem i32 %14, 1000
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %11
%33 = sdiv i64 %27, 1000
%34 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i64 %29, i64 %33)
br label %35
35: ; preds = %32, %11
%36 = phi i64 [ %29, %11 ], [ 0, %32 ]
%37 = phi i64 [ %27, %11 ], [ 0, %32 ]
%38 = add nuw nsw i32 %14, 1
br label %11
}