#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*/ *);
void hardsleep(int us) {
  struct timeval tv, start;
  int secs, usecs;
  gettimeofday(&tv, NULL);
  start = tv;
  usecs = 0;
  while (usecs < us) {
    gettimeofday(&tv, NULL);
    secs = tv.tv_sec - start.tv_sec;
    usecs = tv.tv_usec - start.tv_usec;
    if (secs)
      usecs += 1000000;
  }
}


x86 assembly (clang, -O0):
<hardsleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    117c <hardsleep+0x6c>
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x10(%rbp),%eax
sub    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
sub    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1177 <hardsleep+0x67>
mov    -0x20(%rbp),%eax
add    $0xf4240,%eax
mov    %eax,-0x20(%rbp)
jmp    1137 <hardsleep+0x27>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hardsleep>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r15d
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
test   %r15d,%r15d
jle    1161 <hardsleep+0x51>
mov    (%rsp),%ebp
mov    0x4(%rsp),%ebx
mov    %rsp,%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    0x4(%rsp),%eax
sub    %ebx,%eax
cmp    %ebp,(%rsp)
lea    0xf4240(%rax),%ecx
cmove  %eax,%ecx
cmp    %r15d,%ecx
jl     1140 <hardsleep+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define void @hardsleep(i32 %0) {
%2 = alloca %struct.timeval
%3 = bitcast %struct.timeval* %2 to i8*
%4 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%6 = load i32, i32* %5
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%8 = load i32, i32* %7
%9 = icmp sgt i32 %0, 0
br i1 %9, label %10, label %19
10: ; preds = %1, %10
%11 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%12 = load i32, i32* %5
%13 = load i32, i32* %7
%14 = sub nsw i32 %13, %8
%15 = icmp eq i32 %12, %6
%16 = add nsw i32 %14, 1000000
%17 = select i1 %15, i32 %14, i32 %16
%18 = icmp slt i32 %17, %0
br i1 %18, label %10, label %19
19: ; preds = %10, %1
ret void
}