#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);
void write_buffered(int chunk_size, int fd, char *buffer, int to_write) {
  int w = 0;
  while (to_write) {
    if (to_write < chunk_size)
      chunk_size = 1;
    w = write(fd, buffer, chunk_size);
    to_write -= w;
    buffer += w;
  }
}


x86 assembly (clang, -O0):
<write_buffered>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     117c <write_buffered+0x6c>
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1149 <write_buffered+0x39>
movl   $0x1,-0x4(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x4(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    112c <write_buffered+0x1c>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_buffered>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %ecx,%ecx
je     114e <write_buffered+0x3e>
mov    %ecx,%r15d
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %edi,%ebp
mov    $0x1,%r12d
nopl   (%rax)
cmp    %ebp,%r15d
cmovl  %r12d,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cltq
add    %rax,%rbx
sub    %eax,%r15d
jne    1130 <write_buffered+0x20>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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