#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 hotplug2_event_t {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *get_hotplug2_value_by_key(struct hotplug2_event_t *, char *);
int /*<<< orphan*/ memcpy(char *, char *, int);
char *replace_str(char *, char *, char *);
char *strchr(char *, char);
char *xmalloc(int);
char *replace_key_by_value(char *hay, struct hotplug2_event_t *event) {
  char *sptr = hay, *ptr = hay;
  char *buf, *replacement;
  while ((sptr = strchr(sptr, '%')) != NULL) {
    ptr = strchr(sptr + 1, '%');
    if (ptr != NULL) {
      buf = xmalloc(ptr - sptr + 2);
      buf[ptr - sptr + 1] = '\0';
      memcpy(buf, sptr, ptr - sptr + 1);
      buf[ptr - sptr] = '\0';
      replacement = get_hotplug2_value_by_key(event, &buf[1]);
      buf[ptr - sptr] = '%';
      if (replacement != NULL) {
        hay = replace_str(hay, buf, replacement);
        sptr = hay;
      } else {
        sptr++;
      }
      free(buf);
    } else {
      sptr++;
    }
  }
  hay = replace_str(hay, "%\\", "%");
  return hay;
}


x86 assembly (clang, -O0):
<replace_key_by_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x25,%esi
call   1040 <strchr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     129c <replace_key_by_value+0x13c>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
mov    $0x25,%esi
call   1040 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     128b <replace_key_by_value+0x12b>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
add    $0x2,%rax
mov    %eax,%edi
call   1050 <xmalloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x0,0x1(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
add    $0x1,%rsi
call   1070 <get_hotplug2_value_by_key@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x25,(%rax,%rcx,1)
cmpq   $0x0,-0x30(%rbp)
je     1271 <replace_key_by_value+0x111>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1080 <replace_str@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    127d <replace_key_by_value+0x11d>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    1297 <replace_key_by_value+0x137>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1180 <replace_key_by_value+0x20>
mov    -0x8(%rbp),%rdi
lea    0xd59(%rip),%rsi
lea    0xd55(%rip),%rdx
call   1080 <replace_str@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_key_by_value>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x25,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     121e <replace_key_by_value+0xbe>
mov    %rax,%rbx
jmp    11ad <replace_key_by_value+0x4d>
nopl   0x0(%rax)
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
mov    $0x25,%esi
call   1040 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     121e <replace_key_by_value+0xbe>
lea    0x1(%rbx),%r12
mov    %r12,%rdi
mov    $0x25,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     1198 <replace_key_by_value+0x38>
mov    %rax,%rbp
sub    %rbx,%rbp
lea    0x2(%rbp),%edi
call   1050 <xmalloc@plt>
mov    %rax,%r13
lea    0x1(%rbp),%rdx
movb   $0x0,0x1(%rax,%rbp,1)
mov    %rax,%rdi
mov    %rbx,%rsi
call   1060 <memcpy@plt>
movb   $0x0,0x0(%r13,%rbp,1)
lea    0x1(%r13),%rsi
mov    %r14,%rdi
call   1070 <get_hotplug2_value_by_key@plt>
movb   $0x25,0x0(%r13,%rbp,1)
test   %rax,%rax
je     1190 <replace_key_by_value+0x30>
mov    %r15,%rdi
mov    %r13,%rsi
mov    %rax,%rdx
call   1080 <replace_str@plt>
mov    %rax,%r12
mov    %rax,%r15
jmp    1190 <replace_key_by_value+0x30>
lea    0xddb(%rip),%rsi
lea    0xdd7(%rip),%rdx
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <replace_str@plt>

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

LLVM IR (-O2):
%struct.hotplug2_event_t = type { i32 }
@.str = [3 x i8] c"%\\\00"
@.str.1 = [2 x i8] c"%\00"
define i8* @replace_key_by_value(i8* %0, %struct.hotplug2_event_t* %1) {
%3 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 37)
%4 = icmp eq i8* %3, null
br i1 %4, label %37, label %5
5: ; preds = %2, %32
%6 = phi i8* [ %35, %32 ], [ %3, %2 ]
%7 = phi i8* [ %34, %32 ], [ %0, %2 ]
%8 = getelementptr inbounds i8, i8* %6, i64 1
%9 = tail call i8* @strchr(i8* %8, i8 signext 37)
%10 = icmp eq i8* %9, null
br i1 %10, label %32, label %11
11: ; preds = %5
%12 = ptrtoint i8* %9 to i64
%13 = ptrtoint i8* %6 to i64
%14 = sub i64 %12, %13
%15 = trunc i64 %14 to i32
%16 = add i32 %15, 2
%17 = tail call i8* @xmalloc(i32 %16)
%18 = add nsw i64 %14, 1
%19 = getelementptr inbounds i8, i8* %17, i64 %18
store i8 0, i8* %19
%20 = trunc i64 %18 to i32
%21 = tail call i32 @memcpy(i8* %17, i8* %6, i32 %20)
%22 = getelementptr inbounds i8, i8* %17, i64 %14
store i8 0, i8* %22
%23 = getelementptr inbounds i8, i8* %17, i64 1
%24 = tail call i8* @get_hotplug2_value_by_key(%struct.hotplug2_event_t* %1, i8* %23)
store i8 37, i8* %22
%25 = icmp eq i8* %24, null
br i1 %25, label %28, label %26
26: ; preds = %11
%27 = tail call i8* @replace_str(i8* %7, i8* %17, i8* %24)
br label %28
28: ; preds = %11, %26
%29 = phi i8* [ %27, %26 ], [ %8, %11 ]
%30 = phi i8* [ %27, %26 ], [ %7, %11 ]
%31 = tail call i32 @free(i8* %17)
br label %32
32: ; preds = %5, %28
%33 = phi i8* [ %29, %28 ], [ %8, %5 ]
%34 = phi i8* [ %30, %28 ], [ %7, %5 ]
%35 = tail call i8* @strchr(i8* dereferenceable(1) %33, i8 signext 37)
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %5
37: ; preds = %32, %2
%38 = phi i8* [ %0, %2 ], [ %34, %32 ]
%39 = tail call i8* @replace_str(i8* %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
ret i8* %39
}