#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 *, int);
int full_write(int fd, char *buf, int len) {
  int rv;
  int index = 0;
  while (len > 0) {
    rv = write(fd, buf + index, len);
    if (rv < 0)
      return rv;
    index += rv;
    len -= rv;
  }
  return 0;
}


x86 assembly (clang, -O0):
<full_write>:
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    117a <full_write+0x6a>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1161 <full_write+0x51>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1181 <full_write+0x71>
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 <full_write+0x19>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<full_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    114a <full_write+0x3a>
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
movslq %ebp,%rsi
add    %r14,%rsi
mov    %r15d,%edi
mov    %ebx,%edx
call   1030 <write@plt>
test   %eax,%eax
js     114c <full_write+0x3c>
add    %eax,%ebp
sub    %eax,%ebx
jg     1130 <full_write+0x20>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @full_write(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 = %25, %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* %9
%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* %8
%21 = load i32, i32* %8
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %13
%24 = load i32, i32* %8
store i32 %24, i32* %4
br label %33
25: ; preds = %13
%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
store i32 0, i32* %4
br label %33
33: ; preds = %32, %23
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
define i32 @full_write(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %16
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 slt i32 %10, 0
br i1 %11, label %16, label %12
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 = %5, %12, %3
%17 = phi i32 [ 0, %3 ], [ 0, %12 ], [ %10, %5 ]
ret i32 %17
}