#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 */
int /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ R_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
scalar_t__ asprintf(char **, char *, char *, ...);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *get_cpu_str();
char *getenv(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, char *);
scalar_t__ strlen(char *);
const char *json_default_name(void) {
  char *cache;
  char *idstr = get_cpu_str();
  char *res = NULL;
  char *home = NULL;
  char *emap;
  emap = getenv("EVENTMAP");
  if (emap) {
    if (access(emap, R_OK) == 0)
      return emap;
    idstr = malloc(strlen(emap) + strlen("-core") + 1);
    if (!idstr)
      exit(ENOMEM);
    sprintf(idstr, "%s-core", emap);
  }
  cache = getenv("XDG_CACHE_HOME");
  if (!cache) {
    home = getenv("HOME");
    if (!home || asprintf(&cache, "%s/.cache", home) < 0)
      goto out;
  }
  if (cache && idstr)
    asprintf(&res, "%s/pmu-events/%s.json", cache, idstr);
  if (home)
    free(cache);
out:
  free(idstr);
  return res;
}


x86 assembly (clang, -O0):
<json_default_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    $0x0,%al
call   1090 <get_cpu_str@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
lea    0xe46(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1251 <json_default_name+0xc1>
mov    -0x30(%rbp),%rdi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%rax
jne    11f7 <json_default_name+0x67>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    130b <json_default_name+0x17b>
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x38(%rbp)
lea    0xe00(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x38(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    123d <json_default_name+0xad>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rdx
lea    0xdbd(%rip),%rsi
call   10a0 <sprintf@plt>
lea    0xdb9(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    12b1 <json_default_name+0x121>
lea    0xda8(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12a7 <json_default_name+0x117>
mov    -0x28(%rbp),%rdx
lea    -0x10(%rbp),%rdi
lea    0xd8a(%rip),%rsi
mov    $0x0,%al
call   1070 <asprintf@plt>
cmp    $0x0,%rax
jge    12ac <json_default_name+0x11c>
jmp    12fa <json_default_name+0x16a>
jmp    12b1 <json_default_name+0x121>
cmpq   $0x0,-0x10(%rbp)
je     12e1 <json_default_name+0x151>
cmpq   $0x0,-0x18(%rbp)
je     12e1 <json_default_name+0x151>
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    -0x20(%rbp),%rdi
lea    0xd50(%rip),%rsi
mov    $0x0,%al
call   1070 <asprintf@plt>
cmpq   $0x0,-0x28(%rbp)
je     12f5 <json_default_name+0x165>
mov    -0x10(%rbp),%rdi
call   1040 <free@plt>
jmp    12fa <json_default_name+0x16a>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<json_default_name>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1090 <get_cpu_str@plt>
mov    %rax,%r15
movq   $0x0,0x8(%rsp)
lea    0xe4d(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1209 <json_default_name+0x79>
mov    %rax,%rbx
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <access@plt>
test   %rax,%rax
je     1299 <json_default_name+0x109>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x6(%rax),%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     12a6 <json_default_name+0x116>
mov    %rax,%r15
lea    0xe0b(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
call   10a0 <sprintf@plt>
lea    0xe01(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,(%rsp)
test   %rax,%rax
je     1254 <json_default_name+0xc4>
xor    %r14d,%r14d
test   %rax,%rax
je     1244 <json_default_name+0xb4>
test   %r15,%r15
je     1244 <json_default_name+0xb4>
lea    0xdf8(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %rax,%rdx
mov    %r15,%rcx
xor    %eax,%eax
call   1070 <asprintf@plt>
test   %r14,%r14
je     128c <json_default_name+0xfc>
mov    (%rsp),%rdi
call   1040 <free@plt>
jmp    128c <json_default_name+0xfc>
lea    0xdc0(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     128c <json_default_name+0xfc>
mov    %rax,%r14
lea    0xdb1(%rip),%rsi
mov    %rsp,%rdi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <asprintf@plt>
test   %rax,%rax
js     128c <json_default_name+0xfc>
mov    (%rsp),%rax
test   %rax,%rax
jne    1226 <json_default_name+0x96>
jmp    1244 <json_default_name+0xb4>
mov    %r15,%rdi
call   1040 <free@plt>
mov    0x8(%rsp),%rbx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"EVENTMAP\00"
@R_OK = global i32 0
@.str.1 = [6 x i8] c"-core\00"
@ENOMEM = global i32 0
@.str.2 = [8 x i8] c"%s-core\00"
@.str.3 = [15 x i8] c"XDG_CACHE_HOME\00"
@.str.4 = [5 x i8] c"HOME\00"
@.str.5 = [10 x i8] c"%s/.cache\00"
@.str.6 = [22 x i8] c"%s/pmu-events/%s.json\00"
define i8* @json_default_name() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = call i8* (...) @get_cpu_str()
store i8* %7, i8** %3
store i8* null, i8** %4
store i8* null, i8** %5
%8 = call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i8* %8, i8** %6
%9 = load i8*, i8** %6
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %34
11: ; preds = %0
%12 = load i8*, i8** %6
%13 = load i32, i32* @R_OK
%14 = call i64 @access(i8* %12, i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = load i8*, i8** %6
store i8* %17, i8** %1
br label %69
18: ; preds = %11
%19 = load i8*, i8** %6
%20 = call i64 @strlen(i8* %19)
%21 = call i64 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%22 = add nsw i64 %20, %21
%23 = add nsw i64 %22, 1
%24 = call i8* @malloc(i64 %23)
store i8* %24, i8** %3
%25 = load i8*, i8** %3
%26 = icmp ne i8* %25, null
br i1 %26, label %30, label %27
27: ; preds = %18
%28 = load i32, i32* @ENOMEM
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %18
%31 = load i8*, i8** %3
%32 = load i8*, i8** %6
%33 = call i32 @sprintf(i8* %31, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %32)
br label %34
34: ; preds = %30, %0
%35 = call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
store i8* %35, i8** %2
%36 = load i8*, i8** %2
%37 = icmp ne i8* %36, null
br i1 %37, label %48, label %38
38: ; preds = %34
%39 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
store i8* %39, i8** %5
%40 = load i8*, i8** %5
%41 = icmp ne i8* %40, null
br i1 %41, label %42, label %46
42: ; preds = %38
%43 = load i8*, i8** %5
%44 = call i64 (i8**, i8*, i8*, ...) @asprintf(i8** %2, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %43)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %42, %38
br label %65
47: ; preds = %42
br label %48
48: ; preds = %47, %34
%49 = load i8*, i8** %2
%50 = icmp ne i8* %49, null
br i1 %50, label %51, label %58
51: ; preds = %48
%52 = load i8*, i8** %3
%53 = icmp ne i8* %52, null
br i1 %53, label %54, label %58
54: ; preds = %51
%55 = load i8*, i8** %2
%56 = load i8*, i8** %3
%57 = call i64 (i8**, i8*, i8*, ...) @asprintf(i8** %4, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i8* %55, i8* %56)
br label %58
58: ; preds = %54, %51, %48
%59 = load i8*, i8** %5
%60 = icmp ne i8* %59, null
br i1 %60, label %61, label %64
61: ; preds = %58
%62 = load i8*, i8** %2
%63 = call i32 @free(i8* %62)
br label %64
64: ; preds = %61, %58
br label %65
65: ; preds = %64, %46
%66 = load i8*, i8** %3
%67 = call i32 @free(i8* %66)
%68 = load i8*, i8** %4
store i8* %68, i8** %1
br label %69
69: ; preds = %65, %16
%70 = load i8*, i8** %1
ret i8* %70
}

LLVM IR (-O2):
@.str = [9 x i8] c"EVENTMAP\00"
@R_OK = global i32 0
@ENOMEM = global i32 0
@.str.2 = [8 x i8] c"%s-core\00"
@.str.3 = [15 x i8] c"XDG_CACHE_HOME\00"
@.str.4 = [5 x i8] c"HOME\00"
@.str.5 = [10 x i8] c"%s/.cache\00"
@.str.6 = [22 x i8] c"%s/pmu-events/%s.json\00"
define i8* @json_default_name() {
%1 = alloca i8*
%2 = alloca i8*
%3 = bitcast i8** %1 to i8*
%4 = tail call i8* (...) @get_cpu_str()
%5 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%6 = tail call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %22, label %8
8: ; preds = %0
%9 = load i32, i32* @R_OK
%10 = tail call i64 @access(i8* %6, i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %50, label %12
12: ; preds = %8
%13 = tail call i64 @strlen(i8* %6)
%14 = add nsw i64 %13, 6
%15 = tail call i8* @malloc(i64 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = load i32, i32* @ENOMEM
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %12
%21 = tail call i32 @sprintf(i8* %15, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %6)
br label %22
22: ; preds = %20, %0
%23 = phi i8* [ %15, %20 ], [ %4, %0 ]
%24 = tail call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
store i8* %24, i8** %1
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %34
26: ; preds = %22
%27 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%28 = icmp eq i8* %27, null
br i1 %28, label %47, label %29
29: ; preds = %26
%30 = call i64 (i8**, i8*, i8*, ...) @asprintf(i8** %1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %27)
%31 = icmp slt i64 %30, 0
br i1 %31, label %47, label %32
32: ; preds = %29
%33 = load i8*, i8** %1
br label %34
34: ; preds = %32, %22
%35 = phi i8* [ %24, %22 ], [ %33, %32 ]
%36 = phi i8* [ null, %22 ], [ %27, %32 ]
%37 = icmp ne i8* %35, null
%38 = icmp ne i8* %23, null
%39 = select i1 %37, i1 %38, i1 false
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = call i64 (i8**, i8*, i8*, ...) @asprintf(i8** %2, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i8* %35, i8* %23)
br label %42
42: ; preds = %40, %34
%43 = icmp eq i8* %36, null
br i1 %43, label %47, label %44
44: ; preds = %42
%45 = load i8*, i8** %1
%46 = call i32 @free(i8* %45)
br label %47
47: ; preds = %42, %44, %26, %29
%48 = call i32 @free(i8* %23)
%49 = load i8*, i8** %2
br label %50
50: ; preds = %8, %47
%51 = phi i8* [ %49, %47 ], [ %6, %8 ]
ret i8* %51
}