#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 uint32_t;
struct timespec {
  scalar_t__ tv_sec;
  int tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ nanosleep(struct timespec *, struct timespec *);
int /*<<< orphan*/ printf(char *);
void delay_us(uint32_t delay) {
  struct timespec tv_req;
  struct timespec tv_rem;
  int i;
  uint32_t del_ms, del_us;
  del_ms = delay / 1000;
  del_us = delay % 1000;
  for (i = 0; i <= del_ms; i++) {
    tv_req.tv_sec = 0;
    if (i == del_ms)
      tv_req.tv_nsec = del_us * 1000;
    else
      tv_req.tv_nsec = 1000000;
    tv_rem.tv_sec = 0;
    tv_rem.tv_nsec = 0;
    nanosleep(&tv_req, &tv_rem);
    if (tv_rem.tv_sec != 0 || tv_rem.tv_nsec != 0)
      printf("timer oops!\n");
  }
}


x86 assembly (clang, -O0):
<delay_us>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x30(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %edx,-0x34(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jg     11d4 <delay_us+0xb4>
movq   $0x0,-0x18(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jne    117d <delay_us+0x5d>
imul   $0x3e8,-0x34(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1184 <delay_us+0x64>
movl   $0xf4240,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x20(%rbp)
lea    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <nanosleep@plt>
cmpq   $0x0,-0x28(%rbp)
jne    11b5 <delay_us+0x95>
cmpl   $0x0,-0x20(%rbp)
je     11c1 <delay_us+0xa1>
lea    0xe44(%rip),%rdi
call   1030 <printf@plt>
jmp    11c6 <delay_us+0xa6>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    114e <delay_us+0x2e>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<delay_us>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
cmp    $0xfffffc19,%edi
jge    1141 <delay_us+0x21>
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
movslq %edi,%rax
imul   $0x10624dd3,%rax,%rbx
mov    %rbx,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rbx
add    %ecx,%ebx
imul   $0x3e8,%ebx,%ecx
sub    %ecx,%eax
imul   $0x3e8,%eax,%ebp
add    $0x1,%ebx
lea    0x10(%rsp),%r14
mov    %rsp,%r12
lea    0xe88(%rip),%r15
jmp    118d <delay_us+0x6d>
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1030 <puts@plt>
add    $0xffffffff,%ebx
je     1134 <delay_us+0x14>
cmp    $0x1,%ebx
mov    $0xf4240,%eax
cmove  %ebp,%eax
movq   $0x0,0x10(%rsp)
mov    %eax,0x18(%rsp)
movq   $0x0,(%rsp)
movl   $0x0,0x8(%rsp)
mov    %r14,%rdi
mov    %r12,%rsi
call   1040 <nanosleep@plt>
cmpq   $0x0,(%rsp)
jne    1180 <delay_us+0x60>
cmpl   $0x0,0x8(%rsp)
je     1188 <delay_us+0x68>
jmp    1180 <delay_us+0x60>

LLVM IR (-O0):
%struct.timespec = type { i64, i32 }
@.str = [13 x i8] c"timer oops!\0A\00"
define void @delay_us(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %2
%8 = load i32, i32* %2
%9 = sdiv i32 %8, 1000
store i32 %9, i32* %6
%10 = load i32, i32* %2
%11 = srem i32 %10, 1000
store i32 %11, i32* %7
store i32 0, i32* %5
br label %12
12: ; preds = %41, %1
%13 = load i32, i32* %5
%14 = load i32, i32* %6
%15 = icmp sle i32 %13, %14
br i1 %15, label %16, label %44
16: ; preds = %12
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i64 0, i64* %17
%18 = load i32, i32* %5
%19 = load i32, i32* %6
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = load i32, i32* %7
%23 = mul nsw i32 %22, 1000
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i32 %23, i32* %24
br label %27
25: ; preds = %16
%26 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i32 1000000, i32* %26
br label %27
27: ; preds = %25, %21
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i64 0, i64* %28
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i32 0, i32* %29
%30 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %4)
%31 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%32 = load i64, i64* %31
%33 = icmp ne i64 %32, 0
br i1 %33, label %38, label %34
34: ; preds = %27
%35 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%36 = load i32, i32* %35
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34, %27
%39 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %40
40: ; preds = %38, %34
br label %41
41: ; preds = %40
%42 = load i32, i32* %5
%43 = add nsw i32 %42, 1
store i32 %43, i32* %5
br label %12
44: ; preds = %12
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i64, i32 }
@str = [12 x i8] c"timer oops!\00"
define void @delay_us(i32 %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %2 to i8*
%5 = bitcast %struct.timespec* %3 to i8*
%6 = sdiv i32 %0, 1000
%7 = srem i32 %0, 1000
%8 = icmp slt i32 %0, -999
br i1 %8, label %30, label %9
9: ; preds = %1
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%12 = mul nsw i32 %7, 1000
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
br label %15
15: ; preds = %9, %27
%16 = phi i32 [ 0, %9 ], [ %28, %27 ]
store i64 0, i64* %10
%17 = icmp eq i32 %16, %6
%18 = select i1 %17, i32 %12, i32 1000000
store i32 %18, i32* %11
store i64 0, i64* %13
store i32 0, i32* %14
%19 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%20 = load i64, i64* %13
%21 = icmp ne i64 %20, 0
%22 = load i32, i32* %14
%23 = icmp ne i32 %22, 0
%24 = select i1 %21, i1 true, i1 %23
br i1 %24, label %25, label %27
25: ; preds = %15
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %27
27: ; preds = %25, %15
%28 = add nuw nsw i32 %16, 1
%29 = icmp eq i32 %16, %6
br i1 %29, label %30, label %15
30: ; preds = %27, %1
ret void
}