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

/* Variables and functions */
scalar_t__ malloc(size_t);
int /*<<< orphan*/ sscanf(char const *, char *, int /*<<< orphan*/ *);
int strlen(char const *);
void hexToBytes(const char *hex, uint8_t **buffer, size_t *bytes) {
  *bytes = strlen(hex) / 2;
  *buffer = (uint8_t *)malloc(*bytes);
  size_t i;
  for (i = 0; i < *bytes; i++) {
    uint32_t byte;
    sscanf(hex, "%2x", &byte);
    (*buffer)[i] = byte;
    hex += 2;
  }
}


x86 assembly (clang, -O0):
<hexToBytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
movslq %eax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1040 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
jae    11d0 <hexToBytes+0xa0>
mov    -0x8(%rbp),%rdi
lea    0xe67(%rip),%rsi
lea    -0x24(%rbp),%rdx
call   1050 <sscanf@plt>
mov    -0x24(%rbp),%edx
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x20(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x8(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x8(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    117d <hexToBytes+0x4d>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexToBytes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
movslq %ecx,%rdi
mov    %rdi,(%r15)
call   1040 <malloc@plt>
mov    %rax,(%r14)
cmpq   $0x0,(%r15)
je     11a5 <hexToBytes+0x75>
xor    %ebp,%ebp
lea    0xe91(%rip),%r12
lea    0x4(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1050 <sscanf@plt>
mov    0x4(%rsp),%eax
mov    (%r14),%rcx
mov    %eax,(%rcx,%rbp,4)
add    $0x2,%rbx
add    $0x1,%rbp
cmp    (%r15),%rbp
jb     1180 <hexToBytes+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [4 x i8] c"%2x\00"
define void @hexToBytes(i8* %0, i32** nocapture %1, i64* nocapture %2) {
%4 = alloca i32
%5 = tail call i32 @strlen(i8* %0)
%6 = sdiv i32 %5, 2
%7 = sext i32 %6 to i64
store i64 %7, i64* %2
%8 = tail call i64 @malloc(i64 %7)
%9 = inttoptr i64 %8 to i32*
store i32* %9, i32** %1
%10 = load i64, i64* %2
%11 = icmp eq i64 %10, 0
br i1 %11, label %25, label %12
12: ; preds = %3
%13 = bitcast i32* %4 to i8*
br label %14
14: ; preds = %12, %14
%15 = phi i64 [ 0, %12 ], [ %22, %14 ]
%16 = phi i8* [ %0, %12 ], [ %21, %14 ]
%17 = call i32 @sscanf(i8* %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %4)
%18 = load i32, i32* %4
%19 = load i32*, i32** %1
%20 = getelementptr inbounds i32, i32* %19, i64 %15
store i32 %18, i32* %20
%21 = getelementptr inbounds i8, i8* %16, i64 2
%22 = add nuw i64 %15, 1
%23 = load i64, i64* %2
%24 = icmp ult i64 %22, %23
br i1 %24, label %14, label %25
25: ; preds = %14, %3
ret void
}