#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 {
  scalar_t__ st_ino;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int O_APPEND;
int O_CREAT;
int O_EXCL;
int O_RDONLY;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ *fdopen(int, char const *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ fstat(int, struct stat *);
int /*<<< orphan*/ lstat(char const *, struct stat *);
int open(char const *, int, int);
int /*<<< orphan*/ stderr;
FILE *safeopen(const char *path, const char *mode, int flags) {
  int fd;
  int oflags;
  struct stat fb1, fb2;
  if (mode[0] == 'w') {
    oflags = O_WRONLY;
  } else if (mode[0] == 'a') {
    oflags = O_CREAT | O_WRONLY | O_APPEND;
  } else
    oflags = O_RDONLY;
  oflags |= !(flags & (1 << 2)) ? O_CREAT : 0;
  oflags |= (flags & (1 << 1)) ? O_EXCL : 0;
  if ((fd = open(path, oflags, S_IRUSR | S_IWUSR)) < 0)
    return NULL;
  if (!(flags & (1 << 0))) {
    lstat(path, &fb1);
    fstat(fd, &fb2);
    if (fb2.st_ino != fb1.st_ino) {
      fprintf(stderr,
              "safeopen(): refusing to open `%s', "
              "which is a soft link\n",
              path);
      close(fd);
      return NULL;
    }
  }
  return fdopen(fd, mode);
}


x86 assembly (clang, -O0):
<safeopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x77,%eax
jne    1194 <safeopen+0x34>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    11d8 <safeopen+0x78>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x61,%eax
jne    11c7 <safeopen+0x67>
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    0x2df4(%rip),%rcx
or     (%rcx),%eax
mov    0x2de3(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x24(%rbp)
jmp    11d3 <safeopen+0x73>
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    11d8 <safeopen+0x78>
mov    -0x1c(%rbp),%eax
and    $0x4,%eax
cmp    $0x0,%eax
jne    11f8 <safeopen+0x98>
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
jmp    1202 <safeopen+0xa2>
xor    %eax,%eax
mov    %eax,-0x3c(%rbp)
jmp    1202 <safeopen+0xa2>
mov    -0x3c(%rbp),%eax
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     122b <safeopen+0xcb>
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
jmp    1235 <safeopen+0xd5>
xor    %eax,%eax
mov    %eax,-0x40(%rbp)
jmp    1235 <safeopen+0xd5>
mov    -0x40(%rbp),%eax
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    0x2d6c(%rip),%rax
mov    (%rax),%edx
mov    0x2d83(%rip),%rax
or     (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    1275 <safeopen+0x115>
movq   $0x0,-0x8(%rbp)
jmp    12ee <safeopen+0x18e>
mov    -0x1c(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    12de <safeopen+0x17e>
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1040 <lstat@plt>
mov    -0x20(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1080 <fstat@plt>
mov    -0x38(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     12d9 <safeopen+0x179>
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd41(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ee <safeopen+0x18e>
jmp    12de <safeopen+0x17e>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1060 <fdopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safeopen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%r14
mov    (%rsi),%al
cmp    $0x61,%al
je     1185 <safeopen+0x25>
cmp    $0x77,%al
jne    11a2 <safeopen+0x42>
mov    0x2e25(%rip),%rax
jmp    11a9 <safeopen+0x49>
mov    0x2e6c(%rip),%rax
mov    0x2e15(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e02(%rip),%rax
or     (%rax),%esi
jmp    11ab <safeopen+0x4b>
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
test   $0x4,%bpl
jne    11bc <safeopen+0x5c>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %ebp,%ecx
shl    $0x1e,%ecx
sar    $0x1f,%ecx
mov    0x2df5(%rip),%rdx
and    (%rdx),%ecx
or     %ecx,%esi
or     %eax,%esi
mov    0x2de0(%rip),%rax
mov    0x2df9(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %r14,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     124f <safeopen+0xef>
mov    %eax,%ebx
test   $0x1,%bpl
jne    121c <safeopen+0xbc>
lea    0x10(%rsp),%rsi
mov    %r14,%rdi
call   1040 <lstat@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1080 <fstat@plt>
mov    0x8(%rsp),%rax
cmp    0x10(%rsp),%rax
jne    1230 <safeopen+0xd0>
mov    %ebx,%edi
mov    %r15,%rsi
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <fdopen@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
lea    0xdc0(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    %ebx,%edi
call   1030 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@O_RDONLY = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@stderr = global i32 0
@.str = [57 x i8] c"safeopen(): refusing to open `%s', which is a soft link\0A\00"
define i32* @safeopen(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.stat
%11 = alloca %struct.stat
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = load i8*, i8** %6
%13 = getelementptr inbounds i8, i8* %12, i64 0
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 119
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = load i32, i32* @O_WRONLY
store i32 %18, i32* %9
br label %34
19: ; preds = %3
%20 = load i8*, i8** %6
%21 = getelementptr inbounds i8, i8* %20, i64 0
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 97
br i1 %24, label %25, label %31
25: ; preds = %19
%26 = load i32, i32* @O_CREAT
%27 = load i32, i32* @O_WRONLY
%28 = or i32 %26, %27
%29 = load i32, i32* @O_APPEND
%30 = or i32 %28, %29
store i32 %30, i32* %9
br label %33
31: ; preds = %19
%32 = load i32, i32* @O_RDONLY
store i32 %32, i32* %9
br label %33
33: ; preds = %31, %25
br label %34
34: ; preds = %33, %17
%35 = load i32, i32* %7
%36 = and i32 %35, 4
%37 = icmp ne i32 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %34
%39 = load i32, i32* @O_CREAT
br label %41
40: ; preds = %34
br label %41
41: ; preds = %40, %38
%42 = phi i32 [ %39, %38 ], [ 0, %40 ]
%43 = load i32, i32* %9
%44 = or i32 %43, %42
store i32 %44, i32* %9
%45 = load i32, i32* %7
%46 = and i32 %45, 2
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %41
%49 = load i32, i32* @O_EXCL
br label %51
50: ; preds = %41
br label %51
51: ; preds = %50, %48
%52 = phi i32 [ %49, %48 ], [ 0, %50 ]
%53 = load i32, i32* %9
%54 = or i32 %53, %52
store i32 %54, i32* %9
%55 = load i8*, i8** %5
%56 = load i32, i32* %9
%57 = load i32, i32* @S_IRUSR
%58 = load i32, i32* @S_IWUSR
%59 = or i32 %57, %58
%60 = call i32 @open(i8* %55, i32 %56, i32 %59)
store i32 %60, i32* %8
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %63
62: ; preds = %51
store i32* null, i32** %4
br label %88
63: ; preds = %51
%64 = load i32, i32* %7
%65 = and i32 %64, 1
%66 = icmp ne i32 %65, 0
br i1 %66, label %84, label %67
67: ; preds = %63
%68 = load i8*, i8** %5
%69 = call i32 @lstat(i8* %68, %struct.stat* %10)
%70 = load i32, i32* %8
%71 = call i32 @fstat(i32 %70, %struct.stat* %11)
%72 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%73 = load i64, i64* %72
%74 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%75 = load i64, i64* %74
%76 = icmp ne i64 %73, %75
br i1 %76, label %77, label %83
77: ; preds = %67
%78 = load i32, i32* @stderr
%79 = load i8*, i8** %5
%80 = call i32 @fprintf(i32 %78, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i8* %79)
%81 = load i32, i32* %8
%82 = call i32 @close(i32 %81)
store i32* null, i32** %4
br label %88
83: ; preds = %67
br label %84
84: ; preds = %83, %63
%85 = load i32, i32* %8
%86 = load i8*, i8** %6
%87 = call i32* @fdopen(i32 %85, i8* %86)
store i32* %87, i32** %4
br label %88
88: ; preds = %84, %77, %62
%89 = load i32*, i32** %4
ret i32* %89
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@O_RDONLY = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@stderr = global i32 0
@.str = [57 x i8] c"safeopen(): refusing to open `%s', which is a soft link\0A\00"
define noalias i32* @safeopen(i8* %0, i8* nocapture %1, i32 %2) {
%4 = alloca %struct.stat
%5 = alloca %struct.stat
%6 = bitcast %struct.stat* %4 to i8*
%7 = bitcast %struct.stat* %5 to i8*
%8 = load i8, i8* %1
switch i8 %8, label %17 [
i8 119, label %9
i8 97, label %11
]
9: ; preds = %3
%10 = load i32, i32* @O_WRONLY
br label %19
11: ; preds = %3
%12 = load i32, i32* @O_CREAT
%13 = load i32, i32* @O_WRONLY
%14 = or i32 %13, %12
%15 = load i32, i32* @O_APPEND
%16 = or i32 %14, %15
br label %19
17: ; preds = %3
%18 = load i32, i32* @O_RDONLY
br label %19
19: ; preds = %11, %17, %9
%20 = phi i32 [ %10, %9 ], [ %16, %11 ], [ %18, %17 ]
%21 = and i32 %2, 4
%22 = icmp eq i32 %21, 0
%23 = load i32, i32* @O_CREAT
%24 = select i1 %22, i32 %23, i32 0
%25 = or i32 %24, %20
%26 = and i32 %2, 2
%27 = icmp eq i32 %26, 0
%28 = load i32, i32* @O_EXCL
%29 = select i1 %27, i32 0, i32 %28
%30 = or i32 %25, %29
%31 = load i32, i32* @S_IRUSR
%32 = load i32, i32* @S_IWUSR
%33 = or i32 %32, %31
%34 = tail call i32 @open(i8* %0, i32 %30, i32 %33)
%35 = icmp slt i32 %34, 0
br i1 %35, label %53, label %36
36: ; preds = %19
%37 = and i32 %2, 1
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %51
39: ; preds = %36
%40 = call i32 @lstat(i8* %0, %struct.stat* %4)
%41 = call i32 @fstat(i32 %34, %struct.stat* %5)
%42 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%43 = load i64, i64* %42
%44 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%45 = load i64, i64* %44
%46 = icmp eq i64 %43, %45
br i1 %46, label %51, label %47
47: ; preds = %39
%48 = load i32, i32* @stderr
%49 = tail call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i8* %0)
%50 = tail call i32 @close(i32 %34)
br label %53
51: ; preds = %39, %36
%52 = tail call i32* @fdopen(i32 %34, i8* %1)
br label %53
53: ; preds = %19, %51, %47
%54 = phi i32* [ %52, %51 ], [ null, %47 ], [ null, %19 ]
ret i32* %54
}