#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);
int /*<<< orphan*/ printf(char *, unsigned char, unsigned char);
unsigned char toupper(unsigned char);
unsigned char hexchar2num(unsigned char c) {
  if (!(('a' <= c && c <= 'f') || ('A' <= c && c <= 'F') || isdigit(c)))
    return 0;
  printf("before: %c (%d)\n", c, c);
  c = toupper(c);
  c -= 0x30;
  if (c >= 0xa)
    c -= 7;
  printf("after : %c (%d)\n", c, c);
  return c;
}


x86 assembly (clang, -O0):
<hexchar2num>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %dil,%al
mov    %al,-0x2(%rbp)
movzbl -0x2(%rbp),%ecx
mov    $0x61,%eax
cmp    %ecx,%eax
jg     115c <hexchar2num+0x2c>
movzbl -0x2(%rbp),%eax
cmp    $0x66,%eax
jle    1196 <hexchar2num+0x66>
movzbl -0x2(%rbp),%ecx
mov    $0x41,%eax
cmp    %ecx,%eax
jg     117a <hexchar2num+0x4a>
movzbl -0x2(%rbp),%eax
cmp    $0x46,%eax
jle    1196 <hexchar2num+0x66>
movzbl -0x2(%rbp),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
jne    1196 <hexchar2num+0x66>
movb   $0x0,-0x1(%rbp)
jmp    11f5 <hexchar2num+0xc5>
mov    -0x2(%rbp),%al
lea    0xe60(%rip),%rdi
movzbl %al,%esi
movzbl -0x2(%rbp),%edx
call   1050 <printf@plt>
movzbl -0x2(%rbp),%edi
call   1040 <toupper@plt>
mov    %al,-0x2(%rbp)
movzbl -0x2(%rbp),%eax
sub    $0x30,%eax
mov    %al,-0x2(%rbp)
movzbl -0x2(%rbp),%eax
cmp    $0xa,%eax
jl     11d9 <hexchar2num+0xa9>
movzbl -0x2(%rbp),%eax
sub    $0x7,%eax
mov    %al,-0x2(%rbp)
mov    -0x2(%rbp),%al
lea    0xe2e(%rip),%rdi
movzbl %al,%esi
movzbl -0x2(%rbp),%edx
call   1050 <printf@plt>
mov    -0x2(%rbp),%al
mov    %al,-0x1(%rbp)
movzbl -0x1(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexchar2num>:
push   %rbx
lea    -0x41(%rdi),%eax
movzbl %dil,%ebx
cmp    $0x25,%al
ja     118c <hexchar2num+0x5c>
movzbl %al,%eax
movabs $0x3f0000003f,%rcx
bt     %rax,%rcx
jae    118c <hexchar2num+0x5c>
lea    0xeaa(%rip),%rdi
mov    %ebx,%esi
mov    %ebx,%edx
call   1050 <printf@plt>
mov    %ebx,%edi
call   1040 <toupper@plt>
lea    -0x30(%rax),%ecx
add    $0xc9,%al
cmp    $0xa,%cl
movzbl %al,%eax
movzbl %cl,%ebx
cmovae %eax,%ebx
movzbl %bl,%edx
lea    0xe90(%rip),%rdi
mov    %edx,%esi
call   1050 <printf@plt>
mov    %ebx,%eax
pop    %rbx
ret
mov    %ebx,%edi
call   1030 <isdigit@plt>
test   %rax,%rax
jne    114f <hexchar2num+0x1f>
xor    %ebx,%ebx
jmp    1188 <hexchar2num+0x58>

LLVM IR (-O0):
@.str = [17 x i8] c"before: %c (%d)\0A\00"
@.str.1 = [17 x i8] c"after : %c (%d)\0A\00"
define zeroext i8 @hexchar2num(i8 zeroext %0) {
%2 = alloca i8
%3 = alloca i8
store i8 %0, i8* %3
%4 = load i8, i8* %3
%5 = zext i8 %4 to i32
%6 = icmp sle i32 97, %5
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = load i8, i8* %3
%9 = zext i8 %8 to i32
%10 = icmp sle i32 %9, 102
br i1 %10, label %24, label %11
11: ; preds = %7, %1
%12 = load i8, i8* %3
%13 = zext i8 %12 to i32
%14 = icmp sle i32 65, %13
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = load i8, i8* %3
%17 = zext i8 %16 to i32
%18 = icmp sle i32 %17, 70
br i1 %18, label %24, label %19
19: ; preds = %15, %11
%20 = load i8, i8* %3
%21 = call i64 @isdigit(i8 zeroext %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %24, label %23
23: ; preds = %19
store i8 0, i8* %2
br label %47
24: ; preds = %19, %15, %7
%25 = load i8, i8* %3
%26 = load i8, i8* %3
%27 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8 zeroext %25, i8 zeroext %26)
%28 = load i8, i8* %3
%29 = call zeroext i8 @toupper(i8 zeroext %28)
store i8 %29, i8* %3
%30 = load i8, i8* %3
%31 = zext i8 %30 to i32
%32 = sub nsw i32 %31, 48
%33 = trunc i32 %32 to i8
store i8 %33, i8* %3
%34 = load i8, i8* %3
%35 = zext i8 %34 to i32
%36 = icmp sge i32 %35, 10
br i1 %36, label %37, label %42
37: ; preds = %24
%38 = load i8, i8* %3
%39 = zext i8 %38 to i32
%40 = sub nsw i32 %39, 7
%41 = trunc i32 %40 to i8
store i8 %41, i8* %3
br label %42
42: ; preds = %37, %24
%43 = load i8, i8* %3
%44 = load i8, i8* %3
%45 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %43, i8 zeroext %44)
%46 = load i8, i8* %3
store i8 %46, i8* %2
br label %47
47: ; preds = %42, %23
%48 = load i8, i8* %2
ret i8 %48
}

LLVM IR (-O2):
@.str = [17 x i8] c"before: %c (%d)\0A\00"
@.str.1 = [17 x i8] c"after : %c (%d)\0A\00"
define zeroext i8 @hexchar2num(i8 zeroext %0) {
switch i8 %0, label %2 [
i8 102, label %5
i8 101, label %5
i8 100, label %5
i8 99, label %5
i8 98, label %5
i8 97, label %5
i8 70, label %5
i8 69, label %5
i8 68, label %5
i8 67, label %5
i8 66, label %5
i8 65, label %5
]
2: ; preds = %1
%3 = tail call i64 @isdigit(i8 zeroext %0)
%4 = icmp eq i64 %3, 0
br i1 %4, label %13, label %5
5: ; preds = %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %2
%6 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8 zeroext %0, i8 zeroext %0)
%7 = tail call zeroext i8 @toupper(i8 zeroext %0)
%8 = add i8 %7, -48
%9 = icmp ugt i8 %8, 9
%10 = add i8 %7, -55
%11 = select i1 %9, i8 %10, i8 %8
%12 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %11, i8 zeroext %11)
br label %13
13: ; preds = %2, %5
%14 = phi i8 [ %11, %5 ], [ 0, %2 ]
ret i8 %14
}