#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*/ isxdigit(char);
int /*<<< orphan*/ memmove(char *, char *, scalar_t__);
int /*<<< orphan*/ sscanf(char *, char *, int *);
scalar_t__ strlen(char *);
bool percent_decode(char *s) {
  if (!s)
    return true;
  while (*s) {
    if (*s == '+') {
      *s = ' ';
    } else if (*s == '%') {
      int c;
      if (!isxdigit(*(s + 1)) || !isxdigit(*(s + 2)) ||
          !sscanf(s + 1, "%2x", &c)) {
        return false;
      } else {
        *s = c;
        memmove(s + 1, s + 3, strlen(s) - 1);
      }
    }
    ++s;
  }
  return true;
}


x86 assembly (clang, -O0):
<percent_decode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1163 <percent_decode+0x23>
movl   $0x1,-0x4(%rbp)
jmp    125c <percent_decode+0x11c>
jmp    1168 <percent_decode+0x28>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1255 <percent_decode+0x115>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2b,%eax
jne    1191 <percent_decode+0x51>
mov    -0x10(%rbp),%rax
movb   $0x20,(%rax)
jmp    1244 <percent_decode+0x104>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x25,%eax
jne    123f <percent_decode+0xff>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%edi
call   1060 <isxdigit@plt>
cmp    $0x0,%eax
je     11ee <percent_decode+0xae>
mov    -0x10(%rbp),%rax
movsbl 0x2(%rax),%edi
call   1060 <isxdigit@plt>
cmp    $0x0,%eax
je     11ee <percent_decode+0xae>
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
lea    0xe24(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1040 <sscanf@plt>
cmp    $0x0,%eax
jne    11fa <percent_decode+0xba>
movl   $0x0,-0x4(%rbp)
jmp    125c <percent_decode+0x11c>
mov    -0x14(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    %rax,%rdx
sub    $0x1,%rdx
call   1050 <memmove@plt>
jmp    123f <percent_decode+0xff>
jmp    1244 <percent_decode+0x104>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1168 <percent_decode+0x28>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<percent_decode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x1,%r14d
test   %rdi,%rdi
je     11eb <percent_decode+0xab>
mov    %rdi,%rbx
lea    0xe9c(%rip),%r15
lea    0x4(%rsp),%r12
jmp    1177 <percent_decode+0x37>
nopl   0x0(%rax,%rax,1)
movb   $0x20,(%rbx)
add    $0x1,%rbx
movzbl (%rbx),%eax
cmp    $0x25,%al
je     1190 <percent_decode+0x50>
cmp    $0x2b,%al
je     1170 <percent_decode+0x30>
test   %al,%al
jne    11e2 <percent_decode+0xa2>
jmp    11eb <percent_decode+0xab>
nopl   0x0(%rax,%rax,1)
movsbl 0x1(%rbx),%edi
call   1060 <isxdigit@plt>
test   %eax,%eax
je     11e8 <percent_decode+0xa8>
movsbl 0x2(%rbx),%edi
call   1060 <isxdigit@plt>
test   %eax,%eax
je     11e8 <percent_decode+0xa8>
lea    0x1(%rbx),%rbp
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1040 <sscanf@plt>
test   %eax,%eax
je     11e8 <percent_decode+0xa8>
movzbl 0x4(%rsp),%eax
mov    %al,(%rbx)
lea    0x3(%rbx),%r13
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    -0x1(%rax),%rdx
mov    %rbp,%rdi
mov    %r13,%rsi
call   1050 <memmove@plt>
add    $0x1,%rbx
jmp    1177 <percent_decode+0x37>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [4 x i8] c"%2x\00"
define i32 @percent_decode(i8* %0) {
%2 = alloca i32
%3 = icmp eq i8* %0, null
br i1 %3, label %33, label %4
4: ; preds = %1
%5 = bitcast i32* %2 to i8*
br label %6
6: ; preds = %4, %31
%7 = phi i8* [ %32, %31 ], [ %0, %4 ]
%8 = load i8, i8* %7
switch i8 %8, label %31 [
i8 0, label %33
i8 43, label %9
i8 37, label %10
]
9: ; preds = %6
store i8 32, i8* %7
br label %31
10: ; preds = %6
%11 = getelementptr inbounds i8, i8* %7, i64 1
%12 = load i8, i8* %11
%13 = call i32 @isxdigit(i8 signext %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %23, label %15
15: ; preds = %10
%16 = getelementptr inbounds i8, i8* %7, i64 2
%17 = load i8, i8* %16
%18 = call i32 @isxdigit(i8 signext %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %15
%21 = call i32 @sscanf(i8* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %2)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %20, %15, %10
br label %33
24: ; preds = %20
%25 = load i32, i32* %2
%26 = trunc i32 %25 to i8
store i8 %26, i8* %7
%27 = getelementptr inbounds i8, i8* %7, i64 3
%28 = call i64 @strlen(i8* %7)
%29 = add nsw i64 %28, -1
%30 = call i32 @memmove(i8* %11, i8* %27, i64 %29)
br label %31
31: ; preds = %24, %6, %9
%32 = getelementptr inbounds i8, i8* %7, i64 1
br label %6
33: ; preds = %6, %23, %1
%34 = phi i32 [ 1, %1 ], [ 0, %23 ], [ 1, %6 ]
ret i32 %34
}