#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*/ safe_file;
typedef int /*<<< orphan*/ safe_dir;

/* Variables and functions */
int O_EXCL;
int _POSIX_PATH_MAX;
int /*<<< orphan*/ close(int);
int compare_stat(struct stat *, struct stat *);
scalar_t__ fstat(int, struct stat *);
scalar_t__ lstat(char const *, struct stat *);
int mutt_mkwrapdir(char const *, char *, int, char *, int);
int mutt_put_file_in_place(char const *, char *, char *);
int open(char const *, int, int);
int /*<<< orphan*/ rmdir(char *);
int safe_open(const char *path, int flags) {
  struct stat osb, nsb;
  int fd;
  if (flags & O_EXCL) {
    char safe_file[_POSIX_PATH_MAX];
    char safe_dir[_POSIX_PATH_MAX];
    if (mutt_mkwrapdir(path, safe_file, sizeof(safe_file), safe_dir,
                       sizeof(safe_dir)) == -1)
      return -1;
    if ((fd = open(safe_file, flags, 0600)) < 0) {
      rmdir(safe_dir);
      return fd;
    }
    close(fd);
    if (mutt_put_file_in_place(path, safe_file, safe_dir) == -1)
      return -1;
  }
  if ((fd = open(path, flags & ~O_EXCL, 0600)) < 0)
    return fd;
  if (lstat(path, &osb) < 0 || fstat(fd, &nsb) < 0 ||
      compare_stat(&osb, &nsb) == -1) {
    close(fd);
    return (-1);
  }
  return (fd);
}


x86 assembly (clang, -O0):
<safe_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e3f(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12ad <safe_open+0x12d>
mov    0x2e4d(%rip),%rax
mov    (%rax),%ecx
mov    %ecx,%edx
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rdx,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x58(%rbp)
mov    %rsi,%rsp
mov    %rdx,-0x38(%rbp)
mov    (%rax),%eax
mov    %rax,%rdi
add    $0xf,%rdi
and    $0xfffffffffffffff0,%rdi
mov    %rsp,%rcx
sub    %rdi,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
mov    %eax,%r8d
call   1090 <mutt_mkwrapdir@plt>
cmp    $0xffffffff,%eax
jne    1218 <safe_open+0x98>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    128c <safe_open+0x10c>
mov    -0x58(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    $0x180,%edx
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1250 <safe_open+0xd0>
mov    -0x50(%rbp),%rdi
call   1030 <rmdir@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    128c <safe_open+0x10c>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x58(%rbp),%rsi
mov    -0x50(%rbp),%rdx
mov    -0x10(%rbp),%rdi
call   1080 <mutt_put_file_in_place@plt>
cmp    $0xffffffff,%eax
jne    1285 <safe_open+0x105>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    128c <safe_open+0x10c>
movl   $0x0,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x44(%rbp),%eax
test   %eax,%eax
je     12a8 <safe_open+0x128>
jmp    12a3 <safe_open+0x123>
jmp    1340 <safe_open+0x1c0>
jmp    12ad <safe_open+0x12d>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2d1d(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%esi
mov    $0x180,%edx
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    12e3 <safe_open+0x163>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1340 <safe_open+0x1c0>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <lstat@plt>
cmp    $0x0,%rax
jl     1326 <safe_open+0x1a6>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   10a0 <fstat@plt>
cmp    $0x0,%rax
jl     1326 <safe_open+0x1a6>
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <compare_stat@plt>
cmp    $0xffffffff,%eax
jne    133a <safe_open+0x1ba>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1340 <safe_open+0x1c0>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_open>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r15d
mov    %rdi,%r14
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
test   %r15d,%esi
je     123e <safe_open+0xbe>
mov    %rsp,%r12
mov    0x2e45(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%r13
mov    %rdx,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%r13
mov    %r13,%rsp
mov    (%rax),%r8d
mov    %rsp,%rbx
mov    %r8,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %r14,%rdi
mov    %r13,%rsi
mov    %rbx,%rcx
call   1090 <mutt_mkwrapdir@plt>
cmp    $0xffffffff,%eax
mov    %r12,-0x40(%rbp)
je     129b <safe_open+0x11b>
mov    %r13,%rdi
mov    %r15d,%esi
mov    $0x180,%edx
call   1070 <open@plt>
mov    %eax,%r12d
test   %eax,%eax
js     12a3 <safe_open+0x123>
mov    %r12d,%edi
call   1040 <close@plt>
mov    %r14,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1080 <mutt_put_file_in_place@plt>
cmp    $0xffffffff,%eax
mov    -0x40(%rbp),%rsp
je     1293 <safe_open+0x113>
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
not    %esi
and    %r15d,%esi
mov    %r14,%rdi
mov    $0x180,%edx
call   1070 <open@plt>
mov    %eax,%r12d
test   %eax,%eax
js     12af <safe_open+0x12f>
lea    -0x38(%rbp),%rsi
mov    %r14,%rdi
call   1050 <lstat@plt>
test   %rax,%rax
js     128b <safe_open+0x10b>
lea    -0x30(%rbp),%rsi
mov    %r12d,%edi
call   10a0 <fstat@plt>
test   %rax,%rax
js     128b <safe_open+0x10b>
lea    -0x38(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <compare_stat@plt>
cmp    $0xffffffff,%eax
jne    12af <safe_open+0x12f>
mov    %r12d,%edi
call   1040 <close@plt>
mov    $0xffffffff,%r12d
jmp    12af <safe_open+0x12f>
mov    $0xffffffff,%r12d
jmp    12ab <safe_open+0x12b>
mov    %rbx,%rdi
call   1030 <rmdir@plt>
mov    -0x40(%rbp),%rsp
mov    %r12d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_EXCL = global i32 0
@_POSIX_PATH_MAX = global i32 0
define i32 @safe_open(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.stat
%7 = alloca %struct.stat
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%13 = load i32, i32* %5
%14 = load i32, i32* @O_EXCL
%15 = and i32 %13, %14
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %50
17: ; preds = %2
%18 = load i32, i32* @_POSIX_PATH_MAX
%19 = zext i32 %18 to i64
%20 = call i8* @llvm.stacksave()
store i8* %20, i8** %9
%21 = alloca i8, i64 %19
store i64 %19, i64* %10
%22 = load i32, i32* @_POSIX_PATH_MAX
%23 = zext i32 %22 to i64
%24 = alloca i8, i64 %23
store i64 %23, i64* %11
%25 = load i8*, i8** %4
%26 = trunc i64 %19 to i32
%27 = trunc i64 %23 to i32
%28 = call i32 @mutt_mkwrapdir(i8* %25, i8* %21, i32 %26, i8* %24, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %31
30: ; preds = %17
store i32 -1, i32* %3
store i32 1, i32* %12
br label %46
31: ; preds = %17
%32 = load i32, i32* %5
%33 = call i32 @open(i8* %21, i32 %32, i32 384)
store i32 %33, i32* %8
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = call i32 @rmdir(i8* %24)
%37 = load i32, i32* %8
store i32 %37, i32* %3
store i32 1, i32* %12
br label %46
38: ; preds = %31
%39 = load i32, i32* %8
%40 = call i32 @close(i32 %39)
%41 = load i8*, i8** %4
%42 = call i32 @mutt_put_file_in_place(i8* %41, i8* %21, i8* %24)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %45
44: ; preds = %38
store i32 -1, i32* %3
store i32 1, i32* %12
br label %46
45: ; preds = %38
store i32 0, i32* %12
br label %46
46: ; preds = %45, %44, %35, %30
%47 = load i8*, i8** %9
%48 = load i32, i32* %12
switch i32 %48, label %78 [
i32 0, label %49
i32 1, label %76
]
49: ; preds = %46
br label %50
50: ; preds = %49, %2
%51 = load i8*, i8** %4
%52 = load i32, i32* %5
%53 = load i32, i32* @O_EXCL
%54 = xor i32 %53, -1
%55 = and i32 %52, %54
%56 = call i32 @open(i8* %51, i32 %55, i32 384)
store i32 %56, i32* %8
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %50
%59 = load i32, i32* %8
store i32 %59, i32* %3
br label %76
60: ; preds = %50
%61 = load i8*, i8** %4
%62 = call i64 @lstat(i8* %61, %struct.stat* %6)
%63 = icmp slt i64 %62, 0
br i1 %63, label %71, label %64
64: ; preds = %60
%65 = load i32, i32* %8
%66 = call i64 @fstat(i32 %65, %struct.stat* %7)
%67 = icmp slt i64 %66, 0
br i1 %67, label %71, label %68
68: ; preds = %64
%69 = call i32 @compare_stat(%struct.stat* %6, %struct.stat* %7)
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %74
71: ; preds = %68, %64, %60
%72 = load i32, i32* %8
%73 = call i32 @close(i32 %72)
store i32 -1, i32* %3
br label %76
74: ; preds = %68
%75 = load i32, i32* %8
store i32 %75, i32* %3
br label %76
76: ; preds = %74, %71, %58, %46
%77 = load i32, i32* %3
ret i32 %77
78: ; preds = %46
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_EXCL = global i32 0
@_POSIX_PATH_MAX = global i32 0
define i32 @safe_open(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %3 to i8*
%6 = bitcast %struct.stat* %4 to i8*
%7 = load i32, i32* @O_EXCL
%8 = and i32 %7, %1
%9 = icmp eq i32 %8, 0
br i1 %9, label %33, label %10
10: ; preds = %2
%11 = load i32, i32* @_POSIX_PATH_MAX
%12 = zext i32 %11 to i64
%13 = tail call i8* @llvm.stacksave()
%14 = alloca i8, i64 %12
%15 = load i32, i32* @_POSIX_PATH_MAX
%16 = zext i32 %15 to i64
%17 = alloca i8, i64 %16
%18 = call i32 @mutt_mkwrapdir(i8* %0, i8* %14, i32 %11, i8* %17, i32 %15)
%19 = icmp eq i32 %18, -1
br i1 %19, label %25, label %20
20: ; preds = %10
%21 = call i32 @open(i8* %14, i32 %1, i32 384)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = call i32 @rmdir(i8* %17)
br label %25
25: ; preds = %23, %10
%26 = phi i32 [ -1, %10 ], [ %21, %23 ]
br label %50
27: ; preds = %20
%28 = call i32 @close(i32 %21)
%29 = call i32 @mutt_put_file_in_place(i8* %0, i8* %14, i8* %17)
%30 = icmp eq i32 %29, -1
br i1 %30, label %50, label %31
31: ; preds = %27
%32 = load i32, i32* @O_EXCL
br label %33
33: ; preds = %31, %2
%34 = phi i32 [ %32, %31 ], [ %7, %2 ]
%35 = xor i32 %34, -1
%36 = and i32 %35, %1
%37 = call i32 @open(i8* %0, i32 %36, i32 384)
%38 = icmp slt i32 %37, 0
br i1 %38, label %50, label %39
39: ; preds = %33
%40 = call i64 @lstat(i8* %0, %struct.stat* %3)
%41 = icmp slt i64 %40, 0
br i1 %41, label %48, label %42
42: ; preds = %39
%43 = call i64 @fstat(i32 %37, %struct.stat* %4)
%44 = icmp slt i64 %43, 0
br i1 %44, label %48, label %45
45: ; preds = %42
%46 = call i32 @compare_stat(%struct.stat* %3, %struct.stat* %4)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %45, %42, %39
%49 = call i32 @close(i32 %37)
br label %50
50: ; preds = %25, %45, %33, %27, %48
%51 = phi i32 [ -1, %48 ], [ -1, %27 ], [ %37, %33 ], [ %37, %45 ], [ %26, %25 ]
ret i32 %51
}