#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 BUFSIZ;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int my_open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int main(int argc, char **argv) {
  int fd, n;
  char buff[BUFSIZ];
  if (argc != 2) {
    perror("usage: mycat <pathname>");
    exit(1);
  }
  if ((fd = my_open(argv[1], O_RDONLY)) < 0) {
    fprintf(stderr, "cannot open %s\n", argv[1]);
    exit(1);
  }
  while ((n = read(fd, buff, BUFSIZ)) > 0) {
    write(STDOUT_FILENO, buff, n);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11c2 <main+0x62>
lea    0xe4d(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
call   1050 <my_open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    120b <main+0xab>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe1c(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1210 <main+0xb0>
mov    -0x30(%rbp),%rsi
mov    -0x14(%rbp),%edi
mov    0x2daa(%rip),%rax
mov    (%rax),%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    124b <main+0xeb>
mov    -0x30(%rbp),%rsi
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
call   1030 <write@plt>
jmp    1210 <main+0xb0>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e56(%rip),%r12
mov    (%r12),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
cmp    $0x2,%edi
jne    11c8 <main+0x68>
mov    %rsi,%r15
mov    0x8(%rsi),%rdi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
call   1050 <my_open@plt>
test   %eax,%eax
jns    11de <main+0x7e>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r15),%rdx
lea    0xe5f(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe31(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %eax,%r14d
mov    (%r12),%edx
mov    %eax,%edi
mov    %rbx,%rsi
call   1040 <read@plt>
test   %eax,%eax
jle    1220 <main+0xc0>
mov    0x2df6(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    (%r15),%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    (%r12),%edx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1040 <read@plt>
test   %eax,%eax
jg     1200 <main+0xa0>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [24 x i8] c"usage: mycat <pathname>\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [16 x i8] c"cannot open %s\0A\00"
@STDOUT_FILENO = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @BUFSIZ
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %8
%13 = alloca i8, i64 %11
store i64 %11, i64* %9
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 2
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%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 @my_open(i8* %22, i32 %23)
store i32 %24, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %19
%27 = load i32, i32* @stderr
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %30)
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %19
br label %34
34: ; preds = %39, %33
%35 = load i32, i32* %6
%36 = load i32, i32* @BUFSIZ
%37 = call i32 @read(i32 %35, i8* %13, i32 %36)
store i32 %37, i32* %7
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %34
%40 = load i32, i32* @STDOUT_FILENO
%41 = load i32, i32* %7
%42 = call i32 @write(i32 %40, i8* %13, i32 %41)
br label %34
43: ; preds = %34
%44 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [24 x i8] c"usage: mycat <pathname>\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [16 x i8] c"cannot open %s\0A\00"
@STDOUT_FILENO = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i32, i32* @BUFSIZ
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = icmp eq i32 %0, 2
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 @my_open(i8* %12, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %10
%17 = load i32, i32* @BUFSIZ
%18 = call i32 @read(i32 %14, i8* %5, i32 %17)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %25, label %32
20: ; preds = %10
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %11
%23 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %16, %25
%26 = phi i32 [ %30, %25 ], [ %18, %16 ]
%27 = load i32, i32* @STDOUT_FILENO
%28 = call i32 @write(i32 %27, i8* %5, i32 %26)
%29 = load i32, i32* @BUFSIZ
%30 = call i32 @read(i32 %14, i8* %5, i32 %29)
%31 = icmp sgt i32 %30, 0
br i1 %31, label %25, label %32
32: ; preds = %25, %16
%33 = tail call i32 @exit(i32 0)
unreachable
}