#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*/ sprintf(char *, char *, char const);
int strlen(char const *);
void str2hex(char *dest, const char *src, int slen) {
  int i;
  char *p = dest;
  if (slen == 0)
    slen = strlen(src);
  for (i = 0; i < slen; i++) {
    sprintf(p, "%02x", src[i]);
    p += 2;
  }
  *p = 0;
}


x86 assembly (clang, -O0):
<str2hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1151 <str2hex+0x31>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    119a <str2hex+0x7a>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
lea    0xe89(%rip),%rsi
movsbl (%rax,%rcx,1),%edx
call   1040 <sprintf@plt>
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1158 <str2hex+0x38>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str2hex>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r13
test   %edx,%edx
je     1139 <str2hex+0x19>
test   %edx,%edx
jg     1147 <str2hex+0x27>
jmp    117d <str2hex+0x5d>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%edx
test   %edx,%edx
jle    117d <str2hex+0x5d>
mov    %edx,%r12d
xor    %ebx,%ebx
lea    0xead(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl (%r14,%rbx,1),%edx
mov    %r13,%rdi
mov    %r15,%rsi
call   1040 <sprintf@plt>
add    $0x2,%r13
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1160 <str2hex+0x40>
movb   $0x0,0x0(%r13)
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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