#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 *getenv(char *);
int /*<<< orphan*/ memcpy(char *, char const *, int const);
int /*<<< orphan*/ mkdir(char const *, int);
void *strlen(char const *);
void get_user_config_file(char *out, unsigned int maxlen, const char *appname) {
  const char *out_orig = out;
  char *home = getenv("XDG_CONFIG_HOME");
  unsigned int config_len = 0;
  if (!home) {
    home = getenv("HOME");
    if (!home) {
      out[0] = 0;
      return;
    }
    config_len = strlen(".config/");
  }
  unsigned int home_len = strlen(home);
  unsigned int appname_len = strlen(appname);
  const int ext_len = strlen(".conf");
  if (home_len + 1 + config_len + appname_len + ext_len + 1 > maxlen) {
    out[0] = 0;
    return;
  }
  memcpy(out, home, home_len);
  out += home_len;
  *out = '/';
  out++;
  if (config_len) {
    memcpy(out, ".config/", config_len);
    out += config_len;
    mkdir(out_orig, 0755);
  }
  memcpy(out, appname, appname_len);
  out += appname_len;
  memcpy(out, ".conf", ext_len);
  out += ext_len;
  *out = 0;
}


x86 assembly (clang, -O0):
<get_user_config_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    0xe9e(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b3 <get_user_config_file+0x73>
lea    0xe87(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a4 <get_user_config_file+0x64>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
jmp    12ba <get_user_config_file+0x17a>
lea    0xe65(%rip),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x34(%rbp)
lea    0xe47(%rip),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
add    -0x2c(%rbp),%eax
add    -0x34(%rbp),%eax
add    -0x38(%rbp),%eax
add    $0x1,%eax
cmp    -0xc(%rbp),%eax
jbe    1201 <get_user_config_file+0xc1>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
jmp    12ba <get_user_config_file+0x17a>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x30(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,%ecx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     126f <get_user_config_file+0x12f>
mov    -0x8(%rbp),%rdi
mov    -0x2c(%rbp),%edx
lea    0xdc4(%rip),%rsi
call   1060 <memcpy@plt>
mov    -0x2c(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,%ecx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x34(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,%ecx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%edx
lea    0xd7c(%rip),%rsi
call   1060 <memcpy@plt>
mov    -0x38(%rbp),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_user_config_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
mov    %esi,%ebp
mov    %rdi,%r15
lea    0xea3(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1173 <get_user_config_file+0x33>
mov    %rax,%r14
mov    %ebp,0xc(%rsp)
xor    %r13d,%r13d
jmp    119e <get_user_config_file+0x5e>
lea    0xe91(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     124e <get_user_config_file+0x10e>
mov    %rax,%r14
mov    %ebp,0xc(%rsp)
lea    0xe7a(%rip),%rdi
call   1050 <strlen@plt>
mov    %rax,%r13
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbp
mov    %rbx,0x10(%rsp)
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rax,%r12
lea    0xe59(%rip),%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
lea    0x0(%r13,%rbp,1),%eax
add    %r12d,%eax
add    %ebx,%eax
add    $0x2,%eax
cmp    0xc(%rsp),%eax
ja     124e <get_user_config_file+0x10e>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1060 <memcpy@plt>
mov    %ebp,%eax
movb   $0x2f,(%r15,%rax,1)
lea    (%r15,%rax,1),%r14
add    $0x1,%r14
test   %r13d,%r13d
je     1221 <get_user_config_file+0xe1>
lea    0xe0d(%rip),%rsi
mov    %r14,%rdi
mov    %r13d,%edx
call   1060 <memcpy@plt>
mov    %r13d,%eax
add    %rax,%r14
mov    %r15,%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
mov    %r14,%rdi
mov    0x10(%rsp),%rsi
mov    %r12d,%edx
call   1060 <memcpy@plt>
mov    %r12d,%ebp
add    %r14,%rbp
lea    0xddb(%rip),%rsi
mov    %rbp,%rdi
mov    %ebx,%edx
call   1060 <memcpy@plt>
movslq %ebx,%r15
add    %rbp,%r15
movb   $0x0,(%r15)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"XDG_CONFIG_HOME\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [9 x i8] c".config/\00"
@.str.3 = [6 x i8] c".conf\00"
define void @get_user_config_file(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%13 = load i8*, i8** %4
store i8* %13, i8** %7
%14 = call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i8* %14, i8** %8
store i32 0, i32* %9
%15 = load i8*, i8** %8
%16 = icmp ne i8* %15, null
br i1 %16, label %27, label %17
17: ; preds = %3
%18 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %18, i8** %8
%19 = load i8*, i8** %8
%20 = icmp ne i8* %19, null
br i1 %20, label %24, label %21
21: ; preds = %17
%22 = load i8*, i8** %4
%23 = getelementptr inbounds i8, i8* %22, i64 0
store i8 0, i8* %23
br label %91
24: ; preds = %17
%25 = call i8* @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%26 = ptrtoint i8* %25 to i32
store i32 %26, i32* %9
br label %27
27: ; preds = %24, %3
%28 = load i8*, i8** %8
%29 = call i8* @strlen(i8* %28)
%30 = ptrtoint i8* %29 to i32
store i32 %30, i32* %10
%31 = load i8*, i8** %6
%32 = call i8* @strlen(i8* %31)
%33 = ptrtoint i8* %32 to i32
store i32 %33, i32* %11
%34 = call i8* @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%35 = ptrtoint i8* %34 to i32
store i32 %35, i32* %12
%36 = load i32, i32* %10
%37 = add i32 %36, 1
%38 = load i32, i32* %9
%39 = add i32 %37, %38
%40 = load i32, i32* %11
%41 = add i32 %39, %40
%42 = load i32, i32* %12
%43 = add i32 %41, %42
%44 = add i32 %43, 1
%45 = load i32, i32* %5
%46 = icmp ugt i32 %44, %45
br i1 %46, label %47, label %50
47: ; preds = %27
%48 = load i8*, i8** %4
%49 = getelementptr inbounds i8, i8* %48, i64 0
store i8 0, i8* %49
br label %91
50: ; preds = %27
%51 = load i8*, i8** %4
%52 = load i8*, i8** %8
%53 = load i32, i32* %10
%54 = call i32 @memcpy(i8* %51, i8* %52, i32 %53)
%55 = load i32, i32* %10
%56 = load i8*, i8** %4
%57 = zext i32 %55 to i64
%58 = getelementptr inbounds i8, i8* %56, i64 %57
store i8* %58, i8** %4
%59 = load i8*, i8** %4
store i8 47, i8* %59
%60 = load i8*, i8** %4
%61 = getelementptr inbounds i8, i8* %60, i32 1
store i8* %61, i8** %4
%62 = load i32, i32* %9
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %74
64: ; preds = %50
%65 = load i8*, i8** %4
%66 = load i32, i32* %9
%67 = call i32 @memcpy(i8* %65, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %66)
%68 = load i32, i32* %9
%69 = load i8*, i8** %4
%70 = zext i32 %68 to i64
%71 = getelementptr inbounds i8, i8* %69, i64 %70
store i8* %71, i8** %4
%72 = load i8*, i8** %7
%73 = call i32 @mkdir(i8* %72, i32 493)
br label %74
74: ; preds = %64, %50
%75 = load i8*, i8** %4
%76 = load i8*, i8** %6
%77 = load i32, i32* %11
%78 = call i32 @memcpy(i8* %75, i8* %76, i32 %77)
%79 = load i32, i32* %11
%80 = load i8*, i8** %4
%81 = zext i32 %79 to i64
%82 = getelementptr inbounds i8, i8* %80, i64 %81
store i8* %82, i8** %4
%83 = load i8*, i8** %4
%84 = load i32, i32* %12
%85 = call i32 @memcpy(i8* %83, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %84)
%86 = load i32, i32* %12
%87 = load i8*, i8** %4
%88 = sext i32 %86 to i64
%89 = getelementptr inbounds i8, i8* %87, i64 %88
store i8* %89, i8** %4
%90 = load i8*, i8** %4
store i8 0, i8* %90
br label %91
91: ; preds = %74, %47, %21
ret void
}

LLVM IR (-O2):
@.str = [16 x i8] c"XDG_CONFIG_HOME\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [9 x i8] c".config/\00"
@.str.3 = [6 x i8] c".conf\00"
define void @get_user_config_file(i8* %0, i32 %1, i8* %2) {
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %13
6: ; preds = %3
%7 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i8* %7, null
br i1 %8, label %50, label %9
9: ; preds = %6
%10 = tail call i8* @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%11 = ptrtoint i8* %10 to i64
%12 = trunc i64 %11 to i32
br label %13
13: ; preds = %9, %3
%14 = phi i32 [ 0, %3 ], [ %12, %9 ]
%15 = phi i8* [ %4, %3 ], [ %7, %9 ]
%16 = tail call i8* @strlen(i8* %15)
%17 = ptrtoint i8* %16 to i64
%18 = trunc i64 %17 to i32
%19 = tail call i8* @strlen(i8* %2)
%20 = ptrtoint i8* %19 to i64
%21 = trunc i64 %20 to i32
%22 = tail call i8* @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%23 = ptrtoint i8* %22 to i64
%24 = trunc i64 %23 to i32
%25 = add i32 %14, 2
%26 = add i32 %25, %18
%27 = add i32 %26, %21
%28 = add i32 %27, %24
%29 = icmp ugt i32 %28, %1
br i1 %29, label %50, label %30
30: ; preds = %13
%31 = tail call i32 @memcpy(i8* %0, i8* %15, i32 %18)
%32 = and i64 %17, 4294967295
%33 = getelementptr inbounds i8, i8* %0, i64 %32
store i8 47, i8* %33
%34 = getelementptr inbounds i8, i8* %33, i64 1
%35 = icmp eq i32 %14, 0
br i1 %35, label %41, label %36
36: ; preds = %30
%37 = tail call i32 @memcpy(i8* %34, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%38 = zext i32 %14 to i64
%39 = getelementptr inbounds i8, i8* %34, i64 %38
%40 = tail call i32 @mkdir(i8* %0, i32 493)
br label %41
41: ; preds = %36, %30
%42 = phi i8* [ %39, %36 ], [ %34, %30 ]
%43 = tail call i32 @memcpy(i8* %42, i8* %2, i32 %21)
%44 = and i64 %20, 4294967295
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = tail call i32 @memcpy(i8* %45, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%47 = shl i64 %23, 32
%48 = ashr exact i64 %47, 32
%49 = getelementptr inbounds i8, i8* %45, i64 %48
br label %50
50: ; preds = %13, %6, %41
%51 = phi i8* [ %49, %41 ], [ %0, %6 ], [ %0, %13 ]
store i8 0, i8* %51
ret void
}