#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__ isdigit(unsigned char);
scalar_t__ isxdigit(unsigned char);
char tolower(unsigned char);
int hex_to_dec(int ch) {
  if (isdigit((unsigned char)(ch)))
    return ch - '0';
  else if (isxdigit((unsigned char)(ch)))
    return tolower((unsigned char)(ch)) - 'a' + 10;
  else
    return -1;
}


x86 assembly (clang, -O0):
<hex_to_dec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
movzbl %al,%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     115e <hex_to_dec+0x2e>
mov    -0x8(%rbp),%eax
sub    $0x30,%eax
mov    %eax,-0x4(%rbp)
jmp    1196 <hex_to_dec+0x66>
mov    -0x8(%rbp),%eax
movzbl %al,%edi
call   1050 <isxdigit@plt>
cmp    $0x0,%rax
je     118f <hex_to_dec+0x5f>
mov    -0x8(%rbp),%eax
movzbl %al,%edi
call   1040 <tolower@plt>
movsbl %al,%eax
sub    $0x61,%eax
add    $0xa,%eax
mov    %eax,-0x4(%rbp)
jmp    1196 <hex_to_dec+0x66>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_to_dec>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
movzbl %bl,%ebp
mov    %ebp,%edi
call   1030 <isdigit@plt>
test   %rax,%rax
je     1149 <hex_to_dec+0x19>
add    $0xffffffd0,%ebx
jmp    1169 <hex_to_dec+0x39>
mov    %ebp,%edi
call   1050 <isxdigit@plt>
test   %rax,%rax
je     1164 <hex_to_dec+0x34>
mov    %ebp,%edi
call   1040 <tolower@plt>
movsbl %al,%ebx
add    $0xffffffa9,%ebx
jmp    1169 <hex_to_dec+0x39>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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