#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 sscanf(char *, char *, unsigned int *);
int strlen(char *);
unsigned long hexreversetoulong(char *hex) {
  unsigned long ret = 0L;
  unsigned int x;
  char i;
  if (strlen(hex) != 8)
    return 0L;
  for (i = 0; i < 4; ++i) {
    if (sscanf(hex, "%2X", &x) != 1)
      return 0L;
    ret += ((unsigned long)x) << i * 8;
    hex += 2;
  }
  return ret;
}


x86 assembly (clang, -O0):
<hexreversetoulong>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x8,%eax
je     1153 <hexreversetoulong+0x33>
movq   $0x0,-0x8(%rbp)
jmp    11c6 <hexreversetoulong+0xa6>
movb   $0x0,-0x1d(%rbp)
movsbl -0x1d(%rbp),%eax
cmp    $0x4,%eax
jge    11be <hexreversetoulong+0x9e>
mov    -0x10(%rbp),%rdi
lea    0xe91(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1040 <sscanf@plt>
cmp    $0x1,%eax
je     118e <hexreversetoulong+0x6e>
movq   $0x0,-0x8(%rbp)
jmp    11c6 <hexreversetoulong+0xa6>
mov    -0x1c(%rbp),%eax
movsbl -0x1d(%rbp),%ecx
shl    $0x3,%ecx
mov    %ecx,%ecx
shl    %cl,%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x10(%rbp)
mov    -0x1d(%rbp),%al
add    $0x1,%al
mov    %al,-0x1d(%rbp)
jmp    1157 <hexreversetoulong+0x37>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexreversetoulong>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1030 <strlen@plt>
cmp    $0x8,%eax
jne    11cd <hexreversetoulong+0xad>
lea    0xec0(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rbx,%rdi
call   1040 <sscanf@plt>
cmp    $0x1,%eax
jne    11cd <hexreversetoulong+0xad>
mov    0x4(%rsp),%r14d
lea    0x2(%rbx),%rdi
lea    0xe9e(%rip),%rsi
lea    0x4(%rsp),%rdx
call   1040 <sscanf@plt>
cmp    $0x1,%eax
jne    11cd <hexreversetoulong+0xad>
mov    0x4(%rsp),%r15d
lea    0x4(%rbx),%rdi
lea    0xe7f(%rip),%rsi
lea    0x4(%rsp),%rdx
call   1040 <sscanf@plt>
cmp    $0x1,%eax
jne    11cd <hexreversetoulong+0xad>
mov    0x4(%rsp),%r12d
add    $0x6,%rbx
lea    0xe60(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rbx,%rdi
call   1040 <sscanf@plt>
cmp    $0x1,%eax
jne    11cd <hexreversetoulong+0xad>
shl    $0x10,%r12
shl    $0x8,%r15
add    %r14,%r15
add    %r12,%r15
mov    0x4(%rsp),%eax
shl    $0x18,%rax
add    %r15,%rax
jmp    11cf <hexreversetoulong+0xaf>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%2X\00"
define i64 @hexreversetoulong(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8
store i8* %0, i8** %3
store i64 0, i64* %4
%7 = load i8*, i8** %3
%8 = call i32 @strlen(i8* %7)
%9 = icmp ne i32 %8, 8
br i1 %9, label %10, label %11
10: ; preds = %1
store i64 0, i64* %2
br label %38
11: ; preds = %1
store i8 0, i8* %6
br label %12
12: ; preds = %33, %11
%13 = load i8, i8* %6
%14 = sext i8 %13 to i32
%15 = icmp slt i32 %14, 4
br i1 %15, label %16, label %36
16: ; preds = %12
%17 = load i8*, i8** %3
%18 = call i32 @sscanf(i8* %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %5)
%19 = icmp ne i32 %18, 1
br i1 %19, label %20, label %21
20: ; preds = %16
store i64 0, i64* %2
br label %38
21: ; preds = %16
%22 = load i32, i32* %5
%23 = zext i32 %22 to i64
%24 = load i8, i8* %6
%25 = sext i8 %24 to i32
%26 = mul nsw i32 %25, 8
%27 = zext i32 %26 to i64
%28 = shl i64 %23, %27
%29 = load i64, i64* %4
%30 = add i64 %29, %28
store i64 %30, i64* %4
%31 = load i8*, i8** %3
%32 = getelementptr inbounds i8, i8* %31, i64 2
store i8* %32, i8** %3
br label %33
33: ; preds = %21
%34 = load i8, i8* %6
%35 = add i8 %34, 1
store i8 %35, i8* %6
br label %12
36: ; preds = %12
%37 = load i64, i64* %4
store i64 %37, i64* %2
br label %38
38: ; preds = %36, %20, %10
%39 = load i64, i64* %2
ret i64 %39
}

LLVM IR (-O2):
@.str = [4 x i8] c"%2X\00"
define i64 @hexreversetoulong(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i32 @strlen(i8* %0)
%5 = icmp eq i32 %4, 8
br i1 %5, label %6, label %36
6: ; preds = %1
%7 = call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %2)
%8 = icmp eq i32 %7, 1
br i1 %8, label %9, label %36
9: ; preds = %6
%10 = load i32, i32* %2
%11 = getelementptr inbounds i8, i8* %0, i64 2
%12 = call i32 @sscanf(i8* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %2)
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %36
14: ; preds = %9
%15 = load i32, i32* %2
%16 = getelementptr inbounds i8, i8* %0, i64 4
%17 = call i32 @sscanf(i8* %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %2)
%18 = icmp eq i32 %17, 1
br i1 %18, label %19, label %36
19: ; preds = %14
%20 = load i32, i32* %2
%21 = getelementptr inbounds i8, i8* %0, i64 6
%22 = call i32 @sscanf(i8* %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %2)
%23 = icmp eq i32 %22, 1
br i1 %23, label %24, label %36
24: ; preds = %19
%25 = zext i32 %20 to i64
%26 = shl nuw nsw i64 %25, 16
%27 = zext i32 %15 to i64
%28 = shl nuw nsw i64 %27, 8
%29 = zext i32 %10 to i64
%30 = add nuw nsw i64 %28, %29
%31 = add nuw nsw i64 %26, %30
%32 = load i32, i32* %2
%33 = zext i32 %32 to i64
%34 = shl nuw nsw i64 %33, 24
%35 = add nuw nsw i64 %34, %31
br label %36
36: ; preds = %6, %9, %14, %19, %24, %1
%37 = phi i64 [ 0, %1 ], [ 0, %6 ], [ 0, %9 ], [ 0, %14 ], [ 0, %19 ], [ %35, %24 ]
ret i64 %37
}