#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 */
char toupper(char);
void str_to_num(char *source, char *dest, int len) {
  int i = 0;
  char high_byte = 0;
  char low_byte = 0;
  for (i = 0; i < len; i += 2) {
    high_byte = toupper(source[i]);
    low_byte = toupper(source[i + 1]);
    if (high_byte > 0x39)
      high_byte -= 0x37;
    else
      high_byte -= 0x30;
    if (low_byte > 0x39)
      low_byte -= 0x37;
    else
      low_byte -= 0x30;
    dest[i / 2] = (high_byte << 4) | low_byte;
  }
}


x86 assembly (clang, -O0):
<str_to_num>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movb   $0x0,-0x19(%rbp)
movb   $0x0,-0x1a(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11fe <str_to_num+0xee>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <toupper@plt>
mov    %al,-0x19(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <toupper@plt>
mov    %al,-0x1a(%rbp)
movsbl -0x19(%rbp),%eax
cmp    $0x39,%eax
jle    118e <str_to_num+0x7e>
movsbl -0x19(%rbp),%eax
sub    $0x37,%eax
mov    %al,-0x19(%rbp)
jmp    1198 <str_to_num+0x88>
movsbl -0x19(%rbp),%eax
sub    $0x30,%eax
mov    %al,-0x19(%rbp)
movsbl -0x1a(%rbp),%eax
cmp    $0x39,%eax
jle    11b4 <str_to_num+0xa4>
movsbl -0x1a(%rbp),%eax
sub    $0x37,%eax
mov    %al,-0x1a(%rbp)
jmp    11be <str_to_num+0xae>
movsbl -0x1a(%rbp),%eax
sub    $0x30,%eax
mov    %al,-0x1a(%rbp)
movsbl -0x19(%rbp),%eax
shl    $0x4,%eax
movsbl -0x1a(%rbp),%ecx
or     %ecx,%eax
mov    %al,-0x29(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x29(%rbp),%dl
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x18(%rbp)
jmp    1139 <str_to_num+0x29>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_to_num>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    117d <str_to_num+0x6d>
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%r12
xor    %ebp,%ebp
mov    $0xc9,%r13d
movsbl (%r12,%rbp,1),%edi
call   1030 <toupper@plt>
mov    %eax,%r15d
movsbl 0x1(%r12,%rbp,1),%edi
call   1030 <toupper@plt>
cmp    $0x3a,%r15b
mov    $0x0,%ecx
mov    $0x39,%edx
cmovge %edx,%ecx
add    %r15b,%cl
cmp    $0x3a,%al
mov    $0xd0,%edx
cmovge %r13d,%edx
add    %al,%dl
shl    $0x4,%cl
or     %dl,%cl
mov    %cl,(%rbx)
add    $0x2,%rbp
add    $0x1,%rbx
cmp    %r14d,%ebp
jl     1130 <str_to_num+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @str_to_num(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
%9 = alloca i8
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
store i8 0, i8* %8
store i8 0, i8* %9
store i32 0, i32* %7
br label %10
10: ; preds = %68, %3
%11 = load i32, i32* %7
%12 = load i32, i32* %6
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %71
14: ; preds = %10
%15 = load i8*, i8** %4
%16 = load i32, i32* %7
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = load i8, i8* %18
%20 = call signext i8 @toupper(i8 signext %19)
store i8 %20, i8* %8
%21 = load i8*, i8** %4
%22 = load i32, i32* %7
%23 = add nsw i32 %22, 1
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %21, i64 %24
%26 = load i8, i8* %25
%27 = call signext i8 @toupper(i8 signext %26)
store i8 %27, i8* %9
%28 = load i8, i8* %8
%29 = sext i8 %28 to i32
%30 = icmp sgt i32 %29, 57
br i1 %30, label %31, label %36
31: ; preds = %14
%32 = load i8, i8* %8
%33 = sext i8 %32 to i32
%34 = sub nsw i32 %33, 55
%35 = trunc i32 %34 to i8
store i8 %35, i8* %8
br label %41
36: ; preds = %14
%37 = load i8, i8* %8
%38 = sext i8 %37 to i32
%39 = sub nsw i32 %38, 48
%40 = trunc i32 %39 to i8
store i8 %40, i8* %8
br label %41
41: ; preds = %36, %31
%42 = load i8, i8* %9
%43 = sext i8 %42 to i32
%44 = icmp sgt i32 %43, 57
br i1 %44, label %45, label %50
45: ; preds = %41
%46 = load i8, i8* %9
%47 = sext i8 %46 to i32
%48 = sub nsw i32 %47, 55
%49 = trunc i32 %48 to i8
store i8 %49, i8* %9
br label %55
50: ; preds = %41
%51 = load i8, i8* %9
%52 = sext i8 %51 to i32
%53 = sub nsw i32 %52, 48
%54 = trunc i32 %53 to i8
store i8 %54, i8* %9
br label %55
55: ; preds = %50, %45
%56 = load i8, i8* %8
%57 = sext i8 %56 to i32
%58 = shl i32 %57, 4
%59 = load i8, i8* %9
%60 = sext i8 %59 to i32
%61 = or i32 %58, %60
%62 = trunc i32 %61 to i8
%63 = load i8*, i8** %5
%64 = load i32, i32* %7
%65 = sdiv i32 %64, 2
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %63, i64 %66
store i8 %62, i8* %67
br label %68
68: ; preds = %55
%69 = load i32, i32* %7
%70 = add nsw i32 %69, 2
store i32 %70, i32* %7
br label %10
71: ; preds = %10
ret void
}

LLVM IR (-O2):
define void @str_to_num(i8* nocapture %0, i8* nocapture writeonly %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %27
5: ; preds = %3, %5
%6 = phi i64 [ %24, %5 ], [ 0, %3 ]
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = load i8, i8* %7
%9 = tail call signext i8 @toupper(i8 signext %8)
%10 = or i64 %6, 1
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = load i8, i8* %11
%13 = tail call signext i8 @toupper(i8 signext %12)
%14 = icmp sgt i8 %9, 57
%15 = select i1 %14, i8 57, i8 0
%16 = add i8 %15, %9
%17 = icmp sgt i8 %13, 57
%18 = select i1 %17, i8 -55, i8 -48
%19 = add i8 %18, %13
%20 = shl i8 %16, 4
%21 = or i8 %19, %20
%22 = lshr exact i64 %6, 1
%23 = getelementptr inbounds i8, i8* %1, i64 %22
store i8 %21, i8* %23
%24 = add nuw i64 %6, 2
%25 = trunc i64 %24 to i32
%26 = icmp slt i32 %25, %2
br i1 %26, label %5, label %27
27: ; preds = %5, %3
ret void
}