#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*/ assert(int);
scalar_t__ flush_buf(int, char *, size_t *);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int append_buf(const char *str, size_t len, int outfd, char *outbuf,
               size_t *outlen) {
  assert(len <= 524288);
  if (*outlen + len > 524288)
    if (flush_buf(outfd, outbuf, outlen) < 0)
      return -1;
  memcpy(outbuf + *outlen, str, len);
  *outlen += len;
  return 0;
}


x86 assembly (clang, -O0):
<append_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
cmpq   $0x80000,-0x18(%rbp)
setbe  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
add    -0x18(%rbp),%rax
cmp    $0x80000,%rax
jbe    11a2 <append_buf+0x72>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1030 <flush_buf@plt>
cmp    $0x0,%rax
jge    119d <append_buf+0x6d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cf <append_buf+0x9f>
jmp    11a2 <append_buf+0x72>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rax
add    (%rax),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rcx
mov    -0x30(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_buf>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8,%r14
mov    %rcx,%r15
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r12
xor    %edi,%edi
cmp    $0x80001,%rsi
setb   %dil
call   1040 <assert@plt>
mov    (%r14),%rax
lea    (%rax,%rbx,1),%rcx
cmp    $0x80001,%rcx
jb     117d <append_buf+0x4d>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <flush_buf@plt>
test   %rax,%rax
js     1195 <append_buf+0x65>
mov    (%r14),%rax
add    %rax,%r15
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
add    %rbx,(%r14)
xor    %eax,%eax
jmp    119a <append_buf+0x6a>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @append_buf(i8* %0, i64 %1, i32 %2, i8* %3, i64* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64*
store i8* %0, i8** %7
store i64 %1, i64* %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i64* %4, i64** %11
%12 = load i64, i64* %8
%13 = icmp ule i64 %12, 524288
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = load i64*, i64** %11
%17 = load i64, i64* %16
%18 = load i64, i64* %8
%19 = add i64 %17, %18
%20 = icmp ugt i64 %19, 524288
br i1 %20, label %21, label %29
21: ; preds = %5
%22 = load i32, i32* %9
%23 = load i8*, i8** %10
%24 = load i64*, i64** %11
%25 = call i64 @flush_buf(i32 %22, i8* %23, i64* %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %21
store i32 -1, i32* %6
br label %41
28: ; preds = %21
br label %29
29: ; preds = %28, %5
%30 = load i8*, i8** %10
%31 = load i64*, i64** %11
%32 = load i64, i64* %31
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i8*, i8** %7
%35 = load i64, i64* %8
%36 = call i32 @memcpy(i8* %33, i8* %34, i64 %35)
%37 = load i64, i64* %8
%38 = load i64*, i64** %11
%39 = load i64, i64* %38
%40 = add i64 %39, %37
store i64 %40, i64* %38
store i32 0, i32* %6
br label %41
41: ; preds = %29, %27
%42 = load i32, i32* %6
ret i32 %42
}

LLVM IR (-O2):
define i32 @append_buf(i8* %0, i64 %1, i32 %2, i8* %3, i64* %4) {
%6 = icmp ult i64 %1, 524289
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = load i64, i64* %4
%10 = add i64 %9, %1
%11 = icmp ugt i64 %10, 524288
br i1 %11, label %12, label %17
12: ; preds = %5
%13 = tail call i64 @flush_buf(i32 %2, i8* %3, i64* %4)
%14 = icmp slt i64 %13, 0
br i1 %14, label %23, label %15
15: ; preds = %12
%16 = load i64, i64* %4
br label %17
17: ; preds = %15, %5
%18 = phi i64 [ %16, %15 ], [ %9, %5 ]
%19 = getelementptr inbounds i8, i8* %3, i64 %18
%20 = tail call i32 @memcpy(i8* %19, i8* %0, i64 %1)
%21 = load i64, i64* %4
%22 = add i64 %21, %1
store i64 %22, i64* %4
br label %23
23: ; preds = %12, %17
%24 = phi i32 [ 0, %17 ], [ -1, %12 ]
ret i32 %24
}