#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);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int strlen(char const *);
int hex_to_buf(const char *str, unsigned char *buf) {
  int i, d;
  assert(!(strlen(str) % 2));
  char chunk[3];
  for (i = 0; i < strlen(str); i += 2) {
    chunk[0] = str[i];
    chunk[1] = str[i + 1];
    chunk[2] = '\0';
    sscanf(chunk, "%02x", &d);
    buf[i / 2] = d;
  }
  return (i / 2);
}


x86 assembly (clang, -O0):
<hex_to_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
cmp    %ecx,%eax
jge    11f5 <hex_to_buf+0xc5>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x1b(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x1a(%rbp)
movb   $0x0,-0x19(%rbp)
lea    -0x1b(%rbp),%rdi
lea    0xe4a(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1050 <sscanf@plt>
mov    -0x18(%rbp),%eax
mov    %al,-0x29(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%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    -0x14(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x14(%rbp)
jmp    116a <hex_to_buf+0x3a>
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_to_buf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
call   1030 <strlen@plt>
not    %eax
and    $0x1,%eax
mov    %eax,%edi
call   1040 <assert@plt>
mov    %r14,%rdi
call   1030 <strlen@plt>
xor    %ebx,%ebx
test   %eax,%eax
jle    11c4 <hex_to_buf+0x94>
lea    0xe99(%rip),%r12
lea    0x1(%rsp),%r13
lea    0x4(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl (%r14,%rbx,1),%eax
mov    %al,0x1(%rsp)
movzbl 0x1(%r14,%rbx,1),%eax
mov    %al,0x2(%rsp)
movb   $0x0,0x3(%rsp)
mov    %r13,%rdi
mov    %r12,%rsi
mov    %rbp,%rdx
call   1050 <sscanf@plt>
movzbl 0x4(%rsp),%eax
mov    %al,(%r15)
add    $0x2,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
add    $0x1,%r15
cmp    %ebx,%eax
jg     1180 <hex_to_buf+0x50>
shr    %ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%02x\00"
define i32 @hex_to_buf(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [3 x i8]
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = load i8*, i8** %3
%9 = call i32 @strlen(i8* %8)
%10 = srem i32 %9, 2
%11 = icmp ne i32 %10, 0
%12 = xor i1 %11, true
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
store i32 0, i32* %5
br label %15
15: ; preds = %44, %2
%16 = load i32, i32* %5
%17 = load i8*, i8** %3
%18 = call i32 @strlen(i8* %17)
%19 = icmp slt i32 %16, %18
br i1 %19, label %20, label %47
20: ; preds = %15
%21 = load i8*, i8** %3
%22 = load i32, i32* %5
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i8, i8* %24
%26 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
store i8 %25, i8* %26
%27 = load i8*, i8** %3
%28 = load i32, i32* %5
%29 = add nsw i32 %28, 1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %27, i64 %30
%32 = load i8, i8* %31
%33 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 1
store i8 %32, i8* %33
%34 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 2
store i8 0, i8* %34
%35 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
%36 = call i32 @sscanf(i8* %35, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32* %6)
%37 = load i32, i32* %6
%38 = trunc i32 %37 to i8
%39 = load i8*, i8** %4
%40 = load i32, i32* %5
%41 = sdiv i32 %40, 2
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %39, i64 %42
store i8 %38, i8* %43
br label %44
44: ; preds = %20
%45 = load i32, i32* %5
%46 = add nsw i32 %45, 2
store i32 %46, i32* %5
br label %15
47: ; preds = %15
%48 = load i32, i32* %5
%49 = sdiv i32 %48, 2
ret i32 %49
}

LLVM IR (-O2):
@.str = [5 x i8] c"%02x\00"
define i32 @hex_to_buf(i8* %0, i8* nocapture writeonly %1) {
%3 = alloca i32
%4 = alloca [3 x i8]
%5 = bitcast i32* %3 to i8*
%6 = tail call i32 @strlen(i8* %0)
%7 = and i32 %6, 1
%8 = xor i32 %7, 1
%9 = tail call i32 @assert(i32 %8)
%10 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%11 = call i32 @strlen(i8* %0)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %35
13: ; preds = %2
%14 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 1
%15 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 2
br label %16
16: ; preds = %13, %16
%17 = phi i64 [ 0, %13 ], [ %28, %16 ]
%18 = getelementptr inbounds i8, i8* %0, i64 %17
%19 = load i8, i8* %18
store i8 %19, i8* %10
%20 = or i64 %17, 1
%21 = getelementptr inbounds i8, i8* %0, i64 %20
%22 = load i8, i8* %21
store i8 %22, i8* %14
store i8 0, i8* %15
%23 = call i32 @sscanf(i8* %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32* %3)
%24 = load i32, i32* %3
%25 = trunc i32 %24 to i8
%26 = lshr exact i64 %17, 1
%27 = getelementptr inbounds i8, i8* %1, i64 %26
store i8 %25, i8* %27
%28 = add nuw i64 %17, 2
%29 = call i32 @strlen(i8* %0)
%30 = trunc i64 %28 to i32
%31 = icmp sgt i32 %29, %30
br i1 %31, label %16, label %32
32: ; preds = %16
%33 = trunc i64 %28 to i32
%34 = lshr exact i32 %33, 1
br label %35
35: ; preds = %32, %2
%36 = phi i32 [ 0, %2 ], [ %34, %32 ]
ret i32 %36
}