#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*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sprintf(char *, char *, char);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
char *hex_encode(char *in) {
  int i;
  int chars = 0;
  char tmp[3];
  char *out;
  chars = strlen(in) * 2;
  out = malloc((chars + 1) * sizeof(char));
  memset(out, 0, (chars + 1) * sizeof(char));
  for (i = 0; (i < strlen(in)); i++) {
    sprintf(tmp, "%02x", (char)in[i]);
    strncat(out, tmp, 3);
  }
  return out;
}


x86 assembly (clang, -O0):
<hex_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
shl    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1040 <memset@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
jge    1201 <hex_encode+0xb1>
lea    -0x13(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
lea    0xe28(%rip),%rsi
movsbl (%rax,%rcx,1),%edx
call   1070 <sprintf@plt>
mov    -0x20(%rbp),%rdi
lea    -0x13(%rbp),%rsi
mov    $0x3,%edx
call   1050 <strncat@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11a9 <hex_encode+0x59>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_encode>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
call   1030 <strlen@plt>
lea    (%rax,%rax,1),%ebp
add    $0x1,%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1040 <memset@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    11d2 <hex_encode+0x82>
xor    %ebp,%ebp
lea    0xe6b(%rip),%r15
lea    0xd(%rsp),%r12
nopw   0x0(%rax,%rax,1)
movsbl (%rbx,%rbp,1),%edx
mov    %r12,%rdi
mov    %r15,%rsi
call   1070 <sprintf@plt>
mov    %r14,%rdi
mov    %r12,%rsi
mov    $0x3,%edx
call   1050 <strncat@plt>
add    $0x1,%rbp
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
cmp    %rax,%rbp
jl     11a0 <hex_encode+0x50>
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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