#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 hexdigit2number(char const);
int hex2number(const char *buffer, const size_t len) {
  int result = 0, tmp;
  size_t pos;
  for (pos = 0; pos < len; pos++) {
    result = result << 4;
    tmp = hexdigit2number(buffer[pos]);
    if (tmp < 0)
      return tmp;
    result += tmp;
  }
  return result;
}


x86 assembly (clang, -O0):
<hex2number>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1189 <hex2number+0x79>
mov    -0x1c(%rbp),%eax
shl    $0x4,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <hexdigit2number@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    116f <hex2number+0x5f>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    118f <hex2number+0x7f>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    112f <hex2number+0x1f>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex2number>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rsi,%rsi
je     114e <hex2number+0x3e>
mov    %rsi,%r15
mov    %rdi,%r14
xor    %ebx,%ebx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
movsbl (%r14,%rbx,1),%edi
call   1030 <hexdigit2number@plt>
test   %eax,%eax
js     1152 <hex2number+0x42>
shl    $0x4,%ebp
add    %eax,%ebp
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1130 <hex2number+0x20>
jmp    1154 <hex2number+0x44>
xor    %ebp,%ebp
jmp    1154 <hex2number+0x44>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @hex2number(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 0, i32* %6
store i64 0, i64* %8
br label %9
9: ; preds = %29, %2
%10 = load i64, i64* %8
%11 = load i64, i64* %5
%12 = icmp ult i64 %10, %11
br i1 %12, label %13, label %32
13: ; preds = %9
%14 = load i32, i32* %6
%15 = shl i32 %14, 4
store i32 %15, i32* %6
%16 = load i8*, i8** %4
%17 = load i64, i64* %8
%18 = getelementptr inbounds i8, i8* %16, i64 %17
%19 = load i8, i8* %18
%20 = call i32 @hexdigit2number(i8 signext %19)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %13
%24 = load i32, i32* %7
store i32 %24, i32* %3
br label %34
25: ; preds = %13
%26 = load i32, i32* %7
%27 = load i32, i32* %6
%28 = add nsw i32 %27, %26
store i32 %28, i32* %6
br label %29
29: ; preds = %25
%30 = load i64, i64* %8
%31 = add i64 %30, 1
store i64 %31, i64* %8
br label %9
32: ; preds = %9
%33 = load i32, i32* %6
store i32 %33, i32* %3
br label %34
34: ; preds = %32, %23
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
define i32 @hex2number(i8* nocapture %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %16, label %4
4: ; preds = %2, %11
%5 = phi i64 [ %14, %11 ], [ 0, %2 ]
%6 = phi i32 [ %13, %11 ], [ 0, %2 ]
%7 = getelementptr inbounds i8, i8* %0, i64 %5
%8 = load i8, i8* %7
%9 = tail call i32 @hexdigit2number(i8 signext %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %4
%12 = shl i32 %6, 4
%13 = add nsw i32 %9, %12
%14 = add nuw i64 %5, 1
%15 = icmp eq i64 %14, %1
br i1 %15, label %16, label %4
16: ; preds = %4, %11, %2
%17 = phi i32 [ 0, %2 ], [ %13, %11 ], [ %9, %4 ]
ret i32 %17
}