#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 fullWrite(int fd, const char *buf, int len) {
  int cc;
  int total;
  total = 0;
  while (len > 0) {
    cc = write(fd, buf, len);
    if (cc < 0)
      return -1;
    buf += cc;
    total += cc;
    len -= cc;
  }
  return total;
}


x86 assembly (clang, -O0):
<fullWrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1185 <fullWrite+0x75>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    115b <fullWrite+0x4b>
movl   $0xffffffff,-0x4(%rbp)
jmp    118b <fullWrite+0x7b>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    1129 <fullWrite+0x19>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fullWrite>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    114f <fullWrite+0x3f>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1154 <fullWrite+0x44>
mov    %eax,%ecx
add    %rcx,%rbx
add    %eax,%r14d
sub    %eax,%ebp
jg     1130 <fullWrite+0x20>
jmp    115a <fullWrite+0x4a>
xor    %r14d,%r14d
jmp    115a <fullWrite+0x4a>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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