#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 *, char *, int *);
void decode_url(char *src, int srclen, char *dest) {
  int i;
  char *str = src;
  for (i = 0; i < srclen; i++, str++, dest++) {
    if (*str == '+') {
      *dest = ' ';
    } else if (*str == '%') {
      int code;
      if (sscanf(str + 1, "%2x", &code) != 1) {
        code = '?';
      }
      *dest = code;
      str += 2;
    } else {
      *dest = *str;
    }
  }
  *dest = '\n';
  *++dest = '\0';
}


x86 assembly (clang, -O0):
<decode_url>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11ea <decode_url+0xda>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2b,%eax
jne    115a <decode_url+0x4a>
mov    -0x18(%rbp),%rax
movb   $0x20,(%rax)
jmp    11bf <decode_url+0xaf>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x25,%eax
jne    11ae <decode_url+0x9e>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
lea    0xe87(%rip),%rsi
lea    -0x2c(%rbp),%rdx
call   1030 <sscanf@plt>
cmp    $0x1,%eax
je     1192 <decode_url+0x82>
movl   $0x3f,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x28(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x28(%rbp)
jmp    11ba <decode_url+0xaa>
mov    -0x28(%rbp),%rax
mov    (%rax),%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
jmp    11bf <decode_url+0xaf>
jmp    11c4 <decode_url+0xb4>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1132 <decode_url+0x22>
mov    -0x18(%rbp),%rax
movb   $0xa,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x0,0x1(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decode_url>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
test   %esi,%esi
jle    1195 <decode_url+0x85>
mov    %esi,%r14d
mov    %rdi,%rbp
lea    0xed0(%rip),%r15
lea    0xc(%rsp),%r12
jmp    1150 <decode_url+0x40>
nopw   0x0(%rax,%rax,1)
mov    %al,(%rbx)
add    $0x1,%rbp
add    $0x1,%rbx
add    $0xffffffff,%r14d
je     1195 <decode_url+0x85>
movzbl 0x0(%rbp),%eax
cmp    $0x25,%al
je     1170 <decode_url+0x60>
cmp    $0x2b,%al
jne    1140 <decode_url+0x30>
movb   $0x20,(%rbx)
jmp    1142 <decode_url+0x32>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    0x1(%rbp),%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1030 <sscanf@plt>
mov    $0x3f,%ecx
cmp    $0x1,%eax
jne    118d <decode_url+0x7d>
mov    0xc(%rsp),%ecx
mov    %cl,(%rbx)
add    $0x2,%rbp
jmp    1142 <decode_url+0x32>
movw   $0xa,(%rbx)
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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