#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 */
char *malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned int, unsigned int);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
void binary_to_printable(char *raw, char *payload) {
  int i = 0;
  char *buffer = malloc(sizeof(char) * (strlen(payload) * 2) + 1);
  while (i < strlen(payload)) {
    unsigned int high = (raw[i] >> 4) & 0xf;
    unsigned int low = (raw[i] & 0xf);
    sprintf(buffer + strlen(buffer), "%x%x", high, low);
    ++i;
  }
  strncpy(payload, buffer, strlen(buffer));
}


x86 assembly (clang, -O0):
<binary_to_printable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
shl    $0x1,%eax
cltq
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
cmp    %ecx,%eax
jge    11f5 <binary_to_printable+0xb5>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
sar    $0x4,%eax
and    $0xf,%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
and    $0xf,%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rdi
cltq
add    %rax,%rdi
mov    -0x24(%rbp),%edx
mov    -0x28(%rbp),%ecx
lea    0xe1e(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1178 <binary_to_printable+0x38>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<binary_to_printable>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <strlen@plt>
lea    (%rax,%rax,1),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,%r13
mov    %r15,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    11be <binary_to_printable+0x7e>
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
movzbl (%r14,%r12,1),%ebp
mov    %ebp,%ebx
shr    $0x4,%ebx
and    $0xf,%ebp
mov    %r13,%rdi
call   1040 <strlen@plt>
movslq %eax,%rdi
add    %r13,%rdi
lea    0xe5e(%rip),%rsi
mov    %ebx,%edx
mov    %ebp,%ecx
call   1060 <sprintf@plt>
add    $0x1,%r12
mov    %r15,%rdi
call   1040 <strlen@plt>
cltq
cmp    %rax,%r12
jl     1180 <binary_to_printable+0x40>
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %r15,%rdi
mov    %r13,%rsi
mov    %eax,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <strncpy@plt>

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

LLVM IR (-O2):
@.str = [5 x i8] c"%x%x\00"
define void @binary_to_printable(i8* nocapture %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = shl nsw i32 %3, 1
%5 = or i32 %4, 1
%6 = tail call i8* @malloc(i32 %5)
%7 = tail call i32 @strlen(i8* %1)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %24
9: ; preds = %2, %9
%10 = phi i64 [ %20, %9 ], [ 0, %2 ]
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = load i8, i8* %11
%13 = zext i8 %12 to i32
%14 = lshr i32 %13, 4
%15 = and i32 %13, 15
%16 = tail call i32 @strlen(i8* %6)
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %6, i64 %17
%19 = tail call i32 @sprintf(i8* dereferenceable(1) %18, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %14, i32 %15)
%20 = add nuw nsw i64 %10, 1
%21 = tail call i32 @strlen(i8* %1)
%22 = sext i32 %21 to i64
%23 = icmp slt i64 %20, %22
br i1 %23, label %9, label %24
24: ; preds = %9, %2
%25 = tail call i32 @strlen(i8* %6)
%26 = tail call i32 @strncpy(i8* %1, i8* %6, i32 %25)
ret void
}