#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 st_mode;
};
typedef int mode_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int S_IRGRP;
int S_IRUSR;
int S_IWGRP;
int S_IWUSR;
int open(char *, int /*<<< orphan*/, int);
int stat(char *, struct stat *);
int xattrkv_open(char *dbname) {
  struct stat dbs;
  int ok = stat(dbname, &dbs);
  if (ok == 0) {
    mode_t rw = (S_IWUSR | S_IRUSR);
    if ((dbs.st_mode & rw) == rw) {
      int db = open(dbname, O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
      return db;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<xattrkv_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <stat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11b3 <xattrkv_open+0x93>
mov    0x2e8b(%rip),%rax
mov    (%rax),%eax
mov    0x2e6a(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
and    -0x20(%rbp),%eax
cmp    -0x20(%rbp),%eax
jne    11ae <xattrkv_open+0x8e>
mov    -0x10(%rbp),%rdi
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
mov    0x2e51(%rip),%rax
or     (%rax),%edx
mov    0x2e38(%rip),%rax
or     (%rax),%edx
mov    0x2e47(%rip),%rax
or     (%rax),%edx
call   1040 <open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ba <xattrkv_open+0x9a>
jmp    11b3 <xattrkv_open+0x93>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xattrkv_open>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1030 <stat@plt>
test   %eax,%eax
jne    117a <xattrkv_open+0x5a>
mov    0x2e9b(%rip),%rax
mov    0x2e7c(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x8(%rsp),%eax
not    %eax
test   %eax,%edx
jne    117a <xattrkv_open+0x5a>
mov    0x2e8f(%rip),%rax
mov    0x2e68(%rip),%rcx
or     (%rcx),%edx
mov    (%rax),%esi
mov    0x2e75(%rip),%rax
or     (%rax),%edx
mov    %rbx,%rdi
add    $0x10,%rsp
pop    %rbx
jmp    1040 <open@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@O_RDWR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
define i32 @xattrkv_open(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32 @stat(i8* %8, %struct.stat* %4)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %35
12: ; preds = %1
%13 = load i32, i32* @S_IWUSR
%14 = load i32, i32* @S_IRUSR
%15 = or i32 %13, %14
store i32 %15, i32* %6
%16 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%17 = load i32, i32* %16
%18 = load i32, i32* %6
%19 = and i32 %17, %18
%20 = load i32, i32* %6
%21 = icmp eq i32 %19, %20
br i1 %21, label %22, label %34
22: ; preds = %12
%23 = load i8*, i8** %3
%24 = load i32, i32* @O_RDWR
%25 = load i32, i32* @S_IRUSR
%26 = load i32, i32* @S_IWUSR
%27 = or i32 %25, %26
%28 = load i32, i32* @S_IRGRP
%29 = or i32 %27, %28
%30 = load i32, i32* @S_IWGRP
%31 = or i32 %29, %30
%32 = call i32 @open(i8* %23, i32 %24, i32 %31)
store i32 %32, i32* %7
%33 = load i32, i32* %7
store i32 %33, i32* %2
br label %36
34: ; preds = %12
br label %35
35: ; preds = %34, %1
store i32 -1, i32* %2
br label %36
36: ; preds = %35, %22
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@O_RDWR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
define i32 @xattrkv_open(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i32 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %21
6: ; preds = %1
%7 = load i32, i32* @S_IWUSR
%8 = load i32, i32* @S_IRUSR
%9 = or i32 %8, %7
%10 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%11 = load i32, i32* %10
%12 = and i32 %11, %9
%13 = icmp eq i32 %12, %9
br i1 %13, label %14, label %21
14: ; preds = %6
%15 = load i32, i32* @O_RDWR
%16 = load i32, i32* @S_IRGRP
%17 = or i32 %16, %9
%18 = load i32, i32* @S_IWGRP
%19 = or i32 %17, %18
%20 = tail call i32 @open(i8* %0, i32 %15, i32 %19)
br label %21
21: ; preds = %1, %6, %14
%22 = phi i32 [ %20, %14 ], [ -1, %6 ], [ -1, %1 ]
ret i32 %22
}