#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*/ assert(int /*<<< orphan*/);
scalar_t__ isprint(unsigned char const);
unsigned int print_readable(const unsigned char *data, unsigned int len,
                            unsigned char *result, unsigned int buflen,
                            int encode_always) {
  const unsigned char hex[] = "0123456789ABCDEF";
  unsigned int cnt = 0;
  assert(data || len == 0);
  if (buflen == 0 || len == 0)
    return 0;
  while (len) {
    if (!encode_always && isprint(*data)) {
      if (cnt == buflen)
        break;
      *result++ = *data;
      ++cnt;
    } else {
      if (cnt + 4 < buflen) {
        *result++ = '\\';
        *result++ = 'x';
        *result++ = hex[(*data & 0xf0) >> 4];
        *result++ = hex[*data & 0x0f];
        cnt += 4;
      } else
        break;
    }
    ++data;
    --len;
  }
  *result = '\0';
  return cnt;
}


x86 assembly (clang, -O0):
<print_readable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8d,-0x28(%rbp)
mov    0xebf(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xebc(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xeba(%rip),%al
mov    %al,-0x30(%rbp)
movl   $0x0,-0x44(%rbp)
mov    $0x1,%al
cmpq   $0x0,-0x10(%rbp)
mov    %al,-0x45(%rbp)
jne    117a <print_readable+0x5a>
cmpl   $0x0,-0x14(%rbp)
sete   %al
mov    %al,-0x45(%rbp)
mov    -0x45(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
cmpl   $0x0,-0x24(%rbp)
je     119b <print_readable+0x7b>
cmpl   $0x0,-0x14(%rbp)
jne    11a7 <print_readable+0x87>
movl   $0x0,-0x4(%rbp)
jmp    12c5 <print_readable+0x1a5>
jmp    11ac <print_readable+0x8c>
cmpl   $0x0,-0x14(%rbp)
je     12b8 <print_readable+0x198>
cmpl   $0x0,-0x28(%rbp)
jne    120c <print_readable+0xec>
mov    -0x10(%rbp),%rax
movzbl (%rax),%edi
call   1040 <isprint@plt>
cmp    $0x0,%rax
je     120c <print_readable+0xec>
mov    -0x44(%rbp),%eax
cmp    -0x24(%rbp),%eax
jne    11e7 <print_readable+0xc7>
jmp    12b8 <print_readable+0x198>
mov    -0x10(%rbp),%rax
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    129e <print_readable+0x17e>
mov    -0x44(%rbp),%eax
add    $0x4,%eax
cmp    -0x24(%rbp),%eax
jae    1294 <print_readable+0x174>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x5c,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x78,(%rax)
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
and    $0xf0,%eax
sar    $0x4,%eax
cltq
mov    -0x40(%rbp,%rax,1),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
and    $0xf,%eax
cltq
mov    -0x40(%rbp,%rax,1),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x44(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x44(%rbp)
jmp    1299 <print_readable+0x179>
jmp    12b8 <print_readable+0x198>
jmp    129e <print_readable+0x17e>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    11ac <print_readable+0x8c>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_readable>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,0xc(%rsp)
mov    %ecx,%r15d
mov    %rdx,%rbx
mov    %esi,%r12d
mov    %rdi,0x10(%rsp)
test   %rdi,%rdi
setne  %al
test   %esi,%esi
sete   %cl
or     %al,%cl
movzbl %cl,%edi
call   1030 <assert@plt>
xor    %eax,%eax
test   %r12d,%r12d
je     1217 <print_readable+0xf7>
test   %r15d,%r15d
je     1217 <print_readable+0xf7>
mov    %r12d,%r13d
xor    %ebp,%ebp
lea    0xe8a(%rip),%rsi
xor    %r14d,%r14d
mov    0xc(%rsp),%r8d
mov    0x10(%rsp),%rdx
jmp    11cb <print_readable+0xab>
cs nopw 0x0(%rax,%rax,1)
nop
lea    0x4(%r14),%eax
cmp    %r15d,%eax
jae    120c <print_readable+0xec>
movw   $0x785c,(%rbx)
movzbl (%rdx,%rbp,1),%ecx
shr    $0x4,%rcx
movzbl (%rcx,%rsi,1),%ecx
mov    %cl,0x2(%rbx)
movzbl (%rdx,%rbp,1),%ecx
and    $0xf,%ecx
movzbl (%rcx,%rsi,1),%ecx
mov    %cl,0x3(%rbx)
add    $0x4,%rbx
add    $0x1,%rbp
mov    %eax,%r14d
cmp    %ebp,%r13d
je     1214 <print_readable+0xf4>
test   %r8d,%r8d
jne    1190 <print_readable+0x70>
movzbl (%rdx,%rbp,1),%edi
mov    %r15d,%r12d
mov    %rsi,%r15
call   1040 <isprint@plt>
mov    0x10(%rsp),%rdx
mov    %r15,%rsi
mov    %r12d,%r15d
mov    0xc(%rsp),%r8d
test   %rax,%rax
je     1190 <print_readable+0x70>
cmp    %r15d,%r14d
je     1211 <print_readable+0xf1>
movzbl (%rdx,%rbp,1),%eax
mov    %al,(%rbx)
add    $0x1,%rbx
add    $0x1,%r14d
mov    %r14d,%eax
jmp    11bf <print_readable+0x9f>
mov    %r14d,%eax
jmp    1214 <print_readable+0xf4>
mov    %r15d,%eax
movb   $0x0,(%rbx)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.print_readable.hex = [17 x i8] c"0123456789ABCDEF\00"
define i32 @print_readable(i8* %0, i32 %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca [17 x i8]
%13 = alloca i32
store i8* %0, i8** %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%14 = bitcast [17 x i8]* %12 to i8*
store i32 0, i32* %13
%15 = load i8*, i8** %7
%16 = icmp ne i8* %15, null
br i1 %16, label %20, label %17
17: ; preds = %5
%18 = load i32, i32* %8
%19 = icmp eq i32 %18, 0
br label %20
20: ; preds = %17, %5
%21 = phi i1 [ true, %5 ], [ %19, %17 ]
%22 = zext i1 %21 to i32
%23 = call i32 @assert(i32 %22)
%24 = load i32, i32* %10
%25 = icmp eq i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %20
%27 = load i32, i32* %8
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %26, %20
store i32 0, i32* %6
br label %95
30: ; preds = %26
br label %31
31: ; preds = %87, %30
%32 = load i32, i32* %8
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %92
34: ; preds = %31
%35 = load i32, i32* %11
%36 = icmp ne i32 %35, 0
br i1 %36, label %54, label %37
37: ; preds = %34
%38 = load i8*, i8** %7
%39 = load i8, i8* %38
%40 = call i64 @isprint(i8 zeroext %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %54
42: ; preds = %37
%43 = load i32, i32* %13
%44 = load i32, i32* %10
%45 = icmp eq i32 %43, %44
br i1 %45, label %46, label %47
46: ; preds = %42
br label %92
47: ; preds = %42
%48 = load i8*, i8** %7
%49 = load i8, i8* %48
%50 = load i8*, i8** %9
%51 = getelementptr inbounds i8, i8* %50, i32 1
store i8* %51, i8** %9
store i8 %49, i8* %50
%52 = load i32, i32* %13
%53 = add i32 %52, 1
store i32 %53, i32* %13
br label %87
54: ; preds = %37, %34
%55 = load i32, i32* %13
%56 = add i32 %55, 4
%57 = load i32, i32* %10
%58 = icmp ult i32 %56, %57
br i1 %58, label %59, label %85
59: ; preds = %54
%60 = load i8*, i8** %9
%61 = getelementptr inbounds i8, i8* %60, i32 1
store i8* %61, i8** %9
store i8 92, i8* %60
%62 = load i8*, i8** %9
%63 = getelementptr inbounds i8, i8* %62, i32 1
store i8* %63, i8** %9
store i8 120, i8* %62
%64 = load i8*, i8** %7
%65 = load i8, i8* %64
%66 = zext i8 %65 to i32
%67 = and i32 %66, 240
%68 = ashr i32 %67, 4
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds [17 x i8], [17 x i8]* %12, i64 0, i64 %69
%71 = load i8, i8* %70
%72 = load i8*, i8** %9
%73 = getelementptr inbounds i8, i8* %72, i32 1
store i8* %73, i8** %9
store i8 %71, i8* %72
%74 = load i8*, i8** %7
%75 = load i8, i8* %74
%76 = zext i8 %75 to i32
%77 = and i32 %76, 15
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds [17 x i8], [17 x i8]* %12, i64 0, i64 %78
%80 = load i8, i8* %79
%81 = load i8*, i8** %9
%82 = getelementptr inbounds i8, i8* %81, i32 1
store i8* %82, i8** %9
store i8 %80, i8* %81
%83 = load i32, i32* %13
%84 = add i32 %83, 4
store i32 %84, i32* %13
br label %86
85: ; preds = %54
br label %92
86: ; preds = %59
br label %87
87: ; preds = %86, %47
%88 = load i8*, i8** %7
%89 = getelementptr inbounds i8, i8* %88, i32 1
store i8* %89, i8** %7
%90 = load i32, i32* %8
%91 = add i32 %90, -1
store i32 %91, i32* %8
br label %31
92: ; preds = %85, %46, %31
%93 = load i8*, i8** %9
store i8 0, i8* %93
%94 = load i32, i32* %13
store i32 %94, i32* %6
br label %95
95: ; preds = %92, %29
%96 = load i32, i32* %6
ret i32 %96
}

LLVM IR (-O2):
@__const.print_readable.hex = [17 x i8] c"0123456789ABCDEF\00"
define i32 @print_readable(i8* %0, i32 %1, i8* nocapture writeonly %2, i32 %3, i32 %4) {
%6 = icmp ne i8* %0, null
%7 = icmp eq i32 %1, 0
%8 = or i1 %6, %7
%9 = zext i1 %8 to i32
%10 = tail call i32 @assert(i32 %9)
%11 = icmp eq i32 %3, 0
%12 = or i1 %7, %11
br i1 %12, label %57, label %13
13: ; preds = %5
%14 = icmp eq i32 %4, 0
br label %15
15: ; preds = %13, %48
%16 = phi i32 [ 0, %13 ], [ %50, %48 ]
%17 = phi i8* [ %0, %13 ], [ %51, %48 ]
%18 = phi i8* [ %2, %13 ], [ %49, %48 ]
%19 = phi i32 [ %1, %13 ], [ %52, %48 ]
br i1 %14, label %20, label %30
20: ; preds = %15
%21 = load i8, i8* %17
%22 = tail call i64 @isprint(i8 zeroext %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %30, label %24
24: ; preds = %20
%25 = icmp eq i32 %16, %3
br i1 %25, label %54, label %26
26: ; preds = %24
%27 = load i8, i8* %17
%28 = getelementptr inbounds i8, i8* %18, i64 1
store i8 %27, i8* %18
%29 = add i32 %16, 1
br label %48
30: ; preds = %20, %15
%31 = add i32 %16, 4
%32 = icmp ult i32 %31, %3
br i1 %32, label %33, label %54
33: ; preds = %30
%34 = getelementptr inbounds i8, i8* %18, i64 1
store i8 92, i8* %18
%35 = getelementptr inbounds i8, i8* %18, i64 2
store i8 120, i8* %34
%36 = load i8, i8* %17
%37 = lshr i8 %36, 4
%38 = zext i8 %37 to i64
%39 = getelementptr inbounds [17 x i8], [17 x i8]* @__const.print_readable.hex, i64 0, i64 %38
%40 = load i8, i8* %39
%41 = getelementptr inbounds i8, i8* %18, i64 3
store i8 %40, i8* %35
%42 = load i8, i8* %17
%43 = and i8 %42, 15
%44 = zext i8 %43 to i64
%45 = getelementptr inbounds [17 x i8], [17 x i8]* @__const.print_readable.hex, i64 0, i64 %44
%46 = load i8, i8* %45
%47 = getelementptr inbounds i8, i8* %18, i64 4
store i8 %46, i8* %41
br label %48
48: ; preds = %33, %26
%49 = phi i8* [ %47, %33 ], [ %28, %26 ]
%50 = phi i32 [ %31, %33 ], [ %29, %26 ]
%51 = getelementptr inbounds i8, i8* %17, i64 1
%52 = add i32 %19, -1
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %15
54: ; preds = %48, %24, %30
%55 = phi i8* [ %18, %30 ], [ %18, %24 ], [ %49, %48 ]
%56 = phi i32 [ %16, %30 ], [ %3, %24 ], [ %50, %48 ]
store i8 0, i8* %55
br label %57
57: ; preds = %5, %54
%58 = phi i32 [ %56, %54 ], [ 0, %5 ]
ret i32 %58
}