#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 */
typedef int uint32_t;

/* Variables and functions */
int hex(char);
uint32_t hex2reg(char *p_buf) {
  int n;
  uint32_t val = 0;
  for (n = 0; n < 8; n++) {
    int nyb_shift = 28 - (n * 4);
    val |= hex(p_buf[n]) << nyb_shift;
  }
  return val;
}


x86 assembly (clang, -O0):
<hex2reg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x8,-0xc(%rbp)
jge    116e <hex2reg+0x5e>
mov    -0xc(%rbp),%ecx
shl    $0x2,%ecx
mov    $0x1c,%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <hex@plt>
mov    -0x14(%rbp),%ecx
shl    %cl,%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    112a <hex2reg+0x1a>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

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

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

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