#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 */
typedef int __u8;

/* Variables and functions */
int char2hex(char);
int strlen(char *);
void string2hex(char *str, __u8 *hex, int length) {
  int i;
  __u8 nibble;
  for (i = 0; i < (length * 2); i++) {
    if (i < strlen(str))
      nibble = char2hex(str[i]);
    else
      nibble = 0xf;
    if (i % 2 == 0)
      hex[i / 2] = nibble;
    else
      hex[i / 2] = (hex[i / 2] << 4) | nibble;
  }
}


x86 assembly (clang, -O0):
<string2hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
shl    $0x1,%ecx
cmp    %ecx,%eax
jge    1225 <string2hex+0x105>
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
cmp    %ecx,%eax
jge    1180 <string2hex+0x60>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <char2hex@plt>
mov    %eax,-0x1c(%rbp)
jmp    1187 <string2hex+0x67>
movl   $0xf,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11c8 <string2hex+0xa8>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x2c(%rbp),%edx
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
jmp    1212 <string2hex+0xf2>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
shl    $0x4,%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x3c(%rbp),%edx
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
jmp    1217 <string2hex+0xf7>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    113a <string2hex+0x1a>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string2hex>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    1190 <string2hex+0x70>
mov    %rsi,%r14
mov    %rdi,%r12
add    %edx,%edx
cmp    $0x2,%edx
mov    $0x1,%r15d
cmovge %edx,%r15d
xor    %ebx,%ebx
jmp    115d <string2hex+0x3d>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %eax,(%r14,%rcx,4)
add    $0x1,%rbx
cmp    %rbx,%r15
je     1190 <string2hex+0x70>
mov    %r12,%rdi
call   1040 <strlen@plt>
movslq %eax,%rcx
mov    $0xf,%eax
cmp    %rcx,%rbx
jge    117c <string2hex+0x5c>
movsbl (%r12,%rbx,1),%edi
call   1030 <char2hex@plt>
mov    %ebx,%ecx
shr    %ecx
test   $0x1,%bl
je     1150 <string2hex+0x30>
mov    (%r14,%rcx,4),%edx
shl    $0x4,%edx
or     %edx,%eax
jmp    1150 <string2hex+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @string2hex(i8* %0, i32* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
store i32 0, i32* %7
br label %9
9: ; preds = %54, %3
%10 = load i32, i32* %7
%11 = load i32, i32* %6
%12 = mul nsw i32 %11, 2
%13 = icmp slt i32 %10, %12
br i1 %13, label %14, label %57
14: ; preds = %9
%15 = load i32, i32* %7
%16 = load i8*, i8** %4
%17 = call i32 @strlen(i8* %16)
%18 = icmp slt i32 %15, %17
br i1 %18, label %19, label %26
19: ; preds = %14
%20 = load i8*, i8** %4
%21 = load i32, i32* %7
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = load i8, i8* %23
%25 = call i32 @char2hex(i8 signext %24)
store i32 %25, i32* %8
br label %27
26: ; preds = %14
store i32 15, i32* %8
br label %27
27: ; preds = %26, %19
%28 = load i32, i32* %7
%29 = srem i32 %28, 2
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %38
31: ; preds = %27
%32 = load i32, i32* %8
%33 = load i32*, i32** %5
%34 = load i32, i32* %7
%35 = sdiv i32 %34, 2
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %33, i64 %36
store i32 %32, i32* %37
br label %53
38: ; preds = %27
%39 = load i32*, i32** %5
%40 = load i32, i32* %7
%41 = sdiv i32 %40, 2
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i32, i32* %39, i64 %42
%44 = load i32, i32* %43
%45 = shl i32 %44, 4
%46 = load i32, i32* %8
%47 = or i32 %45, %46
%48 = load i32*, i32** %5
%49 = load i32, i32* %7
%50 = sdiv i32 %49, 2
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i32, i32* %48, i64 %51
store i32 %47, i32* %52
br label %53
53: ; preds = %38, %31
br label %54
54: ; preds = %53
%55 = load i32, i32* %7
%56 = add nsw i32 %55, 1
store i32 %56, i32* %7
br label %9
57: ; preds = %9
ret void
}

LLVM IR (-O2):
define void @string2hex(i8* %0, i32* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %33
5: ; preds = %3
%6 = shl nuw i32 %2, 1
%7 = call i32 @llvm.smax.i32(i32 %6, i32 1)
%8 = zext i32 %7 to i64
br label %9
9: ; preds = %5, %29
%10 = phi i64 [ 0, %5 ], [ %31, %29 ]
%11 = tail call i32 @strlen(i8* %0)
%12 = sext i32 %11 to i64
%13 = icmp slt i64 %10, %12
br i1 %13, label %14, label %18
14: ; preds = %9
%15 = getelementptr inbounds i8, i8* %0, i64 %10
%16 = load i8, i8* %15
%17 = tail call i32 @char2hex(i8 signext %16)
br label %18
18: ; preds = %9, %14
%19 = phi i32 [ %17, %14 ], [ 15, %9 ]
%20 = and i64 %10, 1
%21 = icmp eq i64 %20, 0
%22 = lshr i64 %10, 1
%23 = and i64 %22, 2147483647
%24 = getelementptr inbounds i32, i32* %1, i64 %23
br i1 %21, label %29, label %25
25: ; preds = %18
%26 = load i32, i32* %24
%27 = shl i32 %26, 4
%28 = or i32 %27, %19
br label %29
29: ; preds = %18, %25
%30 = phi i32 [ %28, %25 ], [ %19, %18 ]
store i32 %30, i32* %24
%31 = add nuw nsw i64 %10, 1
%32 = icmp eq i64 %31, %8
br i1 %32, label %33, label %9
33: ; preds = %29, %3
ret void
}