#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*/ O_RDONLY;
int STDIN_FILENO;
int STDOUT_FILENO;
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int write(int, char *, int);
int main(int argv, char *argc[]) {
  int in, out, n;
  char buf[4096];
  in = STDIN_FILENO;
  out = STDOUT_FILENO;
  if (argv > 2) {
    printf("usage:mycat [filename]\n");
    return 0;
  }
  if (argv == 2) {
    if ((in = open(argc[1], O_RDONLY, 0)) == -1) {
      printf("cannot open file %s\n", argc[1]);
      return 1;
    }
  }
  while ((n = read(in, buf, 4096)) > 0) {
    if (write(out, buf, n) != n) {
      printf("write error\n");
      return 1;
    }
  }
  if (n < 0) {
    printf("read error\n");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e84(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    1195 <main+0x55>
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1274 <main+0x134>
cmpl   $0x2,-0x8(%rbp)
jne    11ea <main+0xaa>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11e5 <main+0xa5>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1274 <main+0x134>
jmp    11ea <main+0xaa>
jmp    11ef <main+0xaf>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    1249 <main+0x109>
mov    -0x18(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
cmp    -0x1c(%rbp),%eax
je     1244 <main+0x104>
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1274 <main+0x134>
jmp    11ef <main+0xaf>
cmpl   $0x0,-0x1c(%rbp)
jge    126d <main+0x12d>
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1274 <main+0x134>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
cmp    $0x3,%edi
jl     1177 <main+0x27>
lea    0xec1(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    1217 <main+0xc7>
mov    0x2e72(%rip),%rax
mov    (%rax),%r15d
cmp    $0x2,%edi
jne    11b9 <main+0x69>
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
jne    11c3 <main+0x73>
mov    0x8(%r14),%rsi
lea    0xe50(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1212 <main+0xc2>
mov    0x2e10(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1200 <main+0xb0>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
cmp    %ebp,%eax
je     11d0 <main+0x80>
lea    0xe22(%rip),%rdi
jmp    120d <main+0xbd>
xor    %eax,%eax
test   %ebp,%ebp
jns    1217 <main+0xc7>
lea    0xe08(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str = [24 x i8] c"usage:mycat [filename]\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [21 x i8] c"cannot open file %s\0A\00"
@.str.2 = [13 x i8] c"write error\0A\00"
@.str.3 = [12 x i8] c"read error\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [4096 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @STDIN_FILENO
store i32 %10, i32* %6
%11 = load i32, i32* @STDOUT_FILENO
store i32 %11, i32* %7
%12 = load i32, i32* %4
%13 = icmp sgt i32 %12, 2
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %54
16: ; preds = %2
%17 = load i32, i32* %4
%18 = icmp eq i32 %17, 2
br i1 %18, label %19, label %32
19: ; preds = %16
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @open(i8* %22, i32 %23, i32 0)
store i32 %24, i32* %6
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %31
26: ; preds = %19
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %29)
store i32 1, i32* %3
br label %54
31: ; preds = %19
br label %32
32: ; preds = %31, %16
br label %33
33: ; preds = %47, %32
%34 = load i32, i32* %6
%35 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%36 = call i32 @read(i32 %34, i8* %35, i32 4096)
store i32 %36, i32* %8
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %48
38: ; preds = %33
%39 = load i32, i32* %7
%40 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%41 = load i32, i32* %8
%42 = call i32 @write(i32 %39, i8* %40, i32 %41)
%43 = load i32, i32* %8
%44 = icmp ne i32 %42, %43
br i1 %44, label %45, label %47
45: ; preds = %38
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %54
47: ; preds = %38
br label %33
48: ; preds = %33
%49 = load i32, i32* %8
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %48
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %54
53: ; preds = %48
store i32 0, i32* %3
br label %54
54: ; preds = %53, %51, %45, %26, %14
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [21 x i8] c"cannot open file %s\0A\00"
@str = [11 x i8] c"read error\00"
@str.4 = [12 x i8] c"write error\00"
@str.5 = [23 x i8] c"usage:mycat [filename]\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @STDOUT_FILENO
%6 = icmp sgt i32 %0, 2
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.5, i64 0, i64 0))
br label %35
9: ; preds = %2
%10 = load i32, i32* @STDIN_FILENO
%11 = icmp eq i32 %0, 2
br i1 %11, label %12, label %21
12: ; preds = %9
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = load i32, i32* @O_RDONLY
%16 = tail call i32 @open(i8* %14, i32 %15, i32 0)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = load i8*, i8** %13
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %19)
br label %35
21: ; preds = %12, %9
%22 = phi i32 [ %16, %12 ], [ %10, %9 ]
br label %23
23: ; preds = %26, %21
%24 = call i32 @read(i32 %22, i8* %4, i32 4096)
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %23
%27 = call i32 @write(i32 %5, i8* %4, i32 %24)
%28 = icmp eq i32 %27, %24
br i1 %28, label %23, label %29
29: ; preds = %26
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.4, i64 0, i64 0))
br label %35
31: ; preds = %23
%32 = icmp slt i32 %24, 0
br i1 %32, label %33, label %35
33: ; preds = %31
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
br label %35
35: ; preds = %31, %33, %29, %18, %7
%36 = phi i32 [ 0, %7 ], [ 1, %18 ], [ 1, %29 ], [ 1, %33 ], [ 0, %31 ]
ret i32 %36
}