#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(unsigned int);
int /*<<< orphan*/ sprintf(char *, char *, char const);
unsigned int strlen(char const *);
char *char2hex(const char *str) {
  unsigned int len = strlen(str), i;
  char *hex = malloc(len * 2);
  for (i = 0; i < len; i++) {
    sprintf(hex + (i * 2), "%02x", str[i]);
  }
  return hex;
}


x86 assembly (clang, -O0):
<char2hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
shl    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    119e <char2hex+0x6e>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%eax
shl    $0x1,%eax
mov    %eax,%eax
add    %rax,%rdi
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
lea    0xe79(%rip),%rsi
movsbl (%rax,%rcx,1),%edx
call   1050 <sprintf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    115e <char2hex+0x2e>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<char2hex>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r13
call   1030 <strlen@plt>
mov    %eax,%r15d
lea    (%r15,%r15,1),%edi
call   1040 <malloc@plt>
mov    %rax,%r14
test   %r15d,%r15d
je     1192 <char2hex+0x62>
mov    %r15d,%r12d
add    %r12,%r12
xor    %ebx,%ebx
lea    0xe9c(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,%edi
and    $0xfffffffe,%edi
add    %r14,%rdi
movsbl 0x0(%r13),%edx
mov    %r15,%rsi
call   1050 <sprintf@plt>
add    $0x2,%rbx
add    $0x1,%r13
cmp    %rbx,%r12
jne    1170 <char2hex+0x40>
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%02x\00"
define i8* @char2hex(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32 @strlen(i8* %6)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = mul i32 %8, 2
%10 = call i8* @malloc(i32 %9)
store i8* %10, i8** %5
store i32 0, i32* %4
br label %11
11: ; preds = %27, %1
%12 = load i32, i32* %4
%13 = load i32, i32* %3
%14 = icmp ult i32 %12, %13
br i1 %14, label %15, label %30
15: ; preds = %11
%16 = load i8*, i8** %5
%17 = load i32, i32* %4
%18 = mul i32 %17, 2
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %16, i64 %19
%21 = load i8*, i8** %2
%22 = load i32, i32* %4
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i8, i8* %24
%26 = call i32 @sprintf(i8* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 signext %25)
br label %27
27: ; preds = %15
%28 = load i32, i32* %4
%29 = add i32 %28, 1
store i32 %29, i32* %4
br label %11
30: ; preds = %11
%31 = load i8*, i8** %5
ret i8* %31
}

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