#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*/ sprintf(char *, char *, char, char);
int /*<<< orphan*/ sscanf(char *, char *, char *);
int strlen(char *);
scalar_t__ xmalloc(int);
char *UriDecode(char *uri) {
  int count = 0, ptr = 0;
  char *word = (char *)xmalloc(sizeof(char) * strlen(uri));
  char hex[3], code;
  do {
    switch (*(uri + count)) {
    case '+':
      *(word + ptr) = ' ';
      ptr++;
      break;
    case '%':
      sprintf(hex, "%c%c\x00", *(uri + count + 1), *(uri + count + 2));
      count += 2;
      sscanf(hex, "%x", &code);
      *(word + ptr) = code;
      ptr++;
      break;
    default:
      *(word + ptr) = *(uri + count);
      ptr++;
      break;
    }
    count++;
  } while (count < strlen(uri));
  *(word + ptr) = '\0';
  return word;
}


x86 assembly (clang, -O0):
<UriDecode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <xmalloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    %eax,-0x20(%rbp)
sub    $0x25,%eax
je     11bc <UriDecode+0x7c>
jmp    1191 <UriDecode+0x51>
mov    -0x20(%rbp),%eax
sub    $0x2b,%eax
jne    1221 <UriDecode+0xe1>
jmp    11a2 <UriDecode+0x62>
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x20,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1240 <UriDecode+0x100>
lea    -0x1b(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    0x1(%rax,%rcx,1),%dl
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
lea    0xe25(%rip),%rsi
movsbl %dl,%edx
movsbl 0x2(%rax,%rcx,1),%ecx
call   1060 <sprintf@plt>
mov    -0xc(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0xc(%rbp)
lea    -0x1b(%rbp),%rdi
lea    0xe0a(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1050 <sscanf@plt>
mov    -0x1c(%rbp),%dl
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1240 <UriDecode+0x100>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
jl     1174 <UriDecode+0x34>
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<UriDecode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %eax,%edi
call   1040 <xmalloc@plt>
mov    %rax,%r14
xor    %r15d,%r15d
lea    0x5(%rsp),%r12
xor    %r13d,%r13d
jmp    118b <UriDecode+0x4b>
nopw   0x0(%rax,%rax,1)
mov    $0x20,%al
mov    %al,(%r14,%r15,1)
add    $0x1,%r15
add    $0x1,%r13d
mov    %rbx,%rdi
call   1030 <strlen@plt>
cmp    %eax,%r13d
jge    11d9 <UriDecode+0x99>
movslq %r13d,%rbp
movzbl (%rbx,%rbp,1),%eax
movsbl %al,%ecx
cmp    $0x2b,%ecx
je     1170 <UriDecode+0x30>
cmp    $0x25,%ecx
jne    1172 <UriDecode+0x32>
movsbl 0x1(%rbx,%rbp,1),%edx
movsbl 0x2(%rbx,%rbp,1),%ecx
mov    %r12,%rdi
lea    0xe4d(%rip),%rsi
call   1060 <sprintf@plt>
add    $0x2,%ebp
mov    %r12,%rdi
lea    0xe41(%rip),%rsi
lea    0x4(%rsp),%rdx
call   1050 <sscanf@plt>
movzbl 0x4(%rsp),%eax
mov    %ebp,%r13d
jmp    1172 <UriDecode+0x32>
mov    %r15d,%eax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%c%c\00\00"
@.str.1 = [3 x i8] c"%x\00"
define i8* @UriDecode(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [3 x i8]
%7 = alloca i8
store i8* %0, i8** %2
store i32 0, i32* %3
store i32 0, i32* %4
%8 = load i8*, i8** %2
%9 = call i32 @strlen(i8* %8)
%10 = sext i32 %9 to i64
%11 = mul i64 1, %10
%12 = trunc i64 %11 to i32
%13 = call i64 @xmalloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %5
br label %15
15: ; preds = %70, %1
%16 = load i8*, i8** %2
%17 = load i32, i32* %3
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
switch i32 %21, label %55 [
i32 43, label %22
i32 37, label %29
]
22: ; preds = %15
%23 = load i8*, i8** %5
%24 = load i32, i32* %4
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 32, i8* %26
%27 = load i32, i32* %4
%28 = add nsw i32 %27, 1
store i32 %28, i32* %4
br label %67
29: ; preds = %15
%30 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
%31 = load i8*, i8** %2
%32 = load i32, i32* %3
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = getelementptr inbounds i8, i8* %34, i64 1
%36 = load i8, i8* %35
%37 = load i8*, i8** %2
%38 = load i32, i32* %3
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
%41 = getelementptr inbounds i8, i8* %40, i64 2
%42 = load i8, i8* %41
%43 = call i32 @sprintf(i8* %30, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8 signext %36, i8 signext %42)
%44 = load i32, i32* %3
%45 = add nsw i32 %44, 2
store i32 %45, i32* %3
%46 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
%47 = call i32 @sscanf(i8* %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%48 = load i8, i8* %7
%49 = load i8*, i8** %5
%50 = load i32, i32* %4
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
store i8 %48, i8* %52
%53 = load i32, i32* %4
%54 = add nsw i32 %53, 1
store i32 %54, i32* %4
br label %67
55: ; preds = %15
%56 = load i8*, i8** %2
%57 = load i32, i32* %3
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %56, i64 %58
%60 = load i8, i8* %59
%61 = load i8*, i8** %5
%62 = load i32, i32* %4
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %61, i64 %63
store i8 %60, i8* %64
%65 = load i32, i32* %4
%66 = add nsw i32 %65, 1
store i32 %66, i32* %4
br label %67
67: ; preds = %55, %29, %22
%68 = load i32, i32* %3
%69 = add nsw i32 %68, 1
store i32 %69, i32* %3
br label %70
70: ; preds = %67
%71 = load i32, i32* %3
%72 = load i8*, i8** %2
%73 = call i32 @strlen(i8* %72)
%74 = icmp slt i32 %71, %73
br i1 %74, label %15, label %75
75: ; preds = %70
%76 = load i8*, i8** %5
%77 = load i32, i32* %4
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i8, i8* %76, i64 %78
store i8 0, i8* %79
%80 = load i8*, i8** %5
ret i8* %80
}

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