#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char const *);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int cat_file(int outfd, const char *file) {
  int fd = open(file, O_RDONLY);
  if (fd == -1) {
    perror(file);
    return 1;
  }
  char buffer[1024];
  ssize_t len;
  while ((len = read(fd, buffer, 1023)) > 0) {
    write(outfd, buffer, len);
  }
  if (len == -1) {
    perror("write");
    close(fd);
    return 1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<cat_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1196 <cat_file+0x46>
mov    -0x10(%rbp),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1214 <cat_file+0xc4>
jmp    119b <cat_file+0x4b>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x3ff,%edx
call   1050 <read@plt>
mov    %eax,-0x424(%rbp)
cmp    $0x0,%eax
jle    11d8 <cat_file+0x88>
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x424(%rbp),%edx
call   1030 <write@plt>
jmp    119b <cat_file+0x4b>
cmpl   $0xffffffff,-0x424(%rbp)
jne    1205 <cat_file+0xb5>
lea    0xe14(%rip),%rdi
call   1070 <perror@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1214 <cat_file+0xc4>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11cd <cat_file+0x7d>
mov    %eax,%r14d
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x3ff,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11b0 <cat_file+0x60>
mov    %rsp,%rbx
nop
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x3ff,%edx
call   1050 <read@plt>
test   %eax,%eax
jg     1190 <cat_file+0x40>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
je     11dc <cat_file+0x8c>
mov    %r14d,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %rbx,%rdi
call   1070 <perror@plt>
mov    $0x1,%ebx
jmp    11bf <cat_file+0x6f>
lea    0xe1d(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%ebx
jmp    11b7 <cat_file+0x67>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [6 x i8] c"write\00"
define i32 @cat_file(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [1024 x i8]
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%9 = load i8*, i8** %5
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i8*, i8** %5
%16 = call i32 @perror(i8* %15)
store i32 1, i32* %3
br label %38
17: ; preds = %2
br label %18
18: ; preds = %23, %17
%19 = load i32, i32* %6
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%21 = call i32 @read(i32 %19, i8* %20, i32 1023)
store i32 %21, i32* %8
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %18
%24 = load i32, i32* %4
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%26 = load i32, i32* %8
%27 = call i32 @write(i32 %24, i8* %25, i32 %26)
br label %18
28: ; preds = %18
%29 = load i32, i32* %8
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %35
31: ; preds = %28
%32 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%33 = load i32, i32* %6
%34 = call i32 @close(i32 %33)
store i32 1, i32* %3
br label %38
35: ; preds = %28
%36 = load i32, i32* %6
%37 = call i32 @close(i32 %36)
store i32 0, i32* %3
br label %38
38: ; preds = %35, %31, %14
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [6 x i8] c"write\00"
define i32 @cat_file(i32 %0, i8* nocapture %1) {
%3 = alloca [1024 x i8]
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %1, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @perror(i8* %1)
br label %26
9: ; preds = %2
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%11 = call i32 @read(i32 %5, i8* %10, i32 1023)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %9, %13
%14 = phi i32 [ %16, %13 ], [ %11, %9 ]
%15 = call i32 @write(i32 %0, i8* %10, i32 %14)
%16 = call i32 @read(i32 %5, i8* %10, i32 1023)
%17 = icmp sgt i32 %16, 0
br i1 %17, label %13, label %18
18: ; preds = %13, %9
%19 = phi i32 [ %11, %9 ], [ %16, %13 ]
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %18, %21
%24 = phi i32 [ 1, %21 ], [ 0, %18 ]
%25 = tail call i32 @close(i32 %5)
br label %26
26: ; preds = %23, %7
%27 = phi i32 [ 1, %7 ], [ %24, %23 ]
ret i32 %27
}