#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 */
scalar_t__ _unescape(int /*<<< orphan*/, char const *);
int /*<<< orphan*/ free(char *);
char *malloc(scalar_t__);
scalar_t__ strlen(char const *);
char *unescape(const char *s) {
  char *buf, *bufp;
  const char *p = s;
  buf = malloc(strlen(s) + 1);
  if (!buf)
    return NULL;
  bufp = buf;
  while (*p) {
    if (*p == '%') {
      if (_unescape(bufp++, ++p)) {
        free(buf);
        return NULL;
      }
      p += 2;
    } else {
      *bufp++ = *p++;
    }
  }
  return buf;
}


x86 assembly (clang, -O0):
<unescape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1185 <unescape+0x45>
movq   $0x0,-0x8(%rbp)
jmp    122c <unescape+0xec>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
cmpb   $0x0,(%rax)
je     1224 <unescape+0xe4>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x25,%eax
jne    11fd <unescape+0xbd>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
mov    %eax,%edi
mov    -0x28(%rbp),%rsi
add    $0x1,%rsi
mov    %rsi,-0x28(%rbp)
call   1050 <_unescape@plt>
cmp    $0x0,%rax
je     11ec <unescape+0xac>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    122c <unescape+0xec>
mov    -0x28(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x28(%rbp)
jmp    121f <unescape+0xdf>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
jmp    118d <unescape+0x4d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unescape>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11a4 <unescape+0x64>
mov    %rax,%r14
xor    %ebx,%ebx
movzbl (%r15),%eax
cmp    $0x25,%al
je     1180 <unescape+0x40>
test   %al,%al
je     11a7 <unescape+0x67>
add    $0x1,%r15
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
jmp    1160 <unescape+0x20>
nopw   0x0(%rax,%rax,1)
lea    (%r14,%rbx,1),%edi
lea    0x1(%r15),%rsi
call   1050 <_unescape@plt>
test   %rax,%rax
jne    119c <unescape+0x5c>
add    $0x3,%r15
add    $0x1,%rbx
jmp    1160 <unescape+0x20>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @unescape(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
store i8* %7, i8** %6
%8 = load i8*, i8** %3
%9 = call i64 @strlen(i8* %8)
%10 = add nsw i64 %9, 1
%11 = call i8* @malloc(i64 %10)
store i8* %11, i8** %4
%12 = load i8*, i8** %4
%13 = icmp ne i8* %12, null
br i1 %13, label %15, label %14
14: ; preds = %1
store i8* null, i8** %2
br label %49
15: ; preds = %1
%16 = load i8*, i8** %4
store i8* %16, i8** %5
br label %17
17: ; preds = %46, %15
%18 = load i8*, i8** %6
%19 = load i8, i8* %18
%20 = icmp ne i8 %19, 0
br i1 %20, label %21, label %47
21: ; preds = %17
%22 = load i8*, i8** %6
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 37
br i1 %25, label %26, label %40
26: ; preds = %21
%27 = load i8*, i8** %5
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %5
%29 = ptrtoint i8* %27 to i32
%30 = load i8*, i8** %6
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %6
%32 = call i64 @_unescape(i32 %29, i8* %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %26
%35 = load i8*, i8** %4
%36 = call i32 @free(i8* %35)
store i8* null, i8** %2
br label %49
37: ; preds = %26
%38 = load i8*, i8** %6
%39 = getelementptr inbounds i8, i8* %38, i64 2
store i8* %39, i8** %6
br label %46
40: ; preds = %21
%41 = load i8*, i8** %6
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %6
%43 = load i8, i8* %41
%44 = load i8*, i8** %5
%45 = getelementptr inbounds i8, i8* %44, i32 1
store i8* %45, i8** %5
store i8 %43, i8* %44
br label %46
46: ; preds = %40, %37
br label %17
47: ; preds = %17
%48 = load i8*, i8** %4
store i8* %48, i8** %2
br label %49
49: ; preds = %47, %34, %14
%50 = load i8*, i8** %2
ret i8* %50
}

LLVM IR (-O2):
define i8* @unescape(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = add nsw i64 %2, 1
%4 = tail call i8* @malloc(i64 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %25, label %6
6: ; preds = %1, %22
%7 = phi i8* [ %24, %22 ], [ %4, %1 ]
%8 = phi i8* [ %23, %22 ], [ %0, %1 ]
%9 = load i8, i8* %8
switch i8 %9, label %20 [
i8 0, label %25
i8 37, label %10
]
10: ; preds = %6
%11 = ptrtoint i8* %7 to i64
%12 = trunc i64 %11 to i32
%13 = getelementptr inbounds i8, i8* %8, i64 1
%14 = tail call i64 @_unescape(i32 %12, i8* %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %10
%17 = tail call i32 @free(i8* %4)
br label %25
18: ; preds = %10
%19 = getelementptr inbounds i8, i8* %8, i64 3
br label %22
20: ; preds = %6
%21 = getelementptr inbounds i8, i8* %8, i64 1
store i8 %9, i8* %7
br label %22
22: ; preds = %20, %18
%23 = phi i8* [ %19, %18 ], [ %21, %20 ]
%24 = getelementptr inbounds i8, i8* %7, i64 1
br label %6
25: ; preds = %6, %1, %16
%26 = phi i8* [ null, %16 ], [ null, %1 ], [ %4, %6 ]
ret i8* %26
}