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


x86 assembly (clang, -O0):
<get_user_cache_folder>:
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_cache_folder+0x73>
lea    0xe86(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a4 <get_user_cache_folder+0x64>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
jmp    12b6 <get_user_cache_folder+0x176>
lea    0xe64(%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)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
add    -0x2c(%rbp),%eax
add    -0x34(%rbp),%eax
add    $0x1,%eax
add    $0x1,%eax
cmp    -0xc(%rbp),%eax
jbe    11f2 <get_user_cache_folder+0xb2>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
jmp    12b6 <get_user_cache_folder+0x176>
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     1267 <get_user_cache_folder+0x127>
mov    -0x8(%rbp),%rdi
mov    -0x2c(%rbp),%edx
lea    0xdd2(%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    -0x8(%rbp),%rax
movb   $0x0,(%rax)
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),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
mov    -0x8(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_user_cache_folder>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r13
mov    %esi,%ebx
mov    %rdi,%r12
lea    0xea6(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     116c <get_user_cache_folder+0x2c>
mov    %rax,%rbp
xor    %r15d,%r15d
jmp    1193 <get_user_cache_folder+0x53>
lea    0xe97(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1241 <get_user_cache_folder+0x101>
mov    %rax,%rbp
lea    0xe84(%rip),%rdi
call   1050 <strlen@plt>
mov    %eax,%r15d
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %eax,%r14d
mov    %r13,(%rsp)
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %eax,%r13d
lea    (%r15,%r14,1),%eax
add    %r13d,%eax
add    $0x3,%eax
cmp    %ebx,%eax
ja     1241 <get_user_cache_folder+0x101>
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1060 <memcpy@plt>
mov    %r14d,%eax
movb   $0x2f,(%r12,%rax,1)
lea    (%r12,%rax,1),%rbp
add    $0x1,%rbp
test   %r15d,%r15d
je     1210 <get_user_cache_folder+0xd0>
lea    0xe26(%rip),%rsi
mov    %rbp,%rdi
mov    %r15d,%edx
call   1060 <memcpy@plt>
mov    %r15d,%eax
lea    (%rax,%rbp,1),%rbx
movb   $0x0,0x0(%rbp,%rax,1)
mov    %r12,%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
mov    %rbx,%rbp
mov    %rbp,%rdi
mov    (%rsp),%rsi
mov    %r13d,%edx
call   1060 <memcpy@plt>
mov    %r13d,%ebx
movb   $0x0,0x0(%rbp,%rbx,1)
mov    %r12,%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
movb   $0x2f,0x0(%rbp,%rbx,1)
lea    (%rbx,%rbp,1),%r12
add    $0x1,%r12
movb   $0x0,(%r12)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [15 x i8] c"XDG_CACHE_HOME\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [8 x i8] c".cache/\00"
define void @get_user_cache_folder(i8* %0, i32 %1, i8* %2) {
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %11
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 %38, label %9
9: ; preds = %6
%10 = tail call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %11
11: ; preds = %9, %3
%12 = phi i32 [ 0, %3 ], [ %10, %9 ]
%13 = phi i8* [ %4, %3 ], [ %7, %9 ]
%14 = tail call i32 @strlen(i8* %13)
%15 = tail call i32 @strlen(i8* %2)
%16 = add i32 %12, 3
%17 = add i32 %16, %14
%18 = add i32 %17, %15
%19 = icmp ugt i32 %18, %1
br i1 %19, label %38, label %20
20: ; preds = %11
%21 = tail call i32 @memcpy(i8* %0, i8* %13, i32 %14)
%22 = zext i32 %14 to i64
%23 = getelementptr inbounds i8, i8* %0, i64 %22
store i8 47, i8* %23
%24 = getelementptr inbounds i8, i8* %23, i64 1
%25 = icmp eq i32 %12, 0
br i1 %25, label %31, label %26
26: ; preds = %20
%27 = tail call i32 @memcpy(i8* %24, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%28 = zext i32 %12 to i64
%29 = getelementptr inbounds i8, i8* %24, i64 %28
store i8 0, i8* %29
%30 = tail call i32 @mkdir(i8* %0, i32 493)
br label %31
31: ; preds = %26, %20
%32 = phi i8* [ %29, %26 ], [ %24, %20 ]
%33 = tail call i32 @memcpy(i8* %32, i8* %2, i32 %15)
%34 = zext i32 %15 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 0, i8* %35
%36 = tail call i32 @mkdir(i8* %0, i32 493)
store i8 47, i8* %35
%37 = getelementptr inbounds i8, i8* %35, i64 1
br label %38
38: ; preds = %11, %6, %31
%39 = phi i8* [ %37, %31 ], [ %0, %6 ], [ %0, %11 ]
store i8 0, i8* %39
ret void
}