#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 {
  int tv_nsec;
  scalar_t__ tv_sec;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int nanosleep(struct timespec *, struct timespec *);
void wait_microseconds(int microseconds) {
  struct timespec time = {0};
  struct timespec remaining;
  uint32_t nanoseconds = microseconds * 1000;
  int err = 0;
  time.tv_sec = 0;
  time.tv_nsec = nanoseconds;
  errno = 0;
  err = nanosleep(&time, &remaining);
  while (err == -1 && errno == EINTR) {
    errno = 0;
    time = remaining;
    err = nanosleep(&time, &remaining);
  }
  return;
}


x86 assembly (clang, -O0):
<wait_microseconds>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
imul   $0x3e8,-0x4(%rbp),%eax
mov    %eax,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e7f(%rip),%rax
movq   $0x0,(%rax)
lea    -0x20(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1030 <nanosleep@plt>
mov    %eax,-0x40(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x40(%rbp)
mov    %al,-0x41(%rbp)
jne    11a1 <wait_microseconds+0x81>
mov    0x2e52(%rip),%rax
mov    (%rax),%rax
mov    0x2e40(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    11b1 <wait_microseconds+0x91>
jmp    11ec <wait_microseconds+0xcc>
mov    0x2e28(%rip),%rax
movq   $0x0,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1030 <nanosleep@plt>
mov    %eax,-0x40(%rbp)
jmp    1178 <wait_microseconds+0x58>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_microseconds>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x38,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movq   $0x0,0x10(%rsp)
imul   $0x3e8,%edi,%eax
movq   $0x0,0x8(%rsp)
mov    %eax,(%rsp)
mov    0x2e9c(%rip),%rbx
movq   $0x0,(%rbx)
mov    %rsp,%rdi
lea    0x20(%rsp),%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
jne    11b3 <wait_microseconds+0xa3>
mov    0x2e74(%rip),%r12
mov    (%r12),%rax
cmp    %rax,(%rbx)
jne    11b3 <wait_microseconds+0xa3>
mov    %rsp,%r14
lea    0x20(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nop
movq   $0x0,(%rbx)
movups 0x20(%rsp),%xmm0
movaps %xmm0,(%rsp)
mov    0x30(%rsp),%rax
mov    %rax,0x10(%rsp)
mov    %r14,%rdi
mov    %r15,%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
jne    11b3 <wait_microseconds+0xa3>
mov    (%r12),%rax
cmp    %rax,(%rbx)
je     1180 <wait_microseconds+0x70>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i64, i32 }
@errno = global i64 0
@EINTR = global i64 0
define void @wait_microseconds(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
%7 = bitcast %struct.timespec* %3 to i8*
%8 = load i32, i32* %2
%9 = mul nsw i32 %8, 1000
store i32 %9, i32* %5
store i32 0, i32* %6
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i64 0, i64* %10
%11 = load i32, i32* %5
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 %11, i32* %12
store i64 0, i64* @errno
%13 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %4)
store i32 %13, i32* %6
br label %14
14: ; preds = %23, %1
%15 = load i32, i32* %6
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
br label %21
21: ; preds = %17, %14
%22 = phi i1 [ false, %14 ], [ %20, %17 ]
br i1 %22, label %23, label %27
23: ; preds = %21
store i64 0, i64* @errno
%24 = bitcast %struct.timespec* %3 to i8*
%25 = bitcast %struct.timespec* %4 to i8*
%26 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %4)
store i32 %26, i32* %6
br label %14
27: ; preds = %21
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64, i32 }
@errno = global i64 0
@EINTR = global i64 0
define void @wait_microseconds(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 = mul nsw i32 %0, 1000
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 0, i64* %7
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 %6, i32* %8
store i64 0, i64* @errno
%9 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%10 = icmp eq i32 %9, -1
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
%14 = select i1 %10, i1 %13, i1 false
br i1 %14, label %15, label %22
15: ; preds = %1, %15
store i64 0, i64* @errno
%16 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%17 = icmp eq i32 %16, -1
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
%21 = select i1 %17, i1 %20, i1 false
br i1 %21, label %15, label %22
22: ; preds = %15, %1
ret void
}