#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 upcase(char);
int hex2dec(char *chr) {
  int res = 0;
  int i;
  for (i = 0; i < 2; i++) {
    if (upcase(chr[i]) >= 'A') {
      res += (upcase(chr[i]) - 'A' + 10) << (4 * (1 - i));
    } else {
      res += (chr[i] - '0') << (4 * (1 - i));
    }
  }
  return res;
}


x86 assembly (clang, -O0):
<hex2dec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x2,-0x10(%rbp)
jge    11b8 <hex2dec+0xa8>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <upcase@plt>
movsbl %al,%eax
cmp    $0x41,%eax
jl     1183 <hex2dec+0x73>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <upcase@plt>
movsbl %al,%eax
sub    $0x41,%eax
add    $0xa,%eax
mov    $0x1,%ecx
sub    -0x10(%rbp),%ecx
shl    $0x2,%ecx
shl    %cl,%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    11a5 <hex2dec+0x95>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
sub    $0x30,%eax
mov    $0x1,%ecx
sub    -0x10(%rbp),%ecx
shl    $0x2,%ecx
shl    %cl,%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    11aa <hex2dec+0x9a>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    112a <hex2dec+0x1a>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex2dec>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
movsbl (%rdi),%edi
call   1030 <upcase@plt>
mov    (%rbx),%bpl
mov    $0xfffffd00,%r14d
cmp    $0x41,%al
jl     113d <hex2dec+0x2d>
movsbl %bpl,%edi
call   1030 <upcase@plt>
mov    %eax,%ebp
mov    $0xfffffc90,%r14d
movsbl 0x1(%rbx),%edi
call   1030 <upcase@plt>
mov    %eax,%ecx
mov    0x1(%rbx),%al
mov    $0xffffffd0,%edx
cmp    $0x41,%cl
jl     1162 <hex2dec+0x52>
movsbl %al,%edi
call   1030 <upcase@plt>
mov    $0xffffffc9,%edx
movsbl %bpl,%ecx
shl    $0x4,%ecx
add    %r14d,%ecx
movsbl %al,%eax
add    %edx,%eax
add    %ecx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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