#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 passwd {
  char *pw_dir;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
char *getenv(char *);
int getpwuid_r(int /*<<< orphan*/, struct passwd *, char *, int,
               struct passwd **);
int /*<<< orphan*/ getuid();
char *malloc(size_t);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char const *);
char *strdup(char const *);
int strlen(char const *);
char *get_user_config_dir(void) {
  char *user_config_dir;
  struct passwd pwd;
  struct passwd *pwdbuf;
  const char *home;
  char buf[4096];
  size_t len;
  int rc;
  rc = getpwuid_r(getuid(), &pwd, buf, 4096, &pwdbuf);
  if (rc == 0) {
    home = pwd.pw_dir;
  } else {
    home = getenv("HOME");
    if (home == NULL) {
      return NULL;
    }
    snprintf(buf, sizeof(buf), "%s", home);
    home = buf;
  }
  const char *tmp;
  if (!(tmp = getenv("XDG_CONFIG_HOME"))) {
    len = strlen(home) + strlen("/.config") + 1;
    user_config_dir = malloc(len);
    if (user_config_dir == NULL) {
      return NULL;
    }
    snprintf(user_config_dir, len, "%s/.config", home);
  } else {
    user_config_dir = strdup(tmp);
  }
  return user_config_dir;
}


x86 assembly (clang, -O0):
<get_user_config_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    $0x0,%al
call   1060 <getuid@plt>
mov    %eax,%edi
lea    -0x1030(%rbp),%rdx
lea    -0x18(%rbp),%rsi
mov    $0x1000,%ecx
lea    -0x20(%rbp),%r8
call   1050 <getpwuid_r@plt>
mov    %eax,-0x103c(%rbp)
cmpl   $0x0,-0x103c(%rbp)
jne    11bd <get_user_config_dir+0x4d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    120c <get_user_config_dir+0x9c>
lea    0xe4a(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e5 <get_user_config_dir+0x75>
movq   $0x0,-0x8(%rbp)
jmp    12ba <get_user_config_dir+0x14a>
lea    -0x1030(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1000,%esi
lea    0xe04(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x1030(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    0xdf0(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x1048(%rbp)
cmp    $0x0,%rax
jne    12a2 <get_user_config_dir+0x132>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x104c(%rbp)
lea    0xdd6(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x104c(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cltq
mov    %rax,-0x1038(%rbp)
mov    -0x1038(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1282 <get_user_config_dir+0x112>
movq   $0x0,-0x8(%rbp)
jmp    12ba <get_user_config_dir+0x14a>
mov    -0x10(%rbp),%rdi
mov    -0x1038(%rbp),%rsi
mov    -0x28(%rbp),%rcx
lea    0xd7b(%rip),%rdx
call   1070 <snprintf@plt>
jmp    12b2 <get_user_config_dir+0x142>
mov    -0x1048(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_user_config_dir>:
push   %r15
push   %r14
push   %rbx
sub    $0x1010,%rsp
xor    %eax,%eax
call   1060 <getuid@plt>
mov    %rsp,%rsi
lea    0x10(%rsp),%r14
lea    0x8(%rsp),%r8
mov    %eax,%edi
mov    %r14,%rdx
mov    $0x1000,%ecx
call   1050 <getpwuid_r@plt>
test   %eax,%eax
je     11d1 <get_user_config_dir+0x61>
lea    0xe64(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1238 <get_user_config_dir+0xc8>
lea    0xe41(%rip),%rdx
mov    $0x1000,%esi
mov    %r14,%rdi
mov    %rax,%rcx
call   1070 <snprintf@plt>
jmp    11d5 <get_user_config_dir+0x65>
mov    (%rsp),%r14
lea    0xe27(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11f3 <get_user_config_dir+0x83>
mov    %rax,%rdi
call   1090 <strdup@plt>
mov    %rax,%rbx
jmp    123a <get_user_config_dir+0xca>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
lea    0xe11(%rip),%rdi
call   1040 <strlen@plt>
add    %ebx,%eax
add    $0x1,%eax
movslq %eax,%r15
mov    %r15,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     1238 <get_user_config_dir+0xc8>
mov    %rax,%rbx
lea    0xdeb(%rip),%rdx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rcx
call   1070 <snprintf@plt>
jmp    123a <get_user_config_dir+0xca>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x1010,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"HOME\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [16 x i8] c"XDG_CONFIG_HOME\00"
@.str.3 = [9 x i8] c"/.config\00"
@.str.4 = [11 x i8] c"%s/.config\00"
define i8* @get_user_config_dir() {
%1 = alloca %struct.passwd
%2 = alloca %struct.passwd*
%3 = alloca [4096 x i8]
%4 = bitcast %struct.passwd* %1 to i8*
%5 = bitcast %struct.passwd** %2 to i8*
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%7 = tail call i32 (...) @getuid()
%8 = call i32 @getpwuid_r(i32 %7, %struct.passwd* %1, i8* %6, i32 4096, %struct.passwd** %2)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = getelementptr inbounds %struct.passwd, %struct.passwd* %1, i64 0, i32 0
%12 = load i8*, i8** %11
br label %18
13: ; preds = %0
%14 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%15 = icmp eq i8* %14, null
br i1 %15, label %34, label %16
16: ; preds = %13
%17 = call i32 @snprintf(i8* %6, i64 4096, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %14)
br label %18
18: ; preds = %16, %10
%19 = phi i8* [ %12, %10 ], [ %6, %16 ]
%20 = call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %32
22: ; preds = %18
%23 = call i32 @strlen(i8* %19)
%24 = call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%25 = add i32 %23, 1
%26 = add i32 %25, %24
%27 = sext i32 %26 to i64
%28 = call i8* @malloc(i64 %27)
%29 = icmp eq i8* %28, null
br i1 %29, label %34, label %30
30: ; preds = %22
%31 = call i32 @snprintf(i8* %28, i64 %27, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* %19)
br label %34
32: ; preds = %18
%33 = call i8* @strdup(i8* %20)
br label %34
34: ; preds = %22, %32, %30, %13
%35 = phi i8* [ null, %13 ], [ null, %22 ], [ %33, %32 ], [ %28, %30 ]
ret i8* %35
}