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

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ nop();
int /*<<< orphan*/ timersub(struct timeval *, struct timeval *,
                            struct timeval *);
unsigned int calibration(unsigned int duree_demandee) {
  struct timeval t, t0, t1;
  unsigned int i;
  unsigned int duree;
  unsigned int nb;
  nb = 0;
  gettimeofday(&t0, NULL);
  do {
    for (i = 0; i < 100000; i++) {
      nop();
    }
    gettimeofday(&t1, NULL);
    timersub(&t1, &t0, &t);
    nb += 100000;
    duree = t.tv_sec * 1000 + t.tv_usec / 1000000;
  } while (duree < duree_demandee);
  return nb;
}


x86 assembly (clang, -O0):
<calibration>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
movl   $0x0,-0x24(%rbp)
cmpl   $0x186a0,-0x24(%rbp)
jae    1178 <calibration+0x48>
mov    $0x0,%al
call   1040 <nop@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1156 <calibration+0x26>
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
lea    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   1050 <timersub@plt>
mov    -0x2c(%rbp),%eax
add    $0x186a0,%eax
mov    %eax,-0x2c(%rbp)
imul   $0x3e8,-0x10(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0xc(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x4(%rbp),%eax
jb     114f <calibration+0x1f>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calibration>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r15d
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
lea    0x8(%rsp),%r13
mov    %rsp,%r12
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    $0x186a0,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1040 <nop@plt>
add    $0xffffffff,%ebx
jne    1170 <calibration+0x40>
mov    %r13,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    %r13,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1050 <timersub@plt>
imul   $0x3e8,(%rsp),%eax
add    $0x186a0,%ebp
movslq 0x4(%rsp),%rcx
imul   $0x431bde83,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x32,%rcx
add    %edx,%ecx
add    %eax,%ecx
cmp    %r15d,%ecx
jb     1160 <calibration+0x30>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
define i32 @calibration(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = alloca %struct.timeval
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %8
%9 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
br label %10
10: ; preds = %31, %1
store i32 0, i32* %6
br label %11
11: ; preds = %16, %10
%12 = load i32, i32* %6
%13 = icmp ult i32 %12, 100000
br i1 %13, label %14, label %19
14: ; preds = %11
%15 = call i32 (...) @nop()
br label %16
16: ; preds = %14
%17 = load i32, i32* %6
%18 = add i32 %17, 1
store i32 %18, i32* %6
br label %11
19: ; preds = %11
%20 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%21 = call i32 @timersub(%struct.timeval* %5, %struct.timeval* %4, %struct.timeval* %3)
%22 = load i32, i32* %8
%23 = add i32 %22, 100000
store i32 %23, i32* %8
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%25 = load i32, i32* %24
%26 = mul nsw i32 %25, 1000
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%28 = load i32, i32* %27
%29 = sdiv i32 %28, 1000000
%30 = add nsw i32 %26, %29
store i32 %30, i32* %7
br label %31
31: ; preds = %19
%32 = load i32, i32* %7
%33 = load i32, i32* %2
%34 = icmp ult i32 %32, %33
br i1 %34, label %10, label %35
35: ; preds = %31
%36 = load i32, i32* %8
ret i32 %36
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define i32 @calibration(i32 %0) {
%2 = alloca %struct.timeval
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = bitcast %struct.timeval* %2 to i8*
%6 = bitcast %struct.timeval* %3 to i8*
%7 = bitcast %struct.timeval* %4 to i8*
%8 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
br label %11
11: ; preds = %18, %1
%12 = phi i32 [ 0, %1 ], [ %21, %18 ]
br label %13
13: ; preds = %11, %13
%14 = phi i32 [ 0, %11 ], [ %16, %13 ]
%15 = call i32 (...) @nop()
%16 = add nuw nsw i32 %14, 1
%17 = icmp eq i32 %16, 100000
br i1 %17, label %18, label %13
18: ; preds = %13
%19 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%20 = call i32 @timersub(%struct.timeval* %4, %struct.timeval* %3, %struct.timeval* %2)
%21 = add i32 %12, 100000
%22 = load i32, i32* %9
%23 = mul nsw i32 %22, 1000
%24 = load i32, i32* %10
%25 = sdiv i32 %24, 1000000
%26 = add nsw i32 %25, %23
%27 = icmp ult i32 %26, %0
br i1 %27, label %11, label %28
28: ; preds = %18
ret i32 %21
}