#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 /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  char buffer[64];
  char *error_msg = "open file error\n";
  char *success_msg = "open file success\n";
  char *filename = NULL;
  if (argc >= 2)
    filename = argv[1];
  else
    filename = "readme.txt";
  int fd = open(filename, 0, 0);
  if (fd == -1) {
    write(0, error_msg, strlen(error_msg));
    return -1;
  }
  write(0, success_msg, strlen(success_msg));
  read(fd, buffer, 64);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe90(%rip),%rax
mov    %rax,-0x58(%rbp)
lea    0xe96(%rip),%rax
mov    %rax,-0x60(%rbp)
movq   $0x0,-0x68(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11a2 <main+0x52>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x68(%rbp)
jmp    11ad <main+0x5d>
lea    0xe7b(%rip),%rax
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <open@plt>
mov    %eax,-0x6c(%rbp)
cmpl   $0xffffffff,-0x6c(%rbp)
jne    11f1 <main+0xa1>
mov    -0x58(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x58(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x78(%rbp),%rsi
mov    %eax,%edx
xor    %edi,%edi
call   1030 <write@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122f <main+0xdf>
mov    -0x60(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x60(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x80(%rbp),%rsi
mov    %eax,%edx
xor    %edi,%edi
call   1030 <write@plt>
mov    -0x6c(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x40,%edx
call   1060 <read@plt>
mov    -0x6c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
cmp    $0x2,%edi
jl     1163 <main+0x13>
mov    0x8(%rsi),%rdi
jmp    116a <main+0x1a>
lea    0xeba(%rip),%rdi
xor    %ebx,%ebx
xor    %esi,%esi
xor    %edx,%edx
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11af <main+0x5f>
mov    %eax,%ebp
lea    0xe8e(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
xor    %edi,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x40,%edx
call   1060 <read@plt>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11cf <main+0x7f>
lea    0xe4a(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"open file error\0A\00"
@.str.1 = [19 x i8] c"open file success\0A\00"
@.str.2 = [11 x i8] c"readme.txt\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [64 x i8]
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8** %7
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8** %8
store i8* null, i8** %9
%11 = load i32, i32* %4
%12 = icmp sge i32 %11, 2
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
store i8* %16, i8** %9
br label %18
17: ; preds = %2
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8** %9
br label %18
18: ; preds = %17, %13
%19 = load i8*, i8** %9
%20 = call i32 @open(i8* %19, i32 0, i32 0)
store i32 %20, i32* %10
%21 = load i32, i32* %10
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %28
23: ; preds = %18
%24 = load i8*, i8** %7
%25 = load i8*, i8** %7
%26 = call i32 @strlen(i8* %25)
%27 = call i32 @write(i32 0, i8* %24, i32 %26)
store i32 -1, i32* %3
br label %38
28: ; preds = %18
%29 = load i8*, i8** %8
%30 = load i8*, i8** %8
%31 = call i32 @strlen(i8* %30)
%32 = call i32 @write(i32 0, i8* %29, i32 %31)
%33 = load i32, i32* %10
%34 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%35 = call i32 @read(i32 %33, i8* %34, i32 64)
%36 = load i32, i32* %10
%37 = call i32 @close(i32 %36)
store i32 0, i32* %3
br label %38
38: ; preds = %28, %23
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@.str = [17 x i8] c"open file error\0A\00"
@.str.1 = [19 x i8] c"open file success\0A\00"
@.str.2 = [11 x i8] c"readme.txt\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
br label %9
9: ; preds = %2, %6
%10 = phi i8* [ %8, %6 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), %2 ]
%11 = tail call i32 @open(i8* %10, i32 0, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = tail call i32 @strlen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @write(i32 0, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %21
16: ; preds = %9
%17 = tail call i32 @strlen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @write(i32 0, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = call i32 @read(i32 %11, i8* %4, i32 64)
%20 = tail call i32 @close(i32 %11)
br label %21
21: ; preds = %16, %13
%22 = phi i32 [ -1, %13 ], [ 0, %16 ]
ret i32 %22
}