#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 strlen(char const *);
int write(int, char const *, int);
int write_buffer(int fd, const char *buffer) {
  int to_write = strlen(buffer);
  int written = 0;
  while (written < to_write) {
    int ret;
    ret = write(fd, buffer + written, to_write);
    if (ret) {
      if (ret > 0) {
        to_write -= ret;
        written += ret;
      } else {
        printf("Error writing buffer\n");
        return -1;
      }
    } else
      break;
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11cb <write_buffer+0x9b>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11c1 <write_buffer+0x91>
cmpl   $0x0,-0x1c(%rbp)
jle    11a4 <write_buffer+0x74>
mov    -0x1c(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11bc <write_buffer+0x8c>
lea    0xe55(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d2 <write_buffer+0xa2>
jmp    11c6 <write_buffer+0x96>
jmp    11cb <write_buffer+0x9b>
jmp    1152 <write_buffer+0x22>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r12
mov    %edi,%r15d
mov    %rsi,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
jle    1180 <write_buffer+0x50>
mov    %eax,%ebp
xor    %r14d,%r14d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movslq %ebx,%rsi
add    %r12,%rsi
mov    %r15d,%edi
mov    %ebp,%edx
call   1040 <write@plt>
test   %eax,%eax
je     1197 <write_buffer+0x67>
jle    1185 <write_buffer+0x55>
sub    %eax,%ebp
add    %eax,%ebx
cmp    %ebp,%ebx
jl     1160 <write_buffer+0x30>
jmp    1197 <write_buffer+0x67>
xor    %r14d,%r14d
jmp    1197 <write_buffer+0x67>
lea    0xe74(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"Error writing buffer\0A\00"
define i32 @write_buffer(i32 %0, i8* %1) {
%3 = alloca i32
%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
%9 = load i8*, i8** %5
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %6
store i32 0, i32* %7
br label %11
11: ; preds = %39, %2
%12 = load i32, i32* %7
%13 = load i32, i32* %6
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %40
15: ; preds = %11
%16 = load i32, i32* %4
%17 = load i8*, i8** %5
%18 = load i32, i32* %7
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i32, i32* %6
%22 = call i32 @write(i32 %16, i8* %20, i32 %21)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %38
25: ; preds = %15
%26 = load i32, i32* %8
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %25
%29 = load i32, i32* %8
%30 = load i32, i32* %6
%31 = sub nsw i32 %30, %29
store i32 %31, i32* %6
%32 = load i32, i32* %8
%33 = load i32, i32* %7
%34 = add nsw i32 %33, %32
store i32 %34, i32* %7
br label %37
35: ; preds = %25
%36 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %41
37: ; preds = %28
br label %39
38: ; preds = %15
br label %40
39: ; preds = %37
br label %11
40: ; preds = %38, %11
store i32 0, i32* %3
br label %41
41: ; preds = %40, %35
%42 = load i32, i32* %3
ret i32 %42
}

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