#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 */
typedef scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ write(int, char *, size_t);
int flush_buf(int outfd, char *outbuf, size_t *outlen) {
  size_t writelen = 0;
  while (writelen < *outlen) {
    ssize_t len;
    len = write(outfd, outbuf + writelen, *outlen - writelen);
    if (len < 0) {
      perror("write");
      exit(1);
    }
    writelen += len;
  }
  *outlen = 0;
  return 0;
}


x86 assembly (clang, -O0):
<flush_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
jae    11ad <flush_buf+0x7d>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
sub    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    119c <flush_buf+0x6c>
lea    0xe73(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    114b <flush_buf+0x1b>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flush_buf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    (%rdx),%rdx
test   %rdx,%rdx
je     116f <flush_buf+0x3f>
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
lea    (%r14,%rbp,1),%rsi
sub    %rbp,%rdx
mov    %r15d,%edi
call   1030 <write@plt>
test   %rax,%rax
js     1183 <flush_buf+0x53>
add    %rax,%rbp
mov    (%rbx),%rdx
cmp    %rbp,%rdx
ja     1150 <flush_buf+0x20>
movq   $0x0,(%rbx)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe76(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"write\00"
define i32 @flush_buf(i32 %0, i8* %1, i64* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64*
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64* %2, i64** %6
store i64 0, i64* %7
br label %9
9: ; preds = %29, %3
%10 = load i64, i64* %7
%11 = load i64*, i64** %6
%12 = load i64, i64* %11
%13 = icmp ult i64 %10, %12
br i1 %13, label %14, label %33
14: ; preds = %9
%15 = load i32, i32* %4
%16 = load i8*, i8** %5
%17 = load i64, i64* %7
%18 = getelementptr inbounds i8, i8* %16, i64 %17
%19 = load i64*, i64** %6
%20 = load i64, i64* %19
%21 = load i64, i64* %7
%22 = sub i64 %20, %21
%23 = call i64 @write(i32 %15, i8* %18, i64 %22)
store i64 %23, i64* %8
%24 = load i64, i64* %8
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %14
%27 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %14
%30 = load i64, i64* %8
%31 = load i64, i64* %7
%32 = add i64 %31, %30
store i64 %32, i64* %7
br label %9
33: ; preds = %9
%34 = load i64*, i64** %6
store i64 0, i64* %34
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"write\00"
define i32 @flush_buf(i32 %0, i8* nocapture %1, i64* nocapture %2) {
%4 = load i64, i64* %2
%5 = icmp eq i64 %4, 0
br i1 %5, label %20, label %9
6: ; preds = %9
%7 = load i64, i64* %2
%8 = icmp ugt i64 %7, %16
br i1 %8, label %9, label %20
9: ; preds = %3, %6
%10 = phi i64 [ %7, %6 ], [ %4, %3 ]
%11 = phi i64 [ %16, %6 ], [ 0, %3 ]
%12 = getelementptr inbounds i8, i8* %1, i64 %11
%13 = sub i64 %10, %11
%14 = tail call i64 @write(i32 %0, i8* %12, i64 %13)
%15 = icmp slt i64 %14, 0
%16 = add i64 %14, %11
br i1 %15, label %17, label %6
17: ; preds = %9
%18 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %6, %3
store i64 0, i64* %2
ret i32 0
}