#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 */
typedef int useconds_t;
typedef int uint64_t;
struct timeval {
  int tv_sec;
  int tv_usec;
};

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int usleep(int);
int wait_a_while(useconds_t interval) {
  int num_times = 0;
  int return_value = 1;
  struct timeval start_time;
  gettimeofday(&start_time, NULL);
  uint64_t target = start_time.tv_sec * 1000000 + start_time.tv_usec + interval;
  while (1) {
    num_times++;
    return_value = usleep(interval);
    if (return_value != 0) {
      struct timeval now;
      gettimeofday(&now, NULL);
      interval = target - now.tv_sec * 1000000 + now.tv_usec;
    } else
      break;
  }
  return num_times;
}


x86 assembly (clang, -O0):
<wait_a_while>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x1,-0xc(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
imul   $0xf4240,-0x18(%rbp),%eax
add    -0x14(%rbp),%eax
add    -0x4(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <usleep@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1198 <wait_a_while+0x78>
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x1c(%rbp),%eax
imul   $0xf4240,-0x28(%rbp),%ecx
sub    %ecx,%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    119d <wait_a_while+0x7d>
jmp    11a2 <wait_a_while+0x82>
jmp    1156 <wait_a_while+0x36>
mov    -0x8(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_a_while>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    0x10(%rsp),%r15d
mov    0x14(%rsp),%ebp
mov    %ebx,%edi
call   1040 <usleep@plt>
test   %eax,%eax
je     118d <wait_a_while+0x6d>
add    %ebx,%ebp
mov    $0x1,%ebx
lea    0x8(%rsp),%r14
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    %r15d,%eax
sub    0x8(%rsp),%eax
imul   $0xf4240,%eax,%eax
mov    0xc(%rsp),%edi
add    %ebp,%edi
add    %eax,%edi
add    $0x1,%ebx
call   1040 <usleep@plt>
test   %eax,%eax
jne    1160 <wait_a_while+0x40>
jmp    1192 <wait_a_while+0x72>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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