#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_EXCL;
int O_RDWR;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ close(int);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  int fd = -1;
  char filename[] = "test.txt";
  fd = open(filename, O_RDWR | O_CREAT | O_EXCL, S_IRWXU);
  if (-1 == fd) {
    printf("Open file %s failure!,fd:%d\n", filename, fd);
    fd = open(filename, O_RDWR);
    printf("fd:%d\n", fd);
  } else {
    printf("Open file %s success,fd:%d\n", filename, fd);
  }
  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)
movl   $0xffffffff,-0x14(%rbp)
mov    0xeac(%rip),%rax
mov    %rax,-0x1d(%rbp)
mov    0xeaa(%rip),%al
mov    %al,-0x15(%rbp)
lea    -0x1d(%rbp),%rdi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
mov    0x2e83(%rip),%rax
or     (%rax),%esi
mov    0x2e4a(%rip),%rax
or     (%rax),%esi
mov    0x2e59(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    11e3 <main+0xb3>
lea    -0x1d(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x1d(%rbp),%rdi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11f8 <main+0xc8>
lea    -0x1d(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
movabs $0x7478742e74736574,%rax
74 78 74
mov    %rax,0x8(%rsp)
movb   $0x0,0x10(%rsp)
mov    0x2e86(%rip),%r14
mov    0x2e9f(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
mov    0x2e63(%rip),%rax
or     (%rax),%esi
mov    0x2e72(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rdi
xor    %eax,%eax
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     119a <main+0x6a>
mov    %eax,%ebx
lea    0xe9c(%rip),%rdi
lea    0x8(%rsp),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    11d4 <main+0xa4>
lea    0xe68(%rip),%rdi
lea    0x8(%rsp),%rbx
mov    %rbx,%rsi
mov    $0xffffffff,%edx
xor    %eax,%eax
call   1030 <printf@plt>
mov    (%r14),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <open@plt>
mov    %eax,%ebx
lea    0xe54(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.main.filename = [9 x i8] c"test.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRWXU = global i32 0
@.str = [29 x i8] c"Open file %s failure!,fd:%d\0A\00"
@.str.1 = [7 x i8] c"fd:%d\0A\00"
@.str.2 = [28 x i8] c"Open file %s success,fd:%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [9 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
%8 = bitcast [9 x i8]* %7 to i8*
%9 = getelementptr inbounds [9 x i8], [9 x i8]* %7, i64 0, i64 0
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = load i32, i32* @O_EXCL
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IRWXU
%16 = call i32 (i8*, i32, ...) @open(i8* %9, i32 %14, i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp eq i32 -1, %17
br i1 %18, label %19, label %28
19: ; preds = %2
%20 = getelementptr inbounds [9 x i8], [9 x i8]* %7, i64 0, i64 0
%21 = load i32, i32* %6
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %20, i32 %21)
%23 = getelementptr inbounds [9 x i8], [9 x i8]* %7, i64 0, i64 0
%24 = load i32, i32* @O_RDWR
%25 = call i32 (i8*, i32, ...) @open(i8* %23, i32 %24)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %26)
br label %32
28: ; preds = %2
%29 = getelementptr inbounds [9 x i8], [9 x i8]* %7, i64 0, i64 0
%30 = load i32, i32* %6
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %29, i32 %30)
br label %32
32: ; preds = %28, %19
%33 = load i32, i32* %6
%34 = call i32 @close(i32 %33)
ret i32 0
}

LLVM IR (-O2):
@__const.main.filename = [9 x i8] c"test.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRWXU = global i32 0
@.str = [29 x i8] c"Open file %s failure!,fd:%d\0A\00"
@.str.1 = [7 x i8] c"fd:%d\0A\00"
@.str.2 = [28 x i8] c"Open file %s success,fd:%d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [9 x i8]
%4 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = load i32, i32* @O_EXCL
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IRWXU
%11 = call i32 (i8*, i32, ...) @open(i8* %4, i32 %9, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %4, i32 -1)
%15 = load i32, i32* @O_RDWR
%16 = call i32 (i8*, i32, ...) @open(i8* %4, i32 %15)
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %16)
br label %20
18: ; preds = %2
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %4, i32 %11)
br label %20
20: ; preds = %18, %13
%21 = phi i32 [ %16, %13 ], [ %11, %18 ]
%22 = call i32 @close(i32 %21)
ret i32 0
}