#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 /*<<< orphan*/ assert(int);
scalar_t__ malloc(int);
int /*<<< orphan*/ sscanf(char *, char *, unsigned char *);
int strlen(char *);
unsigned char *str2hex(char *str) {
  unsigned char *ret = NULL;
  int str_len = strlen(str);
  int i = 0;
  assert((str_len % 2) == 0);
  ret = (unsigned char *)malloc(str_len / 2);
  for (i = 0; i < str_len; i = i + 2) {
    sscanf(str + i, "%2hhx", &ret[i / 2]);
  }
  return ret;
}


x86 assembly (clang, -O0):
<str2hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11ec <str2hex+0xac>
mov    -0x8(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x20(%rbp),%rdx
cltq
add    %rax,%rdx
lea    0xe27(%rip),%rsi
call   1060 <sscanf@plt>
mov    -0x18(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x18(%rbp)
jmp    119f <str2hex+0x5f>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str2hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
call   1030 <strlen@plt>
mov    %eax,%r13d
mov    %eax,%edi
not    %edi
and    $0x1,%edi
call   1040 <assert@plt>
mov    %r13d,%edi
shr    $0x1f,%edi
add    %r13d,%edi
sar    %edi
call   1050 <malloc@plt>
mov    %rax,%r14
test   %r13d,%r13d
jle    11ac <str2hex+0x6c>
xor    %ebp,%ebp
lea    0xe7d(%rip),%r15
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
lea    (%r12,%rbp,1),%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1060 <sscanf@plt>
add    $0x2,%rbp
add    $0x1,%rbx
cmp    %ebp,%r13d
jg     1190 <str2hex+0x50>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [6 x i8] c"%2hhx\00"
define i8* @str2hex(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = and i32 %2, 1
%4 = xor i32 %3, 1
%5 = tail call i32 @assert(i32 %4)
%6 = sdiv i32 %2, 2
%7 = tail call i64 @malloc(i32 %6)
%8 = inttoptr i64 %7 to i8*
%9 = icmp sgt i32 %2, 0
br i1 %9, label %10, label %19
10: ; preds = %1, %10
%11 = phi i64 [ %16, %10 ], [ 0, %1 ]
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = lshr exact i64 %11, 1
%14 = getelementptr inbounds i8, i8* %8, i64 %13
%15 = tail call i32 @sscanf(i8* %12, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = add nuw i64 %11, 2
%17 = trunc i64 %16 to i32
%18 = icmp sgt i32 %2, %17
br i1 %18, label %10, label %19
19: ; preds = %10, %1
ret i8* %8
}