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

/* Variables and functions */
int EACCES;
int O_NONBLOCK;
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fchmod(int, int /*<<< orphan*/);
scalar_t__ fstat(int, struct stat *);
int open(char const *, int, int /*<<< orphan*/);
int safe_open(const char *path, int flags, mode_t mode) {
  int fd, serrno;
  struct stat stbuf;
  if ((fd = open(path, flags | O_NONBLOCK, mode)) < 0 ||
      fstat(fd, &stbuf) < 0) {
    if (fd >= 0) {
      serrno = errno;
      close(fd);
      errno = serrno;
    }
    return (-1);
  }
  if (!S_ISREG(stbuf.st_mode)) {
    close(fd);
    errno = EACCES;
    return (-1);
  }
  if (mode)
    (void)fchmod(fd, mode);
  return (fd);
}


x86 assembly (clang, -O0):
<safe_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2e78(%rip),%rax
or     (%rax),%esi
mov    -0x18(%rbp),%edx
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jl     119c <safe_open+0x4c>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0x0,%rax
jge    11d2 <safe_open+0x82>
cmpl   $0x0,-0x1c(%rbp)
jl     11c6 <safe_open+0x76>
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x20(%rbp),%ecx
mov    0x2e0c(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1224 <safe_open+0xd4>
mov    -0x28(%rbp),%edi
call   1040 <S_ISREG@plt>
cmp    $0x0,%eax
jne    1209 <safe_open+0xb9>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%ecx
mov    0x2dd5(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1224 <safe_open+0xd4>
cmpl   $0x0,-0x18(%rbp)
je     121e <safe_open+0xce>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1050 <fchmod@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_open>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    0x2e88(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     11cf <safe_open+0x7f>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   1070 <fstat@plt>
test   %rax,%rax
js     11a0 <safe_open+0x50>
mov    (%rsp),%edi
call   1040 <S_ISREG@plt>
test   %eax,%eax
je     11b6 <safe_open+0x66>
test   %ebp,%ebp
je     119b <safe_open+0x4b>
mov    %ebx,%edi
mov    %ebp,%esi
call   1050 <fchmod@plt>
mov    %ebx,%r14d
jmp    11cf <safe_open+0x7f>
mov    0x2e29(%rip),%r15
mov    (%r15),%ebp
mov    %ebx,%edi
call   1030 <close@plt>
mov    %ebp,(%r15)
jmp    11cf <safe_open+0x7f>
mov    %ebx,%edi
call   1030 <close@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    0x2e03(%rip),%rcx
mov    %eax,(%rcx)
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_NONBLOCK = global i32 0
@errno = global i32 0
@EACCES = global i32 0
define i32 @safe_open(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.stat
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%11 = load i8*, i8** %5
%12 = load i32, i32* %6
%13 = load i32, i32* @O_NONBLOCK
%14 = or i32 %12, %13
%15 = load i32, i32* %7
%16 = call i32 @open(i8* %11, i32 %14, i32 %15)
store i32 %16, i32* %8
%17 = icmp slt i32 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %3
%19 = load i32, i32* %8
%20 = call i64 @fstat(i32 %19, %struct.stat* %10)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %31
22: ; preds = %18, %3
%23 = load i32, i32* %8
%24 = icmp sge i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = load i32, i32* @errno
store i32 %26, i32* %9
%27 = load i32, i32* %8
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* %9
store i32 %29, i32* @errno
br label %30
30: ; preds = %25, %22
store i32 -1, i32* %4
br label %49
31: ; preds = %18
%32 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @S_ISREG(i32 %33)
%35 = icmp ne i32 %34, 0
br i1 %35, label %40, label %36
36: ; preds = %31
%37 = load i32, i32* %8
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* @EACCES
store i32 %39, i32* @errno
store i32 -1, i32* %4
br label %49
40: ; preds = %31
%41 = load i32, i32* %7
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %40
%44 = load i32, i32* %8
%45 = load i32, i32* %7
%46 = call i32 @fchmod(i32 %44, i32 %45)
br label %47
47: ; preds = %43, %40
%48 = load i32, i32* %8
store i32 %48, i32* %4
br label %49
49: ; preds = %47, %36, %30
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_NONBLOCK = global i32 0
@errno = global i32 0
@EACCES = global i32 0
define i32 @safe_open(i8* nocapture %0, i32 %1, i32 %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = load i32, i32* @O_NONBLOCK
%7 = or i32 %6, %1
%8 = tail call i32 @open(i8* %0, i32 %7, i32 %2)
%9 = icmp slt i32 %8, 0
br i1 %9, label %28, label %10
10: ; preds = %3
%11 = call i64 @fstat(i32 %8, %struct.stat* %4)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i32, i32* @errno
%15 = tail call i32 @close(i32 %8)
store i32 %14, i32* @errno
br label %28
16: ; preds = %10
%17 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%18 = load i32, i32* %17
%19 = tail call i32 @S_ISREG(i32 %18)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = tail call i32 @close(i32 %8)
%23 = load i32, i32* @EACCES
store i32 %23, i32* @errno
br label %28
24: ; preds = %16
%25 = icmp eq i32 %2, 0
br i1 %25, label %28, label %26
26: ; preds = %24
%27 = tail call i32 @fchmod(i32 %8, i32 %2)
br label %28
28: ; preds = %3, %24, %26, %13, %21
%29 = phi i32 [ -1, %21 ], [ -1, %13 ], [ %8, %26 ], [ %8, %24 ], [ -1, %3 ]
ret i32 %29
}