#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 */

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EWOULDBLOCK;
int /*<<< orphan*/ assert(int);
scalar_t__ errno;
int /*<<< orphan*/ usleep(int);
int write(int const, void const *, int);
int write_persistent(const int fd, const void *p, int sz) {
  int rd = 0;
  int rt;
  while (sz > 0) {
    rt = write(fd, p, sz);
    if (rt < 0) {
      if (errno == EAGAIN || errno == EWOULDBLOCK) {
        usleep(1000);
        continue;
      }
      return rt;
    }
    if (rt == 0)
      break;
    assert(rt <= sz);
    p = (const void *)((const char *)p + rt);
    sz -= rt;
    rd += rt;
  }
  return rd;
}


x86 assembly (clang, -O0):
<write_persistent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1207 <write_persistent+0xd7>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11bd <write_persistent+0x8d>
mov    0x2e6a(%rip),%rax
mov    (%rax),%rax
mov    0x2e48(%rip),%rcx
cmp    (%rcx),%rax
je     11a3 <write_persistent+0x73>
mov    0x2e50(%rip),%rax
mov    (%rax),%rax
mov    0x2e3e(%rip),%rcx
cmp    (%rcx),%rax
jne    11b2 <write_persistent+0x82>
mov    $0x3e8,%edi
call   1050 <usleep@plt>
jmp    1149 <write_persistent+0x19>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120d <write_persistent+0xdd>
cmpl   $0x0,-0x1c(%rbp)
jne    11cc <write_persistent+0x9c>
jmp    1207 <write_persistent+0xd7>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
setle  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1149 <write_persistent+0x19>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_persistent>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    11bd <write_persistent+0x8d>
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %r12d,%r12d
mov    0x2e77(%rip),%r13
jmp    116a <write_persistent+0x3a>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0x3e8,%edi
call   1050 <usleep@plt>
mov    %r15d,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    119b <write_persistent+0x6b>
mov    0x2e5c(%rip),%rax
mov    (%rax),%rax
cmp    0x0(%r13),%rax
je     1160 <write_persistent+0x30>
mov    0x2e44(%rip),%rcx
cmp    (%rcx),%rax
je     1160 <write_persistent+0x30>
jmp    11bf <write_persistent+0x8f>
test   %ebp,%ebp
je     11d0 <write_persistent+0xa0>
xor    %edi,%edi
cmp    %ebp,%ebx
setae  %dil
call   1040 <assert@plt>
mov    %ebp,%eax
add    %rax,%r14
add    %ebp,%r12d
sub    %ebp,%ebx
mov    %r12d,%ebp
jg     116a <write_persistent+0x3a>
jmp    11bf <write_persistent+0x8f>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12d,%ebp
jmp    11bf <write_persistent+0x8f>

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
define i32 @write_persistent(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
br label %10
10: ; preds = %36, %28, %3
%11 = load i32, i32* %7
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %52
13: ; preds = %10
%14 = load i32, i32* %5
%15 = load i8*, i8** %6
%16 = load i32, i32* %7
%17 = call i32 @write(i32 %14, i8* %15, i32 %16)
store i32 %17, i32* %9
%18 = load i32, i32* %9
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %32
20: ; preds = %13
%21 = load i64, i64* @errno
%22 = load i64, i64* @EAGAIN
%23 = icmp eq i64 %21, %22
br i1 %23, label %28, label %24
24: ; preds = %20
%25 = load i64, i64* @errno
%26 = load i64, i64* @EWOULDBLOCK
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %30
28: ; preds = %24, %20
%29 = call i32 @usleep(i32 1000)
br label %10
30: ; preds = %24
%31 = load i32, i32* %9
store i32 %31, i32* %4
br label %54
32: ; preds = %13
%33 = load i32, i32* %9
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %32
br label %52
36: ; preds = %32
%37 = load i32, i32* %9
%38 = load i32, i32* %7
%39 = icmp sle i32 %37, %38
%40 = zext i1 %39 to i32
%41 = call i32 @assert(i32 %40)
%42 = load i8*, i8** %6
%43 = load i32, i32* %9
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
store i8* %45, i8** %6
%46 = load i32, i32* %9
%47 = load i32, i32* %7
%48 = sub nsw i32 %47, %46
store i32 %48, i32* %7
%49 = load i32, i32* %9
%50 = load i32, i32* %8
%51 = add nsw i32 %50, %49
store i32 %51, i32* %8
br label %10
52: ; preds = %35, %10
%53 = load i32, i32* %8
store i32 %53, i32* %4
br label %54
54: ; preds = %52, %30
%55 = load i32, i32* %4
ret i32 %55
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
define i32 @write_persistent(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %35
5: ; preds = %3, %26
%6 = phi i32 [ %33, %26 ], [ 0, %3 ]
%7 = phi i32 [ %32, %26 ], [ %2, %3 ]
%8 = phi i8* [ %31, %26 ], [ %1, %3 ]
%9 = tail call i32 @write(i32 %0, i8* %8, i32 %7)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %23
11: ; preds = %5, %19
%12 = phi i32 [ %21, %19 ], [ %9, %5 ]
%13 = load i64, i64* @errno
%14 = load i64, i64* @EAGAIN
%15 = icmp eq i64 %13, %14
%16 = load i64, i64* @EWOULDBLOCK
%17 = icmp eq i64 %13, %16
%18 = select i1 %15, i1 true, i1 %17
br i1 %18, label %19, label %35
19: ; preds = %11
%20 = tail call i32 @usleep(i32 1000)
%21 = tail call i32 @write(i32 %0, i8* %8, i32 %7)
%22 = icmp slt i32 %21, 0
br i1 %22, label %11, label %23
23: ; preds = %19, %5
%24 = phi i32 [ %9, %5 ], [ %21, %19 ]
%25 = icmp eq i32 %24, 0
br i1 %25, label %35, label %26
26: ; preds = %23
%27 = icmp uge i32 %7, %24
%28 = zext i1 %27 to i32
%29 = tail call i32 @assert(i32 %28)
%30 = zext i32 %24 to i64
%31 = getelementptr inbounds i8, i8* %8, i64 %30
%32 = sub nsw i32 %7, %24
%33 = add nsw i32 %24, %6
%34 = icmp sgt i32 %32, 0
br i1 %34, label %5, label %35
35: ; preds = %23, %26, %11, %3
%36 = phi i32 [ 0, %3 ], [ %12, %11 ], [ %33, %26 ], [ %6, %23 ]
ret i32 %36
}