#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 /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char *malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int main(int argc, char **argv) {
  if (argc == 1)
    exit(0);
  int fd;
  if ((fd = open(argv[1], O_RDONLY)) < 0) {
    printf("open file failed!\n");
    exit(0);
  }
  char *buf = malloc(4096);
  while (4096 == read(fd, buf, 4096))
    write(STDOUT_FILENO, buf, 4096);
  write(STDOUT_FILENO, buf, 4096);
  close(fd);
  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    1197 <main+0x27>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11cc <main+0x5c>
lea    0xe40(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    $0x1000,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,%ecx
mov    $0x1000,%eax
cmp    %ecx,%eax
jne    1216 <main+0xa6>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
jmp    11da <main+0x6a>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x1,%edi
je     121a <main+0xaa>
mov    0x8(%rsi),%rdi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     1221 <main+0xb1>
mov    %eax,%ebp
mov    $0x1000,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %rax,%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
cmp    $0x1000,%eax
jne    11f6 <main+0x86>
mov    0x2e2d(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r14),%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
cmp    $0x1000,%eax
je     11d0 <main+0x60>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1040 <write@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xdd8(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [19 x i8] c"open file failed!\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 i8*
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 @exit(i32 0)
unreachable
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 0)
unreachable
22: ; preds = %12
%23 = call i8* @malloc(i32 4096)
store i8* %23, i8** %7
br label %24
24: ; preds = %29, %22
%25 = load i32, i32* %6
%26 = load i8*, i8** %7
%27 = call i32 @read(i32 %25, i8* %26, i32 4096)
%28 = icmp eq i32 4096, %27
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = load i32, i32* @STDOUT_FILENO
%31 = load i8*, i8** %7
%32 = call i32 @write(i32 %30, i8* %31, i32 4096)
br label %24
33: ; preds = %24
%34 = load i32, i32* @STDOUT_FILENO
%35 = load i8*, i8** %7
%36 = call i32 @write(i32 %34, i8* %35, i32 4096)
%37 = load i32, i32* %6
%38 = call i32 @close(i32 %37)
ret i32 0
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@STDOUT_FILENO = global i32 0
@str = [18 x i8] c"open file failed!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 1
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @exit(i32 0)
unreachable
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* %8, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %6
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 0)
unreachable
15: ; preds = %6
%16 = tail call dereferenceable_or_null(4096) i8* @malloc(i32 4096)
%17 = tail call i32 @read(i32 %10, i8* %16, i32 4096)
%18 = icmp eq i32 %17, 4096
br i1 %18, label %19, label %24
19: ; preds = %15, %19
%20 = load i32, i32* @STDOUT_FILENO
%21 = tail call i32 @write(i32 %20, i8* %16, i32 4096)
%22 = tail call i32 @read(i32 %10, i8* %16, i32 4096)
%23 = icmp eq i32 %22, 4096
br i1 %23, label %19, label %24
24: ; preds = %19, %15
%25 = load i32, i32* @STDOUT_FILENO
%26 = tail call i32 @write(i32 %25, i8* %16, i32 4096)
%27 = tail call i32 @close(i32 %10)
ret i32 0
}