#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 */
scalar_t__ ferror(int /*<<< orphan*/ *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int catfile(FILE *fin, FILE *fout) {
  int read = 0;
  char buffer[1024];
  memset(buffer, 0, 1024);
  while ((read = fread(buffer, sizeof(char), 1024, fin)) != 0) {
    if (fwrite(buffer, sizeof(char), 1024, fout) < read) {
      if (ferror(fout))
        printf("Error writing to /dev/null");
    }
    memset(buffer, 0, 1024);
  }
  if (ferror(fin)) {
    printf("Error reading from file");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<catfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1040 <fread@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     1202 <catfile+0xb2>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1070 <fwrite@plt>
cmp    -0x1c(%rbp),%eax
jge    11ea <catfile+0x9a>
mov    -0x18(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11e5 <catfile+0x95>
lea    0xe20(%rip),%rdi
call   1050 <printf@plt>
jmp    11ea <catfile+0x9a>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
jmp    117d <catfile+0x2d>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     122d <catfile+0xdd>
lea    0xdff(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1234 <catfile+0xe4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<catfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%r13
mov    %rdi,%r15
mov    %rsp,%rbx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
test   %eax,%eax
je     120a <catfile+0xba>
mov    %eax,%ebp
mov    %rsp,%rbx
lea    0xe5f(%rip),%r12
jmp    11da <catfile+0x8a>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
mov    %eax,%ebp
test   %eax,%eax
je     120a <catfile+0xba>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r13,%rcx
call   1070 <fwrite@plt>
cmp    %ebp,%eax
jge    11b0 <catfile+0x60>
mov    %r13,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     11b0 <catfile+0x60>
mov    %r12,%rdi
call   1050 <printf@plt>
jmp    11b0 <catfile+0x60>
mov    %r15,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     1229 <catfile+0xd9>
lea    0xdfd(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"Error writing to /dev/null\00"
@.str.1 = [24 x i8] c"Error reading from file\00"
define i32 @catfile(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca [1024 x i8]
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32 0, i32* %6
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%9 = call i32 @memset(i8* %8, i32 0, i32 1024)
br label %10
10: ; preds = %28, %2
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%12 = load i32*, i32** %4
%13 = call i32 @fread(i8* %11, i32 1, i32 1024, i32* %12)
store i32 %13, i32* %6
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %31
15: ; preds = %10
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%17 = load i32*, i32** %5
%18 = call i32 @fwrite(i8* %16, i32 1, i32 1024, i32* %17)
%19 = load i32, i32* %6
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %28
21: ; preds = %15
%22 = load i32*, i32** %5
%23 = call i64 @ferror(i32* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %27
27: ; preds = %25, %21
br label %28
28: ; preds = %27, %15
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%30 = call i32 @memset(i8* %29, i32 0, i32 1024)
br label %10
31: ; preds = %10
%32 = load i32*, i32** %4
%33 = call i64 @ferror(i32* %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %38
37: ; preds = %31
store i32 0, i32* %3
br label %38
38: ; preds = %37, %35
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@.str = [27 x i8] c"Error writing to /dev/null\00"
@.str.1 = [24 x i8] c"Error reading from file\00"
define i32 @catfile(i32* nocapture %0, i32* nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = call i32 @memset(i8* %4, i32 0, i32 1024)
%6 = call i32 @fread(i8* %4, i32 1, i32 1024, i32* %0)
%7 = icmp eq i32 %6, 0
br i1 %7, label %21, label %8
8: ; preds = %2, %17
%9 = phi i32 [ %19, %17 ], [ %6, %2 ]
%10 = call i32 @fwrite(i8* %4, i32 1, i32 1024, i32* %1)
%11 = icmp slt i32 %10, %9
br i1 %11, label %12, label %17
12: ; preds = %8
%13 = call i64 @ferror(i32* %1)
%14 = icmp eq i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %12, %15, %8
%18 = call i32 @memset(i8* %4, i32 0, i32 1024)
%19 = call i32 @fread(i8* %4, i32 1, i32 1024, i32* %0)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %8
21: ; preds = %17, %2
%22 = call i64 @ferror(i32* %0)
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %21, %24
%27 = phi i32 [ -1, %24 ], [ 0, %21 ]
ret i32 %27
}