#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 *calloc(int, scalar_t__);
char *getenv(char *);
char *str_printf(char *, char *, int /*<<< orphan*/, int /*<<< orphan*/);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
const char *file_get_config_system(const char *dir) {
  static char *dirs = NULL;
  if (!dirs) {
    const char *xdg_sys = getenv("XDG_CONFIG_DIRS");
    if (xdg_sys && *xdg_sys) {
      dirs = calloc(1, strlen(xdg_sys) + 2);
      strcpy(dirs, xdg_sys);
      char *pt = dirs;
      while (NULL != (pt = strchr(pt, ':'))) {
        *pt++ = 0;
      }
    } else {
      dirs = str_printf("%s%c%c", "/etc/xdg", 0, 0);
    }
  }
  if (!dir) {
    dir = dirs;
  } else {
    dir += strlen(dir) + 1;
    if (!*dir) {
      dir = NULL;
    }
  }
  return dir;
}


x86 assembly (clang, -O0):
<file_get_config_system>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,0x2ee4(%rip)
jne    1240 <file_get_config_system+0xe0>
lea    0xe7f(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     121d <file_get_config_system+0xbd>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     121d <file_get_config_system+0xbd>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rsi
add    $0x2,%rsi
mov    $0x1,%edi
call   1070 <calloc@plt>
mov    %rax,0x2e92(%rip)
mov    0x2e8b(%rip),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    0x2e7b(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x3a,%esi
call   1060 <strchr@plt>
mov    %rax,%rcx
mov    %rcx,-0x18(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
je     1218 <file_get_config_system+0xb8>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x0,(%rax)
jmp    11e1 <file_get_config_system+0x81>
jmp    123b <file_get_config_system+0xdb>
lea    0xdec(%rip),%rdi
lea    0xdec(%rip),%rsi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1080 <str_printf@plt>
mov    %rax,0x2e1d(%rip)
jmp    1240 <file_get_config_system+0xe0>
cmpq   $0x0,-0x8(%rbp)
jne    125b <file_get_config_system+0xfb>
mov    0x2e06(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    128a <file_get_config_system+0x12a>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%rax
add    -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1285 <file_get_config_system+0x125>
movq   $0x0,-0x8(%rbp)
jmp    128a <file_get_config_system+0x12a>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_get_config_system>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2eea(%rip),%rbx
test   %rbx,%rbx
jne    120c <file_get_config_system+0xac>
lea    0xe82(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11eb <file_get_config_system+0x8b>
mov    %rax,%rbx
cmpb   $0x0,(%rax)
je     11eb <file_get_config_system+0x8b>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x2(%rax),%rsi
mov    $0x1,%edi
call   1070 <calloc@plt>
mov    %rax,0x2eab(%rip)
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    0x2e99(%rip),%rbx
mov    %rbx,%rdi
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0x3a,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     120c <file_get_config_system+0xac>
movb   $0x0,(%rax)
add    $0x1,%rax
mov    %rax,%rdi
jmp    11d0 <file_get_config_system+0x70>
lea    0xe1e(%rip),%rdi
lea    0xe1e(%rip),%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1080 <str_printf@plt>
mov    %rax,%rbx
mov    %rax,0x2e4c(%rip)
test   %r14,%r14
je     122d <file_get_config_system+0xcd>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%rax,%r14,1),%rcx
add    $0x1,%rcx
xor    %ebx,%ebx
cmpb   $0x0,0x1(%rax,%r14,1)
cmovne %rcx,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@file_get_config_system.dirs = internal global i8* null
@.str = [16 x i8] c"XDG_CONFIG_DIRS\00"
@.str.1 = [7 x i8] c"%s%c%c\00"
@.str.2 = [9 x i8] c"/etc/xdg\00"
define i8* @file_get_config_system(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i8*, i8** @file_get_config_system.dirs
%6 = icmp ne i8* %5, null
br i1 %6, label %36, label %7
7: ; preds = %1
%8 = call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i8* %8, i8** %3
%9 = load i8*, i8** %3
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %33
11: ; preds = %7
%12 = load i8*, i8** %3
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %33
16: ; preds = %11
%17 = load i8*, i8** %3
%18 = call i64 @strlen(i8* %17)
%19 = add nsw i64 %18, 2
%20 = call i8* @calloc(i32 1, i64 %19)
store i8* %20, i8** @file_get_config_system.dirs
%21 = load i8*, i8** @file_get_config_system.dirs
%22 = load i8*, i8** %3
%23 = call i32 @strcpy(i8* %21, i8* %22)
%24 = load i8*, i8** @file_get_config_system.dirs
store i8* %24, i8** %4
br label %25
25: ; preds = %29, %16
%26 = load i8*, i8** %4
%27 = call i8* @strchr(i8* %26, i8 signext 58)
store i8* %27, i8** %4
%28 = icmp ne i8* null, %27
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i8*, i8** %4
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %4
store i8 0, i8* %30
br label %25
32: ; preds = %25
br label %35
33: ; preds = %11, %7
%34 = call i8* @str_printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 0, i32 0)
store i8* %34, i8** @file_get_config_system.dirs
br label %35
35: ; preds = %33, %32
br label %36
36: ; preds = %35, %1
%37 = load i8*, i8** %2
%38 = icmp ne i8* %37, null
br i1 %38, label %41, label %39
39: ; preds = %36
%40 = load i8*, i8** @file_get_config_system.dirs
store i8* %40, i8** %2
br label %52
41: ; preds = %36
%42 = load i8*, i8** %2
%43 = call i64 @strlen(i8* %42)
%44 = add nsw i64 %43, 1
%45 = load i8*, i8** %2
%46 = getelementptr inbounds i8, i8* %45, i64 %44
store i8* %46, i8** %2
%47 = load i8*, i8** %2
%48 = load i8, i8* %47
%49 = icmp ne i8 %48, 0
br i1 %49, label %51, label %50
50: ; preds = %41
store i8* null, i8** %2
br label %51
51: ; preds = %50, %41
br label %52
52: ; preds = %51, %39
%53 = load i8*, i8** %2
ret i8* %53
}

LLVM IR (-O2):
@file_get_config_system.dirs = internal unnamed_addr global i8* null
@.str = [16 x i8] c"XDG_CONFIG_DIRS\00"
@.str.1 = [7 x i8] c"%s%c%c\00"
@.str.2 = [9 x i8] c"/etc/xdg\00"
define i8* @file_get_config_system(i8* %0) {
%2 = load i8*, i8** @file_get_config_system.dirs
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %25
4: ; preds = %1
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i8* %5, null
br i1 %6, label %23, label %7
7: ; preds = %4
%8 = load i8, i8* %5
%9 = icmp eq i8 %8, 0
br i1 %9, label %23, label %10
10: ; preds = %7
%11 = tail call i64 @strlen(i8* %5)
%12 = add nsw i64 %11, 2
%13 = tail call i8* @calloc(i32 1, i64 %12)
store i8* %13, i8** @file_get_config_system.dirs
%14 = tail call i32 @strcpy(i8* %13, i8* %5)
%15 = load i8*, i8** @file_get_config_system.dirs
%16 = tail call i8* @strchr(i8* dereferenceable(1) %15, i8 signext 58)
%17 = icmp eq i8* %16, null
br i1 %17, label %25, label %18
18: ; preds = %10, %18
%19 = phi i8* [ %21, %18 ], [ %16, %10 ]
%20 = getelementptr inbounds i8, i8* %19, i64 1
store i8 0, i8* %19
%21 = tail call i8* @strchr(i8* dereferenceable(1) %20, i8 signext 58)
%22 = icmp eq i8* %21, null
br i1 %22, label %25, label %18
23: ; preds = %7, %4
%24 = tail call i8* @str_printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 0, i32 0)
store i8* %24, i8** @file_get_config_system.dirs
br label %25
25: ; preds = %18, %10, %23, %1
%26 = phi i8* [ %15, %10 ], [ %24, %23 ], [ %2, %1 ], [ %15, %18 ]
%27 = icmp eq i8* %0, null
br i1 %27, label %35, label %28
28: ; preds = %25
%29 = tail call i64 @strlen(i8* %0)
%30 = add nsw i64 %29, 1
%31 = getelementptr inbounds i8, i8* %0, i64 %30
%32 = load i8, i8* %31
%33 = icmp eq i8 %32, 0
%34 = select i1 %33, i8* null, i8* %31
br label %35
35: ; preds = %25, %28
%36 = phi i8* [ %34, %28 ], [ %26, %25 ]
ret i8* %36
}