#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 */
char *malloc(scalar_t__);
scalar_t__ strlen(char const *);
char xtoi(char const *);
char *url_decode(const char *s) {
  char *buf = malloc(strlen(s) + 1);
  char *q = buf;
  if (buf) {
    while (*s) {
      switch (*s) {
      case '+':
        s++;
        *q++ = ' ';
        break;
      case '%':
        if ((*q = xtoi(s + 1)) != '\0') {
          s += 3;
          q++;
        } else {
          *q++ = *s++;
        }
        break;
      default:
        *q++ = *s++;
        break;
      }
    }
    *q = '\0';
  }
  return (buf);
}


x86 assembly (clang, -O0):
<url_decode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1257 <url_decode+0x127>
jmp    116d <url_decode+0x3d>
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     1250 <url_decode+0x120>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x1c(%rbp)
sub    $0x25,%eax
je     11c6 <url_decode+0x96>
jmp    1192 <url_decode+0x62>
mov    -0x1c(%rbp),%eax
sub    $0x2b,%eax
jne    1229 <url_decode+0xf9>
jmp    11a3 <url_decode+0x73>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x20,(%rax)
jmp    124b <url_decode+0x11b>
mov    -0x8(%rbp),%rdi
add    $0x1,%rdi
call   1050 <xtoi@plt>
mov    -0x18(%rbp),%rcx
mov    %al,(%rcx)
movsbl %al,%eax
cmp    $0x0,%eax
je     1202 <url_decode+0xd2>
mov    -0x8(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1224 <url_decode+0xf4>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    124b <url_decode+0x11b>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    116d <url_decode+0x3d>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<url_decode>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
call   1030 <strlen@plt>
lea    0x1(%rax),%rdi
call   1040 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     11c5 <url_decode+0x95>
mov    (%r12),%al
mov    %r14,%rbx
test   %al,%al
je     11c2 <url_decode+0x92>
mov    %r14,%rbx
jmp    1184 <url_decode+0x54>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %al,(%rbx)
mov    %r12,%r15
add    $0x1,%rbx
movzbl (%r15),%eax
mov    %r15,%r12
test   %al,%al
je     11c2 <url_decode+0x92>
movsbl %al,%ecx
cmp    $0x25,%ecx
je     11a0 <url_decode+0x70>
add    $0x1,%r12
cmp    $0x2b,%ecx
jne    1170 <url_decode+0x40>
movb   $0x20,(%rbx)
jmp    1172 <url_decode+0x42>
nopw   0x0(%rax,%rax,1)
lea    0x1(%r12),%r15
mov    %r15,%rdi
call   1050 <xtoi@plt>
mov    %al,(%rbx)
test   %al,%al
je     11b9 <url_decode+0x89>
add    $0x3,%r12
jmp    1172 <url_decode+0x42>
movzbl (%r12),%eax
mov    %al,(%rbx)
jmp    1175 <url_decode+0x45>
movb   $0x0,(%rbx)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define noalias i8* @url_decode(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 %33, label %6
6: ; preds = %1
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %31, label %9
9: ; preds = %6, %26
%10 = phi i8 [ %29, %26 ], [ %7, %6 ]
%11 = phi i8* [ %28, %26 ], [ %4, %6 ]
%12 = phi i8* [ %27, %26 ], [ %0, %6 ]
%13 = sext i8 %10 to i32
switch i32 %13, label %24 [
i32 43, label %14
i32 37, label %16
]
14: ; preds = %9
%15 = getelementptr inbounds i8, i8* %12, i64 1
store i8 32, i8* %11
br label %26
16: ; preds = %9
%17 = getelementptr inbounds i8, i8* %12, i64 1
%18 = tail call signext i8 @xtoi(i8* %17)
store i8 %18, i8* %11
%19 = icmp eq i8 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %16
%21 = getelementptr inbounds i8, i8* %12, i64 3
br label %26
22: ; preds = %16
%23 = load i8, i8* %12
store i8 %23, i8* %11
br label %26
24: ; preds = %9
%25 = getelementptr inbounds i8, i8* %12, i64 1
store i8 %10, i8* %11
br label %26
26: ; preds = %20, %22, %24, %14
%27 = phi i8* [ %25, %24 ], [ %21, %20 ], [ %17, %22 ], [ %15, %14 ]
%28 = getelementptr inbounds i8, i8* %11, i64 1
%29 = load i8, i8* %27
%30 = icmp eq i8 %29, 0
br i1 %30, label %31, label %9
31: ; preds = %26, %6
%32 = phi i8* [ %4, %6 ], [ %28, %26 ]
store i8 0, i8* %32
br label %33
33: ; preds = %31, %1
ret i8* %4
}