#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 */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int timeDiff(struct timeval *, struct timeval *);
int /*<<< orphan*/ usleep(int);
int worker();
int getWorkerTime() {
  int cnt, i;
  struct timeval tv1, tv2;
  cnt = 0;
  gettimeofday(&tv1, NULL);
  gettimeofday(&tv2, NULL);
  while (1000000 > timeDiff(&tv1, &tv2)) {
    i = worker();
    usleep(1);
    gettimeofday(&tv2, NULL);
  }
  gettimeofday(&tv1, NULL);
  gettimeofday(&tv2, NULL);
  while (4 * 1000000 > timeDiff(&tv1, &tv2)) {
    i = worker();
    usleep(0);
    gettimeofday(&tv2, NULL);
    cnt++;
  }
  return timeDiff(&tv1, &tv2) / cnt;
}


x86 assembly (clang, -O0):
<getWorkerTime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <timeDiff@plt>
mov    %eax,%ecx
mov    $0xf4240,%eax
cmp    %ecx,%eax
jle    11ab <getWorkerTime+0x6b>
mov    $0x0,%al
call   1040 <worker@plt>
mov    %eax,-0x8(%rbp)
mov    $0x1,%edi
call   1060 <usleep@plt>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
jmp    1169 <getWorkerTime+0x29>
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <timeDiff@plt>
mov    %eax,%ecx
mov    $0x3d0900,%eax
cmp    %ecx,%eax
jle    120d <getWorkerTime+0xcd>
mov    $0x0,%al
call   1040 <worker@plt>
mov    %eax,-0x8(%rbp)
xor    %edi,%edi
call   1060 <usleep@plt>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11c5 <getWorkerTime+0x85>
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <timeDiff@plt>
cltd
idivl  -0x4(%rbp)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getWorkerTime>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %ebx,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <timeDiff@plt>
cmp    $0xf423f,%eax
jg     11bd <getWorkerTime+0x7d>
mov    %rsp,%r15
lea    0x8(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <worker@plt>
mov    $0x1,%edi
call   1060 <usleep@plt>
mov    %r15,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <timeDiff@plt>
cmp    $0xf4240,%eax
jl     1190 <getWorkerTime+0x50>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <timeDiff@plt>
cmp    $0x3d08ff,%eax
jg     122d <getWorkerTime+0xed>
mov    %rsp,%r15
lea    0x8(%rsp),%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1040 <worker@plt>
xor    %edi,%edi
call   1060 <usleep@plt>
mov    %r15,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
add    $0x1,%ebx
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <timeDiff@plt>
cmp    $0x3d0900,%eax
jl     1200 <getWorkerTime+0xc0>
lea    0x8(%rsp),%rdi
mov    %rsp,%rsi
call   1050 <timeDiff@plt>
cltd
idiv   %ebx
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
define i32 @getWorkerTime() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
store i32 0, i32* %1
%5 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%6 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
br label %7
7: ; preds = %10, %0
%8 = call i32 @timeDiff(%struct.timeval* %3, %struct.timeval* %4)
%9 = icmp sgt i32 1000000, %8
br i1 %9, label %10, label %14
10: ; preds = %7
%11 = call i32 (...) @worker()
store i32 %11, i32* %2
%12 = call i32 @usleep(i32 1)
%13 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
br label %7
14: ; preds = %7
%15 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%16 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
br label %17
17: ; preds = %20, %14
%18 = call i32 @timeDiff(%struct.timeval* %3, %struct.timeval* %4)
%19 = icmp sgt i32 4000000, %18
br i1 %19, label %20, label %26
20: ; preds = %17
%21 = call i32 (...) @worker()
store i32 %21, i32* %2
%22 = call i32 @usleep(i32 0)
%23 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%24 = load i32, i32* %1
%25 = add nsw i32 %24, 1
store i32 %25, i32* %1
br label %17
26: ; preds = %17
%27 = call i32 @timeDiff(%struct.timeval* %3, %struct.timeval* %4)
%28 = load i32, i32* %1
%29 = sdiv i32 %27, %28
ret i32 %29
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
define i32 @getWorkerTime() {
%1 = alloca %struct.timeval
%2 = alloca %struct.timeval
%3 = bitcast %struct.timeval* %1 to i8*
%4 = bitcast %struct.timeval* %2 to i8*
%5 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%6 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%7 = call i32 @timeDiff(%struct.timeval* %1, %struct.timeval* %2)
%8 = icmp slt i32 %7, 1000000
br i1 %8, label %9, label %15
9: ; preds = %0, %9
%10 = call i32 (...) @worker()
%11 = call i32 @usleep(i32 1)
%12 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%13 = call i32 @timeDiff(%struct.timeval* %1, %struct.timeval* %2)
%14 = icmp slt i32 %13, 1000000
br i1 %14, label %9, label %15
15: ; preds = %9, %0
%16 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%17 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%18 = call i32 @timeDiff(%struct.timeval* %1, %struct.timeval* %2)
%19 = icmp slt i32 %18, 4000000
br i1 %19, label %20, label %28
20: ; preds = %15, %20
%21 = phi i32 [ %25, %20 ], [ 0, %15 ]
%22 = call i32 (...) @worker()
%23 = call i32 @usleep(i32 0)
%24 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%25 = add nuw nsw i32 %21, 1
%26 = call i32 @timeDiff(%struct.timeval* %1, %struct.timeval* %2)
%27 = icmp slt i32 %26, 4000000
br i1 %27, label %20, label %28
28: ; preds = %20, %15
%29 = phi i32 [ 0, %15 ], [ %25, %20 ]
%30 = call i32 @timeDiff(%struct.timeval* %1, %struct.timeval* %2)
%31 = sdiv i32 %30, %29
ret i32 %31
}