#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 */
typedef char uint8_t;
typedef int int8_t;

/* Variables and functions */
scalar_t__ isdigit(char);
char tolowercase(char);
int8_t parseHexDigit(uint8_t digit) {
  digit = tolowercase(digit);
  if (isdigit(digit))
    return (int8_t)digit - '0';
  if ((digit >= 'a') && (digit <= 'f'))
    return (int8_t)digit + 0xA - 'a';
  return -1;
}


x86 assembly (clang, -O0):
<parseHexDigit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %dil,%al
mov    %al,-0x5(%rbp)
movsbl -0x5(%rbp),%edi
call   1040 <tolowercase@plt>
mov    %al,-0x5(%rbp)
movsbl -0x5(%rbp),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     115c <parseHexDigit+0x3c>
movsbl -0x5(%rbp),%eax
sub    $0x30,%eax
mov    %eax,-0x4(%rbp)
jmp    118f <parseHexDigit+0x6f>
movsbl -0x5(%rbp),%eax
cmp    $0x61,%eax
jl     1188 <parseHexDigit+0x68>
movsbl -0x5(%rbp),%eax
cmp    $0x66,%eax
jg     1188 <parseHexDigit+0x68>
movsbl -0x5(%rbp),%eax
add    $0xa,%eax
sub    $0x61,%eax
mov    %eax,-0x4(%rbp)
jmp    118f <parseHexDigit+0x6f>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parseHexDigit>:
push   %r14
push   %rbx
push   %rax
call   1040 <tolowercase@plt>
mov    %eax,%r14d
movsbl %al,%ebx
mov    %ebx,%edi
call   1030 <isdigit@plt>
test   %rax,%rax
je     1140 <parseHexDigit+0x20>
add    $0xffffffd0,%ebx
jmp    1154 <parseHexDigit+0x34>
lea    -0x61(%r14),%eax
mov    $0xffffffff,%ebx
cmp    $0x5,%al
ja     1154 <parseHexDigit+0x34>
movzbl %r14b,%ebx
add    $0xffffffa9,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @parseHexDigit(i8 signext %0) {
%2 = alloca i32
%3 = alloca i8
store i8 %0, i8* %3
%4 = load i8, i8* %3
%5 = call signext i8 @tolowercase(i8 signext %4)
store i8 %5, i8* %3
%6 = load i8, i8* %3
%7 = call i64 @isdigit(i8 signext %6)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i8, i8* %3
%11 = sext i8 %10 to i32
%12 = sub nsw i32 %11, 48
store i32 %12, i32* %2
br label %27
13: ; preds = %1
%14 = load i8, i8* %3
%15 = sext i8 %14 to i32
%16 = icmp sge i32 %15, 97
br i1 %16, label %17, label %26
17: ; preds = %13
%18 = load i8, i8* %3
%19 = sext i8 %18 to i32
%20 = icmp sle i32 %19, 102
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = load i8, i8* %3
%23 = sext i8 %22 to i32
%24 = add nsw i32 %23, 10
%25 = sub nsw i32 %24, 97
store i32 %25, i32* %2
br label %27
26: ; preds = %17, %13
store i32 -1, i32* %2
br label %27
27: ; preds = %26, %21, %9
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
define i32 @parseHexDigit(i8 signext %0) {
%2 = tail call signext i8 @tolowercase(i8 signext %0)
%3 = tail call i64 @isdigit(i8 signext %2)
%4 = icmp eq i64 %3, 0
br i1 %4, label %8, label %5
5: ; preds = %1
%6 = sext i8 %2 to i32
%7 = add nsw i32 %6, -48
br label %14
8: ; preds = %1
%9 = add i8 %2, -97
%10 = icmp ult i8 %9, 6
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = zext i8 %2 to i32
%13 = add nsw i32 %12, -87
br label %14
14: ; preds = %8, %11, %5
%15 = phi i32 [ %7, %5 ], [ %13, %11 ], [ -1, %8 ]
ret i32 %15
}