#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 */
scalar_t__ isprint(unsigned char);
unsigned char_to_printable(unsigned char chr, char *p_print_str) {
  unsigned len;
  if (isprint(chr)) {
    *p_print_str++ = chr;
    len = 1;
  } else if (chr < 128) {
    *p_print_str++ = '^';
    *p_print_str++ = (127 == chr) ? '?' : ('@' + chr);
    len = 2;
  } else {
    *p_print_str++ = 'M';
    *p_print_str++ = '-';
    return 2 + char_to_printable(chr - 128, p_print_str);
  }
  *p_print_str = '\0';
  return len;
}


x86 assembly (clang, -O0):
<char_to_printable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dil,%al
mov    %al,-0x5(%rbp)
mov    %rsi,-0x10(%rbp)
movzbl -0x5(%rbp),%edi
call   1040 <isprint@plt>
cmp    $0x0,%rax
je     1165 <char_to_printable+0x45>
mov    -0x5(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
movl   $0x1,-0x14(%rbp)
jmp    1219 <char_to_printable+0xf9>
movzbl -0x5(%rbp),%eax
cmp    $0x80,%eax
jge    11d0 <char_to_printable+0xb0>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x5e,(%rax)
movzbl -0x5(%rbp),%ecx
mov    $0x7f,%eax
cmp    %ecx,%eax
jne    11a4 <char_to_printable+0x84>
mov    $0x3f,%eax
mov    %eax,-0x18(%rbp)
jmp    11ae <char_to_printable+0x8e>
movzbl -0x5(%rbp),%eax
add    $0x40,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
movl   $0x2,-0x14(%rbp)
jmp    1214 <char_to_printable+0xf4>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x4d,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x2d,(%rax)
movzbl -0x5(%rbp),%eax
sub    $0x80,%eax
mov    -0x10(%rbp),%rsi
movzbl %al,%edi
call   1030 <char_to_printable@plt>
add    $0x2,%eax
mov    %eax,-0x4(%rbp)
jmp    1226 <char_to_printable+0x106>
jmp    1219 <char_to_printable+0xf9>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<char_to_printable>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
call   1030 <isprint@plt>
xor    %r14d,%r14d
test   %rax,%rax
jne    115b <char_to_printable+0x4b>
add    $0x40,%ebp
xor    %r14d,%r14d
nopl   0x0(%rax)
mov    %ebp,%eax
add    $0xc0,%al
jns    1175 <char_to_printable+0x65>
movw   $0x2d4d,(%rbx)
add    $0x2,%rbx
lea    0x40(%rbp),%eax
add    $0x2,%r14d
movzbl %al,%edi
call   1030 <isprint@plt>
add    $0x80,%bpl
test   %rax,%rax
je     1130 <char_to_printable+0x20>
add    $0xc0,%bpl
mov    %bpl,(%rbx)
add    $0x1,%rbx
mov    $0x1,%eax
add    %eax,%r14d
movb   $0x0,(%rbx)
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
cmp    $0xbf,%bpl
movzbl %bpl,%eax
mov    $0x3f,%ecx
cmovne %eax,%ecx
movb   $0x5e,(%rbx)
mov    %cl,0x1(%rbx)
add    $0x2,%rbx
mov    $0x2,%eax
jmp    1167 <char_to_printable+0x57>

LLVM IR (-O0):
define i32 @char_to_printable(i8 zeroext %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8
%5 = alloca i8*
%6 = alloca i32
store i8 %0, i8* %4
store i8* %1, i8** %5
%7 = load i8, i8* %4
%8 = call i64 @isprint(i8 zeroext %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i8, i8* %4
%12 = load i8*, i8** %5
%13 = getelementptr inbounds i8, i8* %12, i32 1
store i8* %13, i8** %5
store i8 %11, i8* %12
store i32 1, i32* %6
br label %47
14: ; preds = %2
%15 = load i8, i8* %4
%16 = zext i8 %15 to i32
%17 = icmp slt i32 %16, 128
br i1 %17, label %18, label %34
18: ; preds = %14
%19 = load i8*, i8** %5
%20 = getelementptr inbounds i8, i8* %19, i32 1
store i8* %20, i8** %5
store i8 94, i8* %19
%21 = load i8, i8* %4
%22 = zext i8 %21 to i32
%23 = icmp eq i32 127, %22
br i1 %23, label %24, label %25
24: ; preds = %18
br label %29
25: ; preds = %18
%26 = load i8, i8* %4
%27 = zext i8 %26 to i32
%28 = add nsw i32 64, %27
br label %29
29: ; preds = %25, %24
%30 = phi i32 [ 63, %24 ], [ %28, %25 ]
%31 = trunc i32 %30 to i8
%32 = load i8*, i8** %5
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %5
store i8 %31, i8* %32
store i32 2, i32* %6
br label %46
34: ; preds = %14
%35 = load i8*, i8** %5
%36 = getelementptr inbounds i8, i8* %35, i32 1
store i8* %36, i8** %5
store i8 77, i8* %35
%37 = load i8*, i8** %5
%38 = getelementptr inbounds i8, i8* %37, i32 1
store i8* %38, i8** %5
store i8 45, i8* %37
%39 = load i8, i8* %4
%40 = zext i8 %39 to i32
%41 = sub nsw i32 %40, 128
%42 = trunc i32 %41 to i8
%43 = load i8*, i8** %5
%44 = call i32 @char_to_printable(i8 zeroext %42, i8* %43)
%45 = add i32 2, %44
store i32 %45, i32* %3
br label %50
46: ; preds = %29
br label %47
47: ; preds = %46, %10
%48 = load i8*, i8** %5
store i8 0, i8* %48
%49 = load i32, i32* %6
store i32 %49, i32* %3
br label %50
50: ; preds = %47, %34
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
define i32 @char_to_printable(i8 zeroext %0, i8* nocapture writeonly %1) {
%3 = tail call i64 @isprint(i8 zeroext %0)
%4 = icmp eq i64 %3, 0
br i1 %4, label %10, label %5
5: ; preds = %21, %2
%6 = phi i32 [ 0, %2 ], [ %24, %21 ]
%7 = phi i8 [ %0, %2 ], [ %23, %21 ]
%8 = phi i8* [ %1, %2 ], [ %22, %21 ]
%9 = getelementptr inbounds i8, i8* %8, i64 1
store i8 %7, i8* %8
br label %27
10: ; preds = %2, %21
%11 = phi i8* [ %22, %21 ], [ %1, %2 ]
%12 = phi i8 [ %23, %21 ], [ %0, %2 ]
%13 = phi i32 [ %24, %21 ], [ 0, %2 ]
%14 = icmp sgt i8 %12, -1
%15 = getelementptr inbounds i8, i8* %11, i64 1
br i1 %14, label %16, label %21
16: ; preds = %10
store i8 94, i8* %11
%17 = icmp eq i8 %12, 127
%18 = add nuw i8 %12, 64
%19 = select i1 %17, i8 63, i8 %18
%20 = getelementptr inbounds i8, i8* %11, i64 2
store i8 %19, i8* %15
br label %27
21: ; preds = %10
store i8 77, i8* %11
%22 = getelementptr inbounds i8, i8* %11, i64 2
store i8 45, i8* %15
%23 = xor i8 %12, -128
%24 = add i32 %13, 2
%25 = tail call i64 @isprint(i8 zeroext %23)
%26 = icmp eq i64 %25, 0
br i1 %26, label %10, label %5
27: ; preds = %16, %5
%28 = phi i32 [ %6, %5 ], [ %13, %16 ]
%29 = phi i8* [ %9, %5 ], [ %20, %16 ]
%30 = phi i32 [ 1, %5 ], [ 2, %16 ]
store i8 0, i8* %29
%31 = add i32 %30, %28
ret i32 %31
}