#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*/ memset(char *, char, unsigned long);
int /*<<< orphan*/ sprintf(char *, char *, char);
int /*<<< orphan*/ strcat(char *, char *);
unsigned long strlen(char *);
char *ascii2hex(char *astr) {
  register unsigned long int i, l;
  l = strlen(astr);
  char c = 0;
  char h[20480], t[4];
  memset(h, '\0', l + 1);
  for (i = 0; i < l; i++) {
    sprintf(t, "%02X", astr[i] & 0xFF);
    strcat(h, t);
  }
  return h;
}


x86 assembly (clang, -O0):
<ascii2hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x5030,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
movb   $0x0,-0x19(%rbp)
lea    -0x5020(%rbp),%rdi
mov    -0x18(%rbp),%rdx
add    $0x1,%rdx
xor    %esi,%esi
call   1040 <memset@plt>
movq   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11d7 <ascii2hex+0x97>
lea    -0x5024(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
and    $0xff,%eax
lea    0xe55(%rip),%rsi
movsbl %al,%edx
call   1060 <sprintf@plt>
lea    -0x5020(%rbp),%rdi
lea    -0x5024(%rbp),%rsi
call   1050 <strcat@plt>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    117e <ascii2hex+0x3e>
lea    -0x5020(%rbp),%rax
add    $0x5030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ascii2hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x5018,%rsp
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %rax,%r12
lea    0x1(%rax),%rdx
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1040 <memset@plt>
test   %r12,%r12
je     11b4 <ascii2hex+0x74>
xor    %ebp,%ebp
lea    0xe86(%rip),%r15
lea    0xc(%rsp),%rbx
lea    0x10(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsbl (%r14,%rbp,1),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1060 <sprintf@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
call   1050 <strcat@plt>
add    $0x1,%rbp
cmp    %rbp,%r12
jne    1190 <ascii2hex+0x50>
lea    0x10(%rsp),%rax
add    $0x5018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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