#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 */
struct stat {
  int dummy;
};
typedef int /*<<< orphan*/ mode_t;

/* Variables and functions */
#define EACCES 135
#define EEXIST 134
#define EINTR 133
#define EISDIR 132
#define ELOOP 131
#define ENAMETOOLONG 130
#define ENOENT 129
#define ENOTDIR 128
int O_EXCL;
int errno;
int open(char const *, int, int /*<<< orphan*/);
int stat(char const *, struct stat *);
int open_weak_exclusive(const char *pathname, int flags, mode_t mode) {
  int ret;
  struct stat filestat;
  ret = open(pathname, flags, mode);
  if (ret != -1 || !(flags & O_EXCL)) {
    return ret;
  }
  switch (errno) {
  case EACCES:
  case EEXIST:
  case EINTR:
  case EISDIR:
  case ELOOP:
  case ENAMETOOLONG:
  case ENOENT:
  case ENOTDIR:
    return ret;
  }
  if (stat(pathname, &filestat) != -1) {
    errno = EEXIST;
    return -1;
  } else {
    return open(pathname, flags & ~O_EXCL, mode);
  }
}


x86 assembly (clang, -O0):
<open_weak_exclusive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1040 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1163 <open_weak_exclusive+0x43>
mov    -0x14(%rbp),%eax
mov    0x2e88(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    116e <open_weak_exclusive+0x4e>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e2 <open_weak_exclusive+0xc2>
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
sub    $0x7,%eax
ja     1193 <open_weak_exclusive+0x73>
jmp    1188 <open_weak_exclusive+0x68>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e2 <open_weak_exclusive+0xc2>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1030 <stat@plt>
cmp    $0xffffffff,%eax
je     11c2 <open_weak_exclusive+0xa2>
mov    0x2e28(%rip),%rax
movl   $0x86,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11e2 <open_weak_exclusive+0xc2>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%esi
mov    -0x18(%rbp),%edx
call   1040 <open@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_weak_exclusive>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%r15
call   1040 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1150 <open_weak_exclusive+0x30>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e89(%rip),%r12
mov    $0xffffffff,%ebx
test   %ebp,(%r12)
je     113f <open_weak_exclusive+0x1f>
mov    0x2e6f(%rip),%r13
mov    0x0(%r13),%eax
and    $0xfffffff8,%eax
cmp    $0x80,%eax
je     113f <open_weak_exclusive+0x1f>
mov    %rsp,%rsi
mov    %r15,%rdi
call   1030 <stat@plt>
cmp    $0xffffffff,%eax
je     1191 <open_weak_exclusive+0x71>
movl   $0x86,0x0(%r13)
jmp    113f <open_weak_exclusive+0x1f>
mov    (%r12),%esi
not    %esi
and    %ebp,%esi
mov    %r15,%rdi
mov    %r14d,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <open@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_EXCL = global i32 0
@errno = global i32 0
define i32 @open_weak_exclusive(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.stat
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = load i32, i32* %6
%12 = load i32, i32* %7
%13 = call i32 @open(i8* %10, i32 %11, i32 %12)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp ne i32 %14, -1
br i1 %15, label %21, label %16
16: ; preds = %3
%17 = load i32, i32* %6
%18 = load i32, i32* @O_EXCL
%19 = and i32 %17, %18
%20 = icmp ne i32 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %16, %3
%22 = load i32, i32* %8
store i32 %22, i32* %4
br label %40
23: ; preds = %16
%24 = load i32, i32* @errno
switch i32 %24, label %27 [
i32 135, label %25
i32 134, label %25
i32 133, label %25
i32 132, label %25
i32 131, label %25
i32 130, label %25
i32 129, label %25
i32 128, label %25
]
25: ; preds = %23, %23, %23, %23, %23, %23, %23, %23
%26 = load i32, i32* %8
store i32 %26, i32* %4
br label %40
27: ; preds = %23
%28 = load i8*, i8** %5
%29 = call i32 @stat(i8* %28, %struct.stat* %9)
%30 = icmp ne i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %27
store i32 134, i32* @errno
store i32 -1, i32* %4
br label %40
32: ; preds = %27
%33 = load i8*, i8** %5
%34 = load i32, i32* %6
%35 = load i32, i32* @O_EXCL
%36 = xor i32 %35, -1
%37 = and i32 %34, %36
%38 = load i32, i32* %7
%39 = call i32 @open(i8* %33, i32 %37, i32 %38)
store i32 %39, i32* %4
br label %40
40: ; preds = %32, %31, %25, %21
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_EXCL = global i32 0
@errno = global i32 0
define i32 @open_weak_exclusive(i8* nocapture %0, i32 %1, i32 %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = tail call i32 @open(i8* %0, i32 %1, i32 %2)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %25
8: ; preds = %3
%9 = load i32, i32* @O_EXCL
%10 = and i32 %9, %1
%11 = icmp eq i32 %10, 0
br i1 %11, label %25, label %12
12: ; preds = %8
%13 = load i32, i32* @errno
%14 = and i32 %13, -8
%15 = icmp eq i32 %14, 128
br i1 %15, label %25, label %16
16: ; preds = %12
%17 = call i32 @stat(i8* %0, %struct.stat* %4)
%18 = icmp eq i32 %17, -1
br i1 %18, label %20, label %19
19: ; preds = %16
store i32 134, i32* @errno
br label %25
20: ; preds = %16
%21 = load i32, i32* @O_EXCL
%22 = xor i32 %21, -1
%23 = and i32 %22, %1
%24 = tail call i32 @open(i8* %0, i32 %23, i32 %2)
br label %25
25: ; preds = %12, %3, %8, %20, %19
%26 = phi i32 [ -1, %19 ], [ %24, %20 ], [ -1, %8 ], [ %6, %3 ], [ -1, %12 ]
ret i32 %26
}