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

/* Variables and functions */
int write(int, char *, int);
ssize_t write_all(int fd, char *buff, int len) {
  ssize_t wrote = 0, w = 0;
  while (len > 0 && (w = write(fd, buff + wrote, len)) > 0) {
    wrote += w;
    len -= w;
  }
  if (w == -1) {
    return -1;
  }
  return wrote;
}


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)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x1d(%rbp)
jle    1161 <write_all+0x51>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
setg   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1171 <write_all+0x61>
jmp    118a <write_all+0x7a>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    1130 <write_all+0x20>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11a0 <write_all+0x90>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a6 <write_all+0x96>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
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
test   %edx,%edx
jle    1152 <write_all+0x42>
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
movslq %ebx,%rsi
add    %r14,%rsi
mov    %r15d,%edi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
jle    114a <write_all+0x3a>
add    %eax,%ebx
sub    %eax,%ebp
jg     1130 <write_all+0x20>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
jmp    1154 <write_all+0x44>
xor    %ebx,%ebx
mov    %ebx,%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
store i32 0, i32* %8
store i32 0, i32* %9
br label %10
10: ; preds = %24, %3
%11 = load i32, i32* %7
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %22
13: ; preds = %10
%14 = load i32, i32* %5
%15 = load i8*, i8** %6
%16 = load i32, i32* %8
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = load i32, i32* %7
%20 = call i32 @write(i32 %14, i8* %18, i32 %19)
store i32 %20, i32* %9
%21 = icmp sgt i32 %20, 0
br label %22
22: ; preds = %13, %10
%23 = phi i1 [ false, %10 ], [ %21, %13 ]
br i1 %23, label %24, label %31
24: ; preds = %22
%25 = load i32, i32* %9
%26 = load i32, i32* %8
%27 = add nsw i32 %26, %25
store i32 %27, i32* %8
%28 = load i32, i32* %9
%29 = load i32, i32* %7
%30 = sub nsw i32 %29, %28
store i32 %30, i32* %7
br label %10
31: ; preds = %22
%32 = load i32, i32* %9
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %35
34: ; preds = %31
store i32 -1, i32* %4
br label %37
35: ; preds = %31
%36 = load i32, i32* %8
store i32 %36, i32* %4
br label %37
37: ; preds = %35, %34
%38 = load i32, i32* %4
ret i32 %38
}

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