#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 */
scalar_t__ EEXIST;
scalar_t__ errno;
char *getenv(char *);
scalar_t__ mkdir(char *, int);
int snprintf(char *, size_t, char *, char const *, char const *);
int fc_datadir(const char *app_id, char *path, size_t path_max) {
  const char *home_path = getenv("XDG_DATA_HOME");
  int result = -1;
  if (home_path && *home_path) {
    result = snprintf(path, path_max, "%s/%s/", home_path, app_id);
  } else {
    home_path = getenv("HOME");
    if (home_path && *home_path) {
      result =
          snprintf(path, path_max, "%s/.local/share/%s/", home_path, app_id);
    }
  }
  if (result <= 0 || (size_t)result >= path_max) {
    path[0] = 0;
    return -1;
  }
  char *ch = path;
  while (*(++ch)) {
    if (*ch == '/') {
      *ch = 0;
      if (mkdir(path, 0700) != 0 && errno != EEXIST) {
        path[0] = 0;
        return -1;
      }
      *ch = '/';
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<fc_datadir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    0xeb5(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     119a <fc_datadir+0x6a>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     119a <fc_datadir+0x6a>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xe81(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x2c(%rbp)
jmp    11e9 <fc_datadir+0xb9>
lea    0xe68(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11e4 <fc_datadir+0xb4>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11e4 <fc_datadir+0xb4>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xe39(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x2c(%rbp)
jmp    11e9 <fc_datadir+0xb9>
cmpl   $0x0,-0x2c(%rbp)
jle    1201 <fc_datadir+0xd1>
movslq -0x2c(%rbp),%rax
cmp    -0x20(%rbp),%rax
jb     1214 <fc_datadir+0xe4>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12a4 <fc_datadir+0x174>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
cmpb   $0x0,0x1(%rax)
je     129d <fc_datadir+0x16d>
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1298 <fc_datadir+0x168>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
mov    $0x1c0,%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
je     1291 <fc_datadir+0x161>
mov    0x2d6d(%rip),%rax
mov    (%rax),%rax
mov    0x2d6b(%rip),%rcx
cmp    (%rcx),%rax
je     1291 <fc_datadir+0x161>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12a4 <fc_datadir+0x174>
mov    -0x38(%rbp),%rax
movb   $0x2f,(%rax)
jmp    121c <fc_datadir+0xec>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fc_datadir>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r15
lea    0xeb8(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1160 <fc_datadir+0x30>
cmpb   $0x0,(%rax)
je     1160 <fc_datadir+0x30>
lea    0xeb0(%rip),%rdx
jmp    117d <fc_datadir+0x4d>
lea    0xea2(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11ed <fc_datadir+0xbd>
cmpb   $0x0,(%rax)
je     11ed <fc_datadir+0xbd>
lea    0xe98(%rip),%rdx
mov    %r12,%rdi
mov    %r14,%rsi
mov    %rax,%rcx
mov    %r15,%r8
call   1050 <snprintf@plt>
test   %eax,%eax
jle    11ed <fc_datadir+0xbd>
cltq
cmp    %r14,%rax
jae    11ed <fc_datadir+0xbd>
lea    0x1(%r12),%rbx
mov    0x2e33(%rip),%r14
mov    0x2e34(%rip),%r15
jmp    11b7 <fc_datadir+0x87>
xchg   %ax,%ax
movb   $0x2f,(%rbx)
add    $0x1,%rbx
movzbl (%rbx),%eax
cmp    $0x2f,%al
je     11d0 <fc_datadir+0xa0>
test   %al,%al
je     11f9 <fc_datadir+0xc9>
add    $0x1,%rbx
jmp    11b7 <fc_datadir+0x87>
nopl   0x0(%rax,%rax,1)
movb   $0x0,(%rbx)
mov    %r12,%rdi
mov    $0x1c0,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
je     11b0 <fc_datadir+0x80>
mov    (%r14),%rax
cmp    (%r15),%rax
je     11b0 <fc_datadir+0x80>
movb   $0x0,(%r12)
mov    $0xffffffff,%eax
jmp    11fb <fc_datadir+0xcb>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [14 x i8] c"XDG_DATA_HOME\00"
@.str.1 = [7 x i8] c"%s/%s/\00"
@.str.2 = [5 x i8] c"HOME\00"
@.str.3 = [20 x i8] c"%s/.local/share/%s/\00"
@errno = global i64 0
@EEXIST = global i64 0
define i32 @fc_datadir(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = call i8* @getenv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %8
store i32 -1, i32* %9
%12 = load i8*, i8** %8
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %25
14: ; preds = %3
%15 = load i8*, i8** %8
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %14
%20 = load i8*, i8** %6
%21 = load i64, i64* %7
%22 = load i8*, i8** %8
%23 = load i8*, i8** %5
%24 = call i32 @snprintf(i8* %20, i64 %21, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %22, i8* %23)
store i32 %24, i32* %9
br label %41
25: ; preds = %14, %3
%26 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i8* %26, i8** %8
%27 = load i8*, i8** %8
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %40
29: ; preds = %25
%30 = load i8*, i8** %8
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %29
%35 = load i8*, i8** %6
%36 = load i64, i64* %7
%37 = load i8*, i8** %8
%38 = load i8*, i8** %5
%39 = call i32 @snprintf(i8* %35, i64 %36, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %37, i8* %38)
store i32 %39, i32* %9
br label %40
40: ; preds = %34, %29, %25
br label %41
41: ; preds = %40, %19
%42 = load i32, i32* %9
%43 = icmp sle i32 %42, 0
br i1 %43, label %49, label %44
44: ; preds = %41
%45 = load i32, i32* %9
%46 = sext i32 %45 to i64
%47 = load i64, i64* %7
%48 = icmp uge i64 %46, %47
br i1 %48, label %49, label %52
49: ; preds = %44, %41
%50 = load i8*, i8** %6
%51 = getelementptr inbounds i8, i8* %50, i64 0
store i8 0, i8* %51
store i32 -1, i32* %4
br label %80
52: ; preds = %44
%53 = load i8*, i8** %6
store i8* %53, i8** %10
br label %54
54: ; preds = %78, %52
%55 = load i8*, i8** %10
%56 = getelementptr inbounds i8, i8* %55, i32 1
store i8* %56, i8** %10
%57 = load i8, i8* %56
%58 = icmp ne i8 %57, 0
br i1 %58, label %59, label %79
59: ; preds = %54
%60 = load i8*, i8** %10
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = icmp eq i32 %62, 47
br i1 %63, label %64, label %78
64: ; preds = %59
%65 = load i8*, i8** %10
store i8 0, i8* %65
%66 = load i8*, i8** %6
%67 = call i64 @mkdir(i8* %66, i32 448)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %76
69: ; preds = %64
%70 = load i64, i64* @errno
%71 = load i64, i64* @EEXIST
%72 = icmp ne i64 %70, %71
br i1 %72, label %73, label %76
73: ; preds = %69
%74 = load i8*, i8** %6
%75 = getelementptr inbounds i8, i8* %74, i64 0
store i8 0, i8* %75
store i32 -1, i32* %4
br label %80
76: ; preds = %69, %64
%77 = load i8*, i8** %10
store i8 47, i8* %77
br label %78
78: ; preds = %76, %59
br label %54
79: ; preds = %54
store i32 0, i32* %4
br label %80
80: ; preds = %79, %73, %49
%81 = load i32, i32* %4
ret i32 %81
}

LLVM IR (-O2):
@.str = [14 x i8] c"XDG_DATA_HOME\00"
@.str.1 = [7 x i8] c"%s/%s/\00"
@.str.2 = [5 x i8] c"HOME\00"
@.str.3 = [20 x i8] c"%s/.local/share/%s/\00"
@errno = global i64 0
@EEXIST = global i64 0
define i32 @fc_datadir(i8* %0, i8* %1, i64 %2) {
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %11, label %6
6: ; preds = %3
%7 = load i8, i8* %4
%8 = icmp eq i8 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = tail call i32 @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %4, i8* %0)
br label %19
11: ; preds = %6, %3
%12 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i8* %12, null
br i1 %13, label %38, label %14
14: ; preds = %11
%15 = load i8, i8* %12
%16 = icmp eq i8 %15, 0
br i1 %16, label %38, label %17
17: ; preds = %14
%18 = tail call i32 @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %12, i8* %0)
br label %19
19: ; preds = %17, %9
%20 = phi i32 [ %10, %9 ], [ %18, %17 ]
%21 = icmp sgt i32 %20, 0
%22 = sext i32 %20 to i64
%23 = icmp ult i64 %22, %2
%24 = and i1 %21, %23
br i1 %24, label %25, label %38
25: ; preds = %19, %29
%26 = phi i8* [ %27, %29 ], [ %1, %19 ]
%27 = getelementptr inbounds i8, i8* %26, i64 1
%28 = load i8, i8* %27
switch i8 %28, label %29 [
i8 0, label %39
i8 47, label %30
]
29: ; preds = %25, %37
br label %25
30: ; preds = %25
store i8 0, i8* %27
%31 = tail call i64 @mkdir(i8* %1, i32 448)
%32 = icmp eq i64 %31, 0
br i1 %32, label %37, label %33
33: ; preds = %30
%34 = load i64, i64* @errno
%35 = load i64, i64* @EEXIST
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %33, %30
store i8 47, i8* %27
br label %29
38: ; preds = %33, %19, %14, %11
store i8 0, i8* %1
br label %39
39: ; preds = %25, %38
%40 = phi i32 [ -1, %38 ], [ 0, %25 ]
ret i32 %40
}