#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*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char *, ...);
char *strchr(char *, char);
int strlen(char *);
char *strstr(char *, char *);
void store_cluster_attr(char **metric, char *name, char *value) {
  char *pos = NULL, *rest;
  char *pattern;
  char *new_metric = NULL;
  int len;
  pattern = malloc(strlen(name) + 2);
  sprintf(pattern, "%s=", name);
  if (*metric)
    pos = strstr(*metric, pattern);
  if (pos) {
    rest = strchr(pos, ';');
    pos[0] = 0;
    if (rest == NULL)
      rest = "";
    new_metric = malloc(strlen(*metric) + strlen(rest) + 1);
    sprintf(new_metric, "%s%s", *metric, rest);
    free(*metric);
  } else
    (new_metric = *metric);
  len = new_metric ? (strlen(new_metric) + strlen(";")) : 0;
  len += strlen(name) + strlen("=") + strlen(value);
  *metric = malloc(len + 1);
  sprintf(*metric, "%s%s%s=%s", new_metric ? new_metric : "",
          new_metric ? ";" : "", name, value);
  free(new_metric);
  free(pattern);
}


x86 assembly (clang, -O0):
<store_cluster_attr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
add    $0x2,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe56(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
je     11d3 <store_cluster_attr+0x73>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x30(%rbp),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1264 <store_cluster_attr+0x104>
mov    -0x20(%rbp),%rdi
mov    $0x3b,%esi
call   1050 <strchr@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
cmpq   $0x0,-0x28(%rbp)
jne    120d <store_cluster_attr+0xad>
lea    0xe01(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x40(%rbp),%edi
add    %eax,%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rdx
mov    -0x28(%rbp),%rcx
lea    0xdb8(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
jmp    126f <store_cluster_attr+0x10f>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     12a1 <store_cluster_attr+0x141>
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x48(%rbp)
lea    0xd7c(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x44(%rbp)
jmp    12ab <store_cluster_attr+0x14b>
xor    %eax,%eax
mov    %eax,-0x44(%rbp)
jmp    12ab <store_cluster_attr+0x14b>
mov    -0x44(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x58(%rbp)
lea    0xd3e(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x58(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x54(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x54(%rbp),%eax
add    %ecx,%eax
add    -0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1321 <store_cluster_attr+0x1c1>
mov    -0x38(%rbp),%rax
mov    %rax,-0x60(%rbp)
jmp    1331 <store_cluster_attr+0x1d1>
lea    0xce2(%rip),%rax
mov    %rax,-0x60(%rbp)
jmp    1331 <store_cluster_attr+0x1d1>
mov    -0x50(%rbp),%rdi
mov    -0x60(%rbp),%rdx
mov    -0x38(%rbp),%rsi
lea    0xcc6(%rip),%rcx
lea    0xcbe(%rip),%rax
cmp    $0x0,%rsi
cmovne %rax,%rcx
mov    -0x10(%rbp),%r8
mov    -0x18(%rbp),%r9
lea    0xca9(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<store_cluster_attr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,0x20(%rsp)
mov    %rsi,%r12
mov    %rdi,%r13
mov    %rsi,%rdi
call   1040 <strlen@plt>
lea    0x2(%rax),%edi
call   1060 <malloc@plt>
mov    %rax,%r14
lea    0xe6d(%rip),%rsi
mov    %rax,%rdi
mov    %r12,%rdx
xor    %eax,%eax
call   1070 <sprintf@plt>
mov    0x0(%r13),%rbx
test   %rbx,%rbx
mov    %r14,0x18(%rsp)
mov    %r13,0x10(%rsp)
je     125c <store_cluster_attr+0xfc>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
je     1233 <store_cluster_attr+0xd3>
mov    %rax,%rbp
mov    %rax,%rdi
mov    $0x3b,%esi
call   1050 <strchr@plt>
mov    %rax,%r15
movb   $0x0,0x0(%rbp)
test   %rax,%rax
lea    0xe22(%rip),%rbp
cmove  %rbp,%r15
mov    0x0(%r13),%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    0x0(%r13),%rdx
lea    0xdec(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rcx
xor    %eax,%eax
call   1070 <sprintf@plt>
mov    0x0(%r13),%rdi
call   1030 <free@plt>
test   %rbx,%rbx
je     1270 <store_cluster_attr+0x110>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
lea    0xdc5(%rip),%rdi
mov    %rdi,0x8(%rsp)
call   1040 <strlen@plt>
lea    (%rax,%rbp,1),%r15d
add    $0x1,%r15d
mov    %rbx,(%rsp)
jmp    1281 <store_cluster_attr+0x121>
lea    0xda7(%rip),%rax
xor    %ebx,%ebx
mov    %rax,0x8(%rsp)
mov    %rax,(%rsp)
jmp    127b <store_cluster_attr+0x11b>
xor    %ebx,%ebx
mov    %rbp,0x8(%rsp)
mov    %rbp,(%rsp)
mov    $0x1,%r15d
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
lea    0xd70(%rip),%rdi
call   1040 <strlen@plt>
mov    %r12,%r14
mov    %eax,%r12d
mov    0x20(%rsp),%r13
mov    %r13,%rdi
call   1040 <strlen@plt>
add    %r15d,%ebp
add    %r12d,%ebp
add    %eax,%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    0x10(%rsp),%rcx
mov    %rax,(%rcx)
lea    0xd43(%rip),%rsi
mov    %rax,%rdi
mov    (%rsp),%rdx
mov    0x8(%rsp),%rcx
mov    %r14,%r8
mov    %r13,%r9
xor    %eax,%eax
call   1070 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x18(%rsp),%rdi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"%s=\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [5 x i8] c"%s%s\00"
@.str.3 = [2 x i8] c";\00"
@.str.4 = [2 x i8] c"=\00"
@.str.5 = [10 x i8] c"%s%s%s=%s\00"
define void @store_cluster_attr(i8** %0, i8* %1, i8* %2) {
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
store i8** %0, i8*** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
store i8* null, i8** %7
store i8* null, i8** %10
%12 = load i8*, i8** %5
%13 = call i32 @strlen(i8* %12)
%14 = add nsw i32 %13, 2
%15 = call i8* @malloc(i32 %14)
store i8* %15, i8** %9
%16 = load i8*, i8** %9
%17 = load i8*, i8** %5
%18 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i8**, i8*** %4
%20 = load i8*, i8** %19
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %27
22: ; preds = %3
%23 = load i8**, i8*** %4
%24 = load i8*, i8** %23
%25 = load i8*, i8** %9
%26 = call i8* @strstr(i8* %24, i8* %25)
store i8* %26, i8** %7
br label %27
27: ; preds = %22, %3
%28 = load i8*, i8** %7
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %55
30: ; preds = %27
%31 = load i8*, i8** %7
%32 = call i8* @strchr(i8* %31, i8 signext 59)
store i8* %32, i8** %8
%33 = load i8*, i8** %7
%34 = getelementptr inbounds i8, i8* %33, i64 0
store i8 0, i8* %34
%35 = load i8*, i8** %8
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %38
37: ; preds = %30
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8** %8
br label %38
38: ; preds = %37, %30
%39 = load i8**, i8*** %4
%40 = load i8*, i8** %39
%41 = call i32 @strlen(i8* %40)
%42 = load i8*, i8** %8
%43 = call i32 @strlen(i8* %42)
%44 = add nsw i32 %41, %43
%45 = add nsw i32 %44, 1
%46 = call i8* @malloc(i32 %45)
store i8* %46, i8** %10
%47 = load i8*, i8** %10
%48 = load i8**, i8*** %4
%49 = load i8*, i8** %48
%50 = load i8*, i8** %8
%51 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %47, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %49, i8* %50)
%52 = load i8**, i8*** %4
%53 = load i8*, i8** %52
%54 = call i32 @free(i8* %53)
br label %58
55: ; preds = %27
%56 = load i8**, i8*** %4
%57 = load i8*, i8** %56
store i8* %57, i8** %10
br label %58
58: ; preds = %55, %38
%59 = load i8*, i8** %10
%60 = icmp ne i8* %59, null
br i1 %60, label %61, label %66
61: ; preds = %58
%62 = load i8*, i8** %10
%63 = call i32 @strlen(i8* %62)
%64 = call i32 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%65 = add nsw i32 %63, %64
br label %67
66: ; preds = %58
br label %67
67: ; preds = %66, %61
%68 = phi i32 [ %65, %61 ], [ 0, %66 ]
store i32 %68, i32* %11
%69 = load i8*, i8** %5
%70 = call i32 @strlen(i8* %69)
%71 = call i32 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%72 = add nsw i32 %70, %71
%73 = load i8*, i8** %6
%74 = call i32 @strlen(i8* %73)
%75 = add nsw i32 %72, %74
%76 = load i32, i32* %11
%77 = add nsw i32 %76, %75
store i32 %77, i32* %11
%78 = load i32, i32* %11
%79 = add nsw i32 %78, 1
%80 = call i8* @malloc(i32 %79)
%81 = load i8**, i8*** %4
store i8* %80, i8** %81
%82 = load i8**, i8*** %4
%83 = load i8*, i8** %82
%84 = load i8*, i8** %10
%85 = icmp ne i8* %84, null
br i1 %85, label %86, label %88
86: ; preds = %67
%87 = load i8*, i8** %10
br label %89
88: ; preds = %67
br label %89
89: ; preds = %88, %86
%90 = phi i8* [ %87, %86 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), %88 ]
%91 = load i8*, i8** %10
%92 = icmp ne i8* %91, null
%93 = zext i1 %92 to i64
%94 = select i1 %92, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0)
%95 = load i8*, i8** %5
%96 = load i8*, i8** %6
%97 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %83, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %90, i8* %94, i8* %95, i8* %96)
%98 = load i8*, i8** %10
%99 = call i32 @free(i8* %98)
%100 = load i8*, i8** %9
%101 = call i32 @free(i8* %100)
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"%s=\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [5 x i8] c"%s%s\00"
@.str.3 = [2 x i8] c";\00"
@.str.4 = [2 x i8] c"=\00"
@.str.5 = [10 x i8] c"%s%s%s=%s\00"
define void @store_cluster_attr(i8** nocapture %0, i8* %1, i8* %2) {
%4 = tail call i32 @strlen(i8* %1)
%5 = add nsw i32 %4, 2
%6 = tail call i8* @malloc(i32 %5)
%7 = tail call i32 (i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %6, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %1)
%8 = load i8*, i8** %0
%9 = icmp eq i8* %8, null
br i1 %9, label %34, label %10
10: ; preds = %3
%11 = tail call i8* @strstr(i8* %8, i8* dereferenceable(1) %6)
%12 = icmp eq i8* %11, null
br i1 %12, label %28, label %13
13: ; preds = %10
%14 = tail call i8* @strchr(i8* %11, i8 signext 59)
store i8 0, i8* %11
%15 = icmp eq i8* %14, null
%16 = select i1 %15, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8* %14
%17 = load i8*, i8** %0
%18 = tail call i32 @strlen(i8* %17)
%19 = tail call i32 @strlen(i8* %16)
%20 = add i32 %18, 1
%21 = add i32 %20, %19
%22 = tail call i8* @malloc(i32 %21)
%23 = load i8*, i8** %0
%24 = tail call i32 (i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %22, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %23, i8* %16)
%25 = load i8*, i8** %0
%26 = tail call i32 @free(i8* %25)
%27 = icmp eq i8* %22, null
br i1 %27, label %34, label %28
28: ; preds = %10, %13
%29 = phi i8* [ %22, %13 ], [ %8, %10 ]
%30 = tail call i32 @strlen(i8* %29)
%31 = tail call i32 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%32 = add nsw i32 %31, %30
%33 = add i32 %32, 1
br label %34
34: ; preds = %3, %13, %28
%35 = phi i8* [ null, %13 ], [ %29, %28 ], [ null, %3 ]
%36 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), %13 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), %28 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), %3 ]
%37 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), %13 ], [ %29, %28 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), %3 ]
%38 = phi i32 [ 1, %13 ], [ %33, %28 ], [ 1, %3 ]
%39 = tail call i32 @strlen(i8* %1)
%40 = tail call i32 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%41 = tail call i32 @strlen(i8* %2)
%42 = add i32 %38, %39
%43 = add i32 %42, %40
%44 = add i32 %43, %41
%45 = tail call i8* @malloc(i32 %44)
store i8* %45, i8** %0
%46 = tail call i32 (i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %45, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %37, i8* %36, i8* %1, i8* %2)
%47 = tail call i32 @free(i8* %35)
%48 = tail call i32 @free(i8* %6)
ret void
}