#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 *);
char *malloc(int);
char *mempcpy(char *, char const *, int);
char *stpcpy(char *, char *);
int strlen(char const *);
int utf8_encoded_valid_unichar(char const *);
char *utf8_escape_invalid(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) {
      s = mempcpy(s, str, len);
      str += len;
    } else {
      s = stpcpy(s, "\xef\xbf\xbd");
      str += 1;
    }
  }
  *s = '\0';
  return p;
}


x86 assembly (clang, -O0):
<utf8_escape_invalid>:
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    11ab <utf8_escape_invalid+0x4b>
movq   $0x0,-0x8(%rbp)
jmp    1231 <utf8_escape_invalid+0xd1>
jmp    11b0 <utf8_escape_invalid+0x50>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1222 <utf8_escape_invalid+0xc2>
mov    -0x10(%rbp),%rdi
call   1080 <utf8_encoded_valid_unichar@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    11fd <utf8_escape_invalid+0x9d>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1070 <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    121d <utf8_escape_invalid+0xbd>
mov    -0x20(%rbp),%rdi
lea    0xdf8(%rip),%rsi
call   1030 <stpcpy@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11b0 <utf8_escape_invalid+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_invalid>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1040 <assert@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    0x1(,%rax,4),%edi
call   1050 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     11d0 <utf8_escape_invalid+0x80>
cmpb   $0x0,(%rbx)
mov    %r14,%r15
je     11cc <utf8_escape_invalid+0x7c>
mov    %r14,%r15
jmp    11aa <utf8_escape_invalid+0x5a>
nopl   0x0(%rax,%rax,1)
movl   $0xbdbfef,(%r15)
add    $0x3,%r15
mov    $0x1,%eax
cmpb   $0x0,(%rbx,%rax,1)
lea    (%rbx,%rax,1),%rbx
je     11cc <utf8_escape_invalid+0x7c>
mov    %rbx,%rdi
call   1070 <utf8_encoded_valid_unichar@plt>
test   %eax,%eax
jle    1190 <utf8_escape_invalid+0x40>
mov    %eax,%ebp
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1060 <mempcpy@plt>
mov    %rax,%r15
mov    %ebp,%eax
jmp    11a0 <utf8_escape_invalid+0x50>
movb   $0x0,(%r15)
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_invalid(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 %45
17: ; preds = %1
br label %18
18: ; preds = %41, %17
%19 = load i8*, i8** %3
%20 = load i8, i8* %19
%21 = icmp ne i8 %20, 0
br i1 %21, label %22, label %42
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 %36
27: ; preds = %22
%28 = load i8*, i8** %5
%29 = load i8*, i8** %3
%30 = load i32, i32* %6
%31 = call i8* @mempcpy(i8* %28, i8* %29, i32 %30)
store i8* %31, i8** %5
%32 = load i32, i32* %6
%33 = load i8*, i8** %3
%34 = sext i32 %32 to i64
%35 = getelementptr inbounds i8, i8* %33, i64 %34
store i8* %35, i8** %3
br label %41
36: ; preds = %22
%37 = load i8*, i8** %5
%38 = call i8* @stpcpy(i8* %37, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i8* %38, i8** %5
%39 = load i8*, i8** %3
%40 = getelementptr inbounds i8, i8* %39, i64 1
store i8* %40, i8** %3
br label %41
41: ; preds = %36, %27
br label %18
42: ; preds = %18
%43 = load i8*, i8** %5
store i8 0, i8* %43
%44 = load i8*, i8** %4
store i8* %44, i8** %2
br label %45
45: ; preds = %42, %16
%46 = load i8*, i8** %2
ret i8* %46
}

LLVM IR (-O2):
define i8* @utf8_escape_invalid(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 %30, label %8
8: ; preds = %1
%9 = load i8, i8* %0
%10 = icmp eq i8 %9, 0
br i1 %10, label %28, label %11
11: ; preds = %8, %22
%12 = phi i8* [ %24, %22 ], [ %6, %8 ]
%13 = phi i8* [ %25, %22 ], [ %0, %8 ]
%14 = tail call i32 @utf8_encoded_valid_unichar(i8* %13)
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = tail call i8* @mempcpy(i8* %12, i8* %13, i32 %14)
%18 = zext i32 %14 to i64
br label %22
19: ; preds = %11
%20 = getelementptr i8, i8* %12, i64 3
%21 = bitcast i8* %12 to i32*
store i32 12435439, i32* %21
br label %22
22: ; preds = %19, %16
%23 = phi i64 [ 1, %19 ], [ %18, %16 ]
%24 = phi i8* [ %20, %19 ], [ %17, %16 ]
%25 = getelementptr inbounds i8, i8* %13, i64 %23
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 0
br i1 %27, label %28, label %11
28: ; preds = %22, %8
%29 = phi i8* [ %6, %8 ], [ %24, %22 ]
store i8 0, i8* %29
br label %30
30: ; preds = %1, %28
ret i8* %6
}