#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*/ strcpy(char *, char *);
int strlen(char *);
char *convert_query_hexchar(char *txt) {
  int d1, d2;
  if (strlen(txt) < 3) {
    *txt = '\0';
    return txt - 1;
  }
  d1 = hextoint(*(txt + 1));
  d2 = hextoint(*(txt + 2));
  if (d1 < 0 || d2 < 0) {
    strcpy(txt, txt + 3);
    return txt - 1;
  } else {
    *txt = d1 * 16 + d2;
    strcpy(txt + 1, txt + 3);
    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   1040 <strlen@plt>
cmp    $0x3,%eax
jge    1166 <convert_query_hexchar+0x36>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    11ea <convert_query_hexchar+0xba>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%edi
call   1050 <hextoint@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movsbl 0x2(%rax),%edi
call   1050 <hextoint@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     119a <convert_query_hexchar+0x6a>
cmpl   $0x0,-0x18(%rbp)
jge    11bc <convert_query_hexchar+0x8c>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x3,%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    11ea <convert_query_hexchar+0xba>
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
call   1030 <strcpy@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   %rbx
push   %rax
mov    %rdi,%rbx
call   1040 <strlen@plt>
cmp    $0x2,%eax
jg     1145 <convert_query_hexchar+0x15>
movb   $0x0,(%rbx)
jmp    1183 <convert_query_hexchar+0x53>
movsbl 0x1(%rbx),%edi
call   1050 <hextoint@plt>
mov    %eax,%ebp
movsbl 0x2(%rbx),%edi
call   1050 <hextoint@plt>
test   %ebp,%ebp
js     1177 <convert_query_hexchar+0x47>
test   %eax,%eax
js     1177 <convert_query_hexchar+0x47>
lea    0x1(%rbx),%rdi
shl    $0x4,%ebp
add    %ebp,%eax
mov    %al,(%rbx)
lea    0x3(%rbx),%rsi
call   1030 <strcpy@plt>
jmp    1187 <convert_query_hexchar+0x57>
lea    0x3(%rbx),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
add    $0xffffffffffffffff,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
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
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @strlen(i8* %6)
%8 = icmp slt i32 %7, 3
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i8*, i8** %3
store i8 0, i8* %10
%11 = load i8*, i8** %3
%12 = getelementptr inbounds i8, i8* %11, i64 -1
store i8* %12, i8** %2
br label %47
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = getelementptr inbounds i8, i8* %14, i64 1
%16 = load i8, i8* %15
%17 = call i32 @hextoint(i8 signext %16)
store i32 %17, i32* %4
%18 = load i8*, i8** %3
%19 = getelementptr inbounds i8, i8* %18, i64 2
%20 = load i8, i8* %19
%21 = call i32 @hextoint(i8 signext %20)
store i32 %21, i32* %5
%22 = load i32, i32* %4
%23 = icmp slt i32 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %13
%25 = load i32, i32* %5
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %24, %13
%28 = load i8*, i8** %3
%29 = load i8*, i8** %3
%30 = getelementptr inbounds i8, i8* %29, i64 3
%31 = call i32 @strcpy(i8* %28, i8* %30)
%32 = load i8*, i8** %3
%33 = getelementptr inbounds i8, i8* %32, i64 -1
store i8* %33, i8** %2
br label %47
34: ; preds = %24
%35 = load i32, i32* %4
%36 = mul nsw i32 %35, 16
%37 = load i32, i32* %5
%38 = add nsw i32 %36, %37
%39 = trunc i32 %38 to i8
%40 = load i8*, i8** %3
store i8 %39, i8* %40
%41 = load i8*, i8** %3
%42 = getelementptr inbounds i8, i8* %41, i64 1
%43 = load i8*, i8** %3
%44 = getelementptr inbounds i8, i8* %43, i64 3
%45 = call i32 @strcpy(i8* %42, i8* %44)
%46 = load i8*, i8** %3
store i8* %46, i8** %2
br label %47
47: ; preds = %34, %27, %9
%48 = load i8*, i8** %2
ret i8* %48
}

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 %26
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 %20
16: ; preds = %6
%17 = getelementptr inbounds i8, i8* %0, i64 3
%18 = tail call i32 @strcpy(i8* %0, i8* %17)
%19 = getelementptr inbounds i8, i8* %0, i64 -1
br label %26
20: ; preds = %6
%21 = shl nsw i32 %9, 4
%22 = add nsw i32 %12, %21
%23 = trunc i32 %22 to i8
store i8 %23, i8* %0
%24 = getelementptr inbounds i8, i8* %0, i64 3
%25 = tail call i32 @strcpy(i8* %7, i8* %24)
br label %26
26: ; preds = %20, %16, %4
%27 = phi i8* [ %5, %4 ], [ %19, %16 ], [ %0, %20 ]
ret i8* %27
}