#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*/ memmove(char *, char *, int);
char *strchr(char *, char);
int strlen(char *);
char strtol(char *, int /*<<< orphan*/ *, int);
void translate_query(char *s) {
  char *t;
  char *start = NULL;
  while ((t = strchr(s, '%')) != NULL) {
    char c = t[3];
    t[3] = 0;
    t[0] = strtol(t + 1, NULL, 16);
    t[3] = c;
    if (start != NULL) {
      size_t n = t - s + 1;
      memmove(start, s, n);
      start += n;
    } else {
      start = t + 1;
    }
    s = t + 3;
  }
  if (start != NULL) {
    memmove(start, s, strlen(s) + 1);
  }
}


x86 assembly (clang, -O0):
<translate_query>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x25,%esi
call   1040 <strchr@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1209 <translate_query+0xc9>
mov    -0x10(%rbp),%rax
mov    0x3(%rax),%al
mov    %al,-0x19(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,0x3(%rax)
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x19(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x3(%rax)
cmpq   $0x0,-0x18(%rbp)
je     11ec <translate_query+0xac>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    %eax,%edx
call   1060 <memmove@plt>
mov    -0x28(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    11f8 <translate_query+0xb8>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x8(%rbp)
jmp    1154 <translate_query+0x14>
cmpq   $0x0,-0x18(%rbp)
je     123f <translate_query+0xff>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1060 <memmove@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<translate_query>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
mov    $0x25,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11f5 <translate_query+0xb5>
mov    %rax,%rbx
xor    %r12d,%r12d
jmp    118b <translate_query+0x4b>
nopl   0x0(%rax)
mov    %r14,%rdi
mov    $0x25,%esi
call   1040 <strchr@plt>
mov    %rax,%rbx
mov    %r14,%r15
mov    %r13,%r12
test   %rax,%rax
je     11d1 <translate_query+0x91>
lea    0x3(%rbx),%r14
movzbl 0x3(%rbx),%ebp
movb   $0x0,0x3(%rbx)
lea    0x1(%rbx),%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %al,(%rbx)
mov    %bpl,0x3(%rbx)
test   %r12,%r12
je     1170 <translate_query+0x30>
sub    %r15,%rbx
lea    0x1(%rbx),%rdx
mov    %r12,%rdi
mov    %r15,%rsi
call   1060 <memmove@plt>
lea    (%r12,%rbx,1),%r13
add    $0x1,%r13
jmp    1170 <translate_query+0x30>
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%edx
mov    %r13,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <memmove@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @translate_query(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i64
store i8* %0, i8** %2
store i8* null, i8** %4
br label %7
7: ; preds = %45, %1
%8 = load i8*, i8** %2
%9 = call i8* @strchr(i8* %8, i8 signext 37)
store i8* %9, i8** %3
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %48
11: ; preds = %7
%12 = load i8*, i8** %3
%13 = getelementptr inbounds i8, i8* %12, i64 3
%14 = load i8, i8* %13
store i8 %14, i8* %5
%15 = load i8*, i8** %3
%16 = getelementptr inbounds i8, i8* %15, i64 3
store i8 0, i8* %16
%17 = load i8*, i8** %3
%18 = getelementptr inbounds i8, i8* %17, i64 1
%19 = call signext i8 @strtol(i8* %18, i32* null, i32 16)
%20 = load i8*, i8** %3
%21 = getelementptr inbounds i8, i8* %20, i64 0
store i8 %19, i8* %21
%22 = load i8, i8* %5
%23 = load i8*, i8** %3
%24 = getelementptr inbounds i8, i8* %23, i64 3
store i8 %22, i8* %24
%25 = load i8*, i8** %4
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %42
27: ; preds = %11
%28 = load i8*, i8** %3
%29 = load i8*, i8** %2
%30 = ptrtoint i8* %28 to i64
%31 = ptrtoint i8* %29 to i64
%32 = sub i64 %30, %31
%33 = add nsw i64 %32, 1
store i64 %33, i64* %6
%34 = load i8*, i8** %4
%35 = load i8*, i8** %2
%36 = load i64, i64* %6
%37 = trunc i64 %36 to i32
%38 = call i32 @memmove(i8* %34, i8* %35, i32 %37)
%39 = load i64, i64* %6
%40 = load i8*, i8** %4
%41 = getelementptr inbounds i8, i8* %40, i64 %39
store i8* %41, i8** %4
br label %45
42: ; preds = %11
%43 = load i8*, i8** %3
%44 = getelementptr inbounds i8, i8* %43, i64 1
store i8* %44, i8** %4
br label %45
45: ; preds = %42, %27
%46 = load i8*, i8** %3
%47 = getelementptr inbounds i8, i8* %46, i64 3
store i8* %47, i8** %2
br label %7
48: ; preds = %7
%49 = load i8*, i8** %4
%50 = icmp ne i8* %49, null
br i1 %50, label %51, label %58
51: ; preds = %48
%52 = load i8*, i8** %4
%53 = load i8*, i8** %2
%54 = load i8*, i8** %2
%55 = call i32 @strlen(i8* %54)
%56 = add nsw i32 %55, 1
%57 = call i32 @memmove(i8* %52, i8* %53, i32 %56)
br label %58
58: ; preds = %51, %48
ret void
}

LLVM IR (-O2):
define void @translate_query(i8* %0) {
%2 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 37)
%3 = icmp eq i8* %2, null
br i1 %3, label %29, label %4
4: ; preds = %1, %21
%5 = phi i8* [ %23, %21 ], [ %2, %1 ]
%6 = phi i8* [ %8, %21 ], [ %0, %1 ]
%7 = phi i8* [ %22, %21 ], [ null, %1 ]
%8 = getelementptr inbounds i8, i8* %5, i64 3
%9 = load i8, i8* %8
store i8 0, i8* %8
%10 = getelementptr inbounds i8, i8* %5, i64 1
%11 = tail call signext i8 @strtol(i8* nocapture %10, i32* null, i32 16)
store i8 %11, i8* %5
store i8 %9, i8* %8
%12 = icmp eq i8* %7, null
br i1 %12, label %21, label %13
13: ; preds = %4
%14 = ptrtoint i8* %5 to i64
%15 = ptrtoint i8* %6 to i64
%16 = sub i64 %14, %15
%17 = add nsw i64 %16, 1
%18 = trunc i64 %17 to i32
%19 = tail call i32 @memmove(i8* %7, i8* %6, i32 %18)
%20 = getelementptr inbounds i8, i8* %7, i64 %17
br label %21
21: ; preds = %4, %13
%22 = phi i8* [ %20, %13 ], [ %10, %4 ]
%23 = tail call i8* @strchr(i8* dereferenceable(1) %8, i8 signext 37)
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %4
25: ; preds = %21
%26 = tail call i32 @strlen(i8* %8)
%27 = add nsw i32 %26, 1
%28 = tail call i32 @memmove(i8* %22, i8* %8, i32 %27)
br label %29
29: ; preds = %1, %25
ret void
}