#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__ isalpha(char);
scalar_t__ isdigit(char);
int read_hex_nible(char *s) {
  char c = *s;
  if (isdigit(c)) {
    return c - '0';
  } else if (isalpha(c)) {
    if ('A' <= c && c <= 'F')
      return c - 'A' + 10;
    else if ('a' <= c && c <= 'f')
      return c - 'a' + 10;
  }
  return -1;
}


x86 assembly (clang, -O0):
<read_hex_nible>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x11(%rbp)
movsbl -0x11(%rbp),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     1157 <read_hex_nible+0x37>
movsbl -0x11(%rbp),%eax
sub    $0x30,%eax
mov    %eax,-0x4(%rbp)
jmp    11e0 <read_hex_nible+0xc0>
movsbl -0x11(%rbp),%edi
call   1040 <isalpha@plt>
cmp    $0x0,%rax
je     11d4 <read_hex_nible+0xb4>
movsbl -0x11(%rbp),%ecx
mov    $0x41,%eax
cmp    %ecx,%eax
jg     119a <read_hex_nible+0x7a>
movsbl -0x11(%rbp),%eax
cmp    $0x46,%eax
jg     119a <read_hex_nible+0x7a>
movsbl -0x11(%rbp),%eax
sub    $0x41,%eax
add    $0xa,%eax
mov    %eax,-0x4(%rbp)
jmp    11e0 <read_hex_nible+0xc0>
movsbl -0x11(%rbp),%ecx
mov    $0x61,%eax
cmp    %ecx,%eax
jg     11ca <read_hex_nible+0xaa>
movsbl -0x11(%rbp),%eax
cmp    $0x66,%eax
jg     11ca <read_hex_nible+0xaa>
movsbl -0x11(%rbp),%eax
sub    $0x61,%eax
add    $0xa,%eax
mov    %eax,-0x4(%rbp)
jmp    11e0 <read_hex_nible+0xc0>
jmp    11cf <read_hex_nible+0xaf>
jmp    11d4 <read_hex_nible+0xb4>
jmp    11d9 <read_hex_nible+0xb9>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_hex_nible>:
push   %rbx
movsbl (%rdi),%ebx
mov    %ebx,%edi
call   1030 <isdigit@plt>
test   %rax,%rax
je     1137 <read_hex_nible+0x17>
add    $0xffffffd0,%ebx
mov    %ebx,%eax
pop    %rbx
ret
mov    %ebx,%edi
call   1040 <isalpha@plt>
test   %rax,%rax
je     114f <read_hex_nible+0x2f>
lea    -0x41(%rbx),%eax
cmp    $0x5,%al
ja     1156 <read_hex_nible+0x36>
add    $0xffffffc9,%ebx
jmp    1133 <read_hex_nible+0x13>
mov    $0xffffffff,%ebx
jmp    1133 <read_hex_nible+0x13>
lea    -0x61(%rbx),%eax
add    $0xffffffa9,%ebx
cmp    $0x6,%al
mov    $0xffffffff,%eax
cmovb  %ebx,%eax
mov    %eax,%ebx
jmp    1133 <read_hex_nible+0x13>

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

LLVM IR (-O2):
define i32 @read_hex_nible(i8* nocapture %0) {
%2 = load i8, i8* %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 %22
8: ; preds = %1
%9 = tail call i64 @isalpha(i8 signext %2)
%10 = icmp eq i64 %9, 0
br i1 %10, label %22, label %11
11: ; preds = %8
%12 = sext i8 %2 to i32
%13 = add i8 %2, -65
%14 = icmp ult i8 %13, 6
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = add nsw i32 %12, -55
br label %22
17: ; preds = %11
%18 = add i8 %2, -97
%19 = icmp ult i8 %18, 6
%20 = add nsw i32 %12, -87
%21 = select i1 %19, i32 %20, i32 -1
br label %22
22: ; preds = %17, %8, %15, %5
%23 = phi i32 [ %7, %5 ], [ %16, %15 ], [ -1, %8 ], [ %21, %17 ]
ret i32 %23
}