#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 *append(char *, char *);
int /*<<< orphan*/ err(int, char *, char *);
int /*<<< orphan*/ errx(int, char *);
char *getenv(char *);
scalar_t__ mkdir_recursive(char *);
char *strdup(char *);
char *plandir(void) {
  char *dirpath;
  if ((dirpath = getenv("READING_PLAN_DIR")))
    dirpath = strdup(dirpath);
  else if ((dirpath = getenv("XDG_DATA_HOME")))
    dirpath = append(strdup(dirpath), "/reading");
  else if ((dirpath = getenv("HOME")))
    dirpath = append(strdup(dirpath), "/.local/share/reading");
  else
    errx(1, "could not find plan directory");
  if (mkdir_recursive(dirpath))
    err(1, "could not create plan directory '%s'", dirpath);
  return dirpath;
}


x86 assembly (clang, -O0):
<plandir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xe91(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
je     1194 <plandir+0x34>
mov    -0x8(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1225 <plandir+0xc5>
lea    0xe76(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
je     11cf <plandir+0x6f>
mov    -0x8(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,%rdi
lea    0xe6b(%rip),%rsi
call   1040 <append@plt>
mov    %rax,-0x8(%rbp)
jmp    1220 <plandir+0xc0>
lea    0xe44(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
je     120a <plandir+0xaa>
mov    -0x8(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,%rdi
lea    0xe23(%rip),%rsi
call   1040 <append@plt>
mov    %rax,-0x8(%rbp)
jmp    121b <plandir+0xbb>
mov    $0x1,%edi
lea    0xe1f(%rip),%rsi
call   1050 <errx@plt>
jmp    1220 <plandir+0xc0>
jmp    1225 <plandir+0xc5>
mov    -0x8(%rbp),%rdi
call   1080 <mkdir_recursive@plt>
cmp    $0x0,%rax
je     124d <plandir+0xed>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xe0b(%rip),%rsi
call   1060 <err@plt>
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<plandir>:
push   %rbx
lea    0xe98(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     117c <plandir+0x1c>
mov    %rax,%rdi
call   1070 <strdup@plt>
jmp    11c6 <plandir+0x66>
lea    0xe8e(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     119e <plandir+0x3e>
mov    %rax,%rdi
call   1070 <strdup@plt>
lea    0xe90(%rip),%rsi
jmp    11be <plandir+0x5e>
lea    0xe75(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11ef <plandir+0x8f>
mov    %rax,%rdi
call   1070 <strdup@plt>
lea    0xe61(%rip),%rsi
mov    %rax,%rdi
call   1040 <append@plt>
mov    %rax,%rbx
mov    %rbx,%rdi
call   1080 <mkdir_recursive@plt>
test   %rax,%rax
je     11ea <plandir+0x8a>
lea    0xe76(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
call   1060 <err@plt>
mov    %rbx,%rax
pop    %rbx
ret
lea    0xe3f(%rip),%rsi
mov    $0x1,%edi
call   1050 <errx@plt>
xor    %ebx,%ebx
jmp    11c9 <plandir+0x69>

LLVM IR (-O0):
@.str = [17 x i8] c"READING_PLAN_DIR\00"
@.str.1 = [14 x i8] c"XDG_DATA_HOME\00"
@.str.2 = [9 x i8] c"/reading\00"
@.str.3 = [5 x i8] c"HOME\00"
@.str.4 = [22 x i8] c"/.local/share/reading\00"
@.str.5 = [30 x i8] c"could not find plan directory\00"
@.str.6 = [37 x i8] c"could not create plan directory '%s'\00"
define i8* @plandir() {
%1 = alloca i8*
%2 = call i8* @getenv(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i8* %2, i8** %1
%3 = icmp ne i8* %2, null
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = load i8*, i8** %1
%6 = call i8* @strdup(i8* %5)
store i8* %6, i8** %1
br label %25
7: ; preds = %0
%8 = call i8* @getenv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i8* %8, i8** %1
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %14
10: ; preds = %7
%11 = load i8*, i8** %1
%12 = call i8* @strdup(i8* %11)
%13 = call i8* @append(i8* %12, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
store i8* %13, i8** %1
br label %24
14: ; preds = %7
%15 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i8* %15, i8** %1
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i8*, i8** %1
%19 = call i8* @strdup(i8* %18)
%20 = call i8* @append(i8* %19, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
store i8* %20, i8** %1
br label %23
21: ; preds = %14
%22 = call i32 @errx(i32 1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0))
br label %23
23: ; preds = %21, %17
br label %24
24: ; preds = %23, %10
br label %25
25: ; preds = %24, %4
%26 = load i8*, i8** %1
%27 = call i64 @mkdir_recursive(i8* %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i8*, i8** %1
%31 = call i32 @err(i32 1, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i8* %30)
br label %32
32: ; preds = %29, %25
%33 = load i8*, i8** %1
ret i8* %33
}

LLVM IR (-O2):
@.str = [17 x i8] c"READING_PLAN_DIR\00"
@.str.1 = [14 x i8] c"XDG_DATA_HOME\00"
@.str.2 = [9 x i8] c"/reading\00"
@.str.3 = [5 x i8] c"HOME\00"
@.str.4 = [22 x i8] c"/.local/share/reading\00"
@.str.5 = [30 x i8] c"could not find plan directory\00"
@.str.6 = [37 x i8] c"could not create plan directory '%s'\00"
define i8* @plandir() {
%1 = tail call i8* @getenv(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i8* %1, null
br i1 %2, label %5, label %3
3: ; preds = %0
%4 = tail call i8* @strdup(i8* %1)
br label %19
5: ; preds = %0
%6 = tail call i8* @getenv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %11, label %8
8: ; preds = %5
%9 = tail call i8* @strdup(i8* %6)
%10 = tail call i8* @append(i8* %9, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %19
11: ; preds = %5
%12 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%13 = icmp eq i8* %12, null
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = tail call i8* @strdup(i8* %12)
%16 = tail call i8* @append(i8* %15, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %19
17: ; preds = %11
%18 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0))
br label %19
19: ; preds = %8, %17, %14, %3
%20 = phi i8* [ %4, %3 ], [ %10, %8 ], [ %16, %14 ], [ null, %17 ]
%21 = tail call i64 @mkdir_recursive(i8* %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %19
%24 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i8* %20)
br label %25
25: ; preds = %23, %19
ret i8* %20
}