#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 */
unsigned long mu_hex2ul(char const);
size_t mu_hexstr2ul(unsigned long *ul, const char *hex, size_t len) {
  size_t r;
  *ul = 0;
  for (r = 0; r < len; r++) {
    unsigned long v = mu_hex2ul(hex[r]);
    if (v == (unsigned long)-1)
      return r;
    *ul = *ul * 16 + v;
  }
  return r;
}


x86 assembly (clang, -O0):
<mu_hexstr2ul>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1199 <mu_hexstr2ul+0x89>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <mu_hex2ul@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0xffffffffffffffff,-0x30(%rbp)
jne    1172 <mu_hexstr2ul+0x62>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11a1 <mu_hexstr2ul+0x91>
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
shl    $0x4,%rcx
add    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1137 <mu_hexstr2ul+0x27>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mu_hexstr2ul>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
movq   $0x0,(%rdi)
test   %rdx,%rdx
je     115d <mu_hexstr2ul+0x4d>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
xor    %ebx,%ebx
nop
movsbl (%r15,%rbx,1),%edi
call   1030 <mu_hex2ul@plt>
cmp    $0xffffffffffffffff,%rax
je     115f <mu_hexstr2ul+0x4f>
mov    (%r12),%rcx
shl    $0x4,%rcx
add    %rax,%rcx
mov    %rcx,(%r12)
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1130 <mu_hexstr2ul+0x20>
mov    %r14,%rbx
jmp    115f <mu_hexstr2ul+0x4f>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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