#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 errno;
int write(int, unsigned char *, size_t);
int proxy_write(int fd, unsigned char *buf, size_t len) {
  size_t count;
  for (count = 0; count < len;) {
    int i = write(fd, buf + count, len - count);
    if (i < 0)
      return -errno;
    count += i;
  }
  return 0;
}


x86 assembly (clang, -O0):
<proxy_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1182 <proxy_write+0x72>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1171 <proxy_write+0x61>
xor    %eax,%eax
mov    0x2e79(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1189 <proxy_write+0x79>
movslq -0x24(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    112b <proxy_write+0x1b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<proxy_write>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %edi,%r12d
xor    %r14d,%r14d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
cmp    %rbx,%rbp
jae    115d <proxy_write+0x4d>
lea    (%r15,%rbp,1),%rsi
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    %r12d,%edi
call   1030 <write@plt>
cltq
add    %rax,%rbp
test   %eax,%eax
jns    1130 <proxy_write+0x20>
mov    0x2e89(%rip),%rax
xor    %r14d,%r14d
sub    (%rax),%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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