#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 hextoint(char);
int /*<<< orphan*/ memmove(char *, char *, int);
int strlen(char *);
char *convert_query_hexchar(char *txt) {
  int d1, d2, n;
  n = strlen(txt);
  if (n < 3) {
    *txt = '\0';
    return txt - 1;
  }
  d1 = hextoint(*(txt + 1));
  d2 = hextoint(*(txt + 2));
  if (d1 < 0 || d2 < 0) {
    memmove(txt, txt + 3, n - 2);
    return txt - 1;
  } else {
    *txt = d1 * 16 + d2;
    memmove(txt + 1, txt + 3, n - 2);
    return txt;
  }
}


x86 assembly (clang, -O0):
<convert_query_hexchar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x3,-0x1c(%rbp)
jge    116a <convert_query_hexchar+0x3a>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    11fa <convert_query_hexchar+0xca>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%edi
call   1040 <hextoint@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movsbl 0x2(%rax),%edi
call   1040 <hextoint@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     119e <convert_query_hexchar+0x6e>
cmpl   $0x0,-0x18(%rbp)
jge    11c6 <convert_query_hexchar+0x96>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x3,%rsi
mov    -0x1c(%rbp),%edx
sub    $0x2,%edx
call   1050 <memmove@plt>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    11fa <convert_query_hexchar+0xca>
mov    -0x14(%rbp),%eax
shl    $0x4,%eax
add    -0x18(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
mov    -0x10(%rbp),%rsi
add    $0x3,%rsi
mov    -0x1c(%rbp),%edx
sub    $0x2,%edx
call   1050 <memmove@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convert_query_hexchar>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
cmp    $0x2,%eax
jg     1146 <convert_query_hexchar+0x16>
movb   $0x0,(%rbx)
jmp    1194 <convert_query_hexchar+0x64>
mov    %eax,%ebp
movsbl 0x1(%rbx),%edi
call   1040 <hextoint@plt>
mov    %eax,%r14d
movsbl 0x2(%rbx),%edi
call   1040 <hextoint@plt>
test   %r14d,%r14d
js     1183 <convert_query_hexchar+0x53>
test   %eax,%eax
js     1183 <convert_query_hexchar+0x53>
lea    0x1(%rbx),%rdi
shl    $0x4,%r14d
add    %r14d,%eax
mov    %al,(%rbx)
lea    0x3(%rbx),%rsi
add    $0xfffffffe,%ebp
mov    %ebp,%edx
call   1050 <memmove@plt>
jmp    1198 <convert_query_hexchar+0x68>
lea    0x3(%rbx),%rsi
add    $0xfffffffe,%ebp
mov    %rbx,%rdi
mov    %ebp,%edx
call   1050 <memmove@plt>
add    $0xffffffffffffffff,%rbx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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