#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 */
scalar_t__ char_to_val(char *);
void str_to_char(char *str, unsigned char *buf, int length) {
  int i;
  for (i = 0; i < length / 2; i++, str += 2, buf++)
    *buf = (unsigned char)char_to_val(str);
}


x86 assembly (clang, -O0):
<str_to_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
cmp    %ecx,%eax
jge    117f <str_to_char+0x6f>
mov    -0x8(%rbp),%rdi
call   1030 <char_to_val@plt>
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    112a <str_to_char+0x1a>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_to_char>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edx
jl     1159 <str_to_char+0x49>
mov    %rsi,%r14
mov    %rdi,%r12
mov    %edx,%eax
shr    $0x1f,%eax
add    %edx,%eax
sar    %eax
mov    %eax,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1030 <char_to_val@plt>
mov    %al,(%r14,%rbx,1)
add    $0x2,%r12
add    $0x1,%rbx
cmp    %ebx,%r15d
jne    1140 <str_to_char+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @str_to_char(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
br label %8
8: ; preds = %18, %3
%9 = load i32, i32* %7
%10 = load i32, i32* %6
%11 = sdiv i32 %10, 2
%12 = icmp slt i32 %9, %11
br i1 %12, label %13, label %25
13: ; preds = %8
%14 = load i8*, i8** %4
%15 = call i64 @char_to_val(i8* %14)
%16 = trunc i64 %15 to i8
%17 = load i8*, i8** %5
store i8 %16, i8* %17
br label %18
18: ; preds = %13
%19 = load i32, i32* %7
%20 = add nsw i32 %19, 1
store i32 %20, i32* %7
%21 = load i8*, i8** %4
%22 = getelementptr inbounds i8, i8* %21, i64 2
store i8* %22, i8** %4
%23 = load i8*, i8** %5
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %5
br label %8
25: ; preds = %8
ret void
}

LLVM IR (-O2):
define void @str_to_char(i8* %0, i8* nocapture writeonly %1, i32 %2) {
%4 = sdiv i32 %2, 2
%5 = icmp sgt i32 %2, 1
br i1 %5, label %6, label %16
6: ; preds = %3, %6
%7 = phi i32 [ %12, %6 ], [ 0, %3 ]
%8 = phi i8* [ %13, %6 ], [ %0, %3 ]
%9 = phi i8* [ %14, %6 ], [ %1, %3 ]
%10 = tail call i64 @char_to_val(i8* %8)
%11 = trunc i64 %10 to i8
store i8 %11, i8* %9
%12 = add nuw nsw i32 %7, 1
%13 = getelementptr inbounds i8, i8* %8, i64 2
%14 = getelementptr inbounds i8, i8* %9, i64 1
%15 = icmp eq i32 %12, %4
br i1 %15, label %16, label %6
16: ; preds = %6, %3
ret void
}