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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ ltr_int_my_perror(char *);
int nanosleep(struct timespec *, struct timespec *);
void ltr_int_usleep(unsigned int usec) {
  struct timespec req, rem;
  req.tv_sec = usec / 1000000;
  req.tv_nsec = (usec % 1000000) * 1000;
  while (1) {
    if (nanosleep(&req, &rem) != -1) {
      break;
    } else {
      if (errno == EINTR) {
        req = rem;
        continue;
      } else {
        ltr_int_my_perror("nanosleep");
        break;
      }
    }
  }
}


x86 assembly (clang, -O0):
<ltr_int_usleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    $0xf4240,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    $0xf4240,%ecx
xor    %edx,%edx
div    %ecx
imul   $0x3e8,%edx,%eax
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
je     116a <ltr_int_usleep+0x4a>
jmp    119d <ltr_int_usleep+0x7d>
mov    0x2e6f(%rip),%rax
mov    (%rax),%rax
mov    0x2e5d(%rip),%rcx
cmp    (%rcx),%rax
jne    1191 <ltr_int_usleep+0x71>
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    114f <ltr_int_usleep+0x2f>
lea    0xe68(%rip),%rdi
call   1040 <ltr_int_my_perror@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ltr_int_usleep>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%eax
imul   $0x431bde83,%rax,%rax
shr    $0x32,%rax
mov    %eax,0x8(%rsp)
imul   $0xf4240,%eax,%eax
sub    %eax,%edi
imul   $0x3e8,%edi,%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
jne    11b1 <ltr_int_usleep+0x91>
mov    0x2e77(%rip),%r15
mov    0x2e68(%rip),%r12
lea    0x8(%rsp),%r14
lea    0x10(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    (%r15),%rax
cmp    (%r12),%rax
jne    11a5 <ltr_int_usleep+0x85>
mov    0x10(%rsp),%rax
mov    %rax,0x8(%rsp)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
je     1180 <ltr_int_usleep+0x60>
jmp    11b1 <ltr_int_usleep+0x91>
lea    0xe54(%rip),%rdi
call   1040 <ltr_int_my_perror@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
@.str = [10 x i8] c"nanosleep\00"
define void @ltr_int_usleep(i32 %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to %struct.timespec*
%4 = alloca i64
%5 = bitcast i64* %4 to %struct.timespec*
%6 = bitcast i64* %2 to i8*
%7 = bitcast i64* %4 to i8*
%8 = udiv i32 %0, 1000000
%9 = bitcast i64* %2 to i32*
store i32 %8, i32* %9
%10 = urem i32 %0, 1000000
%11 = mul nuw nsw i32 %10, 1000
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
store i32 %11, i32* %12
%13 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %5)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %25
15: ; preds = %1, %19
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINTR
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = load i64, i64* %4
store i64 %20, i64* %2
%21 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %5)
%22 = icmp eq i32 %21, -1
br i1 %22, label %15, label %25
23: ; preds = %15
%24 = call i32 @ltr_int_my_perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %19, %1, %23
ret void
}