#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*/ strcat(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
char *xmalloc(int);
char *xstrcpyprint(char *str) {
  char *cpy = xmalloc(strlen(str) * 2 + 1);
  int i;
  *cpy = '\0';
  for (i = 0; i < strlen(str); i++) {
    if (str[i] == '\n')
      strcat(cpy, "\\n");
    else if (str[i] == '\r')
      strcat(cpy, "\\r");
    else
      strncat(cpy, &str[i], 1);
  }
  return cpy;
}


x86 assembly (clang, -O0):
<xstrcpyprint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%edi
shl    $0x1,%edi
add    $0x1,%edi
call   1040 <xmalloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
cmp    %ecx,%eax
jge    1215 <xstrcpyprint+0xd5>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11ba <xstrcpyprint+0x7a>
mov    -0x10(%rbp),%rdi
lea    0xe50(%rip),%rsi
call   1060 <strcat@plt>
jmp    1202 <xstrcpyprint+0xc2>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
jne    11e4 <xstrcpyprint+0xa4>
mov    -0x10(%rbp),%rdi
lea    0xe29(%rip),%rsi
call   1060 <strcat@plt>
jmp    11fd <xstrcpyprint+0xbd>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1050 <strncat@plt>
jmp    1202 <xstrcpyprint+0xc2>
jmp    1207 <xstrcpyprint+0xc7>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1174 <xstrcpyprint+0x34>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xstrcpyprint>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r13
call   1030 <strlen@plt>
lea    (%rax,%rax,1),%edi
add    $0x1,%edi
call   1040 <xmalloc@plt>
mov    %rax,%r14
movb   $0x0,(%rax)
mov    %r13,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    11cd <xstrcpyprint+0x8d>
xor    %ebx,%ebx
lea    0xe8c(%rip),%r15
lea    0xe82(%rip),%r12
jmp    11a4 <xstrcpyprint+0x64>
lea    (%rbx,%r13,1),%rsi
mov    %r14,%rdi
mov    $0x1,%edx
call   1050 <strncat@plt>
add    $0x1,%rbx
mov    %r13,%rdi
call   1030 <strlen@plt>
cltq
cmp    %rax,%rbx
jge    11cd <xstrcpyprint+0x8d>
movzbl 0x0(%r13,%rbx,1),%eax
cmp    $0xd,%al
je     11c0 <xstrcpyprint+0x80>
cmp    $0xa,%al
jne    1180 <xstrcpyprint+0x40>
mov    %r14,%rdi
mov    %r12,%rsi
call   1060 <strcat@plt>
jmp    1191 <xstrcpyprint+0x51>
nop
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <strcat@plt>
jmp    1191 <xstrcpyprint+0x51>
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"\\n\00"
@.str.1 = [3 x i8] c"\\r\00"
define i8* @xstrcpyprint(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = shl nsw i32 %2, 1
%4 = or i32 %3, 1
%5 = tail call i8* @xmalloc(i32 %4)
store i8 0, i8* %5
%6 = tail call i32 @strlen(i8* %0)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %23
8: ; preds = %1, %18
%9 = phi i64 [ %19, %18 ], [ 0, %1 ]
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = load i8, i8* %10
switch i8 %11, label %16 [
i8 10, label %12
i8 13, label %14
]
12: ; preds = %8
%13 = tail call i32 @strcat(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %18
14: ; preds = %8
%15 = tail call i32 @strcat(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %18
16: ; preds = %8
%17 = tail call i32 @strncat(i8* %5, i8* %10, i32 1)
br label %18
18: ; preds = %12, %16, %14
%19 = add nuw nsw i64 %9, 1
%20 = tail call i32 @strlen(i8* %0)
%21 = sext i32 %20 to i64
%22 = icmp slt i64 %19, %21
br i1 %22, label %8, label %23
23: ; preds = %18, %1
ret i8* %5
}