#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 {
  long tv_sec;
  long tv_nsec;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ nanosleep(struct timespec *, int /*<<< orphan*/ *);
int readlocked(int, int, char *, size_t, int /*<<< orphan*/ *);
int readlockedw(int fd, int fdadm, char *buffer, size_t size, long wtim,
                off_t *off) {
  ssize_t rres;
  struct timespec interval;
  interval.tv_sec = wtim / 1000;
  interval.tv_nsec = (wtim * 1000000) % 1000000000;
  while ((rres = readlocked(fd, fdadm, buffer, size, off)) == 0) {
    nanosleep(&interval, NULL);
  }
  return rres;
}


x86 assembly (clang, -O0):
<readlockedw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    %r9,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,-0x40(%rbp)
imul   $0xf4240,-0x20(%rbp),%rax
mov    $0x3b9aca00,%ecx
cqto
idiv   %rcx
mov    %rdx,-0x38(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x28(%rbp),%r8
call   1040 <readlocked@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jne    119b <readlockedw+0x7b>
lea    -0x40(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <nanosleep@plt>
jmp    1166 <readlockedw+0x46>
mov    -0x2c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readlockedw>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,%r14
mov    %rcx,%r15
mov    %rdx,%r12
mov    %esi,%ebp
mov    %edi,%ebx
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
mov    %r8,%rax
imul   %rcx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x7,%rdx
add    %rax,%rdx
mov    %rdx,0x8(%rsp)
imul   $0xf4240,%r8,%rcx
movabs $0x112e0be826d694b3,%rdx
0b 2e 11
mov    %rcx,%rax
imul   %rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x1a,%rdx
add    %rax,%rdx
imul   $0x3b9aca00,%rdx,%rax
sub    %rax,%rcx
mov    %rcx,0x10(%rsp)
mov    %r12,%rdx
mov    %r15,%rcx
mov    %r9,%r8
call   1040 <readlocked@plt>
test   %eax,%eax
jne    11d0 <readlockedw+0xb0>
lea    0x8(%rsp),%r13
nopl   0x0(%rax)
mov    %r13,%rdi
xor    %esi,%esi
call   1030 <nanosleep@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %r12,%rdx
mov    %r15,%rcx
mov    %r14,%r8
call   1040 <readlocked@plt>
test   %eax,%eax
je     11b0 <readlockedw+0x90>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
define i32 @readlockedw(i32 %0, i32 %1, i8* %2, i64 %3, i64 %4, i32* %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i32*
%13 = alloca i32
%14 = alloca %struct.timespec
store i32 %0, i32* %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i64 %3, i64* %10
store i64 %4, i64* %11
store i32* %5, i32** %12
%15 = load i64, i64* %11
%16 = sdiv i64 %15, 1000
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %14, i32 0, i32 0
store i64 %16, i64* %17
%18 = load i64, i64* %11
%19 = mul nsw i64 %18, 1000000
%20 = srem i64 %19, 1000000000
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %14, i32 0, i32 1
store i64 %20, i64* %21
br label %22
22: ; preds = %30, %6
%23 = load i32, i32* %7
%24 = load i32, i32* %8
%25 = load i8*, i8** %9
%26 = load i64, i64* %10
%27 = load i32*, i32** %12
%28 = call i32 @readlocked(i32 %23, i32 %24, i8* %25, i64 %26, i32* %27)
store i32 %28, i32* %13
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %22
%31 = call i32 @nanosleep(%struct.timespec* %14, i32* null)
br label %22
32: ; preds = %22
%33 = load i32, i32* %13
ret i32 %33
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
define i32 @readlockedw(i32 %0, i32 %1, i8* %2, i64 %3, i64 %4, i32* %5) {
%7 = alloca %struct.timespec
%8 = bitcast %struct.timespec* %7 to i8*
%9 = sdiv i64 %4, 1000
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i64 0, i32 0
store i64 %9, i64* %10
%11 = mul nsw i64 %4, 1000000
%12 = srem i64 %11, 1000000000
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i64 0, i32 1
store i64 %12, i64* %13
%14 = call i32 @readlocked(i32 %0, i32 %1, i8* %2, i64 %3, i32* %5)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %6, %16
%17 = call i32 @nanosleep(%struct.timespec* %7, i32* null)
%18 = call i32 @readlocked(i32 %0, i32 %1, i8* %2, i64 %3, i32* %5)
%19 = icmp eq i32 %18, 0
br i1 %19, label %16, label %20
20: ; preds = %16, %6
%21 = phi i32 [ %14, %6 ], [ %18, %16 ]
ret i32 %21
}