#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*/ R_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
scalar_t__ asprintf(char **, char *, char *, ...);
int /*<<< orphan*/ free(char *);
char *get_cpu_str_type(char *, char **);
char *getenv(char *);
char *getenv_copy(char *);
const char *json_default_name(char *type) {
  char *cache = NULL;
  char *idstr_step = NULL;
  char *idstr = get_cpu_str_type(type, &idstr_step);
  char *res = NULL;
  char *home = NULL;
  char *emap;
  emap = getenv("EVENTMAP");
  if (emap) {
    if (access(emap, R_OK) == 0)
      return emap;
    free(idstr);
    if (asprintf(&idstr, "%s%s", emap, type) < 0)
      goto out;
  }
  cache = getenv_copy("JEVENTS_CACHEDIR");
  if (!cache)
    cache = getenv_copy("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_step);
    if (access(res, R_OK) != 0) {
      free(res);
      asprintf(&res, "%s/pmu-events/%s.json", cache, idstr);
    }
  }
out:
  free(cache);
  free(idstr);
  free(idstr_step);
  return res;
}


x86 assembly (clang, -O0):
<json_default_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <get_cpu_str_type@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
lea    0xe5c(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1218 <json_default_name+0xb8>
mov    -0x40(%rbp),%rdi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%rax
jne    11e1 <json_default_name+0x81>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    132f <json_default_name+0x1cf>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x40(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    -0x28(%rbp),%rdi
lea    0xe0c(%rip),%rsi
mov    $0x0,%al
call   1070 <asprintf@plt>
cmp    $0x0,%rax
jge    1213 <json_default_name+0xb3>
jmp    130c <json_default_name+0x1ac>
jmp    1218 <json_default_name+0xb8>
lea    0xdef(%rip),%rdi
call   1060 <getenv_copy@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1243 <json_default_name+0xe3>
lea    0xde5(%rip),%rdi
call   1060 <getenv_copy@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1293 <json_default_name+0x133>
lea    0xdd4(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1289 <json_default_name+0x129>
mov    -0x38(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    0xdb6(%rip),%rsi
mov    $0x0,%al
call   1070 <asprintf@plt>
cmp    $0x0,%rax
jge    128e <json_default_name+0x12e>
jmp    130c <json_default_name+0x1ac>
jmp    1293 <json_default_name+0x133>
cmpq   $0x0,-0x18(%rbp)
je     1307 <json_default_name+0x1a7>
cmpq   $0x0,-0x28(%rbp)
je     1307 <json_default_name+0x1a7>
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    -0x30(%rbp),%rdi
lea    0xd7c(%rip),%rsi
mov    $0x0,%al
call   1070 <asprintf@plt>
mov    -0x30(%rbp),%rdi
mov    0x2d12(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%rax
je     1302 <json_default_name+0x1a2>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    -0x30(%rbp),%rdi
lea    0xd3d(%rip),%rsi
mov    $0x0,%al
call   1070 <asprintf@plt>
jmp    1307 <json_default_name+0x1a7>
jmp    130c <json_default_name+0x1ac>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x30(%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    $0x20,%rsp
mov    %rdi,%r14
movq   $0x0,(%rsp)
movq   $0x0,0x18(%rsp)
lea    0x18(%rsp),%rsi
call   1050 <get_cpu_str_type@plt>
mov    %rax,%r15
mov    %rax,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0xe61(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11f0 <json_default_name+0x90>
mov    %rax,%rbx
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <access@plt>
test   %rax,%rax
je     12a6 <json_default_name+0x146>
mov    %r15,%rdi
call   1040 <free@plt>
lea    0xe34(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %rbx,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1070 <asprintf@plt>
test   %rax,%rax
js     1284 <json_default_name+0x124>
lea    0xe17(%rip),%rdi
call   1060 <getenv_copy@plt>
mov    %rax,(%rsp)
test   %rax,%rax
jne    121e <json_default_name+0xbe>
lea    0xe13(%rip),%rdi
call   1060 <getenv_copy@plt>
mov    %rax,(%rsp)
test   %rax,%rax
je     12b3 <json_default_name+0x153>
test   %rax,%rax
je     1284 <json_default_name+0x124>
cmpq   $0x0,0x10(%rsp)
je     1284 <json_default_name+0x124>
mov    0x18(%rsp),%rcx
lea    0xe01(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <asprintf@plt>
mov    0x8(%rsp),%rdi
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
test   %rax,%rax
je     1284 <json_default_name+0x124>
mov    0x8(%rsp),%rdi
call   1040 <free@plt>
mov    (%rsp),%rdx
mov    0x10(%rsp),%rcx
lea    0xdc0(%rip),%rsi
lea    0x8(%rsp),%rdi
xor    %eax,%eax
call   1070 <asprintf@plt>
mov    (%rsp),%rdi
call   1040 <free@plt>
mov    0x10(%rsp),%rdi
call   1040 <free@plt>
mov    0x18(%rsp),%rdi
call   1040 <free@plt>
mov    0x8(%rsp),%rbx
mov    %rbx,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd6f(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1284 <json_default_name+0x124>
lea    0xd63(%rip),%rsi
mov    %rsp,%rdi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <asprintf@plt>
test   %rax,%rax
js     1284 <json_default_name+0x124>
mov    (%rsp),%rax
test   %rax,%rax
jne    1223 <json_default_name+0xc3>
jmp    1284 <json_default_name+0x124>

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

LLVM IR (-O2):
@.str = [9 x i8] c"EVENTMAP\00"
@R_OK = global i32 0
@.str.1 = [5 x i8] c"%s%s\00"
@.str.2 = [17 x i8] c"JEVENTS_CACHEDIR\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(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%7 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%8 = bitcast i8** %4 to i8*
%9 = call i8* @get_cpu_str_type(i8* %0, i8** %3)
store i8* %9, i8** %4
%10 = bitcast i8** %5 to i8*
store i8* null, i8** %5
%11 = call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i8* %11, null
br i1 %12, label %21, label %13
13: ; preds = %1
%14 = load i32, i32* @R_OK
%15 = call i64 @access(i8* %11, i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %62, label %17
17: ; preds = %13
%18 = call i32 @free(i8* %9)
%19 = call i64 (i8**, i8*, i8*, ...) @asprintf(i8** %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %11, i8* %0)
%20 = icmp slt i64 %19, 0
br i1 %20, label %54, label %21
21: ; preds = %17, %1
%22 = call i8* @getenv_copy(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i8* %22, i8** %2
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %35
24: ; preds = %21
%25 = call i8* @getenv_copy(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
store i8* %25, i8** %2
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %35
27: ; preds = %24
%28 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%29 = icmp eq i8* %28, null
br i1 %29, label %54, label %30
30: ; preds = %27
%31 = call i64 (i8**, i8*, i8*, ...) @asprintf(i8** %2, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %28)
%32 = icmp slt i64 %31, 0
br i1 %32, label %54, label %33
33: ; preds = %30
%34 = load i8*, i8** %2
br label %35
35: ; preds = %33, %21, %24
%36 = phi i8* [ %34, %33 ], [ %22, %21 ], [ %25, %24 ]
%37 = icmp ne i8* %36, null
%38 = load i8*, i8** %4
%39 = icmp ne i8* %38, null
%40 = select i1 %37, i1 %39, i1 false
br i1 %40, label %41, label %54
41: ; preds = %35
%42 = load i8*, i8** %3
%43 = call i64 (i8**, i8*, i8*, ...) @asprintf(i8** %5, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i8* %36, i8* %42)
%44 = load i8*, i8** %5
%45 = load i32, i32* @R_OK
%46 = call i64 @access(i8* %44, i32 %45)
%47 = icmp eq i64 %46, 0
br i1 %47, label %54, label %48
48: ; preds = %41
%49 = load i8*, i8** %5
%50 = call i32 @free(i8* %49)
%51 = load i8*, i8** %2
%52 = load i8*, i8** %4
%53 = call i64 (i8**, i8*, i8*, ...) @asprintf(i8** %5, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i8* %51, i8* %52)
br label %54
54: ; preds = %35, %48, %41, %27, %30, %17
%55 = load i8*, i8** %2
%56 = call i32 @free(i8* %55)
%57 = load i8*, i8** %4
%58 = call i32 @free(i8* %57)
%59 = load i8*, i8** %3
%60 = call i32 @free(i8* %59)
%61 = load i8*, i8** %5
br label %62
62: ; preds = %13, %54
%63 = phi i8* [ %61, %54 ], [ %11, %13 ]
ret i8* %63
}