int hex_str_to_byte_array(char *byte_array, char *str, int size) {
  unsigned int byte;
  char *end = byte_array + size;
  int length = 0;
  while (str[0]) {
    if (!str[1])
      m2s_opencl_fatal("%s: hex string ended mid-byte", __FUNCTION__);
    if (byte_array >= end)
      m2s_opencl_fatal("%s: hex string too long", __FUNCTION__);
    sscanf(str, "%2x", &byte);
    byte_array[0] = (unsigned char)byte;
    byte_array += 1;
    length++;
    str += 2;
  }
  return length;
}


x86 assembly (clang, -O0):
<hex_str_to_byte_array>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     11e3 <hex_str_to_byte_array+0xc3>
mov    -0x10(%rbp),%rax
cmpb   $0x0,0x1(%rax)
jne    1179 <hex_str_to_byte_array+0x59>
lea    0xe95(%rip),%rdi
lea    0xeac(%rip),%rsi
mov    $0x0,%al
call   1030 <m2s_opencl_fatal@plt>
mov    -0x8(%rbp),%rax
cmp    -0x20(%rbp),%rax
jb     119c <hex_str_to_byte_array+0x7c>
lea    0xea6(%rip),%rdi
lea    0xe89(%rip),%rsi
mov    $0x0,%al
call   1030 <m2s_opencl_fatal@plt>
mov    -0x10(%rbp),%rdi
lea    0xea5(%rip),%rsi
lea    -0x18(%rbp),%rdx
mov    $0x0,%al
call   1040 <sscanf@plt>
mov    -0x18(%rbp),%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x10(%rbp)
jmp    1149 <hex_str_to_byte_array+0x29>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_str_to_byte_array>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmpb   $0x0,(%rsi)
je     11c1 <hex_str_to_byte_array+0xa1>
mov    %rsi,%r14
mov    %rdi,%rbx
movslq %edx,%r12
add    %rdi,%r12
lea    0xf05(%rip),%r13
lea    0x4(%rsp),%r15
xor    %ebp,%ebp
jmp    1179 <hex_str_to_byte_array+0x59>
mov    %r14,%rdi
mov    %r13,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <sscanf@plt>
movzbl 0x4(%rsp),%eax
mov    %al,(%rbx)
add    $0x1,%rbx
add    $0x1,%ebp
cmpb   $0x0,0x2(%r14)
lea    0x2(%r14),%r14
je     11c3 <hex_str_to_byte_array+0xa3>
cmpb   $0x0,0x1(%r14)
je     1190 <hex_str_to_byte_array+0x70>
cmp    %r12,%rbx
jb     1150 <hex_str_to_byte_array+0x30>
jmp    11aa <hex_str_to_byte_array+0x8a>
nopw   0x0(%rax,%rax,1)
lea    0xe69(%rip),%rdi
lea    0xe80(%rip),%rsi
xor    %eax,%eax
call   1030 <m2s_opencl_fatal@plt>
cmp    %r12,%rbx
jb     1150 <hex_str_to_byte_array+0x30>
lea    0xe83(%rip),%rdi
lea    0xe66(%rip),%rsi
xor    %eax,%eax
call   1030 <m2s_opencl_fatal@plt>
jmp    1150 <hex_str_to_byte_array+0x30>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"%s: hex string ended mid-byte\00"
@__FUNCTION__.hex_str_to_byte_array = [22 x i8] c"hex_str_to_byte_array\00"
@.str.1 = [24 x i8] c"%s: hex string too long\00"
@.str.2 = [4 x i8] c"%2x\00"
define i32 @hex_str_to_byte_array(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = load i8*, i8** %4
%11 = load i32, i32* %6
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %10, i64 %12
store i8* %13, i8** %8
store i32 0, i32* %9
br label %14
14: ; preds = %32, %3
%15 = load i8*, i8** %5
%16 = getelementptr inbounds i8, i8* %15, i64 0
%17 = load i8, i8* %16
%18 = icmp ne i8 %17, 0
br i1 %18, label %19, label %45
19: ; preds = %14
%20 = load i8*, i8** %5
%21 = getelementptr inbounds i8, i8* %20, i64 1
%22 = load i8, i8* %21
%23 = icmp ne i8 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %19
%25 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @m2s_opencl_fatal to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @__FUNCTION__.hex_str_to_byte_array, i64 0, i64 0))
br label %26
26: ; preds = %24, %19
%27 = load i8*, i8** %4
%28 = load i8*, i8** %8
%29 = icmp uge i8* %27, %28
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @m2s_opencl_fatal to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @__FUNCTION__.hex_str_to_byte_array, i64 0, i64 0))
br label %32
32: ; preds = %30, %26
%33 = load i8*, i8** %5
%34 = call i32 (i8*, i8*, ...) @sscanf(i8* %33, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %7)
%35 = load i32, i32* %7
%36 = trunc i32 %35 to i8
%37 = load i8*, i8** %4
%38 = getelementptr inbounds i8, i8* %37, i64 0
store i8 %36, i8* %38
%39 = load i8*, i8** %4
%40 = getelementptr inbounds i8, i8* %39, i64 1
store i8* %40, i8** %4
%41 = load i32, i32* %9
%42 = add nsw i32 %41, 1
store i32 %42, i32* %9
%43 = load i8*, i8** %5
%44 = getelementptr inbounds i8, i8* %43, i64 2
store i8* %44, i8** %5
br label %14
45: ; preds = %14
%46 = load i32, i32* %9
ret i32 %46
}

LLVM IR (-O2):
@.str = [30 x i8] c"%s: hex string ended mid-byte\00"
@__FUNCTION__.hex_str_to_byte_array = [22 x i8] c"hex_str_to_byte_array\00"
@.str.1 = [24 x i8] c"%s: hex string too long\00"
@.str.2 = [4 x i8] c"%2x\00"
define i32 @hex_str_to_byte_array(i8* writeonly %0, i8* nocapture %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = sext i32 %2 to i64
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = load i8, i8* %1
%9 = icmp eq i8 %8, 0
br i1 %9, label %32, label %10
10: ; preds = %3, %23
%11 = phi i32 [ %28, %23 ], [ 0, %3 ]
%12 = phi i8* [ %27, %23 ], [ %0, %3 ]
%13 = phi i8* [ %29, %23 ], [ %1, %3 ]
%14 = getelementptr inbounds i8, i8* %13, i64 1
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %10
%18 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @m2s_opencl_fatal to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @__FUNCTION__.hex_str_to_byte_array, i64 0, i64 0))
br label %19
19: ; preds = %17, %10
%20 = icmp ult i8* %12, %7
br i1 %20, label %23, label %21
21: ; preds = %19
%22 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @m2s_opencl_fatal to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @__FUNCTION__.hex_str_to_byte_array, i64 0, i64 0))
br label %23
23: ; preds = %21, %19
%24 = call i32 (i8*, i8*, ...) @sscanf(i8* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%25 = load i32, i32* %4
%26 = trunc i32 %25 to i8
store i8 %26, i8* %12
%27 = getelementptr inbounds i8, i8* %12, i64 1
%28 = add nuw nsw i32 %11, 1
%29 = getelementptr inbounds i8, i8* %13, i64 2
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 0
br i1 %31, label %32, label %10
32: ; preds = %23, %3
%33 = phi i32 [ 0, %3 ], [ %28, %23 ]
ret i32 %33
}