#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 scalar_t__ ssize_t;

/* Variables and functions */
#define EAGAIN 129
#define EINTR 128
scalar_t__ PlaybackDieNow(int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ printf(char *);
scalar_t__ usleep(int);
scalar_t__ write(int, void const *, size_t);
ssize_t write_with_retry(int fd, const void *buf, size_t size) {
  ssize_t ret;
  int retval = 0;
  while (size > 0 && 0 == PlaybackDieNow(0)) {
    ret = write(fd, buf, size);
    if (ret < 0) {
      switch (errno) {
      case EINTR:
      case EAGAIN:
        usleep(1000);
        continue;
      default:
        retval = -3;
        break;
      }
      if (retval < 0) {
        break;
      }
    }
    if (ret < 0) {
      return ret;
    }
    size -= ret;
    buf += ret;
    if (size > 0) {
      if (usleep(1000)) {
        do {
          printf("usleep error \n");
        } while (0);
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_with_retry>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x2d(%rbp)
jbe    117f <write_with_retry+0x3f>
xor    %edi,%edi
call   1050 <PlaybackDieNow@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
sete   %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    118f <write_with_retry+0x4f>
jmp    1264 <write_with_retry+0x124>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    11f2 <write_with_retry+0xb2>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
sub    $0x1,%eax
ja     11d7 <write_with_retry+0x97>
jmp    11c8 <write_with_retry+0x88>
mov    $0x3e8,%edi
call   1060 <usleep@plt>
jmp    115a <write_with_retry+0x1a>
movl   $0xfffffffd,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11ed <write_with_retry+0xad>
jmp    1264 <write_with_retry+0x124>
jmp    11f2 <write_with_retry+0xb2>
cmpq   $0x0,-0x28(%rbp)
jge    120a <write_with_retry+0xca>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    126c <write_with_retry+0x12c>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    125f <write_with_retry+0x11f>
mov    $0x3e8,%edi
call   1060 <usleep@plt>
cmp    $0x0,%rax
je     125a <write_with_retry+0x11a>
jmp    1249 <write_with_retry+0x109>
lea    0xdb0(%rip),%rdi
call   1040 <printf@plt>
jmp    125a <write_with_retry+0x11a>
jmp    125f <write_with_retry+0x11f>
jmp    115a <write_with_retry+0x1a>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_with_retry>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     1165 <write_with_retry+0x25>
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r12d
xor    %edi,%edi
call   1050 <PlaybackDieNow@plt>
test   %rax,%rax
je     1176 <write_with_retry+0x36>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e63(%rip),%r13
lea    0xe7c(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r12d,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <write@plt>
test   %rax,%rax
jns    11c9 <write_with_retry+0x89>
mov    0x0(%r13),%eax
and    $0xfffffffe,%eax
cmp    $0x80,%eax
jne    1165 <write_with_retry+0x25>
mov    $0x3e8,%edi
call   1060 <usleep@plt>
xor    %edi,%edi
call   1050 <PlaybackDieNow@plt>
test   %rax,%rax
je     1190 <write_with_retry+0x50>
jmp    1165 <write_with_retry+0x25>
mov    %rax,%rbp
sub    %rax,%rbx
je     1165 <write_with_retry+0x25>
mov    $0x3e8,%edi
call   1060 <usleep@plt>
test   %rax,%rax
je     11e8 <write_with_retry+0xa8>
mov    %r15,%rdi
call   1030 <puts@plt>
add    %rbp,%r14
jmp    11bb <write_with_retry+0x7b>

LLVM IR (-O0):
@errno = global i32 0
@.str = [15 x i8] c"usleep error \0A\00"
define i64 @write_with_retry(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 0, i32* %9
br label %10
10: ; preds = %57, %27, %3
%11 = load i64, i64* %7
%12 = icmp ugt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = call i64 @PlaybackDieNow(i32 0)
%15 = icmp eq i64 0, %14
br label %16
16: ; preds = %13, %10
%17 = phi i1 [ false, %10 ], [ %15, %13 ]
br i1 %17, label %18, label %58
18: ; preds = %16
%19 = load i32, i32* %5
%20 = load i8*, i8** %6
%21 = load i64, i64* %7
%22 = call i64 @write(i32 %19, i8* %20, i64 %21)
store i64 %22, i64* %8
%23 = load i64, i64* %8
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %35
25: ; preds = %18
%26 = load i32, i32* @errno
switch i32 %26, label %29 [
i32 128, label %27
i32 129, label %27
]
27: ; preds = %25, %25
%28 = call i64 @usleep(i32 1000)
br label %10
29: ; preds = %25
store i32 -3, i32* %9
br label %30
30: ; preds = %29
%31 = load i32, i32* %9
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %30
br label %58
34: ; preds = %30
br label %35
35: ; preds = %34, %18
%36 = load i64, i64* %8
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = load i64, i64* %8
store i64 %39, i64* %4
br label %59
40: ; preds = %35
%41 = load i64, i64* %8
%42 = load i64, i64* %7
%43 = sub i64 %42, %41
store i64 %43, i64* %7
%44 = load i64, i64* %8
%45 = load i8*, i8** %6
%46 = getelementptr i8, i8* %45, i64 %44
store i8* %46, i8** %6
%47 = load i64, i64* %7
%48 = icmp ugt i64 %47, 0
br i1 %48, label %49, label %57
49: ; preds = %40
%50 = call i64 @usleep(i32 1000)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %56
52: ; preds = %49
br label %53
53: ; preds = %52
%54 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %55
55: ; preds = %53
br label %56
56: ; preds = %55, %49
br label %57
57: ; preds = %56, %40
br label %10
58: ; preds = %33, %16
store i64 0, i64* %4
br label %59
59: ; preds = %58, %38
%60 = load i64, i64* %4
ret i64 %60
}

LLVM IR (-O2):
@errno = global i32 0
@str = [14 x i8] c"usleep error \00"
define i64 @write_with_retry(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %34, label %5
5: ; preds = %3
%6 = tail call i64 @PlaybackDieNow(i32 0)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %34
8: ; preds = %5, %31
%9 = phi i8* [ %24, %31 ], [ %1, %5 ]
%10 = phi i64 [ %23, %31 ], [ %2, %5 ]
br label %11
11: ; preds = %8, %18
%12 = tail call i64 @write(i32 %0, i8* %9, i64 %10)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %22
14: ; preds = %11
%15 = load i32, i32* @errno
%16 = and i32 %15, -2
%17 = icmp eq i32 %16, 128
br i1 %17, label %18, label %34
18: ; preds = %14
%19 = tail call i64 @usleep(i32 1000)
%20 = tail call i64 @PlaybackDieNow(i32 0)
%21 = icmp eq i64 %20, 0
br i1 %21, label %11, label %34
22: ; preds = %11
%23 = sub i64 %10, %12
%24 = getelementptr i8, i8* %9, i64 %12
%25 = icmp eq i64 %23, 0
br i1 %25, label %34, label %26
26: ; preds = %22
%27 = tail call i64 @usleep(i32 1000)
%28 = icmp eq i64 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %26
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %31
31: ; preds = %26, %29
%32 = tail call i64 @PlaybackDieNow(i32 0)
%33 = icmp eq i64 %32, 0
br i1 %33, label %8, label %34
34: ; preds = %22, %31, %18, %14, %5, %3
ret i64 0
}