#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__ isalpha(char);
char *malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char);
char *str_format_hex(char *buf, int len) {
  int l = len * 2;
  char *hex = malloc(sizeof(char) * l);
  for (int i = 0; i < len; i++) {
    if (isalpha(buf[i])) {
      sprintf(&hex[i * 2], " %c", buf[i]);
    } else {
      sprintf(&hex[i * 2], "%02x", buf[i]);
    }
  }
  return hex;
}


x86 assembly (clang, -O0):
<str_format_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x10(%rbp)
movslq -0x10(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11ef <str_format_hex+0xbf>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isalpha@plt>
cmp    $0x0,%rax
je     11b5 <str_format_hex+0x85>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
cltq
add    %rax,%rdi
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
lea    0xe59(%rip),%rsi
movsbl (%rax,%rcx,1),%edx
call   1050 <sprintf@plt>
jmp    11dc <str_format_hex+0xac>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
cltq
add    %rax,%rdi
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
lea    0xe31(%rip),%rsi
movsbl (%rax,%rcx,1),%edx
call   1050 <sprintf@plt>
jmp    11e1 <str_format_hex+0xb1>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1162 <str_format_hex+0x32>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_format_hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r15
lea    (%rbx,%rbx,1),%edi
call   1030 <malloc@plt>
mov    %rax,(%rsp)
test   %ebx,%ebx
jle    119e <str_format_hex+0x6e>
mov    %ebx,%r12d
xor    %ebx,%ebx
lea    0xea8(%rip),%rbp
lea    0xe9d(%rip),%r14
mov    (%rsp),%r13
nopw   0x0(%rax,%rax,1)
movsbl (%r15,%rbx,1),%edi
call   1040 <isalpha@plt>
test   %rax,%rax
movsbl (%r15,%rbx,1),%edx
mov    %r14,%rsi
cmove  %rbp,%rsi
mov    %r13,%rdi
call   1050 <sprintf@plt>
add    $0x1,%rbx
add    $0x2,%r13
cmp    %rbx,%r12
jne    1170 <str_format_hex+0x40>
mov    (%rsp),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [4 x i8] c" %c\00"
@.str.1 = [5 x i8] c"%02x\00"
define noalias i8* @str_format_hex(i8* nocapture %0, i32 %1) {
%3 = shl nsw i32 %1, 1
%4 = tail call i8* @malloc(i32 %3)
%5 = icmp sgt i32 %1, 0
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = zext i32 %1 to i64
br label %9
8: ; preds = %9, %2
ret i8* %4
9: ; preds = %6, %9
%10 = phi i64 [ 0, %6 ], [ %20, %9 ]
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = load i8, i8* %11
%13 = tail call i64 @isalpha(i8 signext %12)
%14 = icmp eq i64 %13, 0
%15 = shl nuw nsw i64 %10, 1
%16 = getelementptr inbounds i8, i8* %4, i64 %15
%17 = load i8, i8* %11
%18 = select i1 %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0)
%19 = tail call i32 @sprintf(i8* dereferenceable(1) %16, i8* dereferenceable(1) %18, i8 signext %17)
%20 = add nuw nsw i64 %10, 1
%21 = icmp eq i64 %20, %7
br i1 %21, label %8, label %9
}