#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 */
unsigned char hex(unsigned char const);
void str_to_hex(const unsigned char *str, unsigned int len,
                unsigned char *result) {
  unsigned int i;
  for (i = 0; i < len; i++) {
    result[i * 2] = hex(str[i] >> 4);
    result[i * 2 + 1] = hex(str[i] & 0xF);
  }
}


x86 assembly (clang, -O0):
<str_to_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    1195 <str_to_hex+0x85>
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
movzbl (%rax,%rcx,1),%eax
sar    $0x4,%eax
movzbl %al,%edi
call   1030 <hex@plt>
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
shl    $0x1,%ecx
mov    %ecx,%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
movzbl (%rax,%rcx,1),%eax
and    $0xf,%eax
movzbl %al,%edi
call   1030 <hex@plt>
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
mov    %ecx,%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    112a <str_to_hex+0x1a>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_to_hex>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %esi,%esi
je     1167 <str_to_hex+0x57>
mov    %rdx,%r14
mov    %rdi,%r12
mov    %esi,%r15d
add    %r15,%r15
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movzbl (%r12),%eax
shr    $0x4,%al
movzbl %al,%edi
call   1030 <hex@plt>
mov    %ebx,%ecx
mov    %al,(%r14,%rcx,1)
movzbl (%r12),%edi
and    $0xf,%edi
call   1030 <hex@plt>
lea    0x1(%rbx),%ecx
mov    %al,(%r14,%rcx,1)
add    $0x1,%r12
add    $0x2,%rbx
cmp    %rbx,%r15
jne    1130 <str_to_hex+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @str_to_hex(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
store i32 0, i32* %7
br label %8
8: ; preds = %42, %3
%9 = load i32, i32* %7
%10 = load i32, i32* %5
%11 = icmp ult i32 %9, %10
br i1 %11, label %12, label %45
12: ; preds = %8
%13 = load i8*, i8** %4
%14 = load i32, i32* %7
%15 = zext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = load i8, i8* %16
%18 = zext i8 %17 to i32
%19 = ashr i32 %18, 4
%20 = trunc i32 %19 to i8
%21 = call zeroext i8 @hex(i8 zeroext %20)
%22 = load i8*, i8** %6
%23 = load i32, i32* %7
%24 = mul i32 %23, 2
%25 = zext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %22, i64 %25
store i8 %21, i8* %26
%27 = load i8*, i8** %4
%28 = load i32, i32* %7
%29 = zext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i8, i8* %30
%32 = zext i8 %31 to i32
%33 = and i32 %32, 15
%34 = trunc i32 %33 to i8
%35 = call zeroext i8 @hex(i8 zeroext %34)
%36 = load i8*, i8** %6
%37 = load i32, i32* %7
%38 = mul i32 %37, 2
%39 = add i32 %38, 1
%40 = zext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %36, i64 %40
store i8 %35, i8* %41
br label %42
42: ; preds = %12
%43 = load i32, i32* %7
%44 = add i32 %43, 1
store i32 %44, i32* %7
br label %8
45: ; preds = %8
ret void
}

LLVM IR (-O2):
define void @str_to_hex(i8* nocapture %0, i32 %1, i8* nocapture writeonly %2) {
%4 = icmp eq i32 %1, 0
br i1 %4, label %25, label %5
5: ; preds = %3
%6 = zext i32 %1 to i64
br label %7
7: ; preds = %5, %7
%8 = phi i64 [ 0, %5 ], [ %23, %7 ]
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = load i8, i8* %9
%11 = lshr i8 %10, 4
%12 = tail call zeroext i8 @hex(i8 zeroext %11)
%13 = trunc i64 %8 to i32
%14 = shl i32 %13, 1
%15 = zext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %2, i64 %15
store i8 %12, i8* %16
%17 = load i8, i8* %9
%18 = and i8 %17, 15
%19 = tail call zeroext i8 @hex(i8 zeroext %18)
%20 = or i32 %14, 1
%21 = zext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %2, i64 %21
store i8 %19, i8* %22
%23 = add nuw nsw i64 %8, 1
%24 = icmp eq i64 %23, %6
br i1 %24, label %25, label %7
25: ; preds = %7, %3
ret void
}