#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 hex(char);
unsigned long int hex2reg(char *buf) {
  int n;
  unsigned long int val = 0;
  for (n = 0; n < 8; n++) {
    int nyb_shift;
    if (n % 2 == 0)
      nyb_shift = (n + 1) * 4;
    else
      nyb_shift = (n - 1) * 4;
    val |= hex(buf[n]) << nyb_shift;
  }
  return val;
}


x86 assembly (clang, -O0):
<hex2reg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x8,-0xc(%rbp)
jge    1194 <hex2reg+0x84>
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    115a <hex2reg+0x4a>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
shl    $0x2,%eax
mov    %eax,-0x1c(%rbp)
jmp    1166 <hex2reg+0x56>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
shl    $0x2,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <hex@plt>
mov    -0x1c(%rbp),%ecx
shl    %cl,%eax
cltq
or     -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    112b <hex2reg+0x1b>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex2reg>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r15
movsbl (%rdi),%edi
call   1030 <hex@plt>
mov    %eax,%ebp
shl    $0x4,%ebp
movsbl 0x1(%r15),%edi
call   1030 <hex@plt>
mov    %eax,%r14d
or     %ebp,%r14d
movsbl 0x2(%r15),%edi
call   1030 <hex@plt>
mov    %eax,%ebx
shl    $0xc,%ebx
movsbl 0x3(%r15),%edi
call   1030 <hex@plt>
mov    %eax,%r12d
shl    $0x8,%r12d
or     %ebx,%r12d
or     %r14d,%r12d
movsbl 0x4(%r15),%edi
call   1030 <hex@plt>
mov    %eax,%r14d
shl    $0x14,%r14d
movsbl 0x5(%r15),%edi
call   1030 <hex@plt>
mov    %eax,%ebx
shl    $0x10,%ebx
or     %r14d,%ebx
movsbl 0x6(%r15),%edi
call   1030 <hex@plt>
mov    %eax,%ebp
shl    $0x1c,%ebp
or     %ebx,%ebp
or     %r12d,%ebp
movsbl 0x7(%r15),%edi
call   1030 <hex@plt>
shl    $0x18,%eax
or     %ebp,%eax
cltq
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i64 @hex2reg(i8* nocapture %0) {
%2 = load i8, i8* %0
%3 = tail call i32 @hex(i8 signext %2)
%4 = shl i32 %3, 4
%5 = getelementptr inbounds i8, i8* %0, i64 1
%6 = load i8, i8* %5
%7 = tail call i32 @hex(i8 signext %6)
%8 = or i32 %4, %7
%9 = getelementptr inbounds i8, i8* %0, i64 2
%10 = load i8, i8* %9
%11 = tail call i32 @hex(i8 signext %10)
%12 = shl i32 %11, 12
%13 = or i32 %8, %12
%14 = getelementptr inbounds i8, i8* %0, i64 3
%15 = load i8, i8* %14
%16 = tail call i32 @hex(i8 signext %15)
%17 = shl i32 %16, 8
%18 = or i32 %13, %17
%19 = getelementptr inbounds i8, i8* %0, i64 4
%20 = load i8, i8* %19
%21 = tail call i32 @hex(i8 signext %20)
%22 = shl i32 %21, 20
%23 = or i32 %18, %22
%24 = getelementptr inbounds i8, i8* %0, i64 5
%25 = load i8, i8* %24
%26 = tail call i32 @hex(i8 signext %25)
%27 = shl i32 %26, 16
%28 = or i32 %23, %27
%29 = getelementptr inbounds i8, i8* %0, i64 6
%30 = load i8, i8* %29
%31 = tail call i32 @hex(i8 signext %30)
%32 = shl i32 %31, 28
%33 = or i32 %28, %32
%34 = getelementptr inbounds i8, i8* %0, i64 7
%35 = load i8, i8* %34
%36 = tail call i32 @hex(i8 signext %35)
%37 = shl i32 %36, 24
%38 = or i32 %33, %37
%39 = sext i32 %38 to i64
ret i64 %39
}