#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 hexchar2num(unsigned char);
int /*<<< orphan*/ printf(char *, int, int);
unsigned int hexstr2num(unsigned char *str, int len) {
  int num;
  int c;
  int i;
  if (!str || len <= 0)
    return -1;
  for (num = 0, i = 0; *str && i < len; ++str, ++i) {
    c = hexchar2num(*str);
    num = (num << 4) | c;
    printf("num: %x(h) %d\n", num, num);
    printf("c  : %x(h) %d\n", c, c);
  }
  return num;
}


x86 assembly (clang, -O0):
<hexstr2num>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1144 <hexstr2num+0x24>
cmpl   $0x0,-0x14(%rbp)
jg     1150 <hexstr2num+0x30>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <hexstr2num+0xce>
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movzbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x21(%rbp)
je     117f <hexstr2num+0x5f>
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
setl   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    118f <hexstr2num+0x6f>
jmp    11e8 <hexstr2num+0xc8>
mov    -0x10(%rbp),%rax
movzbl (%rax),%edi
call   1040 <hexchar2num@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
shl    $0x4,%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xe49(%rip),%rdi
call   1030 <printf@plt>
mov    -0x1c(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe46(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    115e <hexstr2num+0x3e>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexstr2num>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%ebx
test   %rdi,%rdi
je     11a1 <hexstr2num+0x81>
mov    %esi,%r14d
test   %esi,%esi
jle    11a1 <hexstr2num+0x81>
mov    %rdi,%r13
mov    (%rdi),%al
xor    %ebx,%ebx
test   %al,%al
je     11a1 <hexstr2num+0x81>
mov    $0x1,%r15d
lea    0xebb(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzbl %al,%edi
call   1040 <hexchar2num@plt>
mov    %eax,%ebp
shl    $0x4,%ebx
or     %eax,%ebx
lea    0xe8a(%rip),%rdi
mov    %ebx,%esi
mov    %ebx,%edx
call   1030 <printf@plt>
mov    %r12,%rdi
mov    %ebp,%esi
mov    %ebp,%edx
call   1030 <printf@plt>
movzbl 0x0(%r13,%r15,1),%eax
test   %al,%al
je     11a1 <hexstr2num+0x81>
lea    0x1(%r15),%rcx
cmp    %r14d,%r15d
mov    %rcx,%r15
jl     1160 <hexstr2num+0x40>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"num: %x(h) %d\0A\00"
@.str.1 = [15 x i8] c"c : %x(h) %d\0A\00"
define i32 @hexstr2num(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* %5
%13 = icmp sle i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %11, %2
store i32 -1, i32* %3
br label %48
15: ; preds = %11
store i32 0, i32* %6
store i32 0, i32* %8
br label %16
16: ; preds = %41, %15
%17 = load i8*, i8** %4
%18 = load i8, i8* %17
%19 = zext i8 %18 to i32
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = load i32, i32* %8
%23 = load i32, i32* %5
%24 = icmp slt i32 %22, %23
br label %25
25: ; preds = %21, %16
%26 = phi i1 [ false, %16 ], [ %24, %21 ]
br i1 %26, label %27, label %46
27: ; preds = %25
%28 = load i8*, i8** %4
%29 = load i8, i8* %28
%30 = call i32 @hexchar2num(i8 zeroext %29)
store i32 %30, i32* %7
%31 = load i32, i32* %6
%32 = shl i32 %31, 4
%33 = load i32, i32* %7
%34 = or i32 %32, %33
store i32 %34, i32* %6
%35 = load i32, i32* %6
%36 = load i32, i32* %6
%37 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %35, i32 %36)
%38 = load i32, i32* %7
%39 = load i32, i32* %7
%40 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %38, i32 %39)
br label %41
41: ; preds = %27
%42 = load i8*, i8** %4
%43 = getelementptr inbounds i8, i8* %42, i32 1
store i8* %43, i8** %4
%44 = load i32, i32* %8
%45 = add nsw i32 %44, 1
store i32 %45, i32* %8
br label %16
46: ; preds = %25
%47 = load i32, i32* %6
store i32 %47, i32* %3
br label %48
48: ; preds = %46, %14
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@.str = [15 x i8] c"num: %x(h) %d\0A\00"
@.str.1 = [15 x i8] c"c : %x(h) %d\0A\00"
define i32 @hexstr2num(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
%4 = icmp slt i32 %1, 1
%5 = or i1 %3, %4
br i1 %5, label %25, label %6
6: ; preds = %2
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %25, label %9
9: ; preds = %6, %9
%10 = phi i8 [ %21, %9 ], [ %7, %6 ]
%11 = phi i32 [ %20, %9 ], [ 0, %6 ]
%12 = phi i32 [ %16, %9 ], [ 0, %6 ]
%13 = phi i8* [ %19, %9 ], [ %0, %6 ]
%14 = tail call i32 @hexchar2num(i8 zeroext %10)
%15 = shl i32 %12, 4
%16 = or i32 %14, %15
%17 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %16)
%18 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %14, i32 %14)
%19 = getelementptr inbounds i8, i8* %13, i64 1
%20 = add nuw nsw i32 %11, 1
%21 = load i8, i8* %19
%22 = icmp ne i8 %21, 0
%23 = icmp slt i32 %20, %1
%24 = select i1 %22, i1 %23, i1 false
br i1 %24, label %9, label %25
25: ; preds = %9, %6, %2
%26 = phi i32 [ -1, %2 ], [ 0, %6 ], [ %16, %9 ]
ret i32 %26
}