#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 /*<<< orphan*/ perror(char *);
int write(int, void const *, int);
int fixed_write(int s, const void *buf, int len) {
  int written = 0;
  while (written < len) {
    int ret;
    ret = write(s, buf + written, len - written);
    if (ret < 0) {
      if (errno != EINTR) {
        perror("write");
        written = -1;
        break;
      }
    } else {
      written += ret;
    }
  }
  return written;
}


x86 assembly (clang, -O0):
<fixed_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11b0 <fixed_write+0x90>
mov    -0x4(%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   $0x0,-0x1c(%rbp)
jge    11a2 <fixed_write+0x82>
mov    0x2e6e(%rip),%rax
mov    (%rax),%rax
mov    0x2e5c(%rip),%rcx
cmp    (%rcx),%rax
je     119d <fixed_write+0x7d>
lea    0xe74(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x18(%rbp)
jmp    11b0 <fixed_write+0x90>
jmp    11ab <fixed_write+0x8b>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1139 <fixed_write+0x19>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fixed_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    118a <fixed_write+0x6a>
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebx,%ebx
mov    0x2ea0(%rip),%r12
mov    0x2e91(%rip),%r13
jmp    1156 <fixed_write+0x36>
nopl   0x0(%rax)
add    %eax,%ebx
cmp    %ebp,%ebx
jge    118c <fixed_write+0x6c>
movslq %ebx,%rax
lea    (%r14,%rax,1),%rsi
mov    %ebp,%edx
sub    %eax,%edx
mov    %r15d,%edi
call   1030 <write@plt>
test   %eax,%eax
jns    1150 <fixed_write+0x30>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1152 <fixed_write+0x32>
lea    0xe82(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%ebx
jmp    118c <fixed_write+0x6c>
xor    %ebx,%ebx
mov    %ebx,%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
@.str = [6 x i8] c"write\00"
define i32 @fixed_write(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
br label %9
9: ; preds = %36, %3
%10 = load i32, i32* %7
%11 = load i32, i32* %6
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %37
13: ; preds = %9
%14 = load i32, i32* %4
%15 = load i8*, i8** %5
%16 = load i32, i32* %7
%17 = sext i32 %16 to i64
%18 = getelementptr i8, i8* %15, i64 %17
%19 = load i32, i32* %6
%20 = load i32, i32* %7
%21 = sub nsw i32 %19, %20
%22 = call i32 @write(i32 %14, i8* %18, i32 %21)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %13
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINTR
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %7
br label %37
31: ; preds = %25
br label %36
32: ; preds = %13
%33 = load i32, i32* %8
%34 = load i32, i32* %7
%35 = add nsw i32 %34, %33
store i32 %35, i32* %7
br label %36
36: ; preds = %32, %31
br label %9
37: ; preds = %29, %9
%38 = load i32, i32* %7
ret i32 %38
}

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