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

/* Variables and functions */
int /*<<< orphan*/ errno;
int gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ prerror(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void udelay(unsigned int usecs) {
  int err;
  struct timeval time, deadline;
  err = gettimeofday(&deadline, NULL);
  if (err)
    goto error;
  deadline.tv_usec += usecs;
  if (deadline.tv_usec >= 1000000) {
    deadline.tv_sec++;
    deadline.tv_usec -= 1000000;
  }
  while (1) {
    err = gettimeofday(&time, NULL);
    if (err)
      goto error;
    if (time.tv_sec < deadline.tv_sec)
      continue;
    if (time.tv_usec >= deadline.tv_usec)
      break;
  }
  return;
error:
  prerror("gettimeofday() failed with: %s\n", strerror(errno));
}


x86 assembly (clang, -O0):
<udelay>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     115a <udelay+0x2a>
jmp    11d9 <udelay+0xa9>
mov    -0x4(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0xf4240,-0x28(%rbp)
jb     1187 <udelay+0x57>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%eax
sub    $0xf4240,%eax
mov    %eax,-0x28(%rbp)
jmp    118c <udelay+0x5c>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     11ab <udelay+0x7b>
jmp    11d9 <udelay+0xa9>
mov    -0x10(%rbp),%rax
cmp    -0x20(%rbp),%rax
jge    11be <udelay+0x8e>
jmp    118c <udelay+0x5c>
mov    -0x18(%rbp),%eax
cmp    -0x28(%rbp),%eax
jb     11cf <udelay+0x9f>
jmp    11d4 <udelay+0xa4>
jmp    118c <udelay+0x5c>
jmp    11f5 <udelay+0xc5>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %eax,%esi
lea    0xe10(%rip),%rdi
call   1040 <prerror@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udelay>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebp
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
test   %eax,%eax
jne    116f <udelay+0x3f>
add    (%rsp),%ebp
cmp    $0xf4240,%ebp
jb     115f <udelay+0x2f>
addq   $0x1,0x8(%rsp)
add    $0xfff0bdc0,%ebp
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
test   %eax,%eax
je     1193 <udelay+0x63>
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe7c(%rip),%rdi
mov    %eax,%esi
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <prerror@plt>
mov    0x8(%rsp),%rbx
lea    0x10(%rsp),%r14
jmp    11ae <udelay+0x7e>
nop
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
test   %eax,%eax
jne    116f <udelay+0x3f>
cmp    %rbx,0x18(%rsp)
jl     11a0 <udelay+0x70>
cmp    %ebp,0x10(%rsp)
jb     11a0 <udelay+0x70>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [32 x i8] c"gettimeofday() failed with: %s\0A\00"
@errno = global i32 0
define void @udelay(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = alloca %struct.timeval
store i32 %0, i32* %2
%6 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %1
br label %47
10: ; preds = %1
%11 = load i32, i32* %2
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%13 = load i32, i32* %12
%14 = add i32 %13, %11
store i32 %14, i32* %12
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%16 = load i32, i32* %15
%17 = icmp uge i32 %16, 1000000
br i1 %17, label %18, label %25
18: ; preds = %10
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%20 = load i64, i64* %19
%21 = add nsw i64 %20, 1
store i64 %21, i64* %19
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%23 = load i32, i32* %22
%24 = sub i32 %23, 1000000
store i32 %24, i32* %22
br label %25
25: ; preds = %18, %10
br label %26
26: ; preds = %25, %37, %45
%27 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
store i32 %27, i32* %3
%28 = load i32, i32* %3
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %26
br label %47
31: ; preds = %26
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%33 = load i64, i64* %32
%34 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%35 = load i64, i64* %34
%36 = icmp slt i64 %33, %35
br i1 %36, label %37, label %38
37: ; preds = %31
br label %26
38: ; preds = %31
%39 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%40 = load i32, i32* %39
%41 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%42 = load i32, i32* %41
%43 = icmp uge i32 %40, %42
br i1 %43, label %44, label %45
44: ; preds = %38
br label %46
45: ; preds = %38
br label %26
46: ; preds = %44
br label %51
47: ; preds = %30, %9
%48 = load i32, i32* @errno
%49 = call i32 @strerror(i32 %48)
%50 = call i32 @prerror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %47, %46
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [32 x i8] c"gettimeofday() failed with: %s\0A\00"
@errno = global i32 0
define void @udelay(i32 %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 = icmp eq i32 %6, 0
br i1 %7, label %8, label %36
8: ; preds = %1
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%10 = load i32, i32* %9
%11 = add i32 %10, %0
%12 = icmp ugt i32 %11, 999999
br i1 %12, label %13, label %18
13: ; preds = %8
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%15 = load i64, i64* %14
%16 = add nsw i64 %15, 1
store i64 %16, i64* %14
%17 = add i32 %11, -1000000
br label %18
18: ; preds = %13, %8
%19 = phi i32 [ %17, %13 ], [ %11, %8 ]
%20 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %36
22: ; preds = %18
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%25 = load i64, i64* %24
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
br label %27
27: ; preds = %22, %33
%28 = load i64, i64* %23
%29 = icmp slt i64 %28, %25
%30 = load i32, i32* %26
%31 = icmp ult i32 %30, %19
%32 = select i1 %29, i1 true, i1 %31
br i1 %32, label %33, label %40
33: ; preds = %27
%34 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%35 = icmp eq i32 %34, 0
br i1 %35, label %27, label %36
36: ; preds = %33, %18, %1
%37 = load i32, i32* @errno
%38 = tail call i32 @strerror(i32 %37)
%39 = tail call i32 @prerror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %38)
br label %40
40: ; preds = %27, %36
ret void
}