#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__ EINTR;
scalar_t__ errno;
int write(int, void *, int);
int MyWrite(int fd, void *data, int len) {
  int result, left;
  left = len;
  while (left > 0) {
    result = write(fd, data, left);
    if (result > 0) {
      data = ((char *)data) + result;
      left -= result;
    } else if (errno != EINTR)
      return result;
  }
  return len;
}


x86 assembly (clang, -O0):
<MyWrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    119c <MyWrite+0x8c>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    116d <MyWrite+0x5d>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
jmp    1197 <MyWrite+0x87>
mov    0x2e6c(%rip),%rax
mov    (%rax),%rax
mov    0x2e5a(%rip),%rcx
cmp    (%rcx),%rax
je     1192 <MyWrite+0x82>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a2 <MyWrite+0x92>
jmp    1197 <MyWrite+0x87>
jmp    1128 <MyWrite+0x18>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<MyWrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
test   %edx,%edx
jle    1169 <MyWrite+0x59>
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2eb1(%rip),%r12
mov    0x2ea2(%rip),%r13
mov    %r14d,%ebp
jmp    114b <MyWrite+0x3b>
nopl   0x0(%rax,%rax,1)
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
test   %ebp,%ebp
jle    1169 <MyWrite+0x59>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
jg     1140 <MyWrite+0x30>
mov    (%r12),%rcx
cmp    0x0(%r13),%rcx
je     1147 <MyWrite+0x37>
mov    %eax,%r14d
mov    %r14d,%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
define i32 @MyWrite(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
%10 = load i32, i32* %7
store i32 %10, i32* %9
br label %11
11: ; preds = %36, %3
%12 = load i32, i32* %9
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %37
14: ; preds = %11
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i32, i32* %9
%18 = call i32 @write(i32 %15, i8* %16, i32 %17)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %14
%22 = load i8*, i8** %6
%23 = load i32, i32* %8
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
store i8* %25, i8** %6
%26 = load i32, i32* %8
%27 = load i32, i32* %9
%28 = sub nsw i32 %27, %26
store i32 %28, i32* %9
br label %36
29: ; preds = %14
%30 = load i64, i64* @errno
%31 = load i64, i64* @EINTR
%32 = icmp ne i64 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = load i32, i32* %8
store i32 %34, i32* %4
br label %39
35: ; preds = %29
br label %36
36: ; preds = %35, %21
br label %11
37: ; preds = %11
%38 = load i32, i32* %7
store i32 %38, i32* %4
br label %39
39: ; preds = %37, %33
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i32 @MyWrite(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %22
5: ; preds = %3, %18
%6 = phi i32 [ %20, %18 ], [ %2, %3 ]
%7 = phi i8* [ %19, %18 ], [ %1, %3 ]
%8 = tail call i32 @write(i32 %0, i8* %7, i32 %6)
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %5
%11 = zext i32 %8 to i64
%12 = getelementptr inbounds i8, i8* %7, i64 %11
%13 = sub nsw i32 %6, %8
br label %18
14: ; preds = %5
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %22
18: ; preds = %14, %10
%19 = phi i8* [ %12, %10 ], [ %7, %14 ]
%20 = phi i32 [ %13, %10 ], [ %6, %14 ]
%21 = icmp sgt i32 %20, 0
br i1 %21, label %5, label %22
22: ; preds = %14, %18, %3
%23 = phi i32 [ %2, %3 ], [ %2, %18 ], [ %8, %14 ]
ret i32 %23
}