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

/* Variables and functions */
char *getenv(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, char *, char const *);
char *strdup(char const *);
scalar_t__ strlen(char *);
char *get_cmus_home_dir(void) {
  const char cmusdir[] = "/cmus";
  char *fullpath;
  char *tmp;
  if ((tmp = getenv("XDG_CONFIG_HOME")) != NULL) {
    fullpath = malloc(strlen(tmp) + sizeof(cmusdir));
    sprintf(fullpath, "%s%s", tmp, cmusdir);
    return fullpath;
  }
  if ((tmp = getenv("HOME")) != NULL) {
    fullpath = malloc(strlen(tmp) + 8 + sizeof(cmusdir));
    sprintf(fullpath, "%s/.config%s", tmp, cmusdir);
    return fullpath;
  }
  return strdup(cmusdir);
}


x86 assembly (clang, -O0):
<get_cmus_home_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0xea2(%rip),%eax
mov    %eax,-0xe(%rbp)
mov    0xe9c(%rip),%ax
mov    %ax,-0xa(%rbp)
lea    0xe93(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11c4 <get_cmus_home_dir+0x74>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x6,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    -0xe(%rbp),%rcx
lea    0xe64(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    122d <get_cmus_home_dir+0xdd>
lea    0xe46(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1220 <get_cmus_home_dir+0xd0>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x8,%rdi
add    $0x6,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    -0xe(%rbp),%rcx
lea    0xe0d(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    122d <get_cmus_home_dir+0xdd>
lea    -0xe(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_cmus_home_dir>:
push   %r14
push   %rbx
push   %rax
movw   $0x73,0x4(%rsp)
movl   $0x756d632f,(%rsp)
lea    0xe9d(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1193 <get_cmus_home_dir+0x43>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <strlen@plt>
lea    0x6(%rax),%rdi
call   1050 <malloc@plt>
mov    %rax,%r14
lea    0xe85(%rip),%rsi
jmp    11c2 <get_cmus_home_dir+0x72>
lea    0xe77(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11db <get_cmus_home_dir+0x8b>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <strlen@plt>
lea    0xe(%rax),%rdi
call   1050 <malloc@plt>
mov    %rax,%r14
lea    0xe59(%rip),%rsi
mov    %rsp,%rcx
mov    %rax,%rdi
mov    %rbx,%rdx
call   1060 <sprintf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %rsp,%rdi
call   1070 <strdup@plt>
mov    %rax,%r14
jmp    11d0 <get_cmus_home_dir+0x80>

LLVM IR (-O0):
@__const.get_cmus_home_dir.cmusdir = [6 x i8] c"/cmus\00"
@.str = [16 x i8] c"XDG_CONFIG_HOME\00"
@.str.1 = [5 x i8] c"%s%s\00"
@.str.2 = [5 x i8] c"HOME\00"
@.str.3 = [13 x i8] c"%s/.config%s\00"
define i8* @get_cmus_home_dir() {
%1 = alloca i8*
%2 = alloca [6 x i8]
%3 = alloca i8*
%4 = alloca i8*
%5 = bitcast [6 x i8]* %2 to i8*
%6 = call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i8* %6, i8** %4
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %18
8: ; preds = %0
%9 = load i8*, i8** %4
%10 = call i64 @strlen(i8* %9)
%11 = add i64 %10, 6
%12 = call i8* @malloc(i64 %11)
store i8* %12, i8** %3
%13 = load i8*, i8** %3
%14 = load i8*, i8** %4
%15 = getelementptr inbounds [6 x i8], [6 x i8]* %2, i64 0, i64 0
%16 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %14, i8* %15)
%17 = load i8*, i8** %3
store i8* %17, i8** %1
br label %35
18: ; preds = %0
%19 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i8* %19, i8** %4
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %32
21: ; preds = %18
%22 = load i8*, i8** %4
%23 = call i64 @strlen(i8* %22)
%24 = add nsw i64 %23, 8
%25 = add i64 %24, 6
%26 = call i8* @malloc(i64 %25)
store i8* %26, i8** %3
%27 = load i8*, i8** %3
%28 = load i8*, i8** %4
%29 = getelementptr inbounds [6 x i8], [6 x i8]* %2, i64 0, i64 0
%30 = call i32 @sprintf(i8* %27, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %28, i8* %29)
%31 = load i8*, i8** %3
store i8* %31, i8** %1
br label %35
32: ; preds = %18
%33 = getelementptr inbounds [6 x i8], [6 x i8]* %2, i64 0, i64 0
%34 = call i8* @strdup(i8* %33)
store i8* %34, i8** %1
br label %35
35: ; preds = %32, %21, %8
%36 = load i8*, i8** %1
ret i8* %36
}

LLVM IR (-O2):
@__const.get_cmus_home_dir.cmusdir = [6 x i8] c"/cmus\00"
@.str = [16 x i8] c"XDG_CONFIG_HOME\00"
@.str.1 = [5 x i8] c"%s%s\00"
@.str.2 = [5 x i8] c"HOME\00"
@.str.3 = [13 x i8] c"%s/.config%s\00"
define noalias i8* @get_cmus_home_dir() {
%1 = alloca [6 x i8]
%2 = getelementptr inbounds [6 x i8], [6 x i8]* %1, i64 0, i64 0
%3 = tail call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %10, label %5
5: ; preds = %0
%6 = tail call i64 @strlen(i8* %3)
%7 = add i64 %6, 6
%8 = tail call i8* @malloc(i64 %7)
%9 = call i32 @sprintf(i8* dereferenceable(1) %8, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %3, i8* %2)
br label %20
10: ; preds = %0
%11 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp eq i8* %11, null
br i1 %12, label %18, label %13
13: ; preds = %10
%14 = tail call i64 @strlen(i8* %11)
%15 = add i64 %14, 14
%16 = tail call i8* @malloc(i64 %15)
%17 = call i32 @sprintf(i8* dereferenceable(1) %16, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %11, i8* %2)
br label %20
18: ; preds = %10
%19 = call i8* @strdup(i8* %2)
br label %20
20: ; preds = %18, %13, %5
%21 = phi i8* [ %8, %5 ], [ %16, %13 ], [ %19, %18 ]
ret i8* %21
}