#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;
};

/* Variables and functions */
int asprintf(char **, char *, char const *);
int /*<<< orphan*/ free(char *);
char *get_string_option(char *);
char *getenv(char *);
scalar_t__ stat(char *, struct stat *);
char *strdup(char const *);
char *config_file_name(void) {
  const char *alt_config_file = get_string_option("alt_config_file");
  if (*alt_config_file)
    return strdup(alt_config_file);
  const char *home = getenv("HOME");
  if (NULL == home)
    return NULL;
  char *fname = NULL;
  const char *xdg_config_home = getenv("XDG_CONFIG_HOME");
  if (xdg_config_home == NULL || *xdg_config_home == '\0') {
    if (asprintf(&fname, "%s/.config/xcowsayrc", home) == -1)
      return NULL;
  } else if (asprintf(&fname, "%s/xcowsayrc", xdg_config_home) == -1)
    return NULL;
  struct stat dummy;
  if (stat(fname, &dummy) == 0)
    return fname;
  free(fname);
  if (asprintf(&fname, "%s/.xcowsayrc", home) == -1)
    return NULL;
  if (stat(fname, &dummy) == 0)
    return fname;
  free(fname);
  return NULL;
}


x86 assembly (clang, -O0):
<config_file_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe91(%rip),%rdi
call   1070 <get_string_option@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1197 <config_file_name+0x37>
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    12dd <config_file_name+0x17d>
lea    0xe7d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    11c0 <config_file_name+0x60>
movq   $0x0,-0x8(%rbp)
jmp    12dd <config_file_name+0x17d>
movq   $0x0,-0x20(%rbp)
lea    0xe41(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11f3 <config_file_name+0x93>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1222 <config_file_name+0xc2>
mov    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    0xe1e(%rip),%rsi
call   1060 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    121d <config_file_name+0xbd>
movq   $0x0,-0x8(%rbp)
jmp    12dd <config_file_name+0x17d>
jmp    1251 <config_file_name+0xf1>
mov    -0x28(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    0xe04(%rip),%rsi
call   1060 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    124c <config_file_name+0xec>
movq   $0x0,-0x8(%rbp)
jmp    12dd <config_file_name+0x17d>
jmp    1251 <config_file_name+0xf1>
mov    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jne    1275 <config_file_name+0x115>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12dd <config_file_name+0x17d>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    0xdb5(%rip),%rsi
call   1060 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    12a8 <config_file_name+0x148>
movq   $0x0,-0x8(%rbp)
jmp    12dd <config_file_name+0x17d>
mov    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jne    12cc <config_file_name+0x16c>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12dd <config_file_name+0x17d>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<config_file_name>:
push   %rbx
sub    $0x10,%rsp
lea    0xe94(%rip),%rdi
call   1070 <get_string_option@plt>
cmpb   $0x0,(%rax)
je     1183 <config_file_name+0x23>
mov    %rax,%rdi
add    $0x10,%rsp
pop    %rbx
jmp    1080 <strdup@plt>
lea    0xe91(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     123a <config_file_name+0xda>
mov    %rax,%rbx
movq   $0x0,(%rsp)
lea    0xe66(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11c8 <config_file_name+0x68>
cmpb   $0x0,(%rax)
je     11c8 <config_file_name+0x68>
lea    0xe75(%rip),%rsi
mov    %rsp,%rdi
mov    %rax,%rdx
jmp    11d5 <config_file_name+0x75>
lea    0xe51(%rip),%rsi
mov    %rsp,%rdi
mov    %rbx,%rdx
call   1060 <asprintf@plt>
cmp    $0xffffffff,%eax
je     123a <config_file_name+0xda>
mov    (%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
mov    %rax,%rcx
mov    (%rsp),%rax
test   %rcx,%rcx
je     123c <config_file_name+0xdc>
mov    %rax,%rdi
call   1040 <free@plt>
lea    0xe3a(%rip),%rsi
mov    %rsp,%rdi
mov    %rbx,%rdx
call   1060 <asprintf@plt>
cmp    $0xffffffff,%eax
je     123a <config_file_name+0xda>
mov    (%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
mov    %rax,%rcx
mov    (%rsp),%rax
test   %rcx,%rcx
je     123c <config_file_name+0xdc>
mov    %rax,%rdi
call   1040 <free@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [16 x i8] c"alt_config_file\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [16 x i8] c"XDG_CONFIG_HOME\00"
@.str.3 = [21 x i8] c"%s/.config/xcowsayrc\00"
@.str.4 = [13 x i8] c"%s/xcowsayrc\00"
@.str.5 = [14 x i8] c"%s/.xcowsayrc\00"
define i8* @config_file_name() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = call i8* @get_string_option(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i8* %7, i8** %2
%8 = load i8*, i8** %2
%9 = load i8, i8* %8
%10 = icmp ne i8 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = load i8*, i8** %2
%13 = call i8* @strdup(i8* %12)
store i8* %13, i8** %1
br label %62
14: ; preds = %0
%15 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %15, i8** %3
%16 = load i8*, i8** %3
%17 = icmp eq i8* null, %16
br i1 %17, label %18, label %19
18: ; preds = %14
store i8* null, i8** %1
br label %62
19: ; preds = %14
store i8* null, i8** %4
%20 = call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i8* %20, i8** %5
%21 = load i8*, i8** %5
%22 = icmp eq i8* %21, null
br i1 %22, label %28, label %23
23: ; preds = %19
%24 = load i8*, i8** %5
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %23, %19
%29 = load i8*, i8** %3
%30 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %33
32: ; preds = %28
store i8* null, i8** %1
br label %62
33: ; preds = %28
br label %40
34: ; preds = %23
%35 = load i8*, i8** %5
%36 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %39
38: ; preds = %34
store i8* null, i8** %1
br label %62
39: ; preds = %34
br label %40
40: ; preds = %39, %33
%41 = load i8*, i8** %4
%42 = call i64 @stat(i8* %41, %struct.stat* %6)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = load i8*, i8** %4
store i8* %45, i8** %1
br label %62
46: ; preds = %40
%47 = load i8*, i8** %4
%48 = call i32 @free(i8* %47)
%49 = load i8*, i8** %3
%50 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* %49)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %53
52: ; preds = %46
store i8* null, i8** %1
br label %62
53: ; preds = %46
%54 = load i8*, i8** %4
%55 = call i64 @stat(i8* %54, %struct.stat* %6)
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %53
%58 = load i8*, i8** %4
store i8* %58, i8** %1
br label %62
59: ; preds = %53
%60 = load i8*, i8** %4
%61 = call i32 @free(i8* %60)
store i8* null, i8** %1
br label %62
62: ; preds = %59, %57, %52, %44, %38, %32, %18, %11
%63 = load i8*, i8** %1
ret i8* %63
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [16 x i8] c"alt_config_file\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [16 x i8] c"XDG_CONFIG_HOME\00"
@.str.3 = [21 x i8] c"%s/.config/xcowsayrc\00"
@.str.4 = [13 x i8] c"%s/xcowsayrc\00"
@.str.5 = [14 x i8] c"%s/.xcowsayrc\00"
define i8* @config_file_name() {
%1 = alloca i8*
%2 = alloca %struct.stat
%3 = tail call i8* @get_string_option(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%4 = load i8, i8* %3
%5 = icmp eq i8 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %0
%7 = tail call i8* @strdup(i8* %3)
br label %45
8: ; preds = %0
%9 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i8* %9, null
br i1 %10, label %45, label %11
11: ; preds = %8
%12 = bitcast i8** %1 to i8*
store i8* null, i8** %1
%13 = tail call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i8* %13, null
br i1 %14, label %18, label %15
15: ; preds = %11
%16 = load i8, i8* %13
%17 = icmp eq i8 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15, %11
%19 = call i32 @asprintf(i8** %1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %9)
%20 = icmp eq i32 %19, -1
br i1 %20, label %43, label %24
21: ; preds = %15
%22 = call i32 @asprintf(i8** %1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %13)
%23 = icmp eq i32 %22, -1
br i1 %23, label %43, label %24
24: ; preds = %21, %18
%25 = bitcast %struct.stat* %2 to i8*
%26 = load i8*, i8** %1
%27 = call i64 @stat(i8* %26, %struct.stat* %2)
%28 = icmp eq i64 %27, 0
%29 = load i8*, i8** %1
br i1 %28, label %41, label %30
30: ; preds = %24
%31 = call i32 @free(i8* %29)
%32 = call i32 @asprintf(i8** %1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* %9)
%33 = icmp eq i32 %32, -1
br i1 %33, label %41, label %34
34: ; preds = %30
%35 = load i8*, i8** %1
%36 = call i64 @stat(i8* %35, %struct.stat* %2)
%37 = icmp eq i64 %36, 0
%38 = load i8*, i8** %1
br i1 %37, label %41, label %39
39: ; preds = %34
%40 = call i32 @free(i8* %38)
br label %41
41: ; preds = %34, %24, %30, %39
%42 = phi i8* [ null, %39 ], [ null, %30 ], [ %29, %24 ], [ %38, %34 ]
br label %43
43: ; preds = %21, %18, %41
%44 = phi i8* [ %42, %41 ], [ null, %18 ], [ null, %21 ]
br label %45
45: ; preds = %43, %8, %6
%46 = phi i8* [ %7, %6 ], [ %44, %43 ], [ null, %8 ]
ret i8* %46
}