#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 size) {
  int bytes_write;
  int this_write;
  bytes_write = 0;
  do {
    this_write = write(fd, buf, size - bytes_write);
    if (this_write < 0) {
      return this_write;
    }
    bytes_write += this_write;
    buf += this_write;
  } while (bytes_write < size);
  return bytes_write;
}


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,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1153 <full_write+0x43>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    117f <full_write+0x6f>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     1129 <full_write+0x19>
mov    -0x18(%rbp),%eax
mov    %eax,-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
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %edi,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14d,%edx
sub    %ebx,%edx
mov    %r15d,%edi
mov    %rbp,%rsi
call   1030 <write@plt>
test   %eax,%eax
js     1152 <full_write+0x42>
add    %eax,%ebx
mov    %eax,%eax
add    %rax,%rbp
cmp    %r14d,%ebx
jl     1130 <full_write+0x20>
jmp    1154 <full_write+0x44>
mov    %eax,%ebx
mov    %ebx,%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* %8
br label %10
10: ; preds = %29, %3
%11 = load i32, i32* %5
%12 = load i8*, i8** %6
%13 = load i32, i32* %7
%14 = load i32, i32* %8
%15 = sub nsw i32 %13, %14
%16 = call i32 @write(i32 %11, i8* %12, i32 %15)
store i32 %16, i32* %9
%17 = load i32, i32* %9
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %10
%20 = load i32, i32* %9
store i32 %20, i32* %4
br label %35
21: ; preds = %10
%22 = load i32, i32* %9
%23 = load i32, i32* %8
%24 = add nsw i32 %23, %22
store i32 %24, i32* %8
%25 = load i32, i32* %9
%26 = load i8*, i8** %6
%27 = sext i32 %25 to i64
%28 = getelementptr inbounds i8, i8* %26, i64 %27
store i8* %28, i8** %6
br label %29
29: ; preds = %21
%30 = load i32, i32* %8
%31 = load i32, i32* %7
%32 = icmp slt i32 %30, %31
br i1 %32, label %10, label %33
33: ; preds = %29
%34 = load i32, i32* %8
store i32 %34, i32* %4
br label %35
35: ; preds = %33, %19
%36 = load i32, i32* %4
ret i32 %36
}

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