#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 EOF;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int filecopy(int);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  int fd1;
  int status;
  if (argc == 1) {
    filecopy(0);
  } else {
    while (--argc > 0) {
      if ((fd1 = open(*++argv, O_RDONLY, 0)) == EOF) {
        printf("cat: can't open %s\n", *argv);
      } else {
        status = filecopy(fd1);
        close(fd1);
        if (status == -2) {
          printf("cat: error reading file %s\n", *argv);
          return -2;
        } else if (status == -3) {
          printf("cat: error writing to stdout\n");
          return -3;
        }
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    116c <main+0x2c>
xor    %edi,%edi
call   1050 <filecopy@plt>
jmp    1248 <main+0x108>
jmp    1171 <main+0x31>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jle    1243 <main+0x103>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    0x8(%rax),%rdi
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e30(%rip),%rcx
cmp    (%rcx),%eax
jne    11d2 <main+0x92>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    123e <main+0xfe>
mov    -0x14(%rbp),%edi
call   1050 <filecopy@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmpl   $0xfffffffe,-0x18(%rbp)
jne    1210 <main+0xd0>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    124f <main+0x10f>
cmpl   $0xfffffffd,-0x18(%rbp)
jne    1234 <main+0xf4>
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    124f <main+0x10f>
jmp    1239 <main+0xf9>
jmp    123e <main+0xfe>
jmp    1171 <main+0x31>
jmp    1248 <main+0x108>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x1,%edi
jne    116f <main+0x1f>
xor    %r14d,%r14d
xor    %edi,%edi
call   1060 <filecopy@plt>
jmp    1221 <main+0xd1>
mov    %edi,%r15d
xor    %r14d,%r14d
cmp    $0x2,%edi
jl     1221 <main+0xd1>
mov    %rsi,%rbx
add    $0x1,%r15d
add    $0x8,%rbx
mov    0x2e58(%rip),%r13
mov    0x2e49(%rip),%r14
jmp    11bf <main+0x6f>
nopl   0x0(%rax)
mov    (%rbx),%rsi
lea    0xe56(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0xffffffff,%r15d
add    $0x8,%rbx
cmp    $0x2,%r15d
jbe    120a <main+0xba>
mov    (%rbx),%rdi
mov    0x0(%r13),%esi
xor    %edx,%edx
call   1070 <open@plt>
cmp    (%r14),%eax
je     11a0 <main+0x50>
mov    %eax,%ebp
mov    %eax,%edi
call   1060 <filecopy@plt>
mov    %eax,%r12d
mov    %ebp,%edi
call   1050 <close@plt>
cmp    $0xfffffffd,%r12d
je     120f <main+0xbf>
cmp    $0xfffffffe,%r12d
jne    11b1 <main+0x61>
mov    (%rbx),%rsi
lea    0xe19(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xfffffffe,%r14d
jmp    1221 <main+0xd1>
xor    %r14d,%r14d
jmp    1221 <main+0xd1>
lea    0xe1a(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffd,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@EOF = global i32 0
@.str = [20 x i8] c"cat: can't open %s\0A\00"
@.str.1 = [28 x i8] c"cat: error reading file %s\0A\00"
@.str.2 = [30 x i8] c"cat: error writing to stdout\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @filecopy(i32 0)
br label %49
12: ; preds = %2
br label %13
13: ; preds = %47, %12
%14 = load i32, i32* %4
%15 = add nsw i32 %14, -1
store i32 %15, i32* %4
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %48
17: ; preds = %13
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i32 1
store i8** %19, i8*** %5
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* %20, i32 %21, i32 0)
store i32 %22, i32* %6
%23 = load i32, i32* @EOF
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %29
25: ; preds = %17
%26 = load i8**, i8*** %5
%27 = load i8*, i8** %26
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %27)
br label %47
29: ; preds = %17
%30 = load i32, i32* %6
%31 = call i32 @filecopy(i32 %30)
store i32 %31, i32* %7
%32 = load i32, i32* %6
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %7
%35 = icmp eq i32 %34, -2
br i1 %35, label %36, label %40
36: ; preds = %29
%37 = load i8**, i8*** %5
%38 = load i8*, i8** %37
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %38)
store i32 -2, i32* %3
br label %50
40: ; preds = %29
%41 = load i32, i32* %7
%42 = icmp eq i32 %41, -3
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
store i32 -3, i32* %3
br label %50
45: ; preds = %40
br label %46
46: ; preds = %45
br label %47
47: ; preds = %46, %25
br label %13
48: ; preds = %13
br label %49
49: ; preds = %48, %10
store i32 0, i32* %3
br label %50
50: ; preds = %49, %43, %36
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@EOF = global i32 0
@.str = [20 x i8] c"cat: can't open %s\0A\00"
@.str.1 = [28 x i8] c"cat: error reading file %s\0A\00"
@str = [29 x i8] c"cat: error writing to stdout\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 1
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = icmp sgt i32 %0, 1
br i1 %5, label %8, label %31
6: ; preds = %2
%7 = tail call i32 @filecopy(i32 0)
br label %31
8: ; preds = %4, %29
%9 = phi i32 [ %11, %29 ], [ %0, %4 ]
%10 = phi i8** [ %12, %29 ], [ %1, %4 ]
%11 = add nsw i32 %9, -1
%12 = getelementptr inbounds i8*, i8** %10, i64 1
%13 = load i8*, i8** %12
%14 = load i32, i32* @O_RDONLY
%15 = tail call i32 @open(i8* %13, i32 %14, i32 0)
%16 = load i32, i32* @EOF
%17 = icmp eq i32 %15, %16
br i1 %17, label %18, label %21
18: ; preds = %8
%19 = load i8*, i8** %12
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %19)
br label %29
21: ; preds = %8
%22 = tail call i32 @filecopy(i32 %15)
%23 = tail call i32 @close(i32 %15)
switch i32 %22, label %29 [
i32 -2, label %24
i32 -3, label %27
]
24: ; preds = %21
%25 = load i8*, i8** %12
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %25)
br label %31
27: ; preds = %21
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %31
29: ; preds = %21, %18
%30 = icmp ugt i32 %9, 2
br i1 %30, label %8, label %31
31: ; preds = %29, %4, %6, %27, %24
%32 = phi i32 [ -2, %24 ], [ -3, %27 ], [ 0, %6 ], [ 0, %4 ], [ 0, %29 ]
ret i32 %32
}