#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(char const *);
void *hexchar(int);
char *malloc(int);
char *mempcpy(char *, char const *, int);
char *stpcpy(char *, char *);
int strlen(char const *);
int utf8_encoded_valid_unichar(char const *);
scalar_t__ utf8_is_printable_newline(char const *, int, int);
char *utf8_escape_non_printable(const char *str) {
  char *p, *s;
  assert(str);
  p = s = malloc(strlen(str) * 4 + 1);
  if (!p)
    return NULL;
  while (*str) {
    int len;
    len = utf8_encoded_valid_unichar(str);
    if (len > 0) {
      if (utf8_is_printable_newline(str, len, true)) {
        s = mempcpy(s, str, len);
        str += len;
      } else {
        while (len > 0) {
          *(s++) = '\\';
          *(s++) = 'x';
          *(s++) = hexchar((int)*str >> 4);
          *(s++) = hexchar((int)*str);
          str += 1;
          len--;
        }
      }
    } else {
      s = stpcpy(s, "\xef\xbf\xbd");
      str += 1;
    }
  }
  *s = '\0';
  return p;
}


x86 assembly (clang, -O0):
<utf8_escape_non_printable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
shl    $0x2,%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11cb <utf8_escape_non_printable+0x4b>
movq   $0x0,-0x8(%rbp)
jmp    1304 <utf8_escape_non_printable+0x184>
jmp    11d0 <utf8_escape_non_printable+0x50>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     12f5 <utf8_escape_non_printable+0x175>
mov    -0x10(%rbp),%rdi
call   10a0 <utf8_encoded_valid_unichar@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    12d0 <utf8_escape_non_printable+0x150>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    $0x1,%edx
call   1090 <utf8_is_printable_newline@plt>
cmp    $0x0,%rax
je     1238 <utf8_escape_non_printable+0xb8>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1080 <mempcpy@plt>
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    12cb <utf8_escape_non_printable+0x14b>
jmp    123d <utf8_escape_non_printable+0xbd>
cmpl   $0x0,-0x24(%rbp)
jle    12c6 <utf8_escape_non_printable+0x146>
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
movsbl (%rax),%edi
sar    $0x4,%edi
call   1070 <hexchar@plt>
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1070 <hexchar@plt>
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    123d <utf8_escape_non_printable+0xbd>
jmp    12cb <utf8_escape_non_printable+0x14b>
jmp    12f0 <utf8_escape_non_printable+0x170>
mov    -0x20(%rbp),%rdi
lea    0xd25(%rip),%rsi
call   1030 <stpcpy@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11d0 <utf8_escape_non_printable+0x50>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8_escape_non_printable>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
call   1040 <assert@plt>
mov    %r15,%rdi
call   1030 <strlen@plt>
lea    0x1(,%rax,4),%edi
call   1050 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     126b <utf8_escape_non_printable+0xfb>
cmpb   $0x0,(%r15)
mov    %r14,%rbx
je     1268 <utf8_escape_non_printable+0xf8>
mov    %r14,%rbx
jmp    11d8 <utf8_escape_non_printable+0x68>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movl   $0xbdbfef,(%rbx)
add    $0x3,%rbx
add    $0x1,%r15
cmpb   $0x0,(%r15)
je     1268 <utf8_escape_non_printable+0xf8>
mov    %r15,%rdi
call   1090 <utf8_encoded_valid_unichar@plt>
test   %eax,%eax
jle    11c0 <utf8_escape_non_printable+0x50>
mov    %eax,%ebp
mov    %r15,%rdi
mov    %eax,%esi
mov    $0x1,%edx
call   1080 <utf8_is_printable_newline@plt>
test   %rax,%rax
je     1220 <utf8_escape_non_printable+0xb0>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1070 <mempcpy@plt>
mov    %rax,%rbx
mov    %ebp,%eax
add    %rax,%r15
jmp    11ce <utf8_escape_non_printable+0x5e>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movw   $0x785c,(%rbx)
movzbl (%r15),%eax
sar    $0x4,%al
movsbl %al,%edi
call   1060 <hexchar@plt>
mov    %al,0x2(%rbx)
movsbl (%r15),%edi
call   1060 <hexchar@plt>
mov    %al,0x3(%rbx)
add    $0x4,%rbx
add    $0x1,%r15
add    $0xffffffff,%ebp
cmp    $0x1,%ebp
ja     1230 <utf8_escape_non_printable+0xc0>
jmp    11ce <utf8_escape_non_printable+0x5e>
movb   $0x0,(%rbx)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"\EF\BF\BD\00"
define i8* @utf8_escape_non_printable(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32 @assert(i8* %7)
%9 = load i8*, i8** %3
%10 = call i32 @strlen(i8* %9)
%11 = mul nsw i32 %10, 4
%12 = add nsw i32 %11, 1
%13 = call i8* @malloc(i32 %12)
store i8* %13, i8** %5
store i8* %13, i8** %4
%14 = load i8*, i8** %4
%15 = icmp ne i8* %14, null
br i1 %15, label %17, label %16
16: ; preds = %1
store i8* null, i8** %2
br label %80
17: ; preds = %1
br label %18
18: ; preds = %76, %17
%19 = load i8*, i8** %3
%20 = load i8, i8* %19
%21 = icmp ne i8 %20, 0
br i1 %21, label %22, label %77
22: ; preds = %18
%23 = load i8*, i8** %3
%24 = call i32 @utf8_encoded_valid_unichar(i8* %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %71
27: ; preds = %22
%28 = load i8*, i8** %3
%29 = load i32, i32* %6
%30 = call i64 @utf8_is_printable_newline(i8* %28, i32 %29, i32 1)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %41
32: ; preds = %27
%33 = load i8*, i8** %5
%34 = load i8*, i8** %3
%35 = load i32, i32* %6
%36 = call i8* @mempcpy(i8* %33, i8* %34, i32 %35)
store i8* %36, i8** %5
%37 = load i32, i32* %6
%38 = load i8*, i8** %3
%39 = sext i32 %37 to i64
%40 = getelementptr inbounds i8, i8* %38, i64 %39
store i8* %40, i8** %3
br label %70
41: ; preds = %27
br label %42
42: ; preds = %45, %41
%43 = load i32, i32* %6
%44 = icmp sgt i32 %43, 0
br i1 %44, label %45, label %69
45: ; preds = %42
%46 = load i8*, i8** %5
%47 = getelementptr inbounds i8, i8* %46, i32 1
store i8* %47, i8** %5
store i8 92, i8* %46
%48 = load i8*, i8** %5
%49 = getelementptr inbounds i8, i8* %48, i32 1
store i8* %49, i8** %5
store i8 120, i8* %48
%50 = load i8*, i8** %3
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = ashr i32 %52, 4
%54 = call i8* @hexchar(i32 %53)
%55 = ptrtoint i8* %54 to i8
%56 = load i8*, i8** %5
%57 = getelementptr inbounds i8, i8* %56, i32 1
store i8* %57, i8** %5
store i8 %55, i8* %56
%58 = load i8*, i8** %3
%59 = load i8, i8* %58
%60 = sext i8 %59 to i32
%61 = call i8* @hexchar(i32 %60)
%62 = ptrtoint i8* %61 to i8
%63 = load i8*, i8** %5
%64 = getelementptr inbounds i8, i8* %63, i32 1
store i8* %64, i8** %5
store i8 %62, i8* %63
%65 = load i8*, i8** %3
%66 = getelementptr inbounds i8, i8* %65, i64 1
store i8* %66, i8** %3
%67 = load i32, i32* %6
%68 = add nsw i32 %67, -1
store i32 %68, i32* %6
br label %42
69: ; preds = %42
br label %70
70: ; preds = %69, %32
br label %76
71: ; preds = %22
%72 = load i8*, i8** %5
%73 = call i8* @stpcpy(i8* %72, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i8* %73, i8** %5
%74 = load i8*, i8** %3
%75 = getelementptr inbounds i8, i8* %74, i64 1
store i8* %75, i8** %3
br label %76
76: ; preds = %71, %70
br label %18
77: ; preds = %18
%78 = load i8*, i8** %5
store i8 0, i8* %78
%79 = load i8*, i8** %4
store i8* %79, i8** %2
br label %80
80: ; preds = %77, %16
%81 = load i8*, i8** %2
ret i8* %81
}

LLVM IR (-O2):
define i8* @utf8_escape_non_printable(i8* %0) {
%2 = tail call i32 @assert(i8* %0)
%3 = tail call i32 @strlen(i8* %0)
%4 = shl nsw i32 %3, 2
%5 = or i32 %4, 1
%6 = tail call i8* @malloc(i32 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %56, label %8
8: ; preds = %1
%9 = load i8, i8* %0
%10 = icmp eq i8 %9, 0
br i1 %10, label %54, label %11
11: ; preds = %8, %49
%12 = phi i8* [ %51, %49 ], [ %6, %8 ]
%13 = phi i8* [ %50, %49 ], [ %0, %8 ]
%14 = tail call i32 @utf8_encoded_valid_unichar(i8* %13)
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %45
16: ; preds = %11
%17 = tail call i64 @utf8_is_printable_newline(i8* %13, i32 %14, i32 1)
%18 = icmp eq i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %16
%20 = tail call i8* @mempcpy(i8* %12, i8* %13, i32 %14)
%21 = zext i32 %14 to i64
%22 = getelementptr inbounds i8, i8* %13, i64 %21
br label %49
23: ; preds = %16, %23
%24 = phi i32 [ %43, %23 ], [ %14, %16 ]
%25 = phi i8* [ %41, %23 ], [ %12, %16 ]
%26 = phi i8* [ %42, %23 ], [ %13, %16 ]
%27 = getelementptr inbounds i8, i8* %25, i64 1
store i8 92, i8* %25
%28 = getelementptr inbounds i8, i8* %25, i64 2
store i8 120, i8* %27
%29 = load i8, i8* %26
%30 = ashr i8 %29, 4
%31 = sext i8 %30 to i32
%32 = tail call i8* @hexchar(i32 %31)
%33 = ptrtoint i8* %32 to i64
%34 = trunc i64 %33 to i8
%35 = getelementptr inbounds i8, i8* %25, i64 3
store i8 %34, i8* %28
%36 = load i8, i8* %26
%37 = sext i8 %36 to i32
%38 = tail call i8* @hexchar(i32 %37)
%39 = ptrtoint i8* %38 to i64
%40 = trunc i64 %39 to i8
%41 = getelementptr inbounds i8, i8* %25, i64 4
store i8 %40, i8* %35
%42 = getelementptr inbounds i8, i8* %26, i64 1
%43 = add nsw i32 %24, -1
%44 = icmp ugt i32 %24, 1
br i1 %44, label %23, label %49
45: ; preds = %11
%46 = getelementptr i8, i8* %12, i64 3
%47 = bitcast i8* %12 to i32*
store i32 12435439, i32* %47
%48 = getelementptr inbounds i8, i8* %13, i64 1
br label %49
49: ; preds = %23, %19, %45
%50 = phi i8* [ %22, %19 ], [ %48, %45 ], [ %42, %23 ]
%51 = phi i8* [ %20, %19 ], [ %46, %45 ], [ %41, %23 ]
%52 = load i8, i8* %50
%53 = icmp eq i8 %52, 0
br i1 %53, label %54, label %11
54: ; preds = %49, %8
%55 = phi i8* [ %6, %8 ], [ %51, %49 ]
store i8 0, i8* %55
br label %56
56: ; preds = %1, %54
ret i8* %6
}