#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 */
struct perf_event_attr {
  int /*<<< orphan*/ config2;
  int /*<<< orphan*/ config1;
  int /*<<< orphan*/ config;
  int /*<<< orphan*/ type;
};

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *resolve_pmu(int /*<<< orphan*/);
int snprintf(char *, int, char *, ...);
scalar_t__ sprintf(char *, char *, int /*<<< orphan*/);
char *strdup(char *);
char *format_raw_event(struct perf_event_attr *attr, char *name) {
  char buf[1024];
  int off = 0;
  char *pmu;
  pmu = resolve_pmu(attr->type);
  if (!pmu)
    return NULL;
  off = snprintf(buf, 1024, "%s/config=%#llx", pmu, attr->config);
  free(pmu);
  if (attr->config1)
    off += sprintf(buf + off, ",config1=%#llx", attr->config1);
  if (attr->config2)
    off += sprintf(buf + off, ",config2=%#llx", attr->config2);
  if (name)
    off += snprintf(buf + off, 1024 - off, ",name=%s", name);
  off += snprintf(buf + off, 1024 - off, "/");
  return strdup(buf);
}


x86 assembly (clang, -O0):
<format_raw_event>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x424(%rbp)
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%edi
call   1050 <resolve_pmu@plt>
mov    %rax,-0x430(%rbp)
cmpq   $0x0,-0x430(%rbp)
jne    119b <format_raw_event+0x4b>
movq   $0x0,-0x8(%rbp)
jmp    12e9 <format_raw_event+0x199>
lea    -0x420(%rbp),%rdi
mov    -0x430(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%r8d
mov    $0x400,%esi
lea    0xe43(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    %eax,-0x424(%rbp)
mov    -0x430(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
cmpl   $0x0,0x4(%rax)
je     121b <format_raw_event+0xcb>
lea    -0x420(%rbp),%rdi
movslq -0x424(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edx
lea    0xe0d(%rip),%rsi
call   1060 <sprintf@plt>
mov    %rax,%rcx
movslq -0x424(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x424(%rbp)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
je     125e <format_raw_event+0x10e>
lea    -0x420(%rbp),%rdi
movslq -0x424(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
lea    0xdd9(%rip),%rsi
call   1060 <sprintf@plt>
mov    %rax,%rcx
movslq -0x424(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x424(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12a3 <format_raw_event+0x153>
lea    -0x420(%rbp),%rdi
movslq -0x424(%rbp),%rax
add    %rax,%rdi
mov    $0x400,%esi
sub    -0x424(%rbp),%esi
mov    -0x18(%rbp),%rcx
lea    0xd9e(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
add    -0x424(%rbp),%eax
mov    %eax,-0x424(%rbp)
lea    -0x420(%rbp),%rdi
movslq -0x424(%rbp),%rax
add    %rax,%rdi
mov    $0x400,%esi
sub    -0x424(%rbp),%esi
lea    0xd71(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
add    -0x424(%rbp),%eax
mov    %eax,-0x424(%rbp)
lea    -0x420(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_raw_event>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0xc(%rdi),%edi
call   1050 <resolve_pmu@plt>
test   %rax,%rax
je     122e <format_raw_event+0xde>
mov    %rax,%rbx
mov    0x8(%r15),%r8d
lea    0xe7d(%rip),%rdx
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    %eax,%r12d
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x4(%r15),%edx
test   %edx,%edx
je     11c1 <format_raw_event+0x71>
movslq %r12d,%rax
lea    (%rsp,%rax,1),%rdi
lea    0xe5a(%rip),%rsi
call   1060 <sprintf@plt>
add    %r12d,%eax
mov    %eax,%r12d
mov    (%r15),%edx
test   %edx,%edx
je     11de <format_raw_event+0x8e>
movslq %r12d,%r12
lea    (%rsp,%r12,1),%rdi
lea    0xe49(%rip),%rsi
call   1060 <sprintf@plt>
add    %eax,%r12d
test   %r14,%r14
je     1208 <format_raw_event+0xb8>
movslq %r12d,%rbx
lea    (%rsp,%rbx,1),%rdi
mov    $0x400,%esi
sub    %ebx,%esi
lea    0xe36(%rip),%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    %eax,%r12d
add    %ebx,%r12d
movslq %r12d,%rax
lea    (%rsp,%rax,1),%rdi
mov    $0x400,%esi
sub    %eax,%esi
lea    0xe1a(%rip),%rdx
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    %rsp,%rdi
call   1070 <strdup@plt>
jmp    1230 <format_raw_event+0xe0>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.perf_event_attr = type { i32, i32, i32, i32 }
@.str = [16 x i8] c"%s/config=%#llx\00"
@.str.1 = [15 x i8] c",config1=%#llx\00"
@.str.2 = [15 x i8] c",config2=%#llx\00"
@.str.3 = [9 x i8] c",name=%s\00"
@.str.4 = [2 x i8] c"/\00"
define i8* @format_raw_event(%struct.perf_event_attr* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca %struct.perf_event_attr*
%5 = alloca i8*
%6 = alloca [1024 x i8]
%7 = alloca i32
%8 = alloca i8*
store %struct.perf_event_attr* %0, %struct.perf_event_attr** %4
store i8* %1, i8** %5
store i32 0, i32* %7
%9 = load %struct.perf_event_attr*, %struct.perf_event_attr** %4
%10 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %9, i32 0, i32 3
%11 = load i32, i32* %10
%12 = call i8* @resolve_pmu(i32 %11)
store i8* %12, i8** %8
%13 = load i8*, i8** %8
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %2
store i8* null, i8** %3
br label %86
16: ; preds = %2
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%18 = load i8*, i8** %8
%19 = load %struct.perf_event_attr*, %struct.perf_event_attr** %4
%20 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %19, i32 0, i32 2
%21 = load i32, i32* %20
%22 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %17, i32 1024, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %18, i32 %21)
store i32 %22, i32* %7
%23 = load i8*, i8** %8
%24 = call i32 @free(i8* %23)
%25 = load %struct.perf_event_attr*, %struct.perf_event_attr** %4
%26 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %25, i32 0, i32 1
%27 = load i32, i32* %26
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %42
29: ; preds = %16
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%31 = load i32, i32* %7
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load %struct.perf_event_attr*, %struct.perf_event_attr** %4
%35 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %34, i32 0, i32 1
%36 = load i32, i32* %35
%37 = call i64 @sprintf(i8* %33, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %36)
%38 = load i32, i32* %7
%39 = sext i32 %38 to i64
%40 = add nsw i64 %39, %37
%41 = trunc i64 %40 to i32
store i32 %41, i32* %7
br label %42
42: ; preds = %29, %16
%43 = load %struct.perf_event_attr*, %struct.perf_event_attr** %4
%44 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %60
47: ; preds = %42
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%49 = load i32, i32* %7
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
%52 = load %struct.perf_event_attr*, %struct.perf_event_attr** %4
%53 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %52, i32 0, i32 0
%54 = load i32, i32* %53
%55 = call i64 @sprintf(i8* %51, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %54)
%56 = load i32, i32* %7
%57 = sext i32 %56 to i64
%58 = add nsw i64 %57, %55
%59 = trunc i64 %58 to i32
store i32 %59, i32* %7
br label %60
60: ; preds = %47, %42
%61 = load i8*, i8** %5
%62 = icmp ne i8* %61, null
br i1 %62, label %63, label %74
63: ; preds = %60
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%65 = load i32, i32* %7
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %64, i64 %66
%68 = load i32, i32* %7
%69 = sub nsw i32 1024, %68
%70 = load i8*, i8** %5
%71 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %67, i32 %69, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %70)
%72 = load i32, i32* %7
%73 = add nsw i32 %72, %71
store i32 %73, i32* %7
br label %74
74: ; preds = %63, %60
%75 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%76 = load i32, i32* %7
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds i8, i8* %75, i64 %77
%79 = load i32, i32* %7
%80 = sub nsw i32 1024, %79
%81 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %78, i32 %80, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%82 = load i32, i32* %7
%83 = add nsw i32 %82, %81
store i32 %83, i32* %7
%84 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%85 = call i8* @strdup(i8* %84)
store i8* %85, i8** %3
br label %86
86: ; preds = %74, %15
%87 = load i8*, i8** %3
ret i8* %87
}

LLVM IR (-O2):
%struct.perf_event_attr = type { i32, i32, i32, i32 }
@.str = [16 x i8] c"%s/config=%#llx\00"
@.str.1 = [15 x i8] c",config1=%#llx\00"
@.str.2 = [15 x i8] c",config2=%#llx\00"
@.str.3 = [9 x i8] c",name=%s\00"
@.str.4 = [2 x i8] c"/\00"
define noalias i8* @format_raw_event(%struct.perf_event_attr* nocapture %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %0, i64 0, i32 3
%6 = load i32, i32* %5
%7 = tail call i8* @resolve_pmu(i32 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %50, label %9
9: ; preds = %2
%10 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %0, i64 0, i32 2
%11 = load i32, i32* %10
%12 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %4, i32 1024, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %7, i32 %11)
%13 = call i32 @free(i8* %7)
%14 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %0, i64 0, i32 1
%15 = load i32, i32* %14
%16 = icmp eq i32 %15, 0
br i1 %16, label %23, label %17
17: ; preds = %9
%18 = sext i32 %12 to i64
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %18
%20 = call i64 @sprintf(i8* %19, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%21 = trunc i64 %20 to i32
%22 = add i32 %12, %21
br label %23
23: ; preds = %17, %9
%24 = phi i32 [ %22, %17 ], [ %12, %9 ]
%25 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %0, i64 0, i32 0
%26 = load i32, i32* %25
%27 = icmp eq i32 %26, 0
br i1 %27, label %34, label %28
28: ; preds = %23
%29 = sext i32 %24 to i64
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %29
%31 = call i64 @sprintf(i8* %30, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%32 = trunc i64 %31 to i32
%33 = add i32 %24, %32
br label %34
34: ; preds = %28, %23
%35 = phi i32 [ %33, %28 ], [ %24, %23 ]
%36 = icmp eq i8* %1, null
br i1 %36, label %43, label %37
37: ; preds = %34
%38 = sext i32 %35 to i64
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %38
%40 = sub nsw i32 1024, %35
%41 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %39, i32 %40, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %1)
%42 = add nsw i32 %41, %35
br label %43
43: ; preds = %37, %34
%44 = phi i32 [ %42, %37 ], [ %35, %34 ]
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %45
%47 = sub nsw i32 1024, %44
%48 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %46, i32 %47, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%49 = call i8* @strdup(i8* %4)
br label %50
50: ; preds = %2, %43
%51 = phi i8* [ %49, %43 ], [ null, %2 ]
ret i8* %51
}