#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 */
scalar_t__ flush_buf(int, char *, size_t *);
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, char *, int);
int copy_buf(int fd, int outfd, char *outbuf, size_t *outlen) {
  for (;;) {
    ssize_t len;
    if (*outlen == 524288)
      if (flush_buf(outfd, outbuf, outlen) < 0)
        return -1;
    len = read(fd, outbuf + *outlen, 524288 - *outlen);
    if (len < 0) {
      perror("read");
      return -1;
    } else if (len == 0)
      break;
    *outlen += len;
  }
  return 0;
}


x86 assembly (clang, -O0):
<copy_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpq   $0x80000,(%rax)
jne    1182 <copy_buf+0x52>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <flush_buf@plt>
cmp    $0x0,%rax
jge    117d <copy_buf+0x4d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f9 <copy_buf+0xc9>
jmp    1182 <copy_buf+0x52>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rax
add    (%rax),%rsi
mov    -0x20(%rbp),%rcx
mov    $0x80000,%eax
sub    (%rcx),%rax
mov    %eax,%edx
call   1040 <read@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    11ca <copy_buf+0x9a>
lea    0xe47(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f9 <copy_buf+0xc9>
cmpq   $0x0,-0x28(%rbp)
jne    11da <copy_buf+0xaa>
jmp    11f2 <copy_buf+0xc2>
jmp    11df <copy_buf+0xaf>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
jmp    1146 <copy_buf+0x16>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_buf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%rbx
mov    %rdx,%r14
mov    %esi,%r15d
mov    %edi,%ebp
mov    (%rcx),%rax
cmp    $0x80000,%rax
jne    1174 <copy_buf+0x44>
jmp    115e <copy_buf+0x2e>
nop
add    (%rbx),%rax
mov    %rax,(%rbx)
cmp    $0x80000,%rax
jne    1174 <copy_buf+0x44>
mov    %r15d,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <flush_buf@plt>
test   %rax,%rax
js     119d <copy_buf+0x6d>
mov    (%rbx),%rax
lea    (%r14,%rax,1),%rsi
mov    $0x80000,%edx
sub    %eax,%edx
mov    %ebp,%edi
call   1040 <read@plt>
test   %rax,%rax
js     1191 <copy_buf+0x61>
jne    1150 <copy_buf+0x20>
xor    %eax,%eax
jmp    11a2 <copy_buf+0x72>
lea    0xe68(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"read\00"
define i32 @copy_buf(i32 %0, i32 %1, i8* %2, i64* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64*
%10 = alloca i64
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i64* %3, i64** %9
br label %11
11: ; preds = %43, %4
%12 = load i64*, i64** %9
%13 = load i64, i64* %12
%14 = icmp eq i64 %13, 524288
br i1 %14, label %15, label %23
15: ; preds = %11
%16 = load i32, i32* %7
%17 = load i8*, i8** %8
%18 = load i64*, i64** %9
%19 = call i64 @flush_buf(i32 %16, i8* %17, i64* %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %15
store i32 -1, i32* %5
br label %49
22: ; preds = %15
br label %23
23: ; preds = %22, %11
%24 = load i32, i32* %6
%25 = load i8*, i8** %8
%26 = load i64*, i64** %9
%27 = load i64, i64* %26
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i64*, i64** %9
%30 = load i64, i64* %29
%31 = sub i64 524288, %30
%32 = trunc i64 %31 to i32
%33 = call i64 @read(i32 %24, i8* %28, i32 %32)
store i64 %33, i64* %10
%34 = load i64, i64* %10
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %23
%37 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %49
38: ; preds = %23
%39 = load i64, i64* %10
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %38
br label %48
42: ; preds = %38
br label %43
43: ; preds = %42
%44 = load i64, i64* %10
%45 = load i64*, i64** %9
%46 = load i64, i64* %45
%47 = add i64 %46, %44
store i64 %47, i64* %45
br label %11
48: ; preds = %41
store i32 0, i32* %5
br label %49
49: ; preds = %48, %36, %21
%50 = load i32, i32* %5
ret i32 %50
}

LLVM IR (-O2):
@.str = [5 x i8] c"read\00"
define i32 @copy_buf(i32 %0, i32 %1, i8* %2, i64* %3) {
%5 = load i64, i64* %3
br label %6
6: ; preds = %25, %4
%7 = phi i64 [ %27, %25 ], [ %5, %4 ]
%8 = icmp eq i64 %7, 524288
br i1 %8, label %9, label %14
9: ; preds = %6
%10 = tail call i64 @flush_buf(i32 %1, i8* %2, i64* %3)
%11 = icmp slt i64 %10, 0
br i1 %11, label %28, label %12
12: ; preds = %9
%13 = load i64, i64* %3
br label %14
14: ; preds = %12, %6
%15 = phi i64 [ %13, %12 ], [ %7, %6 ]
%16 = getelementptr inbounds i8, i8* %2, i64 %15
%17 = trunc i64 %15 to i32
%18 = sub i32 524288, %17
%19 = tail call i64 @read(i32 %0, i8* %16, i32 %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %28
23: ; preds = %14
%24 = icmp eq i64 %19, 0
br i1 %24, label %28, label %25
25: ; preds = %23
%26 = load i64, i64* %3
%27 = add i64 %26, %19
store i64 %27, i64* %3
br label %6
28: ; preds = %9, %23, %21
%29 = phi i32 [ -1, %21 ], [ -1, %9 ], [ 0, %23 ]
ret i32 %29
}