#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 dummy;
};

/* Variables and functions */
scalar_t__ caltv(struct timeval *, struct timeval *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, double);
int /*<<< orphan*/ sleep_us(int);
void cal_avg() {
  struct timeval stv, etv;
  int i;
  long sum;
  double avg;
  for (i = 0, sum = 0; i < 100000; i++) {
    gettimeofday(&stv, NULL);
    sleep_us(1000 * 10);
    gettimeofday(&etv, NULL);
    sum += caltv(&stv, &etv);
  }
  avg = (double)sum / i;
  printf("AVG is %lf\n", avg);
}


x86 assembly (clang, -O0):
<cal_avg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
cmpl   $0x186a0,-0x14(%rbp)
jge    11ab <cal_avg+0x6b>
lea    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    $0x2710,%edi
call   1050 <sleep_us@plt>
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
lea    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1060 <caltv@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1157 <cal_avg+0x17>
cvtsi2sdq -0x20(%rbp),%xmm0
cvtsi2sdl -0x14(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
lea    0xe35(%rip),%rdi
call   1030 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cal_avg>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    $0x186a0,%ebp
xor    %ebx,%ebx
lea    0x10(%rsp),%r14
lea    0x8(%rsp),%r15
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    $0x2710,%edi
call   1050 <sleep_us@plt>
mov    %r15,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <caltv@plt>
add    %rax,%rbx
add    $0xffffffff,%ebp
jne    1160 <cal_avg+0x20>
cvtsi2sd %rbx,%xmm0
divsd  0xe62(%rip),%xmm0
lea    0xe63(%rip),%rdi
call   1030 <printf@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
@.str = [12 x i8] c"AVG is %lf\0A\00"
define void @cal_avg() {
%1 = alloca %struct.timeval
%2 = alloca %struct.timeval
%3 = alloca i32
%4 = alloca i64
%5 = alloca double
store i32 0, i32* %3
store i64 0, i64* %4
br label %6
6: ; preds = %16, %0
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 100000
br i1 %8, label %9, label %19
9: ; preds = %6
%10 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%11 = call i32 @sleep_us(i32 10000)
%12 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%13 = call i64 @caltv(%struct.timeval* %1, %struct.timeval* %2)
%14 = load i64, i64* %4
%15 = add nsw i64 %14, %13
store i64 %15, i64* %4
br label %16
16: ; preds = %9
%17 = load i32, i32* %3
%18 = add nsw i32 %17, 1
store i32 %18, i32* %3
br label %6
19: ; preds = %6
%20 = load i64, i64* %4
%21 = sitofp i64 %20 to double
%22 = load i32, i32* %3
%23 = sitofp i32 %22 to double
%24 = fdiv double %21, %23
store double %24, double* %5
%25 = load double, double* %5
%26 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), double %25)
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@.str = [12 x i8] c"AVG is %lf\0A\00"
define void @cal_avg() {
%1 = alloca %struct.timeval
%2 = alloca %struct.timeval
%3 = bitcast %struct.timeval* %1 to i8*
%4 = bitcast %struct.timeval* %2 to i8*
br label %5
5: ; preds = %0, %5
%6 = phi i32 [ 0, %0 ], [ %13, %5 ]
%7 = phi i64 [ 0, %0 ], [ %12, %5 ]
%8 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%9 = call i32 @sleep_us(i32 10000)
%10 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%11 = call i64 @caltv(%struct.timeval* %1, %struct.timeval* %2)
%12 = add nsw i64 %11, %7
%13 = add nuw nsw i32 %6, 1
%14 = icmp eq i32 %13, 100000
br i1 %14, label %15, label %5
15: ; preds = %5
%16 = sitofp i64 %12 to double
%17 = fdiv double %16, 1.000000e+05
%18 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), double %17)
ret void
}