#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 */
int write(int, char const *, int);
int write_all(int fd, const char *buf, int len) {
  int left = len;
  int written;
  for (;;) {
    written = write(fd, buf, left);
    if (written == left) {
      return len;
    }
    if (written < 0) {
      return -1;
    }
    left -= written;
    buf += written;
  }
}


x86 assembly (clang, -O0):
<write_all>:
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,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jne    1151 <write_all+0x41>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1188 <write_all+0x78>
cmpl   $0x0,-0x1c(%rbp)
jge    1167 <write_all+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    1188 <write_all+0x78>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1128 <write_all+0x18>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_all>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%r15d
call   1030 <write@plt>
cmp    %r14d,%eax
je     1154 <write_all+0x44>
mov    %r14d,%ebp
nopl   (%rax)
test   %eax,%eax
js     114e <write_all+0x3e>
sub    %eax,%ebp
mov    %eax,%eax
add    %rax,%rbx
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %eax,%ebp
jne    1130 <write_all+0x20>
jmp    1154 <write_all+0x44>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @write_all(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* %8
br label %11
11: ; preds = %25, %3
%12 = load i32, i32* %5
%13 = load i8*, i8** %6
%14 = load i32, i32* %8
%15 = call i32 @write(i32 %12, i8* %13, i32 %14)
store i32 %15, i32* %9
%16 = load i32, i32* %9
%17 = load i32, i32* %8
%18 = icmp eq i32 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %11
%20 = load i32, i32* %7
store i32 %20, i32* %4
br label %33
21: ; preds = %11
%22 = load i32, i32* %9
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %21
store i32 -1, i32* %4
br label %33
25: ; preds = %21
%26 = load i32, i32* %9
%27 = load i32, i32* %8
%28 = sub nsw i32 %27, %26
store i32 %28, i32* %8
%29 = load i32, i32* %9
%30 = load i8*, i8** %6
%31 = sext i32 %29 to i64
%32 = getelementptr inbounds i8, i8* %30, i64 %31
store i8* %32, i8** %6
br label %11
33: ; preds = %24, %19
%34 = load i32, i32* %4
ret i32 %34
}

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