#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*/ close(int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int cat_main(int argc, char *argv[]) {
  printf("cat\n");
  if (argc != 2) {
    printf("cat: wrong number of parameters\n");
    return -1;
  }
  char *path = argv[1];
  printf("path = %s\n", path);
  char text[248];
  int readData = 0;
  int filedes = open(path, 0x0002);
  readData = read(filedes, text, 248);
  printf("Contents of %s : %s\n", path, text);
  close(filedes);
  return 0;
}


x86 assembly (clang, -O0):
<cat_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xea7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x2,-0x8(%rbp)
je     1184 <cat_main+0x44>
lea    0xe94(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1208 <cat_main+0xc8>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x114(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x2,%esi
call   1060 <open@plt>
mov    %eax,-0x118(%rbp)
mov    -0x118(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0xf8,%edx
call   1050 <read@plt>
mov    %eax,-0x114(%rbp)
mov    -0x18(%rbp),%rsi
lea    -0x110(%rbp),%rdx
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x118(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat_main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0xf8,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xeb7(%rip),%rdi
call   1030 <puts@plt>
cmp    $0x2,%ebp
jne    11c9 <cat_main+0x79>
mov    0x8(%rbx),%rbp
lea    0xe82(%rip),%rdi
xor    %r14d,%r14d
mov    %rbp,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbp,%rdi
mov    $0x2,%esi
call   1070 <open@plt>
mov    %eax,%ebx
mov    %rsp,%r15
mov    %eax,%edi
mov    %r15,%rsi
mov    $0xf8,%edx
call   1060 <read@plt>
lea    0xe58(%rip),%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    11db <cat_main+0x8b>
lea    0xe54(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0xf8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"cat\0A\00"
@.str.1 = [33 x i8] c"cat: wrong number of parameters\0A\00"
@.str.2 = [11 x i8] c"path = %s\0A\00"
@.str.3 = [21 x i8] c"Contents of %s : %s\0A\00"
define i32 @cat_main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca [248 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %31
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
store i8* %18, i8** %6
%19 = load i8*, i8** %6
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %19)
store i32 0, i32* %8
%21 = load i8*, i8** %6
%22 = call i32 @open(i8* %21, i32 2)
store i32 %22, i32* %9
%23 = load i32, i32* %9
%24 = getelementptr inbounds [248 x i8], [248 x i8]* %7, i64 0, i64 0
%25 = call i32 @read(i32 %23, i8* %24, i32 248)
store i32 %25, i32* %8
%26 = load i8*, i8** %6
%27 = getelementptr inbounds [248 x i8], [248 x i8]* %7, i64 0, i64 0
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %26, i8* %27)
%29 = load i32, i32* %9
%30 = call i32 @close(i32 %29)
store i32 0, i32* %3
br label %31
31: ; preds = %15, %13
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@.str.2 = [11 x i8] c"path = %s\0A\00"
@.str.3 = [21 x i8] c"Contents of %s : %s\0A\00"
@str = [4 x i8] c"cat\00"
@str.4 = [32 x i8] c"cat: wrong number of parameters\00"
define i32 @cat_main(i32 %0, i8** nocapture %1) {
%3 = alloca [248 x i8]
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str, i64 0, i64 0))
%5 = icmp eq i32 %0, 2
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.4, i64 0, i64 0))
br label %17
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %10)
%12 = getelementptr inbounds [248 x i8], [248 x i8]* %3, i64 0, i64 0
%13 = tail call i32 @open(i8* %10, i32 2)
%14 = call i32 @read(i32 %13, i8* %12, i32 248)
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %10, i8* %12)
%16 = call i32 @close(i32 %13)
br label %17
17: ; preds = %8, %6
%18 = phi i32 [ -1, %6 ], [ 0, %8 ]
ret i32 %18
}