#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, unsigned char *, int);
void write_buffer(int fd, unsigned char *data, int size) {
  int result = 0;
  int bytes_sent = 0;
  do {
    bytes_sent = write(fd, data + result, size - result);
    if (bytes_sent > 0)
      result += bytes_sent;
  } while (result < size);
}


x86 assembly (clang, -O0):
<write_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    115f <write_buffer+0x4f>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1164 <write_buffer+0x54>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     1130 <write_buffer+0x20>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r12d
xor    %r15d,%r15d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%esi
add    %r14,%rsi
mov    %ebx,%edx
sub    %ebp,%edx
mov    %r12d,%edi
call   1030 <write@plt>
test   %eax,%eax
cmovs  %r15d,%eax
add    %eax,%ebp
cmp    %ebx,%ebp
jl     1130 <write_buffer+0x20>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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