#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 int /*<<< orphan*/ data;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
unsigned long fread(unsigned char *, int, unsigned long, int /*<<< orphan*/ *);
unsigned long fwrite(unsigned char *, int, unsigned long, int /*<<< orphan*/ *);
int bufferedReadWrite(FILE *input, FILE *output, unsigned long length) {
  unsigned char data[2048];
  unsigned long nread;
  while (length > 0) {
    nread = length;
    if (nread > sizeof(data))
      nread = sizeof(data);
    nread = fread(data, 1, nread, input);
    if (!nread)
      return 0;
    if (fwrite(data, 1, nread, output) != nread)
      return 0;
    length -= nread;
  }
  return 1;
}


x86 assembly (clang, -O0):
<bufferedReadWrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    11f2 <bufferedReadWrite+0xd2>
mov    -0x20(%rbp),%rax
mov    %rax,-0x828(%rbp)
cmpq   $0x800,-0x828(%rbp)
jbe    1169 <bufferedReadWrite+0x49>
movq   $0x800,-0x828(%rbp)
lea    -0x820(%rbp),%rdi
mov    -0x828(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x828(%rbp)
cmpq   $0x0,-0x828(%rbp)
jne    11a6 <bufferedReadWrite+0x86>
movl   $0x0,-0x4(%rbp)
jmp    11f9 <bufferedReadWrite+0xd9>
lea    -0x820(%rbp),%rdi
mov    -0x828(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fwrite@plt>
cmp    -0x828(%rbp),%rax
je     11db <bufferedReadWrite+0xbb>
movl   $0x0,-0x4(%rbp)
jmp    11f9 <bufferedReadWrite+0xd9>
mov    -0x828(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    1137 <bufferedReadWrite+0x17>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bufferedReadWrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
test   %rdx,%rdx
je     1195 <bufferedReadWrite+0x75>
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsp,%r12
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
nop
cmp    $0x800,%rbx
mov    $0x800,%edx
cmovb  %rbx,%rdx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r15,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     119b <bufferedReadWrite+0x7b>
mov    %rax,%rbp
mov    %r12,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %r14,%rcx
call   1040 <fwrite@plt>
cmp    %rbp,%rax
jne    119b <bufferedReadWrite+0x7b>
sub    %rbp,%rbx
jne    1150 <bufferedReadWrite+0x30>
mov    $0x1,%r13d
mov    %r13d,%eax
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @bufferedReadWrite(i32* %0, i32* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i64
%8 = alloca [2048 x i8]
%9 = alloca i64
store i32* %0, i32** %5
store i32* %1, i32** %6
store i64 %2, i64* %7
br label %10
10: ; preds = %34, %3
%11 = load i64, i64* %7
%12 = icmp ugt i64 %11, 0
br i1 %12, label %13, label %38
13: ; preds = %10
%14 = load i64, i64* %7
store i64 %14, i64* %9
%15 = load i64, i64* %9
%16 = icmp ugt i64 %15, 2048
br i1 %16, label %17, label %18
17: ; preds = %13
store i64 2048, i64* %9
br label %18
18: ; preds = %17, %13
%19 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 0
%20 = load i64, i64* %9
%21 = load i32*, i32** %5
%22 = call i64 @fread(i8* %19, i32 1, i64 %20, i32* %21)
store i64 %22, i64* %9
%23 = load i64, i64* %9
%24 = icmp ne i64 %23, 0
br i1 %24, label %26, label %25
25: ; preds = %18
store i32 0, i32* %4
br label %39
26: ; preds = %18
%27 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 0
%28 = load i64, i64* %9
%29 = load i32*, i32** %6
%30 = call i64 @fwrite(i8* %27, i32 1, i64 %28, i32* %29)
%31 = load i64, i64* %9
%32 = icmp ne i64 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %26
store i32 0, i32* %4
br label %39
34: ; preds = %26
%35 = load i64, i64* %9
%36 = load i64, i64* %7
%37 = sub i64 %36, %35
store i64 %37, i64* %7
br label %10
38: ; preds = %10
store i32 1, i32* %4
br label %39
39: ; preds = %38, %33, %25
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
define i32 @bufferedReadWrite(i32* nocapture %0, i32* nocapture %1, i64 %2) {
%4 = alloca [2048 x i8]
%5 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%6 = icmp eq i64 %2, 0
br i1 %6, label %19, label %9
7: ; preds = %15
%8 = icmp eq i64 %18, 0
br i1 %8, label %19, label %9
9: ; preds = %3, %7
%10 = phi i64 [ %18, %7 ], [ %2, %3 ]
%11 = icmp ult i64 %10, 2048
%12 = select i1 %11, i64 %10, i64 2048
%13 = call i64 @fread(i8* %5, i32 1, i64 %12, i32* %0)
%14 = icmp eq i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %9
%16 = call i64 @fwrite(i8* %5, i32 1, i64 %13, i32* %1)
%17 = icmp eq i64 %16, %13
%18 = sub i64 %10, %13
br i1 %17, label %7, label %19
19: ; preds = %9, %15, %7, %3
%20 = phi i32 [ 1, %3 ], [ 1, %7 ], [ 0, %15 ], [ 0, %9 ]
ret i32 %20
}