#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 */
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
char *strToHex(char *buffer, int size) {
  int ch, i;
  char hexch[5];
  char hexstr[16384 * 4] = "";
  for (i = 0; i < size; i++) {
    ch = buffer[i];
    sprintf(hexch, "%x ", ch);
    strcat(hexstr, hexch);
  }
  printf("HexStr: %s\n", hexstr);
  return hexstr;
}


x86 assembly (clang, -O0):
<strToHex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10020,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x10020(%rbp),%rdi
xor    %esi,%esi
mov    $0x10000,%edx
call   1040 <memset@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11b8 <strToHex+0x78>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    %eax,-0x10(%rbp)
lea    -0x19(%rbp),%rdi
mov    -0x10(%rbp),%edx
lea    0xe6b(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x10020(%rbp),%rdi
lea    -0x19(%rbp),%rsi
call   1050 <strcat@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    116c <strToHex+0x2c>
lea    -0x10020(%rbp),%rsi
lea    0xe3e(%rip),%rdi
call   1030 <printf@plt>
lea    -0x10020(%rbp),%rax
add    $0x10020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strToHex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10018,%rsp
mov    %esi,%ebx
mov    %rdi,%r15
lea    0x10(%rsp),%rdi
mov    $0x10000,%edx
xor    %esi,%esi
call   1040 <memset@plt>
test   %ebx,%ebx
jle    11b4 <strToHex+0x74>
mov    %ebx,%r13d
xor    %ebp,%ebp
lea    0xe89(%rip),%r14
lea    0xb(%rsp),%rbx
lea    0x10(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsbl (%r15,%rbp,1),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <sprintf@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1050 <strcat@plt>
add    $0x1,%rbp
cmp    %rbp,%r13
jne    1190 <strToHex+0x50>
lea    0xe49(%rip),%rdi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    %rbx,%rax
add    $0x10018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%x \00"
@.str.1 = [12 x i8] c"HexStr: %s\0A\00"
define i8* @strToHex(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [5 x i8]
%8 = alloca [65536 x i8]
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = bitcast [65536 x i8]* %8 to i8*
store i32 0, i32* %6
br label %10
10: ; preds = %27, %2
%11 = load i32, i32* %6
%12 = load i32, i32* %4
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %30
14: ; preds = %10
%15 = load i8*, i8** %3
%16 = load i32, i32* %6
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
store i32 %20, i32* %5
%21 = getelementptr inbounds [5 x i8], [5 x i8]* %7, i64 0, i64 0
%22 = load i32, i32* %5
%23 = call i32 @sprintf(i8* %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = getelementptr inbounds [65536 x i8], [65536 x i8]* %8, i64 0, i64 0
%25 = getelementptr inbounds [5 x i8], [5 x i8]* %7, i64 0, i64 0
%26 = call i32 @strcat(i8* %24, i8* %25)
br label %27
27: ; preds = %14
%28 = load i32, i32* %6
%29 = add nsw i32 %28, 1
store i32 %29, i32* %6
br label %10
30: ; preds = %10
%31 = getelementptr inbounds [65536 x i8], [65536 x i8]* %8, i64 0, i64 0
%32 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%33 = getelementptr inbounds [65536 x i8], [65536 x i8]* %8, i64 0, i64 0
ret i8* %33
}

LLVM IR (-O2):
@.str = [4 x i8] c"%x \00"
@.str.1 = [12 x i8] c"HexStr: %s\0A\00"
define i8* @strToHex(i8* nocapture %0, i32 %1) {
%3 = alloca [5 x i8]
%4 = alloca [65536 x i8]
%5 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [65536 x i8], [65536 x i8]* %4, i64 0, i64 0
%7 = icmp sgt i32 %1, 0
br i1 %7, label %8, label %19
8: ; preds = %2
%9 = zext i32 %1 to i64
br label %10
10: ; preds = %8, %10
%11 = phi i64 [ 0, %8 ], [ %17, %10 ]
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = call i32 @strcat(i8* %6, i8* %5)
%17 = add nuw nsw i64 %11, 1
%18 = icmp eq i64 %17, %9
br i1 %18, label %19, label %10
19: ; preds = %10, %2
%20 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %6)
ret i8* %6
}