#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__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int write(int, char *, int);
int write_safe(int fd, char *data, int len) {
  int wrote_len = 0;
  while (wrote_len < len) {
    int ret = write(fd, data + wrote_len, len - wrote_len);
    if (ret == -1) {
      if (errno == EINTR || errno == EAGAIN)
        continue;
      perror("write_safe");
      return -1;
    }
    wrote_len += ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_safe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11ca <write_safe+0xaa>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11bc <write_safe+0x9c>
mov    0x2e6e(%rip),%rax
mov    (%rax),%rax
mov    0x2e5c(%rip),%rcx
cmp    (%rcx),%rax
je     119f <write_safe+0x7f>
mov    0x2e54(%rip),%rax
mov    (%rax),%rax
mov    0x2e32(%rip),%rcx
cmp    (%rcx),%rax
jne    11a4 <write_safe+0x84>
jmp    1139 <write_safe+0x19>
lea    0xe55(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d1 <write_safe+0xb1>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1139 <write_safe+0x19>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_safe>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    1197 <write_safe+0x77>
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
mov    0x2ea0(%rip),%r12
mov    0x2e91(%rip),%r13
jmp    1156 <write_safe+0x36>
nopl   0x0(%rax)
add    %eax,%ebp
cmp    %ebx,%ebp
jge    1197 <write_safe+0x77>
movslq %ebp,%rax
lea    (%r14,%rax,1),%rsi
mov    %ebx,%edx
sub    %eax,%edx
mov    %r15d,%edi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1150 <write_safe+0x30>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1152 <write_safe+0x32>
mov    0x2e49(%rip),%rcx
cmp    (%rcx),%rax
je     1152 <write_safe+0x32>
lea    0xe75(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
jmp    1199 <write_safe+0x79>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str = [11 x i8] c"write_safe\00"
define i32 @write_safe(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 = %37, %34, %3
%11 = load i32, i32* %8
%12 = load i32, i32* %7
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %41
14: ; preds = %10
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i32, i32* %8
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i32, i32* %7
%21 = load i32, i32* %8
%22 = sub nsw i32 %20, %21
%23 = call i32 @write(i32 %15, i8* %19, i32 %22)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %37
26: ; preds = %14
%27 = load i64, i64* @errno
%28 = load i64, i64* @EINTR
%29 = icmp eq i64 %27, %28
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = load i64, i64* @errno
%32 = load i64, i64* @EAGAIN
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %35
34: ; preds = %30, %26
br label %10
35: ; preds = %30
%36 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %42
37: ; preds = %14
%38 = load i32, i32* %9
%39 = load i32, i32* %8
%40 = add nsw i32 %39, %38
store i32 %40, i32* %8
br label %10
41: ; preds = %10
store i32 0, i32* %4
br label %42
42: ; preds = %41, %35
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str = [11 x i8] c"write_safe\00"
define i32 @write_safe(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %26
5: ; preds = %3, %23
%6 = phi i32 [ %24, %23 ], [ 0, %3 ]
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %1, i64 %7
%9 = sub nsw i32 %2, %6
%10 = tail call i32 @write(i32 %0, i8* %8, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %21
12: ; preds = %5
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %13, %14
%16 = load i64, i64* @EAGAIN
%17 = icmp eq i64 %13, %16
%18 = select i1 %15, i1 true, i1 %17
br i1 %18, label %23, label %19
19: ; preds = %12
%20 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %26
21: ; preds = %5
%22 = add nsw i32 %10, %6
br label %23
23: ; preds = %12, %21
%24 = phi i32 [ %22, %21 ], [ %6, %12 ]
%25 = icmp slt i32 %24, %2
br i1 %25, label %5, label %26
26: ; preds = %23, %3, %19
%27 = phi i32 [ -1, %19 ], [ 0, %3 ], [ 0, %23 ]
ret i32 %27
}