#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 int32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ ENOENT;
int config_path(char **);
scalar_t__ errno;
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
int snepulator_directory(char **);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int32_t config_open(FILE **config_file, char *mode) {
  char *directory;
  char *path;
  struct stat stat_buf;
  if (snepulator_directory(&directory) == -1) {
    return -1;
  }
  if (config_path(&path) == -1) {
    return -1;
  }
  if (stat(directory, &stat_buf) == -1) {
    if (errno == ENOENT) {
      perror("Error: .snepulator directory not found.");
      return -1;
    } else {
      perror("Error: Unable to stat configuration directory.");
      return -1;
    }
  }
  if (stat(path, &stat_buf) == -1) {
    if (errno == ENOENT) {
      if (strcmp(mode, "r") == 0) {
        return 0;
      }
    } else {
      perror("Error: Unable to stat configuration file.");
      return -1;
    }
  }
  *config_file = fopen(path, mode);
  if (*config_file == NULL) {
    fprintf(stderr, "Error: Unable to open %s.\n", path);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<config_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1030 <snepulator_directory@plt>
cmp    $0xffffffff,%eax
jne    119e <config_open+0x2e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e5 <config_open+0x175>
lea    -0x28(%rbp),%rdi
call   1040 <config_path@plt>
cmp    $0xffffffff,%eax
jne    11bc <config_open+0x4c>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e5 <config_open+0x175>
mov    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
jne    121c <config_open+0xac>
mov    0x2dff(%rip),%rax
mov    (%rax),%rax
mov    0x2de5(%rip),%rcx
cmp    (%rcx),%rax
jne    1204 <config_open+0x94>
lea    0xe0d(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e5 <config_open+0x175>
lea    0xe1d(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e5 <config_open+0x175>
mov    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
jne    1294 <config_open+0x124>
mov    0x2d9f(%rip),%rax
mov    (%rax),%rax
mov    0x2d85(%rip),%rcx
cmp    (%rcx),%rax
jne    1277 <config_open+0x107>
mov    -0x18(%rbp),%rdi
lea    0xe00(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    1272 <config_open+0x102>
movl   $0x0,-0x4(%rbp)
jmp    12e5 <config_open+0x175>
jmp    128f <config_open+0x11f>
lea    0xddb(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e5 <config_open+0x175>
jmp    1294 <config_open+0x124>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <fopen@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    12de <config_open+0x16e>
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xdb6(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e5 <config_open+0x175>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<config_open>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0x18(%rsp),%rdi
call   1030 <snepulator_directory@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     123d <config_open+0xcd>
lea    0x10(%rsp),%rdi
call   1040 <config_path@plt>
cmp    $0xffffffff,%eax
je     123d <config_open+0xcd>
mov    0x18(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     1248 <config_open+0xd8>
mov    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
jne    1203 <config_open+0x93>
mov    0x2dfc(%rip),%rax
mov    (%rax),%rax
mov    0x2de2(%rip),%rcx
cmp    (%rcx),%rax
jne    127a <config_open+0x10a>
lea    0xe61(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     123b <config_open+0xcb>
mov    0x10(%rsp),%rdi
mov    %rbx,%rsi
call   1080 <fopen@plt>
mov    %rax,(%r14)
xor    %ebp,%ebp
test   %rax,%rax
jne    123d <config_open+0xcd>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
mov    0x10(%rsp),%rdx
lea    0xe54(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    123d <config_open+0xcd>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d89(%rip),%rax
mov    (%rax),%rax
mov    0x2d6f(%rip),%rcx
cmp    (%rcx),%rax
jne    126c <config_open+0xfc>
lea    0xd9b(%rip),%rdi
call   1090 <perror@plt>
jmp    1234 <config_open+0xc4>
lea    0xdb5(%rip),%rdi
call   1090 <perror@plt>
jmp    1234 <config_open+0xc4>
lea    0xdd8(%rip),%rdi
call   1090 <perror@plt>
jmp    1234 <config_open+0xc4>

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@.str = [40 x i8] c"Error: .snepulator directory not found.\00"
@.str.1 = [47 x i8] c"Error: Unable to stat configuration directory.\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [42 x i8] c"Error: Unable to stat configuration file.\00"
@stderr = global i32 0
@.str.4 = [27 x i8] c"Error: Unable to open %s.\0A\00"
define i32 @config_open(i32** %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32**
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.stat
store i32** %0, i32*** %4
store i8* %1, i8** %5
%9 = call i32 @snepulator_directory(i8** %6)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %58
12: ; preds = %2
%13 = call i32 @config_path(i8** %7)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %12
store i32 -1, i32* %3
br label %58
16: ; preds = %12
%17 = load i8*, i8** %6
%18 = call i32 @stat(i8* %17, %struct.stat* %8)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %28
20: ; preds = %16
%21 = load i64, i64* @errno
%22 = load i64, i64* @ENOENT
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %58
26: ; preds = %20
%27 = call i32 @perror(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %58
28: ; preds = %16
%29 = load i8*, i8** %7
%30 = call i32 @stat(i8* %29, %struct.stat* %8)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %45
32: ; preds = %28
%33 = load i64, i64* @errno
%34 = load i64, i64* @ENOENT
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %42
36: ; preds = %32
%37 = load i8*, i8** %5
%38 = call i64 @strcmp(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %36
store i32 0, i32* %3
br label %58
41: ; preds = %36
br label %44
42: ; preds = %32
%43 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %58
44: ; preds = %41
br label %45
45: ; preds = %44, %28
%46 = load i8*, i8** %7
%47 = load i8*, i8** %5
%48 = call i32* @fopen(i8* %46, i8* %47)
%49 = load i32**, i32*** %4
store i32* %48, i32** %49
%50 = load i32**, i32*** %4
%51 = load i32*, i32** %50
%52 = icmp eq i32* %51, null
br i1 %52, label %53, label %57
53: ; preds = %45
%54 = load i32, i32* @stderr
%55 = load i8*, i8** %7
%56 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i8* %55)
store i32 -1, i32* %3
br label %58
57: ; preds = %45
store i32 0, i32* %3
br label %58
58: ; preds = %57, %53, %42, %40, %26, %24, %15, %11
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@.str = [40 x i8] c"Error: .snepulator directory not found.\00"
@.str.1 = [47 x i8] c"Error: Unable to stat configuration directory.\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [42 x i8] c"Error: Unable to stat configuration file.\00"
@stderr = global i32 0
@.str.4 = [27 x i8] c"Error: Unable to open %s.\0A\00"
define i32 @config_open(i32** nocapture writeonly %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.stat
%6 = bitcast i8** %3 to i8*
%7 = bitcast i8** %4 to i8*
%8 = bitcast %struct.stat* %5 to i8*
%9 = call i32 @snepulator_directory(i8** %3)
%10 = icmp eq i32 %9, -1
br i1 %10, label %47, label %11
11: ; preds = %2
%12 = call i32 @config_path(i8** %4)
%13 = icmp eq i32 %12, -1
br i1 %13, label %47, label %14
14: ; preds = %11
%15 = load i8*, i8** %3
%16 = call i32 @stat(i8* %15, %struct.stat* %5)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %26
18: ; preds = %14
%19 = load i64, i64* @errno
%20 = load i64, i64* @ENOENT
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %47
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
br label %47
26: ; preds = %14
%27 = load i8*, i8** %4
%28 = call i32 @stat(i8* %27, %struct.stat* %5)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %39
30: ; preds = %26
%31 = load i64, i64* @errno
%32 = load i64, i64* @ENOENT
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
br i1 %36, label %47, label %39
37: ; preds = %30
%38 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
br label %47
39: ; preds = %34, %26
%40 = load i8*, i8** %4
%41 = call i32* @fopen(i8* %40, i8* %1)
store i32* %41, i32** %0
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %47
43: ; preds = %39
%44 = load i32, i32* @stderr
%45 = load i8*, i8** %4
%46 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i8* %45)
br label %47
47: ; preds = %39, %34, %11, %2, %43, %37, %24, %22
%48 = phi i32 [ -1, %22 ], [ -1, %24 ], [ -1, %43 ], [ -1, %37 ], [ -1, %2 ], [ -1, %11 ], [ 0, %34 ], [ 0, %39 ]
ret i32 %48
}