#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*/ snprintf(char *, int, char *, unsigned char);
int redir_chartohex(unsigned char *src, int len, char *dst) {
  char x[3];
  int n = 0;
  for (n = 0; n < len; n++) {
    snprintf(x, 3, "%.2x", src[n]);
    dst[n * 2 + 0] = x[0];
    dst[n * 2 + 1] = x[1];
  }
  dst[len * 2] = 0;
  return 0;
}


x86 assembly (clang, -O0):
<redir_chartohex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1198 <redir_chartohex+0x88>
lea    -0x1b(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    $0x3,%esi
lea    0xeab(%rip),%rdx
movzbl (%rax,%rcx,1),%ecx
call   1030 <snprintf@plt>
mov    -0x1b(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
shl    $0x1,%ecx
add    $0x0,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1a(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1131 <redir_chartohex+0x21>
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redir_chartohex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %esi,0x4(%rsp)
test   %esi,%esi
jle    1172 <redir_chartohex+0x62>
mov    %rdi,%r13
mov    0x4(%rsp),%ebp
xor    %r14d,%r14d
lea    0xec9(%rip),%r15
lea    0x1(%rsp),%r12
nopl   0x0(%rax)
movzbl 0x0(%r13,%r14,1),%ecx
mov    %r12,%rdi
mov    $0x3,%esi
mov    %r15,%rdx
call   1030 <snprintf@plt>
movzbl 0x1(%rsp),%eax
mov    %al,(%rbx,%r14,2)
movzbl 0x2(%rsp),%eax
mov    %al,0x1(%rbx,%r14,2)
add    $0x1,%r14
cmp    %r14,%rbp
jne    1140 <redir_chartohex+0x30>
movslq 0x4(%rsp),%rax
movb   $0x0,(%rbx,%rax,2)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"%.2x\00"
define i32 @redir_chartohex(i8* nocapture %0, i32 %1, i8* nocapture writeonly %2) {
%4 = alloca [3 x i8]
%5 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%6 = icmp sgt i32 %1, 0
br i1 %6, label %7, label %23
7: ; preds = %3
%8 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 1
%9 = zext i32 %1 to i64
br label %10
10: ; preds = %7, %10
%11 = phi i64 [ 0, %7 ], [ %21, %10 ]
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = load i8, i8* %12
%14 = call i32 @snprintf(i8* %5, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %13)
%15 = load i8, i8* %5
%16 = shl nuw nsw i64 %11, 1
%17 = getelementptr inbounds i8, i8* %2, i64 %16
store i8 %15, i8* %17
%18 = load i8, i8* %8
%19 = or i64 %16, 1
%20 = getelementptr inbounds i8, i8* %2, i64 %19
store i8 %18, i8* %20
%21 = add nuw nsw i64 %11, 1
%22 = icmp eq i64 %21, %9
br i1 %22, label %23, label %10
23: ; preds = %10, %3
%24 = shl nsw i32 %1, 1
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %2, i64 %25
store i8 0, i8* %26
ret i32 0
}