#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 */
scalar_t__ EEXIST;
int O_CREAT;
int O_EXCL;
int O_WRONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int main(int argc, char *argv[]) {
  int fd;
  fd = open(argv[1], O_CREAT | O_WRONLY, 0644);
  if (fd < 0) {
    perror("open");
    return -1;
  }
  close(fd);
  fd = open(argv[1], O_CREAT | O_EXCL, 0644);
  if (fd < 0 && errno == EEXIST)
    printf("test case pass\n");
  else
    printf("test case failed\n");
  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)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e93(%rip),%rax
mov    (%rax),%esi
mov    0x2e4a(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    119f <main+0x5f>
lea    0xe72(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121e <main+0xde>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
mov    0x2e11(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1203 <main+0xc3>
mov    0x2de9(%rip),%rax
mov    (%rax),%rax
mov    0x2def(%rip),%rcx
cmp    (%rcx),%rax
jne    1203 <main+0xc3>
lea    0xe0c(%rip),%rdi
call   1030 <printf@plt>
jmp    120f <main+0xcf>
lea    0xe0b(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2ea6(%rip),%r14
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
mov    $0x1a4,%edx
call   1050 <open@plt>
test   %eax,%eax
js     11cf <main+0x8f>
mov    %eax,%edi
call   1040 <close@plt>
mov    0x8(%rbx),%rdi
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
mov    $0x1a4,%edx
call   1050 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11b2 <main+0x72>
mov    0x2e2e(%rip),%rax
mov    (%rax),%rax
mov    0x2e34(%rip),%rcx
cmp    (%rcx),%rax
jne    11b2 <main+0x72>
lea    0xe66(%rip),%rdi
jmp    11b9 <main+0x79>
lea    0xe4c(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe2a(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    11c7 <main+0x87>

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str = [5 x i8] c"open\00"
@O_EXCL = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [16 x i8] c"test case pass\0A\00"
@.str.2 = [18 x i8] c"test case failed\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 i8**, i8*** %5
%8 = getelementptr inbounds i8*, i8** %7, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @O_CREAT
%11 = load i32, i32* @O_WRONLY
%12 = or i32 %10, %11
%13 = call i32 @open(i8* %9, i32 %12, i32 420)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %41
18: ; preds = %2
%19 = load i32, i32* %6
%20 = call i32 @close(i32 %19)
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = load i32, i32* @O_CREAT
%25 = load i32, i32* @O_EXCL
%26 = or i32 %24, %25
%27 = call i32 @open(i8* %23, i32 %26, i32 420)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %18
%31 = load i64, i64* @errno
%32 = load i64, i64* @EEXIST
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %38
36: ; preds = %30, %18
%37 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %36, %34
%39 = load i32, i32* %6
%40 = call i32 @close(i32 %39)
store i32 0, i32* %3
br label %41
41: ; preds = %38, %16
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str = [5 x i8] c"open\00"
@O_EXCL = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@str = [17 x i8] c"test case failed\00"
@str.3 = [15 x i8] c"test case pass\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_WRONLY
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* %4, i32 %7, i32 420)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %29
12: ; preds = %2
%13 = tail call i32 @close(i32 %8)
%14 = load i8*, i8** %3
%15 = load i32, i32* @O_CREAT
%16 = load i32, i32* @O_EXCL
%17 = or i32 %16, %15
%18 = tail call i32 @open(i8* %14, i32 %17, i32 420)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %12
%21 = load i64, i64* @errno
%22 = load i64, i64* @EEXIST
%23 = icmp eq i64 %21, %22
br i1 %23, label %25, label %24
24: ; preds = %20, %12
br label %25
25: ; preds = %20, %24
%26 = phi i8* [ getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0), %24 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @str.3, i64 0, i64 0), %20 ]
%27 = tail call i32 @puts(i8* dereferenceable(1) %26)
%28 = tail call i32 @close(i32 %18)
br label %29
29: ; preds = %25, %10
%30 = phi i32 [ -1, %10 ], [ 0, %25 ]
ret i32 %30
}