#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*/ perror(char *);
int write(int, char *, int);
int buf_flush(int out_fd, char *buffer, int buf_idx) {
  int count;
  while (buf_idx > 0) {
    count = write(out_fd, buffer, buf_idx);
    if (count < 0)
      perror("write");
    buf_idx -= count;
  }
  return buf_idx;
}


x86 assembly (clang, -O0):
<buf_flush>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1174 <buf_flush+0x54>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1164 <buf_flush+0x44>
lea    0xea1(%rip),%rdi
call   1040 <perror@plt>
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    1132 <buf_flush+0x12>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buf_flush>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebx
test   %edx,%edx
jle    1161 <buf_flush+0x41>
mov    %rsi,%r14
mov    %edi,%r12d
lea    0xec5(%rip),%r15
jmp    1144 <buf_flush+0x24>
nopl   (%rax)
sub    %ebp,%ebx
jle    1161 <buf_flush+0x41>
mov    %r12d,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1140 <buf_flush+0x20>
mov    %r15,%rdi
call   1040 <perror@plt>
jmp    1140 <buf_flush+0x20>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"write\00"
define i32 @buf_flush(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
br label %8
8: ; preds = %20, %3
%9 = load i32, i32* %6
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %24
11: ; preds = %8
%12 = load i32, i32* %4
%13 = load i8*, i8** %5
%14 = load i32, i32* %6
%15 = call i32 @write(i32 %12, i8* %13, i32 %14)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %11
%21 = load i32, i32* %7
%22 = load i32, i32* %6
%23 = sub nsw i32 %22, %21
store i32 %23, i32* %6
br label %8
24: ; preds = %8
%25 = load i32, i32* %6
ret i32 %25
}

LLVM IR (-O2):
@.str = [6 x i8] c"write\00"
define i32 @buf_flush(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %14
5: ; preds = %3, %11
%6 = phi i32 [ %12, %11 ], [ %2, %3 ]
%7 = tail call i32 @write(i32 %0, i8* %1, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %5
%12 = sub nsw i32 %6, %7
%13 = icmp sgt i32 %12, 0
br i1 %13, label %5, label %14
14: ; preds = %11, %3
%15 = phi i32 [ %2, %3 ], [ %12, %11 ]
ret i32 %15
}