#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 /*<<< orphan*/ printf(char *);
int write(int, char *, int);
void buffered_write(int chunk_size, int fd, char *buffer, int to_write) {
  printf("Write:\n");
  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;
  }
  printf("end\n");
}


x86 assembly (clang, -O0):
<buffered_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
lea    0xec4(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1198 <buffered_write+0x78>
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1165 <buffered_write+0x45>
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    1148 <buffered_write+0x28>
lea    0xe69(%rip),%rdi
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buffered_write>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r15d
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %edi,%ebp
lea    0xec6(%rip),%rdi
call   1030 <puts@plt>
test   %r15d,%r15d
je     116e <buffered_write+0x4e>
mov    $0x1,%r12d
nopw   0x0(%rax,%rax,1)
cmp    %ebp,%r15d
cmovl  %r12d,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
cltq
add    %rax,%rbx
sub    %eax,%r15d
jne    1150 <buffered_write+0x30>
lea    0xe92(%rip),%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"Write:\0A\00"
@.str.1 = [5 x i8] c"end\0A\00"
define void @buffered_write(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
%10 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %9
br label %11
11: ; preds = %19, %4
%12 = load i32, i32* %8
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %31
14: ; preds = %11
%15 = load i32, i32* %8
%16 = load i32, i32* %5
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %14
store i32 1, i32* %5
br label %19
19: ; preds = %18, %14
%20 = load i32, i32* %6
%21 = load i8*, i8** %7
%22 = load i32, i32* %5
%23 = call i32 @write(i32 %20, i8* %21, i32 %22)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = load i32, i32* %8
%26 = sub nsw i32 %25, %24
store i32 %26, i32* %8
%27 = load i32, i32* %9
%28 = load i8*, i8** %7
%29 = sext i32 %27 to i64
%30 = getelementptr inbounds i8, i8* %28, i64 %29
store i8* %30, i8** %7
br label %11
31: ; preds = %11
%32 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
ret void
}

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