#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 char2hex(char const);
int strlen(char const *);
void str2hex(const char *in, unsigned char *hex) {
  int len = strlen(in);
  int inpos = 0;
  int i = 0;
  for (inpos = 0; inpos < len - 1; inpos += 2)
    hex[i++] = (char2hex(in[inpos]) << 4) + char2hex(in[inpos + 1]);
}


x86 assembly (clang, -O0):
<str2hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11bb <str2hex+0x9b>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <char2hex@plt>
shl    $0x4,%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <char2hex@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
add    %ecx,%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x1c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x18(%rbp)
jmp    1151 <str2hex+0x31>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str2hex>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r12
mov    %rdi,%r14
call   1040 <strlen@plt>
cmp    $0x2,%eax
jl     117d <str2hex+0x5d>
mov    %eax,%r15d
add    $0xffffffff,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsbl (%r14,%rbx,1),%edi
call   1030 <char2hex@plt>
mov    %eax,%ebp
shl    $0x4,%ebp
movsbl 0x1(%r14,%rbx,1),%edi
call   1030 <char2hex@plt>
add    %ebp,%eax
mov    %al,(%r12)
add    $0x2,%rbx
add    $0x1,%r12
cmp    %ebx,%r15d
jg     1150 <str2hex+0x30>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @str2hex(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = load i8*, i8** %3
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %6
br label %10
10: ; preds = %37, %2
%11 = load i32, i32* %6
%12 = load i32, i32* %5
%13 = sub nsw i32 %12, 1
%14 = icmp slt i32 %11, %13
br i1 %14, label %15, label %40
15: ; preds = %10
%16 = load i8*, i8** %3
%17 = load i32, i32* %6
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i8, i8* %19
%21 = call i32 @char2hex(i8 signext %20)
%22 = shl i32 %21, 4
%23 = load i8*, i8** %3
%24 = load i32, i32* %6
%25 = add nsw i32 %24, 1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
%28 = load i8, i8* %27
%29 = call i32 @char2hex(i8 signext %28)
%30 = add nsw i32 %22, %29
%31 = trunc i32 %30 to i8
%32 = load i8*, i8** %4
%33 = load i32, i32* %7
%34 = add nsw i32 %33, 1
store i32 %34, i32* %7
%35 = sext i32 %33 to i64
%36 = getelementptr inbounds i8, i8* %32, i64 %35
store i8 %31, i8* %36
br label %37
37: ; preds = %15
%38 = load i32, i32* %6
%39 = add nsw i32 %38, 2
store i32 %39, i32* %6
br label %10
40: ; preds = %10
ret void
}

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