#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*/ assert(int);
int isxdigit(char);
char toupper(char);
int ctohex(char c) {
  assert(isxdigit(c));
  c = toupper(c);
  c -= '0';
  if (c > 9)
    c -= 7;
  assert(c >= 0);
  assert(c < 16);
  return c;
}


x86 assembly (clang, -O0):
<ctohex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %dil,%al
mov    %al,-0x1(%rbp)
movsbl -0x1(%rbp),%edi
call   1050 <isxdigit@plt>
mov    %eax,%edi
call   1040 <assert@plt>
movsbl -0x1(%rbp),%edi
call   1030 <toupper@plt>
mov    %al,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
sub    $0x30,%eax
mov    %al,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
cmp    $0x9,%eax
jle    117b <ctohex+0x4b>
movsbl -0x1(%rbp),%eax
sub    $0x7,%eax
mov    %al,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
cmp    $0x0,%eax
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
movsbl -0x1(%rbp),%eax
cmp    $0x10,%eax
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
movsbl -0x1(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ctohex>:
push   %rbx
mov    %edi,%ebx
call   1050 <isxdigit@plt>
mov    %eax,%edi
call   1040 <assert@plt>
mov    %ebx,%edi
call   1030 <toupper@plt>
lea    -0x30(%rax),%ecx
add    $0xc9,%al
cmp    $0xa,%cl
movzbl %al,%eax
movzbl %cl,%ecx
cmovge %eax,%ecx
movsbl %cl,%ebx
mov    %ebx,%eax
not    %al
shr    $0x7,%al
movzbl %al,%edi
call   1040 <assert@plt>
xor    %edi,%edi
cmp    $0x10,%bl
setl   %dil
call   1040 <assert@plt>
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @ctohex(i8 signext %0) {
%2 = alloca i8
store i8 %0, i8* %2
%3 = load i8, i8* %2
%4 = call i32 @isxdigit(i8 signext %3)
%5 = call i32 @assert(i32 %4)
%6 = load i8, i8* %2
%7 = call signext i8 @toupper(i8 signext %6)
store i8 %7, i8* %2
%8 = load i8, i8* %2
%9 = sext i8 %8 to i32
%10 = sub nsw i32 %9, 48
%11 = trunc i32 %10 to i8
store i8 %11, i8* %2
%12 = load i8, i8* %2
%13 = sext i8 %12 to i32
%14 = icmp sgt i32 %13, 9
br i1 %14, label %15, label %20
15: ; preds = %1
%16 = load i8, i8* %2
%17 = sext i8 %16 to i32
%18 = sub nsw i32 %17, 7
%19 = trunc i32 %18 to i8
store i8 %19, i8* %2
br label %20
20: ; preds = %15, %1
%21 = load i8, i8* %2
%22 = sext i8 %21 to i32
%23 = icmp sge i32 %22, 0
%24 = zext i1 %23 to i32
%25 = call i32 @assert(i32 %24)
%26 = load i8, i8* %2
%27 = sext i8 %26 to i32
%28 = icmp slt i32 %27, 16
%29 = zext i1 %28 to i32
%30 = call i32 @assert(i32 %29)
%31 = load i8, i8* %2
%32 = sext i8 %31 to i32
ret i32 %32
}

LLVM IR (-O2):
define i32 @ctohex(i8 signext %0) {
%2 = tail call i32 @isxdigit(i8 signext %0)
%3 = tail call i32 @assert(i32 %2)
%4 = tail call signext i8 @toupper(i8 signext %0)
%5 = add i8 %4, -48
%6 = icmp sgt i8 %5, 9
%7 = add i8 %4, -55
%8 = select i1 %6, i8 %7, i8 %5
%9 = sext i8 %8 to i32
%10 = xor i8 %8, -1
%11 = lshr i8 %10, 7
%12 = zext i8 %11 to i32
%13 = tail call i32 @assert(i32 %12)
%14 = icmp slt i8 %8, 16
%15 = zext i1 %14 to i32
%16 = tail call i32 @assert(i32 %15)
ret i32 %9
}