#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 open(char *, int, ...);
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  int fd = -1;
  char filename[] = "test2.txt";
  fd = open(filename, O_RDWR | O_CREAT | O_EXCL, 00700);
  if (-1 == fd) {
    printf("File %s is exist,reopen it", filename);
    fd = open(filename, O_RDWR);
    printf("fd:%d", fd);
  } else {
    printf("File %s open success,fd:%d\n", filename, fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
mov    0xec3(%rip),%rax
mov    %rax,-0x12(%rbp)
mov    0xec0(%rip),%ax
mov    %ax,-0xa(%rbp)
lea    -0x12(%rbp),%rdi
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
mov    0x2e98(%rip),%rax
or     (%rax),%esi
mov    0x2e67(%rip),%rax
or     (%rax),%esi
mov    $0x1c0,%edx
mov    $0x0,%al
call   1040 <open@plt>
mov    %eax,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    11c7 <main+0xa7>
lea    -0x12(%rbp),%rsi
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x12(%rbp),%rdi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11dc <main+0xbc>
lea    -0x12(%rbp),%rsi
mov    -0x8(%rbp),%edx
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
movabs $0x78742e3274736574,%rax
2e 74 78
mov    %rax,0x8(%rsp)
mov    0x2ea3(%rip),%rbx
mov    0x2eb4(%rip),%rax
mov    (%rax),%esi
or     (%rbx),%esi
movw   $0x74,0x10(%rsp)
mov    0x2e7a(%rip),%rax
or     (%rax),%esi
lea    0x8(%rsp),%rdi
mov    $0x1c0,%edx
xor    %eax,%eax
call   1040 <open@plt>
cmp    $0xffffffff,%eax
je     1185 <main+0x65>
lea    0xeb6(%rip),%rdi
lea    0x8(%rsp),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    11b7 <main+0x97>
lea    0xe7e(%rip),%rdi
lea    0x8(%rsp),%r14
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    (%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <open@plt>
lea    0xe77(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@__const.main.filename = [10 x i8] c"test2.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str = [27 x i8] c"File %s is exist,reopen it\00"
@.str.1 = [6 x i8] c"fd:%d\00"
@.str.2 = [28 x i8] c"File %s open success,fd:%d\0A\00"
define i32 @main() {
%1 = alloca [10 x i8]
%2 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = load i32, i32* @O_EXCL
%7 = or i32 %5, %6
%8 = call i32 (i8*, i32, ...) @open(i8* %2, i32 %7, i32 448)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %15
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %2)
%12 = load i32, i32* @O_RDWR
%13 = call i32 (i8*, i32, ...) @open(i8* %2, i32 %12)
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %13)
br label %17
15: ; preds = %0
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %2, i32 %8)
br label %17
17: ; preds = %15, %10
ret i32 0
}