#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 */
char toupper(unsigned char);
int fromhex(char *txt) {
  int num;
  if ((txt[0] >= '0') && (txt[0] <= '9')) {
    num = (txt[0] - '0') * 16;
  } else {
    num = (toupper((unsigned char)(txt[0])) - 'A' + 10) * 16;
  }
  if ((txt[1] >= '0') && (txt[1] <= '9')) {
    num += (txt[1] - '0');
  } else {
    num += (toupper((unsigned char)(txt[1])) - 'A' + 10);
  }
  return num & 255;
}


x86 assembly (clang, -O0):
<fromhex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jl     1151 <fromhex+0x41>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x39,%eax
jg     1151 <fromhex+0x41>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
sub    $0x30,%eax
shl    $0x4,%eax
mov    %eax,-0xc(%rbp)
jmp    116c <fromhex+0x5c>
mov    -0x8(%rbp),%rax
movzbl (%rax),%edi
call   1030 <toupper@plt>
movsbl %al,%eax
sub    $0x41,%eax
add    $0xa,%eax
shl    $0x4,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x30,%eax
jl     11a4 <fromhex+0x94>
mov    -0x8(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x39,%eax
jg     11a4 <fromhex+0x94>
mov    -0x8(%rbp),%rax
movsbl 0x1(%rax),%eax
sub    $0x30,%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    11c0 <fromhex+0xb0>
mov    -0x8(%rbp),%rax
movzbl 0x1(%rax),%edi
call   1030 <toupper@plt>
movsbl %al,%eax
sub    $0x41,%eax
add    $0xa,%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
and    $0xff,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fromhex>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
movzbl (%rdi),%ebx
lea    -0x30(%rbx),%eax
cmp    $0x9,%al
ja     1128 <fromhex+0x18>
mov    $0xfffffd00,%ebp
jmp    1137 <fromhex+0x27>
mov    %ebx,%edi
call   1030 <toupper@plt>
movsbl %al,%ebx
mov    $0xfffffc90,%ebp
mov    0x1(%r14),%al
lea    -0x30(%rax),%edx
mov    $0xffffffd0,%ecx
cmp    $0xa,%dl
jb     1155 <fromhex+0x45>
movzbl %al,%edi
call   1030 <toupper@plt>
mov    $0xffffffc9,%ecx
shl    $0x4,%ebx
add    %ebp,%ebx
movzbl %al,%eax
add    %ecx,%eax
add    %ebx,%eax
movzbl %al,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @fromhex(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = getelementptr inbounds i8, i8* %4, i64 0
%6 = load i8, i8* %5
%7 = sext i8 %6 to i32
%8 = icmp sge i32 %7, 48
br i1 %8, label %9, label %22
9: ; preds = %1
%10 = load i8*, i8** %2
%11 = getelementptr inbounds i8, i8* %10, i64 0
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp sle i32 %13, 57
br i1 %14, label %15, label %22
15: ; preds = %9
%16 = load i8*, i8** %2
%17 = getelementptr inbounds i8, i8* %16, i64 0
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = sub nsw i32 %19, 48
%21 = mul nsw i32 %20, 16
store i32 %21, i32* %3
br label %31
22: ; preds = %9, %1
%23 = load i8*, i8** %2
%24 = getelementptr inbounds i8, i8* %23, i64 0
%25 = load i8, i8* %24
%26 = call signext i8 @toupper(i8 zeroext %25)
%27 = sext i8 %26 to i32
%28 = sub nsw i32 %27, 65
%29 = add nsw i32 %28, 10
%30 = mul nsw i32 %29, 16
store i32 %30, i32* %3
br label %31
31: ; preds = %22, %15
%32 = load i8*, i8** %2
%33 = getelementptr inbounds i8, i8* %32, i64 1
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp sge i32 %35, 48
br i1 %36, label %37, label %51
37: ; preds = %31
%38 = load i8*, i8** %2
%39 = getelementptr inbounds i8, i8* %38, i64 1
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp sle i32 %41, 57
br i1 %42, label %43, label %51
43: ; preds = %37
%44 = load i8*, i8** %2
%45 = getelementptr inbounds i8, i8* %44, i64 1
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = sub nsw i32 %47, 48
%49 = load i32, i32* %3
%50 = add nsw i32 %49, %48
store i32 %50, i32* %3
br label %61
51: ; preds = %37, %31
%52 = load i8*, i8** %2
%53 = getelementptr inbounds i8, i8* %52, i64 1
%54 = load i8, i8* %53
%55 = call signext i8 @toupper(i8 zeroext %54)
%56 = sext i8 %55 to i32
%57 = sub nsw i32 %56, 65
%58 = add nsw i32 %57, 10
%59 = load i32, i32* %3
%60 = add nsw i32 %59, %58
store i32 %60, i32* %3
br label %61
61: ; preds = %51, %43
%62 = load i32, i32* %3
%63 = and i32 %62, 255
ret i32 %63
}

LLVM IR (-O2):
define i32 @fromhex(i8* nocapture %0) {
%2 = load i8, i8* %0
%3 = add i8 %2, -48
%4 = icmp ult i8 %3, 10
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = zext i8 %2 to i32
br label %10
7: ; preds = %1
%8 = tail call signext i8 @toupper(i8 zeroext %2)
%9 = sext i8 %8 to i32
br label %10
10: ; preds = %7, %5
%11 = phi i32 [ %9, %7 ], [ %6, %5 ]
%12 = phi i32 [ -880, %7 ], [ -768, %5 ]
%13 = getelementptr inbounds i8, i8* %0, i64 1
%14 = load i8, i8* %13
%15 = add i8 %14, -48
%16 = icmp ult i8 %15, 10
br i1 %16, label %19, label %17
17: ; preds = %10
%18 = tail call signext i8 @toupper(i8 zeroext %14)
br label %19
19: ; preds = %10, %17
%20 = phi i8 [ %18, %17 ], [ %14, %10 ]
%21 = phi i32 [ -55, %17 ], [ -48, %10 ]
%22 = shl nsw i32 %11, 4
%23 = add nsw i32 %22, %12
%24 = zext i8 %20 to i32
%25 = add nsw i32 %21, %24
%26 = add nsw i32 %25, %23
%27 = and i32 %26, 255
ret i32 %27
}