#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 */
scalar_t__ malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char, char);
int /*<<< orphan*/ sscanf(char *, char *, char *);
int strlen(char *);
char *uri_decode(char *uri) {
  int i = 0;
  int ptr = 0;
  char *command;
  char hexa[3];
  char code;
  command = (char *)malloc(strlen(uri));
  for (i = 0; i < strlen(uri); i++) {
    switch (*(uri + i)) {
    case '+':
      *(command + ptr) = ' ';
      ptr++;
      break;
    case '%':
      sprintf(hexa, "%c%c\x00", *(uri + i + 1), *(uri + i + 2));
      i += 2;
      sscanf(hexa, "%x", &code);
      *(command + ptr) = code;
      ptr++;
      break;
    default:
      *(command + ptr) = *(uri + i);
      ptr++;
      break;
    }
  }
  *(command + ptr) = '\0';
  return command;
}


x86 assembly (clang, -O0):
<uri_decode>:
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>
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
cmp    %ecx,%eax
jge    1270 <uri_decode+0x130>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    %eax,-0x24(%rbp)
sub    $0x25,%eax
je     11d9 <uri_decode+0x99>
jmp    11ae <uri_decode+0x6e>
mov    -0x24(%rbp),%eax
sub    $0x2b,%eax
jne    123e <uri_decode+0xfe>
jmp    11bf <uri_decode+0x7f>
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    125d <uri_decode+0x11d>
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    0xe08(%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    0xded(%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    125d <uri_decode+0x11d>
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)
jmp    1262 <uri_decode+0x122>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1175 <uri_decode+0x35>
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):
<uri_decode>:
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 <malloc@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <strlen@plt>
xor    %r13d,%r13d
test   %eax,%eax
jle    11ec <uri_decode+0xac>
lea    0x5(%rsp),%r12
xor    %r15d,%r15d
jmp    119b <uri_decode+0x5b>
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    11e9 <uri_decode+0xa9>
movslq %r13d,%rbp
movzbl (%rbx,%rbp,1),%eax
movsbl %al,%ecx
cmp    $0x2b,%ecx
je     1180 <uri_decode+0x40>
cmp    $0x25,%ecx
jne    1182 <uri_decode+0x42>
movsbl 0x1(%rbx,%rbp,1),%edx
movsbl 0x2(%rbx,%rbp,1),%ecx
mov    %r12,%rdi
lea    0xe3d(%rip),%rsi
call   1060 <sprintf@plt>
add    $0x2,%ebp
mov    %r12,%rdi
lea    0xe31(%rip),%rsi
lea    0x4(%rsp),%rdx
call   1050 <sscanf@plt>
movzbl 0x4(%rsp),%eax
mov    %ebp,%r13d
jmp    1182 <uri_decode+0x42>
mov    %r15d,%r13d
movb   $0x0,(%r14,%r13,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* @uri_decode(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 = call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %5
store i32 0, i32* %3
br label %12
12: ; preds = %70, %1
%13 = load i32, i32* %3
%14 = load i8*, i8** %2
%15 = call i32 @strlen(i8* %14)
%16 = icmp slt i32 %13, %15
br i1 %16, label %17, label %73
17: ; preds = %12
%18 = load i8*, i8** %2
%19 = load i32, i32* %3
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
switch i32 %23, label %57 [
i32 43, label %24
i32 37, label %31
]
24: ; preds = %17
%25 = load i8*, i8** %5
%26 = load i32, i32* %4
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8 32, i8* %28
%29 = load i32, i32* %4
%30 = add nsw i32 %29, 1
store i32 %30, i32* %4
br label %69
31: ; preds = %17
%32 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
%33 = load i8*, i8** %2
%34 = load i32, i32* %3
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = getelementptr inbounds i8, i8* %36, i64 1
%38 = load i8, i8* %37
%39 = load i8*, i8** %2
%40 = load i32, i32* %3
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
%43 = getelementptr inbounds i8, i8* %42, i64 2
%44 = load i8, i8* %43
%45 = call i32 @sprintf(i8* %32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8 signext %38, i8 signext %44)
%46 = load i32, i32* %3
%47 = add nsw i32 %46, 2
store i32 %47, i32* %3
%48 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
%49 = call i32 @sscanf(i8* %48, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%50 = load i8, i8* %7
%51 = load i8*, i8** %5
%52 = load i32, i32* %4
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
store i8 %50, i8* %54
%55 = load i32, i32* %4
%56 = add nsw i32 %55, 1
store i32 %56, i32* %4
br label %69
57: ; preds = %17
%58 = load i8*, i8** %2
%59 = load i32, i32* %3
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %58, i64 %60
%62 = load i8, i8* %61
%63 = load i8*, i8** %5
%64 = load i32, i32* %4
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %63, i64 %65
store i8 %62, i8* %66
%67 = load i32, i32* %4
%68 = add nsw i32 %67, 1
store i32 %68, i32* %4
br label %69
69: ; preds = %57, %31, %24
br label %70
70: ; preds = %69
%71 = load i32, i32* %3
%72 = add nsw i32 %71, 1
store i32 %72, i32* %3
br label %12
73: ; preds = %12
%74 = load i8*, i8** %5
%75 = load i32, i32* %4
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds i8, i8* %74, i64 %76
store i8 0, i8* %77
%78 = load i8*, i8** %5
ret i8* %78
}

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