#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__ chrrpl(char *, char, char);
char *malloc(short);
int /*<<< orphan*/ strcpy(char *, char const *);
char *strdup(char *);
void *strlen(char const *);
char *extractQuery(char *str) {
  const char d[] = "%22";
  char dl = strlen(d);
  short len = strlen(str);
  char *q;
  if (chrrpl(str, ' ', '+') == 0)
    return strdup(str);
  q = malloc(len + dl + dl + 1);
  strcpy(q, d);
  strcpy(q + dl, str);
  strcpy(q + dl + len, d);
  q[len + dl + dl] = '\0';
  return q;
}


x86 assembly (clang, -O0):
<extractQuery>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0xe9e(%rip),%eax
mov    %eax,-0x14(%rbp)
lea    -0x14(%rbp),%rdi
call   1040 <strlen@plt>
mov    %al,-0x15(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %ax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x20,%esi
mov    $0x2b,%edx
call   1060 <chrrpl@plt>
cmp    $0x0,%rax
jne    11ad <extractQuery+0x5d>
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1231 <extractQuery+0xe1>
movswl -0x18(%rbp),%eax
movsbl -0x15(%rbp),%ecx
add    %ecx,%eax
movsbl -0x15(%rbp),%ecx
add    %ecx,%eax
add    $0x1,%eax
movswl %ax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x14(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rdi
movsbl -0x15(%rbp),%eax
cltq
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rdi
movsbl -0x15(%rbp),%eax
cltq
add    %rax,%rdi
movswl -0x18(%rbp),%eax
cltq
add    %rax,%rdi
lea    -0x14(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rax
movswl -0x18(%rbp),%ecx
movsbl -0x15(%rbp),%edx
add    %edx,%ecx
movsbl -0x15(%rbp),%edx
add    %edx,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extractQuery>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
movl   $0x323225,0x4(%rsp)
lea    0x4(%rsp),%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %rax,%r14
mov    %r12,%rdi
mov    $0x20,%esi
mov    $0x2b,%edx
call   1060 <chrrpl@plt>
test   %rax,%rax
je     11ec <extractQuery+0x9c>
movswl %r14w,%eax
movsbl %r15b,%ecx
lea    (%rax,%rcx,2),%r13d
lea    (%rax,%rcx,2),%eax
add    $0x1,%eax
movswl %ax,%edi
call   1050 <malloc@plt>
mov    %rax,%rbp
lea    0x4(%rsp),%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
movsbq %r15b,%rbx
add    %rbp,%rbx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
movswq %r14w,%rdi
add    %rbx,%rdi
lea    0x4(%rsp),%rsi
call   1030 <strcpy@plt>
movslq %r13d,%rax
movb   $0x0,0x0(%rbp,%rax,1)
jmp    11f7 <extractQuery+0xa7>
mov    %r12,%rdi
call   1070 <strdup@plt>
mov    %rax,%rbp
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.extractQuery.d = [4 x i8] c"%22\00"
define i8* @extractQuery(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [4 x i8]
%5 = alloca i8
%6 = alloca i16
%7 = alloca i8*
store i8* %0, i8** %3
%8 = bitcast [4 x i8]* %4 to i8*
%9 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%10 = call i8* @strlen(i8* %9)
%11 = ptrtoint i8* %10 to i8
store i8 %11, i8* %5
%12 = load i8*, i8** %3
%13 = call i8* @strlen(i8* %12)
%14 = ptrtoint i8* %13 to i16
store i16 %14, i16* %6
%15 = load i8*, i8** %3
%16 = call i64 @chrrpl(i8* %15, i8 signext 32, i8 signext 43)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %1
%19 = load i8*, i8** %3
%20 = call i8* @strdup(i8* %19)
store i8* %20, i8** %2
br label %66
21: ; preds = %1
%22 = load i16, i16* %6
%23 = sext i16 %22 to i32
%24 = load i8, i8* %5
%25 = sext i8 %24 to i32
%26 = add nsw i32 %23, %25
%27 = load i8, i8* %5
%28 = sext i8 %27 to i32
%29 = add nsw i32 %26, %28
%30 = add nsw i32 %29, 1
%31 = trunc i32 %30 to i16
%32 = call i8* @malloc(i16 signext %31)
store i8* %32, i8** %7
%33 = load i8*, i8** %7
%34 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%35 = call i32 @strcpy(i8* %33, i8* %34)
%36 = load i8*, i8** %7
%37 = load i8, i8* %5
%38 = sext i8 %37 to i32
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %36, i64 %39
%41 = load i8*, i8** %3
%42 = call i32 @strcpy(i8* %40, i8* %41)
%43 = load i8*, i8** %7
%44 = load i8, i8* %5
%45 = sext i8 %44 to i32
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %43, i64 %46
%48 = load i16, i16* %6
%49 = sext i16 %48 to i32
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %47, i64 %50
%52 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%53 = call i32 @strcpy(i8* %51, i8* %52)
%54 = load i8*, i8** %7
%55 = load i16, i16* %6
%56 = sext i16 %55 to i32
%57 = load i8, i8* %5
%58 = sext i8 %57 to i32
%59 = add nsw i32 %56, %58
%60 = load i8, i8* %5
%61 = sext i8 %60 to i32
%62 = add nsw i32 %59, %61
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %54, i64 %63
store i8 0, i8* %64
%65 = load i8*, i8** %7
store i8* %65, i8** %2
br label %66
66: ; preds = %21, %18
%67 = load i8*, i8** %2
ret i8* %67
}

LLVM IR (-O2):
define i8* @extractQuery(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 3289637, i32* %2
%4 = call i8* @strlen(i8* %3)
%5 = call i8* @strlen(i8* %0)
%6 = call i64 @chrrpl(i8* %0, i8 signext 32, i8 signext 43)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i8* @strdup(i8* %0)
br label %34
10: ; preds = %1
%11 = ptrtoint i8* %5 to i64
%12 = trunc i64 %11 to i32
%13 = ptrtoint i8* %4 to i64
%14 = trunc i64 %13 to i32
%15 = shl i32 %12, 16
%16 = ashr exact i32 %15, 16
%17 = shl i32 %14, 24
%18 = ashr exact i32 %17, 23
%19 = add nsw i32 %16, %18
%20 = trunc i32 %19 to i16
%21 = add i16 %20, 1
%22 = call i8* @malloc(i16 signext %21)
%23 = call i32 @strcpy(i8* %22, i8* %3)
%24 = shl i64 %13, 56
%25 = ashr exact i64 %24, 56
%26 = getelementptr inbounds i8, i8* %22, i64 %25
%27 = call i32 @strcpy(i8* %26, i8* %0)
%28 = shl i64 %11, 48
%29 = ashr exact i64 %28, 48
%30 = getelementptr inbounds i8, i8* %26, i64 %29
%31 = call i32 @strcpy(i8* %30, i8* %3)
%32 = sext i32 %19 to i64
%33 = getelementptr inbounds i8, i8* %22, i64 %32
store i8 0, i8* %33
br label %34
34: ; preds = %10, %8
%35 = phi i8* [ %9, %8 ], [ %22, %10 ]
ret i8* %35
}