#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__ calloc(unsigned int, int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ memset(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int strlen(char *);
char *decode_query_string(char *query_str) {
  int i;
  int j = 0;
  char *decoded_query_str = NULL;
  int len = strlen(query_str);
  char temp[3];
  int value;
  if (!(decoded_query_str =
            (char *)calloc((unsigned int)(len + 1), sizeof(char)))) {
    printf("Could not alloc memory for decoded query string buffer!\n");
    return NULL;
  }
  for (i = 0; i < len; i++) {
    if (query_str[i] == '\%') {
      memset(temp, 3, 0);
      memcpy(temp, query_str + i + 1, 2);
      value = 0;
      sscanf(temp, "%02X", &value);
      decoded_query_str[j++] = value;
      i += 2;
    } else {
      decoded_query_str[j++] = query_str[i];
    }
  }
  return decoded_query_str;
}


x86 assembly (clang, -O0):
<decode_query_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11be <decode_query_string+0x5e>
lea    0xe54(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1291 <decode_query_string+0x131>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1289 <decode_query_string+0x129>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x25,%eax
jne    1256 <decode_query_string+0xf6>
lea    -0x27(%rbp),%rdi
mov    $0x3,%esi
xor    %edx,%edx
call   1050 <memset@plt>
lea    -0x27(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
add    $0x1,%rsi
mov    $0x2,%edx
call   1070 <memcpy@plt>
movl   $0x0,-0x2c(%rbp)
lea    -0x27(%rbp),%rdi
lea    0xe14(%rip),%rsi
lea    -0x2c(%rbp),%rdx
call   1080 <sscanf@plt>
mov    -0x2c(%rbp),%eax
mov    %al,%dl
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x18(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x14(%rbp)
jmp    1276 <decode_query_string+0x116>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x18(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    127b <decode_query_string+0x11b>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11c5 <decode_query_string+0x65>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decode_query_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %eax,%r15d
lea    0x1(%r15),%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
test   %rax,%rax
je     121b <decode_query_string+0xbb>
test   %r15d,%r15d
jle    1230 <decode_query_string+0xd0>
xor    %edx,%edx
lea    0xd(%rsp),%r12
mov    %rax,0x10(%rsp)
mov    %rax,%r13
jmp    11c0 <decode_query_string+0x60>
nopw   0x0(%rax,%rax,1)
mov    %cl,0x0(%r13)
add    $0x1,%edx
add    $0x1,%r13
cmp    %r15d,%edx
jge    122b <decode_query_string+0xcb>
movslq %edx,%rbp
movzbl (%rbx,%rbp,1),%ecx
cmp    $0x25,%cl
jne    11b0 <decode_query_string+0x50>
lea    (%rbx,%rbp,1),%r14
mov    %r12,%rdi
mov    $0x3,%esi
xor    %edx,%edx
call   1050 <memset@plt>
add    $0x1,%r14
mov    %r12,%rdi
mov    %r14,%rsi
mov    $0x2,%edx
call   1070 <memcpy@plt>
movl   $0x0,0x8(%rsp)
mov    %r12,%rdi
lea    0xdfb(%rip),%rsi
lea    0x8(%rsp),%rdx
call   1080 <sscanf@plt>
movzbl 0x8(%rsp),%ecx
add    $0x2,%ebp
mov    %ebp,%edx
jmp    11b0 <decode_query_string+0x50>
lea    0xde3(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    1230 <decode_query_string+0xd0>
mov    0x10(%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [57 x i8] c"Could not alloc memory for decoded query string buffer!\0A\00"
@.str.1 = [5 x i8] c"%02X\00"
define i8* @decode_query_string(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [3 x i8]
%9 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %5
store i8* null, i8** %6
%10 = load i8*, i8** %3
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = add nsw i32 %12, 1
%14 = call i64 @calloc(i32 %13, i32 1)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %6
%16 = icmp ne i8* %15, null
br i1 %16, label %19, label %17
17: ; preds = %1
%18 = call i32 @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %70
19: ; preds = %1
store i32 0, i32* %4
br label %20
20: ; preds = %65, %19
%21 = load i32, i32* %4
%22 = load i32, i32* %7
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %68
24: ; preds = %20
%25 = load i8*, i8** %3
%26 = load i32, i32* %4
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 37
br i1 %31, label %32, label %53
32: ; preds = %24
%33 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 0
%34 = call i32 @memset(i8* %33, i32 3, i32 0)
%35 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 0
%36 = load i8*, i8** %3
%37 = load i32, i32* %4
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = getelementptr inbounds i8, i8* %39, i64 1
%41 = call i32 @memcpy(i8* %35, i8* %40, i32 2)
store i32 0, i32* %9
%42 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 0
%43 = call i32 @sscanf(i8* %42, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32* %9)
%44 = load i32, i32* %9
%45 = trunc i32 %44 to i8
%46 = load i8*, i8** %6
%47 = load i32, i32* %5
%48 = add nsw i32 %47, 1
store i32 %48, i32* %5
%49 = sext i32 %47 to i64
%50 = getelementptr inbounds i8, i8* %46, i64 %49
store i8 %45, i8* %50
%51 = load i32, i32* %4
%52 = add nsw i32 %51, 2
store i32 %52, i32* %4
br label %64
53: ; preds = %24
%54 = load i8*, i8** %3
%55 = load i32, i32* %4
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %54, i64 %56
%58 = load i8, i8* %57
%59 = load i8*, i8** %6
%60 = load i32, i32* %5
%61 = add nsw i32 %60, 1
store i32 %61, i32* %5
%62 = sext i32 %60 to i64
%63 = getelementptr inbounds i8, i8* %59, i64 %62
store i8 %58, i8* %63
br label %64
64: ; preds = %53, %32
br label %65
65: ; preds = %64
%66 = load i32, i32* %4
%67 = add nsw i32 %66, 1
store i32 %67, i32* %4
br label %20
68: ; preds = %20
%69 = load i8*, i8** %6
store i8* %69, i8** %2
br label %70
70: ; preds = %68, %17
%71 = load i8*, i8** %2
ret i8* %71
}

LLVM IR (-O2):
@.str.1 = [5 x i8] c"%02X\00"
@str = [56 x i8] c"Could not alloc memory for decoded query string buffer!\00"
define i8* @decode_query_string(i8* %0) {
%2 = alloca [3 x i8]
%3 = alloca i32
%4 = tail call i32 @strlen(i8* %0)
%5 = getelementptr inbounds [3 x i8], [3 x i8]* %2, i64 0, i64 0
%6 = bitcast i32* %3 to i8*
%7 = add nsw i32 %4, 1
%8 = tail call i64 @calloc(i32 %7, i32 1)
%9 = inttoptr i64 %8 to i8*
%10 = icmp eq i64 %8, 0
br i1 %10, label %13, label %11
11: ; preds = %1
%12 = icmp sgt i32 %4, 0
br i1 %12, label %15, label %37
13: ; preds = %1
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @str, i64 0, i64 0))
br label %37
15: ; preds = %11, %30
%16 = phi i64 [ %34, %30 ], [ 0, %11 ]
%17 = phi i32 [ %35, %30 ], [ 0, %11 ]
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %0, i64 %18
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 37
br i1 %21, label %22, label %30
22: ; preds = %15
%23 = call i32 @memset(i8* %5, i32 3, i32 0)
%24 = getelementptr inbounds i8, i8* %19, i64 1
%25 = call i32 @memcpy(i8* %5, i8* %24, i32 2)
store i32 0, i32* %3
%26 = call i32 @sscanf(i8* %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%27 = load i32, i32* %3
%28 = trunc i32 %27 to i8
%29 = add nsw i32 %17, 2
br label %30
30: ; preds = %15, %22
%31 = phi i8 [ %28, %22 ], [ %20, %15 ]
%32 = phi i32 [ %29, %22 ], [ %17, %15 ]
%33 = getelementptr inbounds i8, i8* %9, i64 %16
store i8 %31, i8* %33
%34 = add nuw i64 %16, 1
%35 = add nsw i32 %32, 1
%36 = icmp slt i32 %35, %4
br i1 %36, label %15, label %37
37: ; preds = %30, %11, %13
%38 = phi i8* [ null, %13 ], [ %9, %11 ], [ %9, %30 ]
ret i8* %38
}