int writebuffer(int file, const char *buffer, int len) {
  int ret = 0;
  while (len > 0) {
    ret = write(file, buffer, len);
    if (ret <= 0) {
      printf("%s: failed for %p (%d) -> %d\n", __FUNCTION__, buffer, len, ret);
      goto out;
    }
    len -= ret;
    buffer += ret;
  }
out:
  return ret;
}


x86 assembly (clang, -O0):
<writebuffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11a7 <writebuffer+0x87>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    $0x0,%al
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     1186 <writebuffer+0x66>
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
mov    -0x18(%rbp),%r8d
lea    0xe8d(%rip),%rdi
lea    0xea4(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11ac <writebuffer+0x8c>
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    1139 <writebuffer+0x19>
jmp    11ac <writebuffer+0x8c>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writebuffer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    1163 <writebuffer+0x43>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1030 <write@plt>
mov    %eax,%r14d
test   %eax,%eax
jle    1168 <writebuffer+0x48>
mov    %r14d,%eax
add    %rax,%rbx
sub    %r14d,%ebp
jg     1140 <writebuffer+0x20>
jmp    1185 <writebuffer+0x65>
xor    %r14d,%r14d
jmp    1185 <writebuffer+0x65>
lea    0xe91(%rip),%rdi
lea    0xea8(%rip),%rsi
mov    %rbx,%rdx
mov    %ebp,%ecx
mov    %r14d,%r8d
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"%s: failed for %p (%d) -> %d\0A\00"
@__FUNCTION__.writebuffer = [12 x i8] c"writebuffer\00"
define i32 @writebuffer(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
br label %8
8: ; preds = %23, %3
%9 = load i32, i32* %6
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %31
11: ; preds = %8
%12 = load i32, i32* %4
%13 = load i8*, i8** %5
%14 = load i32, i32* %6
%15 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i32, ...)*)(i32 %12, i8* %13, i32 %14)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = icmp sle i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %11
%19 = load i8*, i8** %5
%20 = load i32, i32* %6
%21 = load i32, i32* %7
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @__FUNCTION__.writebuffer, i64 0, i64 0), i8* %19, i32 %20, i32 %21)
br label %32
23: ; preds = %11
%24 = load i32, i32* %7
%25 = load i32, i32* %6
%26 = sub nsw i32 %25, %24
store i32 %26, i32* %6
%27 = load i32, i32* %7
%28 = load i8*, i8** %5
%29 = sext i32 %27 to i64
%30 = getelementptr inbounds i8, i8* %28, i64 %29
store i8* %30, i8** %5
br label %8
31: ; preds = %8
br label %32
32: ; preds = %31, %18
%33 = load i32, i32* %7
ret i32 %33
}

LLVM IR (-O2):
@.str = [30 x i8] c"%s: failed for %p (%d) -> %d\0A\00"
@__FUNCTION__.writebuffer = [12 x i8] c"writebuffer\00"
define i32 @writebuffer(i32 %0, i8* %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %17
5: ; preds = %3, %12
%6 = phi i32 [ %13, %12 ], [ %2, %3 ]
%7 = phi i8* [ %15, %12 ], [ %1, %3 ]
%8 = tail call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i32, ...)*)(i32 %0, i8* %7, i32 %6)
%9 = icmp slt i32 %8, 1
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @__FUNCTION__.writebuffer, i64 0, i64 0), i8* %7, i32 %6, i32 %8)
br label %17
12: ; preds = %5
%13 = sub nsw i32 %6, %8
%14 = zext i32 %8 to i64
%15 = getelementptr inbounds i8, i8* %7, i64 %14
%16 = icmp sgt i32 %13, 0
br i1 %16, label %5, label %17
17: ; preds = %12, %3, %10
%18 = phi i32 [ %8, %10 ], [ 0, %3 ], [ %8, %12 ]
ret i32 %18
}