#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 */

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ LOCK_EX;
int O_DIRECTORY;
int O_RDONLY;
int asprintf(char **, char *, char *);
scalar_t__ errno;
int flock(int, int /*<<< orphan*/);
char *getenv(char *);
int mkdir(char *, int);
int open(char *, int);
int /*<<< orphan*/ warn(char *, char *);
int /*<<< orphan*/ warnx(char *);
int open_berictl_dir(int nocreate) {
  int ccd = -1;
  char *home;
  static char *berictl_dir;
  if (berictl_dir == NULL) {
    berictl_dir = getenv("BERICLT_DIR");
    if (berictl_dir == NULL && (berictl_dir = getenv("CHERICLT_DIR")) != NULL)
      warnx("CHERICLT_DIR is deprecated, use BERICLT_DIR");
    if (berictl_dir == NULL && (home = getenv("HOME")) != NULL &&
        asprintf(&berictl_dir, "%s/.berictl", home) == -1)
      berictl_dir = NULL;
    if (berictl_dir == NULL) {
      warnx("failed to find a berictl dir, "
            "set HOME or BERICLT_DIR");
      return (-1);
    }
  }
  if ((ccd = open(berictl_dir, O_RDONLY | O_DIRECTORY)) == -1 && !nocreate) {
    if (errno == ENOENT) {
      if (mkdir(berictl_dir, 0777) == -1)
        warn("can't create %s", berictl_dir);
      else if ((ccd = open(berictl_dir, O_RDONLY | O_DIRECTORY)) == -1)
        warn("can't open %s after creation", berictl_dir);
    } else
      warn("can't open %s", berictl_dir);
  }
  if (ccd != -1 && flock(ccd, LOCK_EX) == -1)
    warn("Failed to obtain lock on %s", berictl_dir);
  return (ccd);
}


x86 assembly (clang, -O0):
<open_berictl_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
cmpq   $0x0,0x2ed6(%rip)
jne    1258 <open_berictl_dir+0xe8>
lea    0xe96(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,0x2ebd(%rip)
cmpq   $0x0,0x2eb5(%rip)
jne    11da <open_berictl_dir+0x6a>
lea    0xe48(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,0x2e9c(%rip)
cmp    $0x0,%rax
je     11da <open_berictl_dir+0x6a>
lea    0xe38(%rip),%rdi
call   1060 <warnx@plt>
cmpq   $0x0,0x2e7e(%rip)
jne    122d <open_berictl_dir+0xbd>
lea    0xe4a(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     122d <open_berictl_dir+0xbd>
mov    -0x18(%rbp),%rdx
lea    0x2e53(%rip),%rdi
lea    0xe2a(%rip),%rsi
call   1070 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    122d <open_berictl_dir+0xbd>
movq   $0x0,0x2e33(%rip)
cmpq   $0x0,0x2e2b(%rip)
jne    1253 <open_berictl_dir+0xe3>
lea    0xe08(%rip),%rdi
call   1060 <warnx@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1374 <open_berictl_dir+0x204>
jmp    1258 <open_berictl_dir+0xe8>
mov    0x2e01(%rip),%rdi
mov    0x2d7a(%rip),%rax
mov    (%rax),%esi
mov    0x2d61(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    1337 <open_berictl_dir+0x1c7>
cmpl   $0x0,-0x8(%rbp)
jne    1337 <open_berictl_dir+0x1c7>
mov    0x2d35(%rip),%rax
mov    (%rax),%rax
mov    0x2d1b(%rip),%rcx
cmp    (%rcx),%rax
jne    131f <open_berictl_dir+0x1af>
mov    0x2db3(%rip),%rdi
mov    $0x1ff,%esi
call   1050 <mkdir@plt>
cmp    $0xffffffff,%eax
jne    12d8 <open_berictl_dir+0x168>
mov    0x2d99(%rip),%rsi
lea    0xdb2(%rip),%rdi
call   1040 <warn@plt>
jmp    131a <open_berictl_dir+0x1aa>
mov    0x2d81(%rip),%rdi
mov    0x2cfa(%rip),%rax
mov    (%rax),%esi
mov    0x2ce1(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    1315 <open_berictl_dir+0x1a5>
mov    0x2d57(%rip),%rsi
lea    0xd80(%rip),%rdi
call   1040 <warn@plt>
jmp    131a <open_berictl_dir+0x1aa>
jmp    1332 <open_berictl_dir+0x1c2>
mov    0x2d3a(%rip),%rsi
lea    0xd80(%rip),%rdi
call   1040 <warn@plt>
jmp    1337 <open_berictl_dir+0x1c7>
cmpl   $0xffffffff,-0xc(%rbp)
je     136e <open_berictl_dir+0x1fe>
mov    -0xc(%rbp),%edi
mov    0x2c9d(%rip),%rax
mov    (%rax),%esi
call   1090 <flock@plt>
cmp    $0xffffffff,%eax
jne    136e <open_berictl_dir+0x1fe>
mov    0x2cfe(%rip),%rsi
lea    0xd52(%rip),%rdi
call   1040 <warn@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_berictl_dir>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2ee0(%rip),%rdi
test   %rdi,%rdi
jne    11a4 <open_berictl_dir+0x34>
lea    0xea1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
mov    %rax,0x2ec5(%rip)
test   %rax,%rax
je     126d <open_berictl_dir+0xfd>
mov    0x2e35(%rip),%r14
mov    0x2e1e(%rip),%r15
mov    (%r15),%esi
or     (%r14),%esi
call   1080 <open@plt>
mov    %eax,%ebp
test   %ebx,%ebx
jne    122a <open_berictl_dir+0xba>
cmp    $0xffffffff,%ebp
jne    122a <open_berictl_dir+0xba>
mov    0x2df9(%rip),%rax
mov    (%rax),%rax
mov    0x2ddf(%rip),%rcx
mov    0x2e80(%rip),%rsi
cmp    (%rcx),%rax
jne    1264 <open_berictl_dir+0xf4>
mov    %rsi,%rdi
mov    $0x1ff,%esi
call   1050 <mkdir@plt>
mov    0x2e67(%rip),%rcx
cmp    $0xffffffff,%eax
je     12df <open_berictl_dir+0x16f>
mov    (%r15),%esi
or     (%r14),%esi
mov    %rcx,%rdi
call   1080 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1233 <open_berictl_dir+0xc3>
mov    0x2e42(%rip),%rsi
lea    0xe6b(%rip),%rdi
jmp    12e9 <open_berictl_dir+0x179>
cmp    $0xffffffff,%ebp
je     12ee <open_berictl_dir+0x17e>
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1090 <flock@plt>
cmp    $0xffffffff,%eax
jne    12f3 <open_berictl_dir+0x183>
mov    0x2e0d(%rip),%rsi
lea    0xe61(%rip),%rdi
call   1040 <warn@plt>
jmp    12f3 <open_berictl_dir+0x183>
lea    0xe42(%rip),%rdi
jmp    12e9 <open_berictl_dir+0x179>
lea    0xd8c(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,0x2de0(%rip)
test   %rax,%rax
je     12a1 <open_berictl_dir+0x131>
lea    0xd81(%rip),%rdi
call   1060 <warnx@plt>
mov    0x2dc8(%rip),%rdi
test   %rdi,%rdi
jne    11a4 <open_berictl_dir+0x34>
lea    0xd91(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     130b <open_berictl_dir+0x19b>
lea    0x2da7(%rip),%rdi
lea    0xd7e(%rip),%rsi
mov    %rax,%rdx
call   1070 <asprintf@plt>
cmp    $0xffffffff,%eax
je     1300 <open_berictl_dir+0x190>
mov    0x2d8c(%rip),%rdi
test   %rdi,%rdi
jne    11a4 <open_berictl_dir+0x34>
jmp    130b <open_berictl_dir+0x19b>
lea    0xd9a(%rip),%rdi
mov    %rcx,%rsi
call   1040 <warn@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
movq   $0x0,0x2d55(%rip)
lea    0xd38(%rip),%rdi
call   1060 <warnx@plt>
jmp    12ee <open_berictl_dir+0x17e>

LLVM IR (-O0):
@open_berictl_dir.berictl_dir = internal global i8* null
@.str = [12 x i8] c"BERICLT_DIR\00"
@.str.1 = [13 x i8] c"CHERICLT_DIR\00"
@.str.2 = [44 x i8] c"CHERICLT_DIR is deprecated, use BERICLT_DIR\00"
@.str.3 = [5 x i8] c"HOME\00"
@.str.4 = [12 x i8] c"%s/.berictl\00"
@.str.5 = [54 x i8] c"failed to find a berictl dir, set HOME or BERICLT_DIR\00"
@O_RDONLY = global i32 0
@O_DIRECTORY = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str.6 = [16 x i8] c"can't create %s\00"
@.str.7 = [29 x i8] c"can't open %s after creation\00"
@.str.8 = [14 x i8] c"can't open %s\00"
@LOCK_EX = global i32 0
@.str.9 = [28 x i8] c"Failed to obtain lock on %s\00"
define i32 @open_berictl_dir(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i32 %0, i32* %3
store i32 -1, i32* %4
%6 = load i8*, i8** @open_berictl_dir.berictl_dir
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %34
8: ; preds = %1
%9 = call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i8* %9, i8** @open_berictl_dir.berictl_dir
%10 = load i8*, i8** @open_berictl_dir.berictl_dir
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %17
12: ; preds = %8
%13 = call i8* @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i8* %13, i8** @open_berictl_dir.berictl_dir
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @warnx(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %12, %8
%18 = load i8*, i8** @open_berictl_dir.berictl_dir
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %28
20: ; preds = %17
%21 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i8* %21, i8** %5
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = load i8*, i8** %5
%25 = call i32 @asprintf(i8** @open_berictl_dir.berictl_dir, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %28
27: ; preds = %23
store i8* null, i8** @open_berictl_dir.berictl_dir
br label %28
28: ; preds = %27, %23, %20, %17
%29 = load i8*, i8** @open_berictl_dir.berictl_dir
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @warnx(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %2
br label %84
33: ; preds = %28
br label %34
34: ; preds = %33, %1
%35 = load i8*, i8** @open_berictl_dir.berictl_dir
%36 = load i32, i32* @O_RDONLY
%37 = load i32, i32* @O_DIRECTORY
%38 = or i32 %36, %37
%39 = call i32 @open(i8* %35, i32 %38)
store i32 %39, i32* %4
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %71
41: ; preds = %34
%42 = load i32, i32* %3
%43 = icmp ne i32 %42, 0
br i1 %43, label %71, label %44
44: ; preds = %41
%45 = load i64, i64* @errno
%46 = load i64, i64* @ENOENT
%47 = icmp eq i64 %45, %46
br i1 %47, label %48, label %67
48: ; preds = %44
%49 = load i8*, i8** @open_berictl_dir.berictl_dir
%50 = call i32 @mkdir(i8* %49, i32 511)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %55
52: ; preds = %48
%53 = load i8*, i8** @open_berictl_dir.berictl_dir
%54 = call i32 @warn(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i8* %53)
br label %66
55: ; preds = %48
%56 = load i8*, i8** @open_berictl_dir.berictl_dir
%57 = load i32, i32* @O_RDONLY
%58 = load i32, i32* @O_DIRECTORY
%59 = or i32 %57, %58
%60 = call i32 @open(i8* %56, i32 %59)
store i32 %60, i32* %4
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %65
62: ; preds = %55
%63 = load i8*, i8** @open_berictl_dir.berictl_dir
%64 = call i32 @warn(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i8* %63)
br label %65
65: ; preds = %62, %55
br label %66
66: ; preds = %65, %52
br label %70
67: ; preds = %44
%68 = load i8*, i8** @open_berictl_dir.berictl_dir
%69 = call i32 @warn(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0), i8* %68)
br label %70
70: ; preds = %67, %66
br label %71
71: ; preds = %70, %41, %34
%72 = load i32, i32* %4
%73 = icmp ne i32 %72, -1
br i1 %73, label %74, label %82
74: ; preds = %71
%75 = load i32, i32* %4
%76 = load i32, i32* @LOCK_EX
%77 = call i32 @flock(i32 %75, i32 %76)
%78 = icmp eq i32 %77, -1
br i1 %78, label %79, label %82
79: ; preds = %74
%80 = load i8*, i8** @open_berictl_dir.berictl_dir
%81 = call i32 @warn(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0), i8* %80)
br label %82
82: ; preds = %79, %74, %71
%83 = load i32, i32* %4
store i32 %83, i32* %2
br label %84
84: ; preds = %82, %31
%85 = load i32, i32* %2
ret i32 %85
}

LLVM IR (-O2):
@open_berictl_dir.berictl_dir = internal global i8* null
@.str = [12 x i8] c"BERICLT_DIR\00"
@.str.1 = [13 x i8] c"CHERICLT_DIR\00"
@.str.2 = [44 x i8] c"CHERICLT_DIR is deprecated, use BERICLT_DIR\00"
@.str.3 = [5 x i8] c"HOME\00"
@.str.4 = [12 x i8] c"%s/.berictl\00"
@.str.5 = [54 x i8] c"failed to find a berictl dir, set HOME or BERICLT_DIR\00"
@O_RDONLY = global i32 0
@O_DIRECTORY = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str.6 = [16 x i8] c"can't create %s\00"
@.str.7 = [29 x i8] c"can't open %s after creation\00"
@.str.8 = [14 x i8] c"can't open %s\00"
@LOCK_EX = global i32 0
@.str.9 = [28 x i8] c"Failed to obtain lock on %s\00"
define i32 @open_berictl_dir(i32 %0) {
%2 = load i8*, i8** @open_berictl_dir.berictl_dir
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %26
4: ; preds = %1
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i8* %5, i8** @open_berictl_dir.berictl_dir
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %26
7: ; preds = %4
%8 = tail call i8* @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i8* %8, i8** @open_berictl_dir.berictl_dir
%9 = icmp eq i8* %8, null
br i1 %9, label %14, label %10
10: ; preds = %7
%11 = tail call i32 @warnx(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
%12 = load i8*, i8** @open_berictl_dir.berictl_dir
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %26
14: ; preds = %7, %10
%15 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%16 = icmp eq i8* %15, null
br i1 %16, label %24, label %17
17: ; preds = %14
%18 = tail call i32 @asprintf(i8** @open_berictl_dir.berictl_dir, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %15)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %17
store i8* null, i8** @open_berictl_dir.berictl_dir
br label %24
21: ; preds = %17
%22 = load i8*, i8** @open_berictl_dir.berictl_dir
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %26
24: ; preds = %14, %20, %21
%25 = tail call i32 @warnx(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.5, i64 0, i64 0))
br label %67
26: ; preds = %4, %10, %21, %1
%27 = phi i8* [ %5, %4 ], [ %12, %10 ], [ %22, %21 ], [ %2, %1 ]
%28 = load i32, i32* @O_RDONLY
%29 = load i32, i32* @O_DIRECTORY
%30 = or i32 %29, %28
%31 = tail call i32 @open(i8* %27, i32 %30)
%32 = icmp ne i32 %31, -1
%33 = icmp ne i32 %0, 0
%34 = or i1 %33, %32
br i1 %34, label %57, label %35
35: ; preds = %26
%36 = load i64, i64* @errno
%37 = load i64, i64* @ENOENT
%38 = icmp eq i64 %36, %37
%39 = load i8*, i8** @open_berictl_dir.berictl_dir
br i1 %38, label %40, label %55
40: ; preds = %35
%41 = tail call i32 @mkdir(i8* %39, i32 511)
%42 = icmp eq i32 %41, -1
%43 = load i8*, i8** @open_berictl_dir.berictl_dir
br i1 %42, label %44, label %46
44: ; preds = %40
%45 = tail call i32 @warn(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i8* %43)
br label %67
46: ; preds = %40
%47 = load i32, i32* @O_RDONLY
%48 = load i32, i32* @O_DIRECTORY
%49 = or i32 %48, %47
%50 = tail call i32 @open(i8* %43, i32 %49)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %59
52: ; preds = %46
%53 = load i8*, i8** @open_berictl_dir.berictl_dir
%54 = tail call i32 @warn(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i8* %53)
br label %67
55: ; preds = %35
%56 = tail call i32 @warn(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0), i8* %39)
br label %67
57: ; preds = %26
%58 = icmp eq i32 %31, -1
br i1 %58, label %67, label %59
59: ; preds = %46, %57
%60 = phi i32 [ %31, %57 ], [ %50, %46 ]
%61 = load i32, i32* @LOCK_EX
%62 = tail call i32 @flock(i32 %60, i32 %61)
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %67
64: ; preds = %59
%65 = load i8*, i8** @open_berictl_dir.berictl_dir
%66 = tail call i32 @warn(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0), i8* %65)
br label %67
67: ; preds = %55, %52, %44, %57, %59, %64, %24
%68 = phi i32 [ -1, %24 ], [ %60, %64 ], [ %60, %59 ], [ -1, %57 ], [ -1, %44 ], [ -1, %52 ], [ -1, %55 ]
ret i32 %68
}