#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*/ memcpy(char *, char const *, scalar_t__);
int sscanf(char *, char *, char *);
scalar_t__ strlen(char const *);
char *xmalloc(scalar_t__);
char *utils_string_unescape(const char *str) {
  char *ns, *ps, *pd, c;
  ns = xmalloc(strlen(str) + 1);
  memcpy(ns, str, strlen(str) + 1);
  ps = pd = ns;
  while (*ps != '\0') {
    if (ps[0] == '%') {
      if (sscanf(ps, "%%%2hhX", &c) == 1) {
        pd[0] = c;
        ps += 3;
        pd++;
        continue;
      }
    }
    *pd = *ps;
    ps++;
    pd++;
  }
  pd[0] = '\0';
  return ns;
}


x86 assembly (clang, -O0):
<utils_string_unescape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1040 <xmalloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     122f <utils_string_unescape+0xef>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x25,%eax
jne    1206 <utils_string_unescape+0xc6>
mov    -0x18(%rbp),%rdi
lea    0xe37(%rip),%rsi
lea    -0x21(%rbp),%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
jne    1201 <utils_string_unescape+0xc1>
mov    -0x21(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    119e <utils_string_unescape+0x5e>
jmp    1206 <utils_string_unescape+0xc6>
mov    -0x18(%rbp),%rax
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    119e <utils_string_unescape+0x5e>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utils_string_unescape>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1030 <strlen@plt>
lea    0x1(%rax),%rdi
call   1040 <xmalloc@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <memcpy@plt>
lea    0xe83(%rip),%r15
lea    0x7(%rsp),%r12
mov    %r14,%rbx
mov    %r14,%r13
jmp    119f <utils_string_unescape+0x5f>
nopw   0x0(%rax,%rax,1)
test   %al,%al
je     11d5 <utils_string_unescape+0x95>
mov    %al,0x0(%r13)
add    %rbp,%rbx
add    $0x1,%r13
movzbl (%rbx),%eax
mov    $0x1,%ebp
cmp    $0x25,%al
jne    1190 <utils_string_unescape+0x50>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
jne    11d0 <utils_string_unescape+0x90>
mov    $0x3,%ebp
movzbl 0x7(%rsp),%eax
jmp    1194 <utils_string_unescape+0x54>
nopw   0x0(%rax,%rax,1)
movzbl (%rbx),%eax
jmp    1194 <utils_string_unescape+0x54>
movb   $0x0,0x0(%r13)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"%%%2hhX\00"
define i8* @utils_string_unescape(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i64 @strlen(i8* %7)
%9 = add nsw i64 %8, 1
%10 = call i8* @xmalloc(i64 %9)
store i8* %10, i8** %3
%11 = load i8*, i8** %3
%12 = load i8*, i8** %2
%13 = load i8*, i8** %2
%14 = call i64 @strlen(i8* %13)
%15 = add nsw i64 %14, 1
%16 = call i32 @memcpy(i8* %11, i8* %12, i64 %15)
%17 = load i8*, i8** %3
store i8* %17, i8** %5
store i8* %17, i8** %4
br label %18
18: ; preds = %42, %33, %1
%19 = load i8*, i8** %4
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %50
23: ; preds = %18
%24 = load i8*, i8** %4
%25 = getelementptr inbounds i8, i8* %24, i64 0
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 37
br i1 %28, label %29, label %42
29: ; preds = %23
%30 = load i8*, i8** %4
%31 = call i32 @sscanf(i8* %30, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %6)
%32 = icmp eq i32 %31, 1
br i1 %32, label %33, label %41
33: ; preds = %29
%34 = load i8, i8* %6
%35 = load i8*, i8** %5
%36 = getelementptr inbounds i8, i8* %35, i64 0
store i8 %34, i8* %36
%37 = load i8*, i8** %4
%38 = getelementptr inbounds i8, i8* %37, i64 3
store i8* %38, i8** %4
%39 = load i8*, i8** %5
%40 = getelementptr inbounds i8, i8* %39, i32 1
store i8* %40, i8** %5
br label %18
41: ; preds = %29
br label %42
42: ; preds = %41, %23
%43 = load i8*, i8** %4
%44 = load i8, i8* %43
%45 = load i8*, i8** %5
store i8 %44, i8* %45
%46 = load i8*, i8** %4
%47 = getelementptr inbounds i8, i8* %46, i32 1
store i8* %47, i8** %4
%48 = load i8*, i8** %5
%49 = getelementptr inbounds i8, i8* %48, i32 1
store i8* %49, i8** %5
br label %18
50: ; preds = %18
%51 = load i8*, i8** %5
%52 = getelementptr inbounds i8, i8* %51, i64 0
store i8 0, i8* %52
%53 = load i8*, i8** %3
ret i8* %53
}

LLVM IR (-O2):
@.str = [8 x i8] c"%%%2hhX\00"
define i8* @utils_string_unescape(i8* %0) {
%2 = alloca i8
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = add nsw i64 %3, 1
%5 = tail call i8* @xmalloc(i64 %4)
%6 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%7 = add nsw i64 %6, 1
%8 = tail call i32 @memcpy(i8* %5, i8* %0, i64 %7)
br label %9
9: ; preds = %20, %1
%10 = phi i8* [ %5, %1 ], [ %23, %20 ]
%11 = phi i8* [ %5, %1 ], [ %24, %20 ]
%12 = load i8, i8* %10
switch i8 %12, label %20 [
i8 0, label %25
i8 37, label %13
]
13: ; preds = %9
%14 = call i32 @sscanf(i8* %10, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %2)
%15 = icmp eq i32 %14, 1
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = load i8, i8* %10
br label %20
18: ; preds = %13
%19 = load i8, i8* %2
br label %20
20: ; preds = %9, %16, %18
%21 = phi i8 [ %19, %18 ], [ %17, %16 ], [ %12, %9 ]
%22 = phi i64 [ 3, %18 ], [ 1, %16 ], [ 1, %9 ]
store i8 %21, i8* %11
%23 = getelementptr inbounds i8, i8* %10, i64 %22
%24 = getelementptr inbounds i8, i8* %11, i64 1
br label %9
25: ; preds = %9
store i8 0, i8* %11
ret i8* %5
}