#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 */
typedef scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, unsigned char, unsigned char, unsigned char);
int /*<<< orphan*/ write_to_str(unsigned char, int /*<<< orphan*/);
char *hex_to_str(unsigned char *src, char *dest, ssize_t len) {
  unsigned char *d = dest;
  unsigned char *s = src;
  for (; len > 0; --len, s++) {
    printf("%02x - %x %x\n", *s, *s >> 4, *s & 0x0f);
    write_to_str((*s) >> 4, d++);
    write_to_str((*s) & 0x0f, d++);
  }
  return dest;
}


x86 assembly (clang, -O0):
<hex_to_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
jle    11e3 <hex_to_str+0xc3>
mov    -0x28(%rbp),%rax
mov    (%rax),%dl
mov    -0x28(%rbp),%rax
movzbl (%rax),%eax
sar    $0x4,%eax
mov    %al,%cl
mov    -0x28(%rbp),%rax
movzbl (%rax),%eax
and    $0xf,%eax
lea    0xe8e(%rip),%rdi
movzbl %dl,%esi
movzbl %cl,%edx
movzbl %al,%ecx
call   1030 <printf@plt>
mov    -0x28(%rbp),%rax
movzbl (%rax),%eax
sar    $0x4,%eax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %ecx,%esi
movzbl %al,%edi
call   1040 <write_to_str@plt>
mov    -0x28(%rbp),%rax
movzbl (%rax),%eax
and    $0xf,%eax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %ecx,%esi
movzbl %al,%edi
call   1040 <write_to_str@plt>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1144 <hex_to_str+0x24>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_to_str>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
test   %rdx,%rdx
jle    119d <hex_to_str+0x7d>
mov    %rdx,%r15
mov    %rdi,%r13
add    $0x1,%r15
lea    0xebe(%rip),%r12
mov    %r14d,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
movzbl 0x0(%r13),%esi
mov    %esi,%eax
shr    $0x4,%al
mov    %esi,%ecx
and    $0xf,%ecx
movzbl %al,%edx
mov    %r12,%rdi
call   1030 <printf@plt>
movzbl 0x0(%r13),%eax
shr    $0x4,%al
movzbl %al,%edi
mov    %ebx,%esi
call   1040 <write_to_str@plt>
movzbl 0x0(%r13),%edi
lea    0x1(%rbx),%esi
and    $0xf,%edi
call   1040 <write_to_str@plt>
add    $0x1,%r13
add    $0x2,%ebx
add    $0xffffffffffffffff,%r15
cmp    $0x1,%r15
ja     1150 <hex_to_str+0x30>
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [14 x i8] c"%02x - %x %x\0A\00"
define i8* @hex_to_str(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = load i8*, i8** %5
store i8* %9, i8** %7
%10 = load i8*, i8** %4
store i8* %10, i8** %8
br label %11
11: ; preds = %46, %3
%12 = load i64, i64* %6
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %51
14: ; preds = %11
%15 = load i8*, i8** %8
%16 = load i8, i8* %15
%17 = load i8*, i8** %8
%18 = load i8, i8* %17
%19 = zext i8 %18 to i32
%20 = ashr i32 %19, 4
%21 = trunc i32 %20 to i8
%22 = load i8*, i8** %8
%23 = load i8, i8* %22
%24 = zext i8 %23 to i32
%25 = and i32 %24, 15
%26 = trunc i32 %25 to i8
%27 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8 zeroext %16, i8 zeroext %21, i8 zeroext %26)
%28 = load i8*, i8** %8
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = ashr i32 %30, 4
%32 = trunc i32 %31 to i8
%33 = load i8*, i8** %7
%34 = getelementptr inbounds i8, i8* %33, i32 1
store i8* %34, i8** %7
%35 = ptrtoint i8* %33 to i32
%36 = call i32 @write_to_str(i8 zeroext %32, i32 %35)
%37 = load i8*, i8** %8
%38 = load i8, i8* %37
%39 = zext i8 %38 to i32
%40 = and i32 %39, 15
%41 = trunc i32 %40 to i8
%42 = load i8*, i8** %7
%43 = getelementptr inbounds i8, i8* %42, i32 1
store i8* %43, i8** %7
%44 = ptrtoint i8* %42 to i32
%45 = call i32 @write_to_str(i8 zeroext %41, i32 %44)
br label %46
46: ; preds = %14
%47 = load i64, i64* %6
%48 = add nsw i64 %47, -1
store i64 %48, i64* %6
%49 = load i8*, i8** %8
%50 = getelementptr inbounds i8, i8* %49, i32 1
store i8* %50, i8** %8
br label %11
51: ; preds = %11
%52 = load i8*, i8** %5
ret i8* %52
}

LLVM IR (-O2):
@.str = [14 x i8] c"%02x - %x %x\0A\00"
define i8* @hex_to_str(i8* nocapture %0, i8* returned %1, i64 %2) {
%4 = icmp sgt i64 %2, 0
br i1 %4, label %5, label %28
5: ; preds = %3, %5
%6 = phi i8* [ %26, %5 ], [ %0, %3 ]
%7 = phi i8* [ %21, %5 ], [ %1, %3 ]
%8 = phi i64 [ %25, %5 ], [ %2, %3 ]
%9 = load i8, i8* %6
%10 = lshr i8 %9, 4
%11 = and i8 %9, 15
%12 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8 zeroext %9, i8 zeroext %10, i8 zeroext %11)
%13 = load i8, i8* %6
%14 = lshr i8 %13, 4
%15 = getelementptr inbounds i8, i8* %7, i64 1
%16 = ptrtoint i8* %7 to i64
%17 = trunc i64 %16 to i32
%18 = tail call i32 @write_to_str(i8 zeroext %14, i32 %17)
%19 = load i8, i8* %6
%20 = and i8 %19, 15
%21 = getelementptr inbounds i8, i8* %7, i64 2
%22 = ptrtoint i8* %15 to i64
%23 = trunc i64 %22 to i32
%24 = tail call i32 @write_to_str(i8 zeroext %20, i32 %23)
%25 = add nsw i64 %8, -1
%26 = getelementptr inbounds i8, i8* %6, i64 1
%27 = icmp ugt i64 %8, 1
br i1 %27, label %5, label %28
28: ; preds = %5, %3
ret i8* %1
}