#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*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int write(int, char *, int);
void filecopy(int fd1, int fd2) {
  int n;
  char buff[1024];
  while ((n = read(fd1, buff, 1024)) > 0) {
    if (write(fd2, buff, n) != n) {
      printf("Cat cannot write\n");
      exit(0);
    }
  }
  if (n == -1) {
    printf("Cat cannot read");
    exit(0);
  }
  return;
}


x86 assembly (clang, -O0):
<filecopy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jle    11a4 <filecopy+0x64>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1030 <write@plt>
cmp    -0xc(%rbp),%eax
je     119f <filecopy+0x5f>
lea    0xe6d(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>
jmp    1151 <filecopy+0x11>
cmpl   $0xffffffff,-0xc(%rbp)
jne    11c1 <filecopy+0x81>
lea    0xe5d(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filecopy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    11a9 <filecopy+0x59>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %ebx,%edx
call   1040 <write@plt>
cmp    %ebx,%eax
je     1170 <filecopy+0x20>
lea    0xe73(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1070 <exit@plt>
cmp    $0xffffffff,%ebx
je     11bc <filecopy+0x6c>
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe3d(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"Cat cannot write\0A\00"
@.str.1 = [16 x i8] c"Cat cannot read\00"
define void @filecopy(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1024 x i8]
store i32 %0, i32* %3
store i32 %1, i32* %4
br label %7
7: ; preds = %22, %2
%8 = load i32, i32* %3
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%10 = call i32 @read(i32 %8, i8* %9, i32 1024)
store i32 %10, i32* %5
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %23
12: ; preds = %7
%13 = load i32, i32* %4
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%15 = load i32, i32* %5
%16 = call i32 @write(i32 %13, i8* %14, i32 %15)
%17 = load i32, i32* %5
%18 = icmp ne i32 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 0)
unreachable
22: ; preds = %12
br label %7
23: ; preds = %7
%24 = load i32, i32* %5
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 0)
unreachable
29: ; preds = %23
ret void
}

LLVM IR (-O2):
@.str.1 = [16 x i8] c"Cat cannot read\00"
@str = [17 x i8] c"Cat cannot write\00"
define void @filecopy(i32 %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
br label %5
5: ; preds = %8, %2
%6 = call i32 @read(i32 %0, i8* %4, i32 1024)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %14
8: ; preds = %5
%9 = call i32 @write(i32 %1, i8* %4, i32 %6)
%10 = icmp eq i32 %9, %6
br i1 %10, label %5, label %11
11: ; preds = %8
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 0)
unreachable
14: ; preds = %5
%15 = icmp eq i32 %6, -1
br i1 %15, label %16, label %19
16: ; preds = %14
%17 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 0)
unreachable
19: ; preds = %14
ret void
}