#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 {
  unsigned long tv_usec;
  scalar_t__ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
void udelay(unsigned long usec) {
  struct timeval now, deadline;
  gettimeofday(&deadline, NULL);
  deadline.tv_usec += usec;
  deadline.tv_sec += deadline.tv_usec / 1000000;
  deadline.tv_usec %= 1000000;
  do {
    gettimeofday(&now, NULL);
  } while ((now.tv_sec < deadline.tv_sec) ||
           (now.tv_sec == deadline.tv_sec && now.tv_usec < deadline.tv_usec));
}


x86 assembly (clang, -O0):
<udelay>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x8(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    $0xf4240,%ecx
xor    %edx,%edx
div    %rcx
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    $0xf4240,%ecx
xor    %edx,%edx
div    %rcx
mov    %rdx,-0x28(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x10(%rbp),%rcx
mov    $0x1,%al
cmp    -0x20(%rbp),%rcx
mov    %al,-0x29(%rbp)
jl     11a4 <udelay+0x94>
mov    -0x10(%rbp),%rcx
xor    %eax,%eax
cmp    -0x20(%rbp),%rcx
mov    %al,-0x2a(%rbp)
jne    119e <udelay+0x8e>
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
setb   %al
mov    %al,-0x2a(%rbp)
mov    -0x2a(%rbp),%al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    115d <udelay+0x4d>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udelay>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%r14
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
add    (%rsp),%r14
movabs $0x431bde82d7b634db,%rcx
de 1b 43
mov    %r14,%rax
mul    %rcx
mov    %rdx,%rbx
shr    $0x12,%rbx
imul   $0xf4240,%rbx,%rax
add    0x8(%rsp),%rbx
sub    %rax,%r14
lea    0x10(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
cmp    %rbx,0x18(%rsp)
jl     1160 <udelay+0x50>
jne    117a <udelay+0x6a>
cmp    %r14,0x10(%rsp)
jb     1160 <udelay+0x50>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
define void @udelay(i64 %0) {
%2 = alloca i64
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
store i64 %0, i64* %2
%5 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%6 = load i64, i64* %2
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%8 = load i64, i64* %7
%9 = add i64 %8, %6
store i64 %9, i64* %7
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%11 = load i64, i64* %10
%12 = udiv i64 %11, 1000000
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%14 = load i64, i64* %13
%15 = add i64 %14, %12
store i64 %15, i64* %13
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%17 = load i64, i64* %16
%18 = urem i64 %17, 1000000
store i64 %18, i64* %16
br label %19
19: ; preds = %41, %1
%20 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
br label %21
21: ; preds = %19
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%23 = load i64, i64* %22
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%25 = load i64, i64* %24
%26 = icmp slt i64 %23, %25
br i1 %26, label %41, label %27
27: ; preds = %21
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%29 = load i64, i64* %28
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%31 = load i64, i64* %30
%32 = icmp eq i64 %29, %31
br i1 %32, label %33, label %39
33: ; preds = %27
%34 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%35 = load i64, i64* %34
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%37 = load i64, i64* %36
%38 = icmp ult i64 %35, %37
br label %39
39: ; preds = %33, %27
%40 = phi i1 [ false, %27 ], [ %38, %33 ]
br label %41
41: ; preds = %39, %21
%42 = phi i1 [ true, %21 ], [ %40, %39 ]
br i1 %42, label %19, label %43
43: ; preds = %41
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
define void @udelay(i64 %0) {
%2 = alloca %struct.timeval
%3 = alloca %struct.timeval
%4 = bitcast %struct.timeval* %2 to i8*
%5 = bitcast %struct.timeval* %3 to i8*
%6 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%8 = load i64, i64* %7
%9 = add i64 %8, %0
%10 = udiv i64 %9, 1000000
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%12 = load i64, i64* %11
%13 = add i64 %12, %10
%14 = urem i64 %9, 1000000
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
br label %17
17: ; preds = %26, %1
%18 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%19 = load i64, i64* %15
%20 = icmp slt i64 %19, %13
br i1 %20, label %26, label %21
21: ; preds = %17
%22 = icmp eq i64 %19, %13
%23 = load i64, i64* %16
%24 = icmp ult i64 %23, %14
%25 = select i1 %22, i1 %24, i1 false
br i1 %25, label %26, label %27
26: ; preds = %21, %17
br label %17
27: ; preds = %21
ret void
}