#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 */
scalar_t__ scan_fromhex(char const);
unsigned scan_xlong(const char *src, unsigned long *dest) {
  const char *tmp = src;
  unsigned long l = 0;
  unsigned char c;
  while ((c = (char)scan_fromhex(*tmp)) < 16) {
    l = (l << 4) + c;
    ++tmp;
  }
  *dest = l;
  return tmp - src;
}


x86 assembly (clang, -O0):
<scan_xlong>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1030 <scan_fromhex@plt>
mov    %al,-0x21(%rbp)
movzbl %al,%eax
cmp    $0x10,%eax
jge    116f <scan_xlong+0x5f>
mov    -0x20(%rbp),%rax
shl    $0x4,%rax
movzbl -0x21(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1130 <scan_xlong+0x20>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scan_xlong>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
movsbl (%rdi),%edi
call   1030 <scan_fromhex@plt>
xor    %ebx,%ebx
mov    %r15,%r12
test   $0xf0,%al
jne    114d <scan_xlong+0x3d>
nop
shl    $0x4,%rbx
movzbl %al,%eax
add    %rax,%rbx
movsbl 0x1(%r12),%edi
add    $0x1,%r12
call   1030 <scan_fromhex@plt>
test   $0xf0,%al
je     1130 <scan_xlong+0x20>
mov    %rbx,(%r14)
sub    %r15d,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i32 @scan_xlong(i8* %0, i64* nocapture writeonly %1) {
%3 = load i8, i8* %0
%4 = tail call i64 @scan_fromhex(i8 signext %3)
%5 = and i64 %4, 240
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %19
7: ; preds = %2, %7
%8 = phi i64 [ %16, %7 ], [ %4, %2 ]
%9 = phi i8* [ %14, %7 ], [ %0, %2 ]
%10 = phi i64 [ %13, %7 ], [ 0, %2 ]
%11 = shl i64 %10, 4
%12 = and i64 %8, 255
%13 = add i64 %12, %11
%14 = getelementptr inbounds i8, i8* %9, i64 1
%15 = load i8, i8* %14
%16 = tail call i64 @scan_fromhex(i8 signext %15)
%17 = and i64 %16, 240
%18 = icmp eq i64 %17, 0
br i1 %18, label %7, label %19
19: ; preds = %7, %2
%20 = phi i64 [ 0, %2 ], [ %13, %7 ]
%21 = phi i8* [ %0, %2 ], [ %14, %7 ]
store i64 %20, i64* %1
%22 = ptrtoint i8* %21 to i64
%23 = ptrtoint i8* %0 to i64
%24 = sub i64 %22, %23
%25 = trunc i64 %24 to i32
ret i32 %25
}