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

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int, int);
int measure_1ms(void) {
  struct timeval tv_start, tv_end, tv_interval;
  int loop_1ms = 1000;
retry:
  gettimeofday(&tv_start, NULL);
  do {
    int i;
    register int reg1 = 1000, reg2 = 32;
    for (i = 0; i < loop_1ms; i++) {
      do {
        int i;
        for (i = 0; i < 10; i++)
          reg2 = reg1 / reg2;
      } while (0);
    }
  } while (0);
  gettimeofday(&tv_end, NULL);
  do {
    (&tv_interval)->tv_sec = (&tv_end)->tv_sec - (&tv_start)->tv_sec;
    (&tv_interval)->tv_usec = (&tv_end)->tv_usec - (&tv_start)->tv_usec;
    if ((&tv_interval)->tv_usec < 0) {
      --(&tv_interval)->tv_sec;
      (&tv_interval)->tv_usec += 1000000;
    }
  } while (0);
  if (tv_interval.tv_usec != 1000) {
    loop_1ms += 3 * (1000 - tv_interval.tv_usec);
    goto retry;
  }
  printf("%d times loop consumes %d usec\n", loop_1ms, tv_interval.tv_usec);
  return loop_1ms;
}


x86 assembly (clang, -O0):
<measure_1ms>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x3e8,-0x34(%rbp)
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
movl   $0x3e8,-0x3c(%rbp)
movl   $0x20,-0x40(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    11a3 <measure_1ms+0x83>
jmp    1162 <measure_1ms+0x42>
movl   $0x0,-0x44(%rbp)
cmpl   $0xa,-0x44(%rbp)
jge    118b <measure_1ms+0x6b>
mov    -0x3c(%rbp),%eax
cltd
idivl  -0x40(%rbp)
mov    %eax,-0x40(%rbp)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1169 <measure_1ms+0x49>
jmp    1190 <measure_1ms+0x70>
jmp    1195 <measure_1ms+0x75>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1151 <measure_1ms+0x31>
jmp    11a8 <measure_1ms+0x88>
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    -0x18(%rbp),%rax
sub    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
sub    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    11f2 <measure_1ms+0xd2>
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
add    $0xf4240,%rax
mov    %rax,-0x30(%rbp)
jmp    11f7 <measure_1ms+0xd7>
cmpq   $0x3e8,-0x30(%rbp)
je     1221 <measure_1ms+0x101>
mov    $0x3e8,%eax
sub    -0x30(%rbp),%rax
imul   $0x3,%rax,%rcx
movslq -0x34(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x34(%rbp)
jmp    112f <measure_1ms+0xf>
mov    -0x34(%rbp),%esi
mov    -0x30(%rbp),%rax
mov    %eax,%edx
lea    0xdcf(%rip),%rdi
call   1030 <printf@plt>
mov    -0x34(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<measure_1ms>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    %rsp,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    (%rsp),%rcx
sub    0x10(%rsp),%rcx
lea    0xf4240(%rcx),%rax
test   %rcx,%rcx
cmovns %rcx,%rax
mov    $0x3e8,%ebx
cmp    $0x3e8,%rax
je     11af <measure_1ms+0x8f>
mov    $0x3e8,%ebx
lea    0x10(%rsp),%r14
mov    %rsp,%r15
mov    $0x3e8,%ecx
sub    %eax,%ecx
lea    (%rcx,%rcx,2),%eax
add    %eax,%ebx
mov    %r14,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    %r15,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    (%rsp),%rcx
sub    0x10(%rsp),%rcx
lea    0xf4240(%rcx),%rax
test   %rcx,%rcx
cmovns %rcx,%rax
cmp    $0x3e8,%rax
jne    1170 <measure_1ms+0x50>
lea    0xe4a(%rip),%rdi
mov    %ebx,%esi
mov    $0x3e8,%edx
call   1030 <printf@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@.str = [32 x i8] c"%d times loop consumes %d usec\0A\00"
define i32 @measure_1ms() {
%1 = alloca %struct.timeval
%2 = alloca %struct.timeval
%3 = alloca %struct.timeval
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 1000, i32* %4
br label %9
9: ; preds = %64, %0
%10 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
br label %11
11: ; preds = %9
store i32 1000, i32* %6
store i32 32, i32* %7
store i32 0, i32* %5
br label %12
12: ; preds = %30, %11
%13 = load i32, i32* %5
%14 = load i32, i32* %4
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %33
16: ; preds = %12
br label %17
17: ; preds = %16
store i32 0, i32* %8
br label %18
18: ; preds = %25, %17
%19 = load i32, i32* %8
%20 = icmp slt i32 %19, 10
br i1 %20, label %21, label %28
21: ; preds = %18
%22 = load i32, i32* %6
%23 = load i32, i32* %7
%24 = sdiv i32 %22, %23
store i32 %24, i32* %7
br label %25
25: ; preds = %21
%26 = load i32, i32* %8
%27 = add nsw i32 %26, 1
store i32 %27, i32* %8
br label %18
28: ; preds = %18
br label %29
29: ; preds = %28
br label %30
30: ; preds = %29
%31 = load i32, i32* %5
%32 = add nsw i32 %31, 1
store i32 %32, i32* %5
br label %12
33: ; preds = %12
br label %34
34: ; preds = %33
%35 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
br label %36
36: ; preds = %34
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 1
%38 = load i64, i64* %37
%39 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i32 0, i32 1
%40 = load i64, i64* %39
%41 = sub nsw i64 %38, %40
%42 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
store i64 %41, i64* %42
%43 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
%44 = load i64, i64* %43
%45 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i32 0, i32 0
%46 = load i64, i64* %45
%47 = sub nsw i64 %44, %46
%48 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
store i64 %47, i64* %48
%49 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%50 = load i64, i64* %49
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %59
52: ; preds = %36
%53 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%54 = load i64, i64* %53
%55 = add nsw i64 %54, -1
store i64 %55, i64* %53
%56 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%57 = load i64, i64* %56
%58 = add nsw i64 %57, 1000000
store i64 %58, i64* %56
br label %59
59: ; preds = %52, %36
br label %60
60: ; preds = %59
%61 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%62 = load i64, i64* %61
%63 = icmp ne i64 %62, 1000
br i1 %63, label %64, label %73
64: ; preds = %60
%65 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%66 = load i64, i64* %65
%67 = sub nsw i64 1000, %66
%68 = mul nsw i64 3, %67
%69 = load i32, i32* %4
%70 = sext i32 %69 to i64
%71 = add nsw i64 %70, %68
%72 = trunc i64 %71 to i32
store i32 %72, i32* %4
br label %9
73: ; preds = %60
%74 = load i32, i32* %4
%75 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%76 = load i64, i64* %75
%77 = trunc i64 %76 to i32
%78 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %74, i32 %77)
%79 = load i32, i32* %4
ret i32 %79
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@.str = [32 x i8] c"%d times loop consumes %d usec\0A\00"
define i32 @measure_1ms() {
%1 = alloca %struct.timeval
%2 = alloca %struct.timeval
%3 = bitcast %struct.timeval* %1 to i8*
%4 = bitcast %struct.timeval* %2 to i8*
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%7 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%8 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%9 = load i64, i64* %5
%10 = load i64, i64* %6
%11 = sub nsw i64 %9, %10
%12 = icmp slt i64 %11, 0
%13 = add nsw i64 %11, 1000000
%14 = select i1 %12, i64 %13, i64 %11
%15 = icmp eq i64 %14, 1000
br i1 %15, label %32, label %16
16: ; preds = %0, %16
%17 = phi i64 [ %30, %16 ], [ %14, %0 ]
%18 = phi i32 [ %22, %16 ], [ 1000, %0 ]
%19 = trunc i64 %17 to i32
%20 = sub i32 1000, %19
%21 = mul i32 %20, 3
%22 = add i32 %21, %18
%23 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%24 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%25 = load i64, i64* %5
%26 = load i64, i64* %6
%27 = sub nsw i64 %25, %26
%28 = icmp slt i64 %27, 0
%29 = add nsw i64 %27, 1000000
%30 = select i1 %28, i64 %29, i64 %27
%31 = icmp eq i64 %30, 1000
br i1 %31, label %32, label %16
32: ; preds = %16, %0
%33 = phi i32 [ 1000, %0 ], [ %22, %16 ]
%34 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %33, i32 1000)
ret i32 %33
}