#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 */
char *add_path_parts(char *, char *);
char *getenv(char *);
scalar_t__ is_directory_readwritable(char *);
int /*<<< orphan*/ mkdir(char *, int);
int /*<<< orphan*/ perror(char *);
char *get_index_directory() {
  static char *indexdir = NULL;
  if (indexdir != NULL) {
    return indexdir;
  }
  if (is_directory_readwritable("/4gram/")) {
    return (indexdir = "/4gram");
  }
  char *home_cache_dir = add_path_parts(getenv("HOME"), ".cache");
  char *home_4gram_dir = add_path_parts(home_cache_dir, "4gram");
  mkdir(home_cache_dir, 0700);
  mkdir(home_4gram_dir, 0777);
  if (is_directory_readwritable(home_4gram_dir)) {
    return (indexdir = home_4gram_dir);
  }
  perror("Could not find readwritable directory to cache 4grams\n");
  return (NULL);
}


x86 assembly (clang, -O0):
<get_index_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
cmpq   $0x0,0x2ef0(%rip)
je     1176 <get_index_directory+0x26>
mov    0x2ee3(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1234 <get_index_directory+0xe4>
lea    0xe83(%rip),%rdi
call   1050 <is_directory_readwritable@plt>
cmp    $0x0,%rax
je     11aa <get_index_directory+0x5a>
lea    0xe75(%rip),%rax
mov    %rax,0x2eb6(%rip)
lea    0xe67(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1234 <get_index_directory+0xe4>
lea    0xe5e(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
lea    0xe54(%rip),%rsi
call   1070 <add_path_parts@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe35(%rip),%rsi
call   1070 <add_path_parts@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1c0,%esi
call   1040 <mkdir@plt>
mov    -0x18(%rbp),%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
mov    -0x18(%rbp),%rdi
call   1050 <is_directory_readwritable@plt>
cmp    $0x0,%rax
je     1220 <get_index_directory+0xd0>
mov    -0x18(%rbp),%rax
mov    %rax,0x2e39(%rip)
mov    %rax,-0x8(%rbp)
jmp    1234 <get_index_directory+0xe4>
lea    0xdf4(%rip),%rdi
call   1060 <perror@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_index_directory>:
push   %r14
push   %rbx
push   %rax
mov    0x2ef5(%rip),%rbx
test   %rbx,%rbx
jne    11d8 <get_index_directory+0x88>
lea    0xe99(%rip),%rdi
call   1050 <is_directory_readwritable@plt>
test   %rax,%rax
je     117a <get_index_directory+0x2a>
lea    0xe90(%rip),%rbx
jmp    11d1 <get_index_directory+0x81>
lea    0xe8e(%rip),%rdi
call   1030 <getenv@plt>
lea    0xe87(%rip),%rsi
mov    %rax,%rdi
call   1070 <add_path_parts@plt>
mov    %rax,%r14
lea    0xe6a(%rip),%rsi
mov    %rax,%rdi
call   1070 <add_path_parts@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x1c0,%esi
call   1040 <mkdir@plt>
mov    %rbx,%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
mov    %rbx,%rdi
call   1050 <is_directory_readwritable@plt>
test   %rax,%rax
je     11e3 <get_index_directory+0x93>
mov    %rbx,0x2e78(%rip)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe31(%rip),%rdi
call   1060 <perror@plt>
xor    %ebx,%ebx
jmp    11d8 <get_index_directory+0x88>

LLVM IR (-O0):
@get_index_directory.indexdir = internal global i8* null
@.str = [8 x i8] c"/4gram/\00"
@.str.1 = [7 x i8] c"/4gram\00"
@.str.2 = [5 x i8] c"HOME\00"
@.str.3 = [7 x i8] c".cache\00"
@.str.4 = [6 x i8] c"4gram\00"
@.str.5 = [55 x i8] c"Could not find readwritable directory to cache 4grams\0A\00"
define i8* @get_index_directory() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8*
%4 = load i8*, i8** @get_index_directory.indexdir
%5 = icmp ne i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = load i8*, i8** @get_index_directory.indexdir
store i8* %7, i8** %1
br label %28
8: ; preds = %0
%9 = call i64 @is_directory_readwritable(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %8
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8** @get_index_directory.indexdir
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8** %1
br label %28
12: ; preds = %8
%13 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%14 = call i8* @add_path_parts(i8* %13, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
store i8* %14, i8** %2
%15 = load i8*, i8** %2
%16 = call i8* @add_path_parts(i8* %15, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
store i8* %16, i8** %3
%17 = load i8*, i8** %2
%18 = call i32 @mkdir(i8* %17, i32 448)
%19 = load i8*, i8** %3
%20 = call i32 @mkdir(i8* %19, i32 511)
%21 = load i8*, i8** %3
%22 = call i64 @is_directory_readwritable(i8* %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %12
%25 = load i8*, i8** %3
store i8* %25, i8** @get_index_directory.indexdir
store i8* %25, i8** %1
br label %28
26: ; preds = %12
%27 = call i32 @perror(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.5, i64 0, i64 0))
store i8* null, i8** %1
br label %28
28: ; preds = %26, %24, %11, %6
%29 = load i8*, i8** %1
ret i8* %29
}

LLVM IR (-O2):
@get_index_directory.indexdir = internal unnamed_addr global i8* null
@.str = [8 x i8] c"/4gram/\00"
@.str.1 = [7 x i8] c"/4gram\00"
@.str.2 = [5 x i8] c"HOME\00"
@.str.3 = [7 x i8] c".cache\00"
@.str.4 = [6 x i8] c"4gram\00"
@.str.5 = [55 x i8] c"Could not find readwritable directory to cache 4grams\0A\00"
define i8* @get_index_directory() {
%1 = load i8*, i8** @get_index_directory.indexdir
%2 = icmp eq i8* %1, null
br i1 %2, label %3, label %18
3: ; preds = %0
%4 = tail call i64 @is_directory_readwritable(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i64 %4, 0
br i1 %5, label %7, label %6
6: ; preds = %3
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8** @get_index_directory.indexdir
br label %18
7: ; preds = %3
%8 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%9 = tail call i8* @add_path_parts(i8* %8, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%10 = tail call i8* @add_path_parts(i8* %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%11 = tail call i32 @mkdir(i8* %9, i32 448)
%12 = tail call i32 @mkdir(i8* %10, i32 511)
%13 = tail call i64 @is_directory_readwritable(i8* %10)
%14 = icmp eq i64 %13, 0
br i1 %14, label %16, label %15
15: ; preds = %7
store i8* %10, i8** @get_index_directory.indexdir
br label %18
16: ; preds = %7
%17 = tail call i32 @perror(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.5, i64 0, i64 0))
br label %18
18: ; preds = %15, %16, %0, %6
%19 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %6 ], [ %1, %0 ], [ %10, %15 ], [ null, %16 ]
ret i8* %19
}