#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;
};

/* Variables and functions */
scalar_t__ snprintf(char *, int, char *, char *);
int sprintf(char *, char *, ...);
char *strdup(char *);
char *format_raw_event(struct perf_event_attr *attr, char *name) {
  char buf[1024];
  int off = 0;
  off = sprintf(buf, "cpu/config=%#llx", attr->config);
  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 += sprintf(buf + off, "/");
  return strdup(buf);
}


x86 assembly (clang, -O0):
<format_raw_event>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x414(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%edx
lea    0xe9e(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %eax,-0x414(%rbp)
mov    -0x8(%rbp),%rax
cmpl   $0x0,0x4(%rax)
je     11af <format_raw_event+0x7f>
lea    -0x410(%rbp),%rdi
movslq -0x414(%rbp),%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edx
lea    0xe75(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
add    -0x414(%rbp),%eax
mov    %eax,-0x414(%rbp)
mov    -0x8(%rbp),%rax
cmpl   $0x0,(%rax)
je     11ed <format_raw_event+0xbd>
lea    -0x410(%rbp),%rdi
movslq -0x414(%rbp),%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%edx
lea    0xe46(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
add    -0x414(%rbp),%eax
mov    %eax,-0x414(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1237 <format_raw_event+0x107>
lea    -0x410(%rbp),%rdi
movslq -0x414(%rbp),%rax
add    %rax,%rdi
mov    $0x400,%esi
sub    -0x414(%rbp),%esi
mov    -0x10(%rbp),%rcx
lea    0xe10(%rip),%rdx
call   1030 <snprintf@plt>
mov    %rax,%rcx
movslq -0x414(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x414(%rbp)
lea    -0x410(%rbp),%rdi
movslq -0x414(%rbp),%rax
add    %rax,%rdi
lea    0xde9(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
add    -0x414(%rbp),%eax
mov    %eax,-0x414(%rbp)
lea    -0x410(%rbp),%rdi
call   1050 <strdup@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_raw_event>:
push   %r15
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x8(%rdi),%edx
lea    0xeb4(%rip),%rsi
mov    %rsp,%rdi
xor    %eax,%eax
call   1040 <sprintf@plt>
mov    %eax,%ebx
mov    0x4(%r15),%edx
test   %edx,%edx
je     1177 <format_raw_event+0x47>
movslq %ebx,%rax
lea    (%rsp,%rax,1),%rdi
lea    0xea3(%rip),%rsi
xor    %eax,%eax
call   1040 <sprintf@plt>
add    %eax,%ebx
mov    (%r15),%edx
test   %edx,%edx
je     1195 <format_raw_event+0x65>
movslq %ebx,%rbx
lea    (%rsp,%rbx,1),%rdi
lea    0xe94(%rip),%rsi
xor    %eax,%eax
call   1040 <sprintf@plt>
add    %eax,%ebx
test   %r14,%r14
je     11be <format_raw_event+0x8e>
movslq %ebx,%r15
lea    (%rsp,%r15,1),%rdi
mov    $0x400,%esi
sub    %r15d,%esi
lea    0xe7f(%rip),%rdx
mov    %r14,%rcx
call   1030 <snprintf@plt>
mov    %rax,%rbx
add    %r15d,%ebx
movslq %ebx,%rax
movw   $0x2f,(%rsp,%rax,1)
mov    %rsp,%rdi
call   1050 <strdup@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.perf_event_attr = type { i32, i32, i32 }
@.str = [17 x i8] c"cpu/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 %struct.perf_event_attr*
%4 = alloca i8*
%5 = alloca [1024 x i8]
%6 = alloca i32
store %struct.perf_event_attr* %0, %struct.perf_event_attr** %3
store i8* %1, i8** %4
store i32 0, i32* %6
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%8 = load %struct.perf_event_attr*, %struct.perf_event_attr** %3
%9 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %8, i32 0, i32 2
%10 = load i32, i32* %9
%11 = call i32 (i8*, i8*, ...) @sprintf(i8* %7, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %6
%12 = load %struct.perf_event_attr*, %struct.perf_event_attr** %3
%13 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %12, i32 0, i32 1
%14 = load i32, i32* %13
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %27
16: ; preds = %2
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%18 = load i32, i32* %6
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load %struct.perf_event_attr*, %struct.perf_event_attr** %3
%22 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %21, i32 0, i32 1
%23 = load i32, i32* %22
%24 = call i32 (i8*, i8*, ...) @sprintf(i8* %20, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = load i32, i32* %6
%26 = add nsw i32 %25, %24
store i32 %26, i32* %6
br label %27
27: ; preds = %16, %2
%28 = load %struct.perf_event_attr*, %struct.perf_event_attr** %3
%29 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %43
32: ; preds = %27
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%34 = load i32, i32* %6
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = load %struct.perf_event_attr*, %struct.perf_event_attr** %3
%38 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = call i32 (i8*, i8*, ...) @sprintf(i8* %36, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %39)
%41 = load i32, i32* %6
%42 = add nsw i32 %41, %40
store i32 %42, i32* %6
br label %43
43: ; preds = %32, %27
%44 = load i8*, i8** %4
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %59
46: ; preds = %43
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%48 = load i32, i32* %6
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
%51 = load i32, i32* %6
%52 = sub nsw i32 1024, %51
%53 = load i8*, i8** %4
%54 = call i64 @snprintf(i8* %50, i32 %52, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %53)
%55 = load i32, i32* %6
%56 = sext i32 %55 to i64
%57 = add nsw i64 %56, %54
%58 = trunc i64 %57 to i32
store i32 %58, i32* %6
br label %59
59: ; preds = %46, %43
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%61 = load i32, i32* %6
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %60, i64 %62
%64 = call i32 (i8*, i8*, ...) @sprintf(i8* %63, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%65 = load i32, i32* %6
%66 = add nsw i32 %65, %64
store i32 %66, i32* %6
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%68 = call i8* @strdup(i8* %67)
ret i8* %68
}

LLVM IR (-O2):
%struct.perf_event_attr = type { i32, i32, i32 }
@.str = [17 x i8] c"cpu/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"
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 2
%6 = load i32, i32* %5
%7 = call i32 (i8*, i8*, ...) @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %0, i64 0, i32 1
%9 = load i32, i32* %8
%10 = icmp eq i32 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %2
%12 = sext i32 %7 to i64
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %12
%14 = call i32 (i8*, i8*, ...) @sprintf(i8* %13, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%15 = add nsw i32 %14, %7
br label %16
16: ; preds = %11, %2
%17 = phi i32 [ %15, %11 ], [ %7, %2 ]
%18 = getelementptr inbounds %struct.perf_event_attr, %struct.perf_event_attr* %0, i64 0, i32 0
%19 = load i32, i32* %18
%20 = icmp eq i32 %19, 0
br i1 %20, label %26, label %21
21: ; preds = %16
%22 = sext i32 %17 to i64
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %22
%24 = call i32 (i8*, i8*, ...) @sprintf(i8* %23, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%25 = add nsw i32 %24, %17
br label %26
26: ; preds = %21, %16
%27 = phi i32 [ %25, %21 ], [ %17, %16 ]
%28 = icmp eq i8* %1, null
br i1 %28, label %36, label %29
29: ; preds = %26
%30 = sext i32 %27 to i64
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %30
%32 = sub nsw i32 1024, %27
%33 = call i64 @snprintf(i8* %31, i32 %32, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %1)
%34 = trunc i64 %33 to i32
%35 = add i32 %27, %34
br label %36
36: ; preds = %29, %26
%37 = phi i32 [ %35, %29 ], [ %27, %26 ]
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %38
%40 = bitcast i8* %39 to i16*
store i16 47, i16* %40
%41 = call i8* @strdup(i8* %4)
ret i8* %41
}