#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 /*<<< orphan*/ assert(char);
int /*<<< orphan*/ error_log(char *, char, char);
int /*<<< orphan*/ hex_ischar(char);
scalar_t__ isdigit(char);
char tolower(char);
int hex_to_dec(char l, char r) {
  int decimal;
  assert(l);
  assert(r);
  if (!hex_ischar(l) || !hex_ischar(r)) {
    error_log("Invalid hex character: 0x%c%c.", l, r);
    return -1;
  }
  l = tolower(l);
  r = tolower(r);
  l = isdigit(l) ? l - 48 : l - 87;
  r = isdigit(r) ? r - 48 : r - 87;
  decimal = (l << 4) + r;
  return decimal;
}


x86 assembly (clang, -O0):
<hex_to_dec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %dil,%cl
mov    %cl,-0x5(%rbp)
mov    %al,-0x6(%rbp)
movsbl -0x5(%rbp),%edi
call   1040 <assert@plt>
movsbl -0x6(%rbp),%edi
call   1040 <assert@plt>
movsbl -0x5(%rbp),%edi
call   1070 <hex_ischar@plt>
cmp    $0x0,%eax
je     119a <hex_to_dec+0x4a>
movsbl -0x6(%rbp),%edi
call   1070 <hex_ischar@plt>
cmp    $0x0,%eax
jne    11bc <hex_to_dec+0x6c>
mov    -0x5(%rbp),%al
lea    0xe5c(%rip),%rdi
movsbl %al,%esi
movsbl -0x6(%rbp),%edx
call   1060 <error_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124e <hex_to_dec+0xfe>
movsbl -0x5(%rbp),%edi
call   1050 <tolower@plt>
mov    %al,-0x5(%rbp)
movsbl -0x6(%rbp),%edi
call   1050 <tolower@plt>
mov    %al,-0x6(%rbp)
movsbl -0x5(%rbp),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     11f6 <hex_to_dec+0xa6>
movsbl -0x5(%rbp),%eax
sub    $0x30,%eax
mov    %eax,-0x10(%rbp)
jmp    1200 <hex_to_dec+0xb0>
movsbl -0x5(%rbp),%eax
sub    $0x57,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %al,-0x5(%rbp)
movsbl -0x6(%rbp),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     1228 <hex_to_dec+0xd8>
movsbl -0x6(%rbp),%eax
sub    $0x30,%eax
mov    %eax,-0x14(%rbp)
jmp    1232 <hex_to_dec+0xe2>
movsbl -0x6(%rbp),%eax
sub    $0x57,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %al,-0x6(%rbp)
movsbl -0x5(%rbp),%eax
shl    $0x4,%eax
movsbl -0x6(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_to_dec>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%ebx
call   1040 <assert@plt>
mov    %ebp,%edi
call   1040 <assert@plt>
mov    %ebx,%edi
call   1070 <hex_ischar@plt>
movsbl %bpl,%ebp
test   %eax,%eax
je     11db <hex_to_dec+0x8b>
mov    %ebp,%edi
call   1070 <hex_ischar@plt>
test   %eax,%eax
je     11db <hex_to_dec+0x8b>
movsbl %bl,%edi
call   1050 <tolower@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1050 <tolower@plt>
mov    %eax,%ebp
movsbl %bl,%ebx
mov    %ebx,%edi
call   1030 <isdigit@plt>
test   %rax,%rax
mov    $0xa9,%r14d
mov    $0xd0,%r15d
mov    $0xd0,%eax
cmove  %r14d,%eax
add    %al,%bl
movsbl %bpl,%ebp
mov    %ebp,%edi
call   1030 <isdigit@plt>
test   %rax,%rax
cmove  %r14d,%r15d
add    %r15b,%bpl
movsbl %bl,%ecx
shl    $0x4,%ecx
movsbl %bpl,%eax
add    %ecx,%eax
jmp    11f1 <hex_to_dec+0xa1>
movsbl %bl,%esi
lea    0xe1b(%rip),%rdi
mov    %ebp,%edx
call   1060 <error_log@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [31 x i8] c"Invalid hex character: 0x%c%c.\00"
define i32 @hex_to_dec(i8 signext %0, i8 signext %1) {
%3 = alloca i32
%4 = alloca i8
%5 = alloca i8
%6 = alloca i32
store i8 %0, i8* %4
store i8 %1, i8* %5
%7 = load i8, i8* %4
%8 = call i32 @assert(i8 signext %7)
%9 = load i8, i8* %5
%10 = call i32 @assert(i8 signext %9)
%11 = load i8, i8* %4
%12 = call i32 @hex_ischar(i8 signext %11)
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i8, i8* %5
%16 = call i32 @hex_ischar(i8 signext %15)
%17 = icmp ne i32 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %14, %2
%19 = load i8, i8* %4
%20 = load i8, i8* %5
%21 = call i32 @error_log(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8 signext %19, i8 signext %20)
store i32 -1, i32* %3
br label %62
22: ; preds = %14
%23 = load i8, i8* %4
%24 = call signext i8 @tolower(i8 signext %23)
store i8 %24, i8* %4
%25 = load i8, i8* %5
%26 = call signext i8 @tolower(i8 signext %25)
store i8 %26, i8* %5
%27 = load i8, i8* %4
%28 = call i64 @isdigit(i8 signext %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %22
%31 = load i8, i8* %4
%32 = sext i8 %31 to i32
%33 = sub nsw i32 %32, 48
br label %38
34: ; preds = %22
%35 = load i8, i8* %4
%36 = sext i8 %35 to i32
%37 = sub nsw i32 %36, 87
br label %38
38: ; preds = %34, %30
%39 = phi i32 [ %33, %30 ], [ %37, %34 ]
%40 = trunc i32 %39 to i8
store i8 %40, i8* %4
%41 = load i8, i8* %5
%42 = call i64 @isdigit(i8 signext %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %38
%45 = load i8, i8* %5
%46 = sext i8 %45 to i32
%47 = sub nsw i32 %46, 48
br label %52
48: ; preds = %38
%49 = load i8, i8* %5
%50 = sext i8 %49 to i32
%51 = sub nsw i32 %50, 87
br label %52
52: ; preds = %48, %44
%53 = phi i32 [ %47, %44 ], [ %51, %48 ]
%54 = trunc i32 %53 to i8
store i8 %54, i8* %5
%55 = load i8, i8* %4
%56 = sext i8 %55 to i32
%57 = shl i32 %56, 4
%58 = load i8, i8* %5
%59 = sext i8 %58 to i32
%60 = add nsw i32 %57, %59
store i32 %60, i32* %6
%61 = load i32, i32* %6
store i32 %61, i32* %3
br label %62
62: ; preds = %52, %18
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@.str = [31 x i8] c"Invalid hex character: 0x%c%c.\00"
define i32 @hex_to_dec(i8 signext %0, i8 signext %1) {
%3 = tail call i32 @assert(i8 signext %0)
%4 = tail call i32 @assert(i8 signext %1)
%5 = tail call i32 @hex_ischar(i8 signext %0)
%6 = icmp eq i32 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = tail call i32 @hex_ischar(i8 signext %1)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7, %2
%11 = tail call i32 @error_log(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8 signext %0, i8 signext %1)
br label %27
12: ; preds = %7
%13 = tail call signext i8 @tolower(i8 signext %0)
%14 = tail call signext i8 @tolower(i8 signext %1)
%15 = tail call i64 @isdigit(i8 signext %13)
%16 = icmp eq i64 %15, 0
%17 = select i1 %16, i8 -87, i8 -48
%18 = add i8 %17, %13
%19 = tail call i64 @isdigit(i8 signext %14)
%20 = icmp eq i64 %19, 0
%21 = select i1 %20, i8 -87, i8 -48
%22 = add i8 %21, %14
%23 = sext i8 %18 to i32
%24 = shl nsw i32 %23, 4
%25 = sext i8 %22 to i32
%26 = add nsw i32 %24, %25
br label %27
27: ; preds = %12, %10
%28 = phi i32 [ %26, %12 ], [ -1, %10 ]
ret i32 %28
}