#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 sscanf(char const *, char *, unsigned int *);
char *strdup(char const *);
char *unescape(const char *str) {
  char *dup = strdup(str);
  const char *read = dup;
  char *write = dup;
  char c;
  while ((c = *read++)) {
    if (c == '%') {
      unsigned int x;
      int n = sscanf(read, "%2x", &x);
      if (n == 1) {
        read += 2;
      } else {
        return dup;
      }
      *write++ = x;
    } else if (c == '+') {
      *write++ = ' ';
    } else {
      *write++ = c;
    }
  }
  *write = 0;
  return dup;
}


x86 assembly (clang, -O0):
<unescape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
mov    (%rax),%al
mov    %al,-0x29(%rbp)
cmp    $0x0,%al
je     120e <unescape+0xee>
movsbl -0x29(%rbp),%eax
cmp    $0x25,%eax
jne    11cc <unescape+0xac>
mov    -0x20(%rbp),%rdi
lea    0xe83(%rip),%rsi
lea    -0x30(%rbp),%rdx
call   1030 <sscanf@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x1,-0x34(%rbp)
jne    11a4 <unescape+0x84>
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
jmp    11b1 <unescape+0x91>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    121d <unescape+0xfd>
mov    -0x30(%rbp),%eax
mov    %al,%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
jmp    1209 <unescape+0xe9>
movsbl -0x29(%rbp),%eax
cmp    $0x2b,%eax
jne    11f0 <unescape+0xd0>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x20,(%rax)
jmp    1204 <unescape+0xe4>
mov    -0x29(%rbp),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
jmp    1209 <unescape+0xe9>
jmp    1149 <unescape+0x29>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unescape>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
call   1040 <strdup@plt>
mov    %rax,%r14
lea    0xec4(%rip),%r15
lea    0xc(%rsp),%r12
mov    %rax,%r13
mov    %rax,%rbx
jmp    115c <unescape+0x3c>
nopl   0x0(%rax)
movb   $0x20,0x0(%r13)
add    $0x1,%r13
mov    %rdi,%rbx
lea    0x1(%rbx),%rdi
movzbl (%rbx),%eax
cmp    $0x25,%al
je     1180 <unescape+0x60>
cmp    $0x2b,%al
je     1150 <unescape+0x30>
test   %al,%al
je     11a3 <unescape+0x83>
mov    %al,0x0(%r13)
jmp    1155 <unescape+0x35>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rsi
mov    %r12,%rdx
call   1030 <sscanf@plt>
cmp    $0x1,%eax
jne    11a8 <unescape+0x88>
add    $0x3,%rbx
movzbl 0xc(%rsp),%eax
mov    %al,0x0(%r13)
add    $0x1,%r13
jmp    115c <unescape+0x3c>
movb   $0x0,0x0(%r13)
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [4 x i8] c"%2x\00"
define noalias i8* @unescape(i8* nocapture %0) {
%2 = alloca i32
%3 = tail call i8* @strdup(i8* %0)
%4 = bitcast i32* %2 to i8*
br label %5
5: ; preds = %20, %1
%6 = phi i8* [ %3, %1 ], [ %22, %20 ]
%7 = phi i8* [ %3, %1 ], [ %21, %20 ]
%8 = getelementptr inbounds i8, i8* %7, i64 1
%9 = load i8, i8* %7
switch i8 %9, label %19 [
i8 0, label %23
i8 37, label %10
i8 43, label %18
]
10: ; preds = %5
%11 = call i32 @sscanf(i8* %8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %2)
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = getelementptr inbounds i8, i8* %7, i64 3
%15 = load i32, i32* %2
%16 = trunc i32 %15 to i8
store i8 %16, i8* %6
br label %20
17: ; preds = %10
br label %24
18: ; preds = %5
store i8 32, i8* %6
br label %20
19: ; preds = %5
store i8 %9, i8* %6
br label %20
20: ; preds = %13, %18, %19
%21 = phi i8* [ %8, %18 ], [ %8, %19 ], [ %14, %13 ]
%22 = getelementptr inbounds i8, i8* %6, i64 1
br label %5
23: ; preds = %5
store i8 0, i8* %6
br label %24
24: ; preds = %17, %23
ret i8* %3
}