#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;

/* Variables and functions */
scalar_t__ isalpha(char);
scalar_t__ isdigit(char);
char tolower(char);
uint8_t hex2num(char c, int *err) {
  *err = 0;
  if (isdigit(c))
    return c - '0';
  if (isalpha(c) && tolower(c) >= 'a' && tolower(c) <= 'f')
    return tolower(c) - 'a' + 10;
  *err = 1;
  return 0;
}


x86 assembly (clang, -O0):
<hex2num>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %dil,%al
mov    %al,-0x2(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
movsbl -0x2(%rbp),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     116e <hex2num+0x3e>
movsbl -0x2(%rbp),%eax
sub    $0x30,%eax
mov    %al,-0x1(%rbp)
jmp    11d3 <hex2num+0xa3>
movsbl -0x2(%rbp),%edi
call   1050 <isalpha@plt>
cmp    $0x0,%rax
je     11c5 <hex2num+0x95>
movsbl -0x2(%rbp),%edi
call   1040 <tolower@plt>
movsbl %al,%eax
cmp    $0x61,%eax
jl     11c5 <hex2num+0x95>
movsbl -0x2(%rbp),%edi
call   1040 <tolower@plt>
movsbl %al,%eax
cmp    $0x66,%eax
jg     11c5 <hex2num+0x95>
movsbl -0x2(%rbp),%edi
call   1040 <tolower@plt>
movsbl %al,%eax
sub    $0x61,%eax
add    $0xa,%eax
mov    %al,-0x1(%rbp)
jmp    11d3 <hex2num+0xa3>
mov    -0x10(%rbp),%rax
movl   $0x1,(%rax)
movb   $0x0,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex2num>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
movl   $0x0,(%rsi)
call   1030 <isdigit@plt>
test   %rax,%rax
je     114d <hex2num+0x1d>
lea    -0x30(%rbp),%eax
jmp    1186 <hex2num+0x56>
movsbl %bpl,%ebp
mov    %ebp,%edi
call   1050 <isalpha@plt>
test   %rax,%rax
je     117e <hex2num+0x4e>
mov    %ebp,%edi
call   1040 <tolower@plt>
cmp    $0x61,%al
jl     117e <hex2num+0x4e>
mov    %ebp,%edi
call   1040 <tolower@plt>
cmp    $0x66,%al
jg     117e <hex2num+0x4e>
mov    %ebp,%edi
call   1040 <tolower@plt>
add    $0xa9,%al
jmp    1186 <hex2num+0x56>
movl   $0x1,(%rbx)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
define signext i8 @hex2num(i8 signext %0, i32* nocapture writeonly %1) {
store i32 0, i32* %1
%3 = tail call i64 @isdigit(i8 signext %0)
%4 = icmp eq i64 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = add i8 %0, -48
br label %20
7: ; preds = %2
%8 = tail call i64 @isalpha(i8 signext %0)
%9 = icmp eq i64 %8, 0
br i1 %9, label %19, label %10
10: ; preds = %7
%11 = tail call signext i8 @tolower(i8 signext %0)
%12 = icmp sgt i8 %11, 96
br i1 %12, label %13, label %19
13: ; preds = %10
%14 = tail call signext i8 @tolower(i8 signext %0)
%15 = icmp slt i8 %14, 103
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = tail call signext i8 @tolower(i8 signext %0)
%18 = add i8 %17, -87
br label %20
19: ; preds = %13, %10, %7
store i32 1, i32* %1
br label %20
20: ; preds = %19, %16, %5
%21 = phi i8 [ %6, %5 ], [ %18, %16 ], [ 0, %19 ]
ret i8 %21
}