#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*/ bsLCat(char **, char *);
char *bsNew(char *);
scalar_t__ strtol(char *, int /*<<< orphan*/ *, int);
char *urldecode(char *segment) {
  char *cc = segment;
  char *bs = bsNew("");
  char cb[3] = "\0\0\0";
  char c[2] = "\0\0";
  while (*cc != '\0') {
    if (*cc == '+')
      *cc = ' ';
    if (*cc == '%') {
      *cc = '\0';
      bsLCat(&bs, segment);
      cb[0] = *(cc + 1);
      cb[1] = *(cc + 2);
      c[0] = (char)strtol(cb, NULL, 16);
      cc += 2;
      segment = cc + 1;
      bsLCat(&bs, c);
    }
    cc++;
  }
  bsLCat(&bs, segment);
  return bs;
}


x86 assembly (clang, -O0):
<urldecode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
lea    0xea5(%rip),%rdi
call   1060 <bsNew@plt>
mov    %rax,-0x18(%rbp)
lea    -0x1b(%rbp),%rdi
xor    %esi,%esi
mov    $0x3,%edx
call   1040 <memset@plt>
lea    -0x1d(%rbp),%rdi
xor    %esi,%esi
mov    $0x2,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     122e <urldecode+0xee>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2b,%eax
jne    11ab <urldecode+0x6b>
mov    -0x10(%rbp),%rax
movb   $0x20,(%rax)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x25,%eax
jne    121d <urldecode+0xdd>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rsi
lea    -0x18(%rbp),%rdi
call   1030 <bsLCat@plt>
mov    -0x10(%rbp),%rax
mov    0x1(%rax),%al
mov    %al,-0x1b(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2(%rax),%al
mov    %al,-0x1a(%rbp)
lea    -0x1b(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %al,-0x1d(%rbp)
mov    -0x10(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
lea    -0x1d(%rbp),%rsi
lea    -0x18(%rbp),%rdi
call   1030 <bsLCat@plt>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1184 <urldecode+0x44>
mov    -0x8(%rbp),%rsi
lea    -0x18(%rbp),%rdi
call   1030 <bsLCat@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<urldecode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r13
lea    0xeb8(%rip),%rdi
call   1050 <bsNew@plt>
mov    %rax,0x10(%rsp)
movb   $0x0,0xc(%rsp)
movw   $0x0,0xa(%rsp)
movw   $0x0,0xe(%rsp)
lea    0x10(%rsp),%r12
lea    0xa(%rsp),%r14
lea    0xe(%rsp),%r15
mov    %r13,%rbx
jmp    1188 <urldecode+0x58>
nopl   0x0(%rax)
test   %al,%al
je     11ec <urldecode+0xbc>
add    $0x1,%rbx
movzbl (%rbx),%eax
cmp    $0x25,%al
je     11a0 <urldecode+0x70>
cmp    $0x2b,%al
jne    1180 <urldecode+0x50>
movb   $0x20,(%rbx)
add    $0x1,%rbx
jmp    1188 <urldecode+0x58>
nopl   0x0(%rax)
movb   $0x0,(%rbx)
mov    %r12,%rdi
mov    %r13,%rsi
call   1030 <bsLCat@plt>
movzbl 0x1(%rbx),%eax
mov    %al,0xa(%rsp)
lea    0x2(%rbx),%rbp
movzbl 0x2(%rbx),%eax
mov    %al,0xb(%rsp)
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <strtol@plt>
mov    %al,0xe(%rsp)
add    $0x3,%rbx
mov    %r12,%rdi
mov    %r15,%rsi
call   1030 <bsLCat@plt>
mov    %rbx,%r13
mov    %rbp,%rbx
jmp    1184 <urldecode+0x54>
lea    0x10(%rsp),%rdi
mov    %r13,%rsi
call   1030 <bsLCat@plt>
mov    0x10(%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define i8* @urldecode(i8* %0) {
%2 = alloca i8*
%3 = alloca [3 x i8]
%4 = alloca i16
%5 = bitcast i8** %2 to i8*
%6 = tail call i8* @bsNew(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
store i8* %6, i8** %2
%7 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%8 = bitcast i16* %4 to i8*
store i16 0, i16* %4
%9 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 1
br label %10
10: ; preds = %25, %1
%11 = phi i8* [ %0, %1 ], [ %26, %25 ]
%12 = phi i8* [ %0, %1 ], [ %28, %25 ]
%13 = load i8, i8* %12
switch i8 %13, label %25 [
i8 0, label %29
i8 43, label %14
i8 37, label %15
]
14: ; preds = %10
store i8 32, i8* %12
br label %25
15: ; preds = %10
store i8 0, i8* %12
%16 = call i32 @bsLCat(i8** %2, i8* %11)
%17 = getelementptr inbounds i8, i8* %12, i64 1
%18 = load i8, i8* %17
store i8 %18, i8* %7
%19 = getelementptr inbounds i8, i8* %12, i64 2
%20 = load i8, i8* %19
store i8 %20, i8* %9
%21 = call i64 @strtol(i8* nocapture %7, i32* null, i32 16)
%22 = trunc i64 %21 to i8
store i8 %22, i8* %8
%23 = getelementptr inbounds i8, i8* %12, i64 3
%24 = call i32 @bsLCat(i8** %2, i8* %8)
br label %25
25: ; preds = %10, %14, %15
%26 = phi i8* [ %23, %15 ], [ %11, %14 ], [ %11, %10 ]
%27 = phi i8* [ %19, %15 ], [ %12, %14 ], [ %12, %10 ]
%28 = getelementptr inbounds i8, i8* %27, i64 1
br label %10
29: ; preds = %10
%30 = call i32 @bsLCat(i8** %2, i8* %11)
%31 = load i8*, i8** %2
ret i8* %31
}