#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 O_CREAT;
int O_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ puts(char *);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    puts("You haven't input the open filename, please try again!\n");
    return 1;
  }
  int file;
  if ((file = open(argv[1], O_CREAT | O_RDWR, 0755)) < 0) {
    perror("Open file failure!\n");
    return 2;
  } else {
    printf("Open file %d success!\n", file);
  }
  close(file);
  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   $0x2,-0x8(%rbp)
jge    1188 <main+0x38>
lea    0xe89(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    11ee <main+0x9e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    0x2e40(%rip),%rax
or     (%rax),%esi
mov    $0x1ed,%edx
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11d0 <main+0x80>
lea    0xe79(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    11ee <main+0x9e>
mov    -0x14(%rbp),%esi
lea    0xe72(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
cmp    $0x1,%edi
jg     1169 <main+0x19>
lea    0xea3(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
pop    %rbx
ret
mov    0x8(%rsi),%rdi
mov    0x2e84(%rip),%rax
mov    0x2e65(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1ed,%edx
call   1060 <open@plt>
test   %eax,%eax
js     11a8 <main+0x58>
mov    %eax,%ebx
lea    0xeb6(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe89(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [56 x i8] c"You haven't input the open filename, please try again!\0A\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [20 x i8] c"Open file failure!\0A\00"
@.str.2 = [23 x i8] c"Open file %d success!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 2
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @puts(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %28
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = load i32, i32* @O_CREAT
%16 = load i32, i32* @O_RDWR
%17 = or i32 %15, %16
%18 = call i32 @open(i8* %14, i32 %17, i32 493)
store i32 %18, i32* %6
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %11
%21 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %3
br label %28
22: ; preds = %11
%23 = load i32, i32* %6
%24 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %22
%26 = load i32, i32* %6
%27 = call i32 @close(i32 %26)
store i32 0, i32* %3
br label %28
28: ; preds = %25, %20, %9
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@.str = [56 x i8] c"You haven't input the open filename, please try again!\0A\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [20 x i8] c"Open file failure!\0A\00"
@.str.2 = [23 x i8] c"Open file %d success!\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0))
br label %19
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @O_CREAT
%10 = load i32, i32* @O_RDWR
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* %8, i32 %11, i32 493)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %6
%15 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %19
16: ; preds = %6
%17 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%18 = tail call i32 @close(i32 %12)
br label %19
19: ; preds = %14, %16, %4
%20 = phi i32 [ 1, %4 ], [ 2, %14 ], [ 0, %16 ]
ret i32 %20
}