#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*/ getchr();
char peekchr();
int /*<<< orphan*/ putchr(char);
char getnibble(char echo) {
  char c = peekchr();
  int ret = -1;
  if ((c >= '0') && (c <= '9'))
    ret = c - '0';
  else if ((c >= 'A') && (c <= 'F'))
    ret = c - 0x37;
  else if ((c >= 'a') && (c <= 'f'))
    ret = c - 0x57;
  if (ret != -1) {
    getchr();
    if (echo)
      putchr(c);
  }
  return ret;
}


x86 assembly (clang, -O0):
<getnibble>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %dil,%al
mov    %al,-0x1(%rbp)
mov    $0x0,%al
call   1050 <peekchr@plt>
mov    %al,-0x2(%rbp)
movl   $0xffffffff,-0x8(%rbp)
movsbl -0x2(%rbp),%eax
cmp    $0x30,%eax
jl     1178 <getnibble+0x48>
movsbl -0x2(%rbp),%eax
cmp    $0x39,%eax
jg     1178 <getnibble+0x48>
movsbl -0x2(%rbp),%eax
sub    $0x30,%eax
mov    %eax,-0x8(%rbp)
jmp    11cf <getnibble+0x9f>
movsbl -0x2(%rbp),%eax
cmp    $0x41,%eax
jl     11a1 <getnibble+0x71>
movsbl -0x2(%rbp),%eax
cmp    $0x46,%eax
jg     11a1 <getnibble+0x71>
movsbl -0x2(%rbp),%eax
sub    $0x37,%eax
mov    %eax,-0x8(%rbp)
jmp    11ca <getnibble+0x9a>
movsbl -0x2(%rbp),%eax
cmp    $0x61,%eax
jl     11c5 <getnibble+0x95>
movsbl -0x2(%rbp),%eax
cmp    $0x66,%eax
jg     11c5 <getnibble+0x95>
movsbl -0x2(%rbp),%eax
sub    $0x57,%eax
mov    %eax,-0x8(%rbp)
jmp    11ca <getnibble+0x9a>
jmp    11cf <getnibble+0x9f>
cmpl   $0xffffffff,-0x8(%rbp)
je     11f8 <getnibble+0xc8>
mov    $0x0,%al
call   1030 <getchr@plt>
cmpb   $0x0,-0x1(%rbp)
je     11f3 <getnibble+0xc3>
movsbl -0x2(%rbp),%edi
call   1040 <putchr@plt>
jmp    11f8 <getnibble+0xc8>
mov    -0x8(%rbp),%eax
movsbl %al,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getnibble>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
xor    %eax,%eax
call   1050 <peekchr@plt>
movsbl %al,%ebx
lea    -0x30(%rbx),%ecx
cmp    $0x9,%cl
ja     1153 <getnibble+0x23>
lea    -0x30(%rbx),%ebp
cmp    $0xffffffff,%ebp
jne    1163 <getnibble+0x33>
jmp    118b <getnibble+0x5b>
lea    -0x41(%rax),%ecx
cmp    $0x5,%cl
ja     1178 <getnibble+0x48>
lea    -0x37(%rbx),%ebp
cmp    $0xffffffff,%ebp
je     118b <getnibble+0x5b>
xor    %eax,%eax
call   1030 <getchr@plt>
test   %r14b,%r14b
je     1190 <getnibble+0x60>
mov    %ebx,%edi
call   1040 <putchr@plt>
jmp    1190 <getnibble+0x60>
add    $0x9f,%al
mov    $0xffffffff,%ebp
cmp    $0x5,%al
ja     1190 <getnibble+0x60>
lea    -0x57(%rbx),%ebp
cmp    $0xffffffff,%ebp
jne    1163 <getnibble+0x33>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define signext i8 @getnibble(i8 signext %0) {
%2 = tail call signext i8 (...) @peekchr()
%3 = sext i8 %2 to i32
%4 = add i8 %2, -48
%5 = icmp ult i8 %4, 10
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = add nsw i32 %3, -48
br label %17
8: ; preds = %1
%9 = add i8 %2, -65
%10 = icmp ult i8 %9, 6
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = add nsw i32 %3, -55
br label %17
13: ; preds = %8
%14 = add i8 %2, -97
%15 = icmp ult i8 %14, 6
%16 = add nsw i32 %3, -87
br i1 %15, label %17, label %25
17: ; preds = %13, %11, %6
%18 = phi i32 [ %7, %6 ], [ %12, %11 ], [ %16, %13 ]
%19 = icmp eq i32 %18, -1
br i1 %19, label %25, label %20
20: ; preds = %17
%21 = tail call i32 (...) @getchr()
%22 = icmp eq i8 %0, 0
br i1 %22, label %25, label %23
23: ; preds = %20
%24 = tail call i32 @putchr(i8 signext %2)
br label %25
25: ; preds = %13, %20, %23, %17
%26 = phi i32 [ %18, %20 ], [ %18, %23 ], [ -1, %17 ], [ -1, %13 ]
%27 = trunc i32 %26 to i8
ret i8 %27
}