#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int fread(char *, int, int const, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
void copyf(FILE *outfp, FILE *infp, int lencheck) {
  fseek(infp, 0L, SEEK_SET);
  const int bs = 8192;
  char buf[bs];
  int len;
  while (!feof(infp)) {
    len = fread(buf, 1, bs, infp);
    if (len < 0) {
      printf("Read error\n");
      exit(2);
    }
    int res = fwrite(buf, 1, len, outfp);
    if (len != res) {
      printf("Write error\n");
      exit(2);
    }
    lencheck -= res;
  }
  if (lencheck != 0) {
    printf("Length error\n");
    exit(2);
  }
}


x86 assembly (clang, -O0):
<copyf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2030,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e5f(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1060 <fseek@plt>
movl   $0x2000,-0x18(%rbp)
mov    %rsp,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11b8 <copyf+0x58>
jmp    1242 <copyf+0xe2>
mov    -0x10(%rbp),%rcx
lea    -0x2030(%rbp),%rdi
mov    $0x1,%esi
mov    $0x2000,%edx
call   1030 <fread@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11f5 <copyf+0x95>
lea    0xe1a(%rip),%rdi
call   1040 <printf@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
mov    -0x24(%rbp),%edx
mov    -0x8(%rbp),%rcx
lea    -0x2030(%rbp),%rdi
mov    $0x1,%esi
call   1080 <fwrite@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
je     1232 <copyf+0xd2>
lea    0xde9(%rip),%rdi
call   1040 <printf@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    119a <copyf+0x3a>
cmpl   $0x0,-0x14(%rbp)
je     1262 <copyf+0x102>
lea    0xdc6(%rip),%rdi
call   1040 <printf@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
add    $0x2030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copyf>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2000,%rsp
mov    %edx,%ebx
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e62(%rip),%rax
mov    (%rax),%edx
mov    %rsi,%rdi
xor    %esi,%esi
call   1060 <fseek@plt>
mov    %r14,%rdi
call   1050 <feof@plt>
test   %eax,%eax
jne    11df <copyf+0x7f>
mov    %rsp,%r12
nopl   0x0(%rax)
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x2000,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
test   %eax,%eax
js     11f3 <copyf+0x93>
mov    %eax,%ebp
mov    %r12,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %r15,%rcx
call   1080 <fwrite@plt>
cmp    %eax,%ebp
jne    11fc <copyf+0x9c>
sub    %ebp,%ebx
mov    %r14,%rdi
call   1050 <feof@plt>
test   %eax,%eax
je     11a0 <copyf+0x40>
test   %ebx,%ebx
jne    1212 <copyf+0xb2>
add    $0x2000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe1f(%rip),%rdi
jmp    1203 <copyf+0xa3>
lea    0xe0a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
lea    0xde7(%rip),%rdi
jmp    1203 <copyf+0xa3>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [12 x i8] c"Read error\0A\00"
@.str.1 = [13 x i8] c"Write error\0A\00"
@.str.2 = [14 x i8] c"Length error\0A\00"
define void @copyf(i32* %0, i32* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%11 = load i32*, i32** %5
%12 = load i32, i32* @SEEK_SET
%13 = call i32 @fseek(i32* %11, i64 0, i32 %12)
store i32 8192, i32* %7
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %8
%15 = alloca i8, i64 8192
br label %16
16: ; preds = %39, %3
%17 = load i32*, i32** %5
%18 = call i32 @feof(i32* %17)
%19 = icmp ne i32 %18, 0
%20 = xor i1 %19, true
br i1 %20, label %21, label %43
21: ; preds = %16
%22 = load i32*, i32** %5
%23 = call i32 @fread(i8* %15, i32 1, i32 8192, i32* %22)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%28 = call i32 @exit(i32 2)
unreachable
29: ; preds = %21
%30 = load i32, i32* %9
%31 = load i32*, i32** %4
%32 = call i32 @fwrite(i8* %15, i32 1, i32 %30, i32* %31)
store i32 %32, i32* %10
%33 = load i32, i32* %9
%34 = load i32, i32* %10
%35 = icmp ne i32 %33, %34
br i1 %35, label %36, label %39
36: ; preds = %29
%37 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%38 = call i32 @exit(i32 2)
unreachable
39: ; preds = %29
%40 = load i32, i32* %10
%41 = load i32, i32* %6
%42 = sub nsw i32 %41, %40
store i32 %42, i32* %6
br label %16
43: ; preds = %16
%44 = load i32, i32* %6
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %43
%47 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%48 = call i32 @exit(i32 2)
unreachable
49: ; preds = %43
%50 = load i8*, i8** %8
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@str = [13 x i8] c"Length error\00"
@str.3 = [12 x i8] c"Write error\00"
@str.4 = [11 x i8] c"Read error\00"
define void @copyf(i32* nocapture %0, i32* nocapture %1, i32 %2) {
%4 = load i32, i32* @SEEK_SET
%5 = tail call i32 @fseek(i32* %1, i64 0, i32 %4)
%6 = alloca [8192 x i8]
%7 = getelementptr inbounds [8192 x i8], [8192 x i8]* %6, i64 0, i64 0
%8 = tail call i32 @feof(i32* %1)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %27
10: ; preds = %3, %23
%11 = phi i32 [ %24, %23 ], [ %2, %3 ]
%12 = call i32 @fread(i8* %7, i32 1, i32 8192, i32* %1)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.4, i64 0, i64 0))
%16 = tail call i32 @exit(i32 2)
unreachable
17: ; preds = %10
%18 = call i32 @fwrite(i8* %7, i32 1, i32 %12, i32* %0)
%19 = icmp eq i32 %12, %18
br i1 %19, label %23, label %20
20: ; preds = %17
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.3, i64 0, i64 0))
%22 = tail call i32 @exit(i32 2)
unreachable
23: ; preds = %17
%24 = sub nsw i32 %11, %12
%25 = tail call i32 @feof(i32* %1)
%26 = icmp eq i32 %25, 0
br i1 %26, label %10, label %27
27: ; preds = %23, %3
%28 = phi i32 [ %2, %3 ], [ %24, %23 ]
%29 = icmp eq i32 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %27
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%32 = tail call i32 @exit(i32 2)
unreachable
33: ; preds = %27
ret void
}