#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__ EINVAL;
scalar_t__ ERANGE;
scalar_t__ errno;
long strtol(char const *, int /*<<< orphan*/ *, int);
int parse_hex(const char *p, long minval, long maxval) {
  long v;
  errno = 0;
  v = strtol(p, NULL, 16);
  if (v <= minval || v >= maxval || errno == EINVAL || errno == ERANGE)
    v = -9999;
  return (int)v;
}


x86 assembly (clang, -O0):
<parse_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2ead(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1030 <strtol@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jle    1198 <parse_hex+0x88>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    1198 <parse_hex+0x88>
mov    0x2e6d(%rip),%rax
mov    (%rax),%rax
mov    0x2e5b(%rip),%rcx
cmp    (%rcx),%rax
je     1198 <parse_hex+0x88>
mov    0x2e53(%rip),%rax
mov    (%rax),%rax
mov    0x2e59(%rip),%rcx
cmp    (%rcx),%rax
jne    11a0 <parse_hex+0x90>
movq   $0xffffffffffffd8f1,-0x20(%rbp)
ff
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_hex>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    0x2eb6(%rip),%r15
movq   $0x0,(%r15)
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <strtol@plt>
cmp    %rbx,%rax
jle    115a <parse_hex+0x4a>
cmp    %r14,%rax
jge    115a <parse_hex+0x4a>
mov    (%r15),%rcx
mov    0x2e87(%rip),%rdx
cmp    (%rdx),%rcx
je     115a <parse_hex+0x4a>
mov    0x2e93(%rip),%rdx
cmp    (%rdx),%rcx
jne    1161 <parse_hex+0x51>
mov    $0xffffffffffffd8f1,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@EINVAL = global i64 0
@ERANGE = global i64 0
define i32 @parse_hex(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
store i64 %2, i64* %6
store i64 0, i64* @errno
%8 = load i8*, i8** %4
%9 = call i64 @strtol(i8* %8, i32* null, i32 16)
store i64 %9, i64* %7
%10 = load i64, i64* %7
%11 = load i64, i64* %5
%12 = icmp sle i64 %10, %11
br i1 %12, label %25, label %13
13: ; preds = %3
%14 = load i64, i64* %7
%15 = load i64, i64* %6
%16 = icmp sge i64 %14, %15
br i1 %16, label %25, label %17
17: ; preds = %13
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINVAL
%20 = icmp eq i64 %18, %19
br i1 %20, label %25, label %21
21: ; preds = %17
%22 = load i64, i64* @errno
%23 = load i64, i64* @ERANGE
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %21, %17, %13, %3
store i64 -9999, i64* %7
br label %26
26: ; preds = %25, %21
%27 = load i64, i64* %7
%28 = trunc i64 %27 to i32
ret i32 %28
}

LLVM IR (-O2):
@errno = global i64 0
@EINVAL = global i64 0
@ERANGE = global i64 0
define i32 @parse_hex(i8* nocapture %0, i64 %1, i64 %2) {
store i64 0, i64* @errno
%4 = tail call i64 @strtol(i8* nocapture %0, i32* null, i32 16)
%5 = icmp sgt i64 %4, %1
%6 = icmp slt i64 %4, %2
%7 = and i1 %5, %6
br i1 %7, label %8, label %15
8: ; preds = %3
%9 = load i64, i64* @errno
%10 = load i64, i64* @EINVAL
%11 = icmp eq i64 %9, %10
%12 = load i64, i64* @ERANGE
%13 = icmp eq i64 %9, %12
%14 = select i1 %11, i1 true, i1 %13
br i1 %14, label %15, label %16
15: ; preds = %8, %3
br label %16
16: ; preds = %8, %15
%17 = phi i64 [ -9999, %15 ], [ %4, %8 ]
%18 = trunc i64 %17 to i32
ret i32 %18
}