#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 /*<<< orphan*/ perror(char *);
int write(int, char const *, int);
int writeall(int fd, void *buf, int *len) {
  int nleft;
  int nwrite;
  int total;
  const char *ptr;
  ptr = buf;
  nleft = *len;
  total = 0;
  while (nleft > 0) {
    if ((nwrite = write(fd, ptr, *len)) == -1) {
      perror("writeall");
      break;
    }
    nleft -= nwrite;
    ptr += nwrite;
    total += nwrite;
    *len = nleft;
  }
  *len = total;
  return (nwrite == -1) ? -1 : 0;
}


x86 assembly (clang, -O0):
<writeall>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    11b7 <writeall+0x97>
mov    -0x4(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    1184 <writeall+0x64>
lea    0xe86(%rip),%rdi
call   1040 <perror@plt>
jmp    11b7 <writeall+0x97>
mov    -0x20(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    114b <writeall+0x2b>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%edx
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    $0xffffffff,%edx
cmove  %ecx,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeall>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    (%rdx),%ebp
test   %ebp,%ebp
jle    1164 <writeall+0x44>
mov    %rsi,%rbx
mov    %edi,%r15d
xor    %r12d,%r12d
nopw   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1175 <writeall+0x55>
movslq %eax,%rcx
add    %rcx,%rbx
add    %eax,%r12d
sub    %eax,%ebp
mov    %ebp,(%r14)
jg     1140 <writeall+0x20>
jmp    1167 <writeall+0x47>
xor    %r12d,%r12d
xor    %eax,%eax
mov    %r12d,(%r14)
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe84(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
jmp    1169 <writeall+0x49>

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

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