#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 */
struct stat {
  int st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
scalar_t__ fstat(int, struct stat *);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read(int, char *, int);
scalar_t__ strcmp(char *, char *);
int write(int, char *, int);
int main(int argc, char **argv) {
  if (argc < 2) {
    printf("Usage: cat [-n] [file]\n");
    exit(1);
  }
  char *path = NULL;
  int x;
  for (x = 1; x < argc; x++) {
    if (strcmp(argv[x], "-n") == 0) {
    } else {
      if (path) {
        printf("Usage: cat [-n] [file]\n");
        exit(1);
      }
      path = argv[1];
    }
  }
  int fileDescript = open(path, O_RDONLY);
  if (fileDescript == -1) {
    printf("cat: File does not exist.\n");
    exit(1);
  }
  struct stat st;
  if (fstat(fileDescript, &st))
    return -1;
  int fileLength = st.st_size;
  lseek(fileDescript, 0, SEEK_SET);
  char buffer[512];
  int i;
  for (i = 0; i < fileLength;) {
    int left = fileLength - i;
    if (left > 512 - 1)
      left = 512 - 1;
    read(fileDescript, buffer, left);
    buffer[512 - 1] = 0;
    if (write(1, buffer, left) != left)
      return -1;
    i += left;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11b9 <main+0x39>
lea    0xe56(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movq   $0x0,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    123b <main+0xbb>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xe31(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    11fb <main+0x7b>
jmp    1228 <main+0xa8>
cmpq   $0x0,-0x18(%rbp)
je     121c <main+0x9c>
lea    0xdf3(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    122d <main+0xad>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11c8 <main+0x48>
mov    -0x18(%rbp),%rdi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1270 <main+0xf0>
lea    0xdba(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   10a0 <fstat@plt>
cmp    $0x0,%rax
je     1292 <main+0x112>
movl   $0xffffffff,-0x4(%rbp)
jmp    1353 <main+0x1d3>
mov    -0x28(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2d36(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
movl   $0x0,-0x234(%rbp)
mov    -0x234(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    134c <main+0x1cc>
mov    -0x2c(%rbp),%eax
sub    -0x234(%rbp),%eax
mov    %eax,-0x238(%rbp)
cmpl   $0x1ff,-0x238(%rbp)
jle    12ed <main+0x16d>
movl   $0x1ff,-0x238(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    -0x238(%rbp),%edx
call   1060 <read@plt>
movb   $0x0,-0x31(%rbp)
lea    -0x230(%rbp),%rsi
mov    -0x238(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
cmp    -0x238(%rbp),%eax
je     1335 <main+0x1b5>
movl   $0xffffffff,-0x4(%rbp)
jmp    1353 <main+0x1d3>
mov    -0x238(%rbp),%eax
add    -0x234(%rbp),%eax
mov    %eax,-0x234(%rbp)
jmp    12b5 <main+0x135>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
cmp    $0x2,%edi
jl     11d3 <main+0x53>
mov    %rsi,%rbx
mov    %edi,%r15d
mov    $0x1,%ebp
xor    %r12d,%r12d
lea    0xe55(%rip),%r14
jmp    11bd <main+0x3d>
nopl   (%rax)
mov    0x8(%rbx),%r12
add    $0x1,%rbp
cmp    %rbp,%r15
je     11e9 <main+0x69>
mov    (%rbx,%rbp,8),%rdi
mov    %r14,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     11b4 <main+0x34>
test   %r12,%r12
je     11b0 <main+0x30>
lea    0xe43(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     12aa <main+0x12a>
mov    %eax,%r15d
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10a0 <fstat@plt>
mov    $0xffffffff,%ecx
test   %rax,%rax
jne    1296 <main+0x116>
mov    0x8(%rsp),%r12d
mov    0x2db0(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    $0x1ff,%r13d
lea    0x10(%rsp),%rbp
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12d,%ebx
sub    %r14d,%ebx
jle    1294 <main+0x114>
cmp    $0x1ff,%ebx
cmovge %r13d,%ebx
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1060 <read@plt>
movb   $0x0,0x20f(%rsp)
mov    $0x1,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1040 <write@plt>
add    %ebx,%r14d
cmp    %ebx,%eax
je     1250 <main+0xd0>
mov    $0xffffffff,%ecx
jmp    1296 <main+0x116>
xor    %ecx,%ecx
mov    %ecx,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd52(%rip),%rdi
jmp    11da <main+0x5a>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [24 x i8] c"Usage: cat [-n] [file]\0A\00"
@.str.1 = [3 x i8] c"-n\00"
@O_RDONLY = global i32 0
@.str.2 = [27 x i8] c"cat: File does not exist.\0A\00"
@SEEK_SET = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.stat
%10 = alloca i32
%11 = alloca [512 x i8]
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 2
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
store i8* null, i8** %6
store i32 1, i32* %7
br label %20
20: ; preds = %44, %19
%21 = load i32, i32* %7
%22 = load i32, i32* %4
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %47
24: ; preds = %20
%25 = load i8**, i8*** %5
%26 = load i32, i32* %7
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8*, i8** %25, i64 %27
%29 = load i8*, i8** %28
%30 = call i64 @strcmp(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %24
br label %43
33: ; preds = %24
%34 = load i8*, i8** %6
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %33
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 1
%42 = load i8*, i8** %41
store i8* %42, i8** %6
br label %43
43: ; preds = %39, %32
br label %44
44: ; preds = %43
%45 = load i32, i32* %7
%46 = add nsw i32 %45, 1
store i32 %46, i32* %7
br label %20
47: ; preds = %20
%48 = load i8*, i8** %6
%49 = load i32, i32* @O_RDONLY
%50 = call i32 @open(i8* %48, i32 %49)
store i32 %50, i32* %8
%51 = load i32, i32* %8
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %56
53: ; preds = %47
%54 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %47
%57 = load i32, i32* %8
%58 = call i64 @fstat(i32 %57, %struct.stat* %9)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %56
store i32 -1, i32* %3
br label %95
61: ; preds = %56
%62 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%63 = load i32, i32* %62
store i32 %63, i32* %10
%64 = load i32, i32* %8
%65 = load i32, i32* @SEEK_SET
%66 = call i32 @lseek(i32 %64, i32 0, i32 %65)
store i32 0, i32* %12
br label %67
67: ; preds = %90, %61
%68 = load i32, i32* %12
%69 = load i32, i32* %10
%70 = icmp slt i32 %68, %69
br i1 %70, label %71, label %94
71: ; preds = %67
%72 = load i32, i32* %10
%73 = load i32, i32* %12
%74 = sub nsw i32 %72, %73
store i32 %74, i32* %13
%75 = load i32, i32* %13
%76 = icmp sgt i32 %75, 511
br i1 %76, label %77, label %78
77: ; preds = %71
store i32 511, i32* %13
br label %78
78: ; preds = %77, %71
%79 = load i32, i32* %8
%80 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%81 = load i32, i32* %13
%82 = call i32 @read(i32 %79, i8* %80, i32 %81)
%83 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 511
store i8 0, i8* %83
%84 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%85 = load i32, i32* %13
%86 = call i32 @write(i32 1, i8* %84, i32 %85)
%87 = load i32, i32* %13
%88 = icmp ne i32 %86, %87
br i1 %88, label %89, label %90
89: ; preds = %78
store i32 -1, i32* %3
br label %95
90: ; preds = %78
%91 = load i32, i32* %13
%92 = load i32, i32* %12
%93 = add nsw i32 %92, %91
store i32 %93, i32* %12
br label %67
94: ; preds = %67
store i32 0, i32* %3
br label %95
95: ; preds = %94, %89, %60
%96 = load i32, i32* %3
ret i32 %96
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str.1 = [3 x i8] c"-n\00"
@O_RDONLY = global i32 0
@SEEK_SET = global i32 0
@str = [26 x i8] c"cat: File does not exist.\00"
@str.4 = [23 x i8] c"Usage: cat [-n] [file]\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca [512 x i8]
%5 = icmp slt i32 %0, 2
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = zext i32 %0 to i64
br label %12
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.4, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %6, %26
%13 = phi i64 [ 1, %6 ], [ %28, %26 ]
%14 = phi i8* [ null, %6 ], [ %27, %26 ]
%15 = getelementptr inbounds i8*, i8** %1, i64 %13
%16 = load i8*, i8** %15
%17 = tail call i64 @strcmp(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %26, label %19
19: ; preds = %12
%20 = icmp eq i8* %14, null
br i1 %20, label %24, label %21
21: ; preds = %19
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.4, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %19
%25 = load i8*, i8** %7
br label %26
26: ; preds = %24, %12
%27 = phi i8* [ %14, %12 ], [ %25, %24 ]
%28 = add nuw nsw i64 %13, 1
%29 = icmp eq i64 %28, %8
br i1 %29, label %30, label %12
30: ; preds = %26
%31 = load i32, i32* @O_RDONLY
%32 = tail call i32 @open(i8* %27, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%36 = tail call i32 @exit(i32 1)
unreachable
37: ; preds = %30
%38 = bitcast %struct.stat* %3 to i8*
%39 = call i64 @fstat(i32 %32, %struct.stat* %3)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %61
41: ; preds = %37
%42 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%43 = load i32, i32* %42
%44 = load i32, i32* @SEEK_SET
%45 = tail call i32 @lseek(i32 %32, i32 0, i32 %44)
%46 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%47 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 511
br label %48
48: ; preds = %51, %41
%49 = phi i32 [ 0, %41 ], [ %58, %51 ]
%50 = icmp sgt i32 %43, %49
br i1 %50, label %51, label %59
51: ; preds = %48
%52 = sub nsw i32 %43, %49
%53 = icmp slt i32 %52, 511
%54 = select i1 %53, i32 %52, i32 511
%55 = call i32 @read(i32 %32, i8* %46, i32 %54)
store i8 0, i8* %47
%56 = call i32 @write(i32 1, i8* %46, i32 %54)
%57 = icmp eq i32 %56, %54
%58 = add nsw i32 %54, %49
br i1 %57, label %48, label %59
59: ; preds = %48, %51
%60 = phi i32 [ -1, %51 ], [ 0, %48 ]
br label %61
61: ; preds = %37, %59
%62 = phi i32 [ %60, %59 ], [ -1, %37 ]
ret i32 %62
}