#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(int);
int convert_nibble(int ch) {
  if (isdigit(ch))
    return (ch - '0');
  if (ch >= 'a' && ch <= 'f')
    return (ch - 'a' + 10);
  if (ch >= 'A' && ch <= 'F')
    return (ch - 'A' + 10);
  return -1;
}


x86 assembly (clang, -O0):
<convert_nibble>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     113b <convert_nibble+0x2b>
mov    -0x8(%rbp),%eax
sub    $0x30,%eax
mov    %eax,-0x4(%rbp)
jmp    118c <convert_nibble+0x7c>
cmpl   $0x61,-0x8(%rbp)
jl     1160 <convert_nibble+0x50>
cmpl   $0x66,-0x8(%rbp)
jg     1160 <convert_nibble+0x50>
mov    -0x8(%rbp),%eax
sub    $0x61,%eax
add    $0xa,%eax
mov    %eax,-0x4(%rbp)
jmp    118c <convert_nibble+0x7c>
cmpl   $0x41,-0x8(%rbp)
jl     1185 <convert_nibble+0x75>
cmpl   $0x46,-0x8(%rbp)
jg     1185 <convert_nibble+0x75>
mov    -0x8(%rbp),%eax
sub    $0x41,%eax
add    $0xa,%eax
mov    %eax,-0x4(%rbp)
jmp    118c <convert_nibble+0x7c>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convert_nibble>:
push   %rbx
mov    %edi,%ebx
call   1030 <isdigit@plt>
test   %rax,%rax
je     1122 <convert_nibble+0x12>
add    $0xffffffd0,%ebx
jmp    1142 <convert_nibble+0x32>
lea    -0x61(%rbx),%eax
cmp    $0x5,%eax
ja     112f <convert_nibble+0x1f>
add    $0xffffffa9,%ebx
jmp    1142 <convert_nibble+0x32>
lea    -0x41(%rbx),%eax
add    $0xffffffc9,%ebx
cmp    $0x6,%eax
mov    $0xffffffff,%eax
cmovb  %ebx,%eax
mov    %eax,%ebx
mov    %ebx,%eax
pop    %rbx
ret

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

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