#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 hex2num(char);
int strlen(char *);
int /*<<< orphan*/ strncasecmp(char *, char *, int);
int ishexstring(void *hex) {
  int i, a;
  char *p = hex;
  int len = strlen(p);
  if (!strncasecmp("0x", p, 2)) {
    p += 2;
    len -= 2;
  }
  for (i = 0; i < len; i++) {
    a = hex2num(*p);
    if (a < 0)
      return -1;
    p++;
  }
  return 0;
}


x86 assembly (clang, -O0):
<ishexstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xea5(%rip),%rdi
mov    $0x2,%edx
call   1040 <strncasecmp@plt>
cmp    $0x0,%eax
jne    1183 <ishexstring+0x53>
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    11d5 <ishexstring+0xa5>
mov    -0x20(%rbp),%rax
movsbl (%rax),%edi
call   1050 <hex2num@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11bb <ishexstring+0x8b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dc <ishexstring+0xac>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    118a <ishexstring+0x5a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ishexstring>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %eax,%ebx
lea    0xeba(%rip),%rdi
mov    %r14,%rsi
mov    $0x2,%edx
call   1040 <strncasecmp@plt>
lea    -0x2(%rbx),%ecx
test   %eax,%eax
cmovne %ebx,%ecx
test   %ecx,%ecx
jle    1187 <ishexstring+0x57>
xor    %edx,%edx
test   %eax,%eax
sete   %dl
lea    (%r14,%rdx,2),%r15
mov    %ecx,%r14d
xor    %ebx,%ebx
nop
movsbl (%r15,%rbx,1),%edi
call   1050 <hex2num@plt>
test   %eax,%eax
js     118b <ishexstring+0x5b>
add    $0x1,%rbx
cmp    %ebx,%r14d
jne    1170 <ishexstring+0x40>
xor    %eax,%eax
jmp    1190 <ishexstring+0x60>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"0x\00"
define i32 @ishexstring(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
store i8* %8, i8** %6
%9 = load i8*, i8** %6
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %7
%11 = load i8*, i8** %6
%12 = call i32 @strncasecmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %11, i32 2)
%13 = icmp ne i32 %12, 0
br i1 %13, label %19, label %14
14: ; preds = %1
%15 = load i8*, i8** %6
%16 = getelementptr inbounds i8, i8* %15, i64 2
store i8* %16, i8** %6
%17 = load i32, i32* %7
%18 = sub nsw i32 %17, 2
store i32 %18, i32* %7
br label %19
19: ; preds = %14, %1
store i32 0, i32* %4
br label %20
20: ; preds = %34, %19
%21 = load i32, i32* %4
%22 = load i32, i32* %7
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %37
24: ; preds = %20
%25 = load i8*, i8** %6
%26 = load i8, i8* %25
%27 = call i32 @hex2num(i8 signext %26)
store i32 %27, i32* %5
%28 = load i32, i32* %5
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %24
store i32 -1, i32* %2
br label %38
31: ; preds = %24
%32 = load i8*, i8** %6
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %6
br label %34
34: ; preds = %31
%35 = load i32, i32* %4
%36 = add nsw i32 %35, 1
store i32 %36, i32* %4
br label %20
37: ; preds = %20
store i32 0, i32* %2
br label %38
38: ; preds = %37, %30
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
@.str = [3 x i8] c"0x\00"
define i32 @ishexstring(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = tail call i32 @strncasecmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0, i32 2)
%4 = icmp eq i32 %3, 0
%5 = add nsw i32 %2, -2
%6 = select i1 %4, i32 %5, i32 %2
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %21
8: ; preds = %1
%9 = select i1 %4, i64 2, i64 0
%10 = getelementptr i8, i8* %0, i64 %9
br label %11
11: ; preds = %8, %17
%12 = phi i8* [ %18, %17 ], [ %10, %8 ]
%13 = phi i32 [ %19, %17 ], [ 0, %8 ]
%14 = load i8, i8* %12
%15 = tail call i32 @hex2num(i8 signext %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %11
%18 = getelementptr inbounds i8, i8* %12, i64 1
%19 = add nuw nsw i32 %13, 1
%20 = icmp eq i32 %19, %6
br i1 %20, label %21, label %11
21: ; preds = %11, %17, %1
%22 = phi i32 [ 0, %1 ], [ 0, %17 ], [ -1, %11 ]
ret i32 %22
}