#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 */
char *calloc(unsigned long, int);
char stlstr_escape_for(char);
unsigned long strlen(char *);
char *stlstr_copy_and_escape(char *str) {
  unsigned long len = strlen(str);
  unsigned long i, o;
  char *cop = calloc(len + 1, sizeof(char));
  for (i = o = 0; i < len; ++i, ++o) {
    if (str[i] != '\\') {
      cop[o] = str[i];
      continue;
    }
    char e = stlstr_escape_for(str[i + 1]);
    if (e > 1) {
      i++;
      cop[o] = e;
    } else
      cop[o] = str[i];
  }
  return cop;
}


x86 assembly (clang, -O0):
<stlstr_copy_and_escape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1226 <stlstr_copy_and_escape+0xf6>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x5c,%eax
je     11ad <stlstr_copy_and_escape+0x7d>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1209 <stlstr_copy_and_escape+0xd9>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movsbl 0x1(%rax,%rcx,1),%edi
call   1050 <stlstr_escape_for@plt>
mov    %al,-0x29(%rbp)
movsbl -0x29(%rbp),%eax
cmp    $0x1,%eax
jle    11ee <stlstr_copy_and_escape+0xbe>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x29(%rbp),%dl
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1204 <stlstr_copy_and_escape+0xd4>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1209 <stlstr_copy_and_escape+0xd9>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    116f <stlstr_copy_and_escape+0x3f>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stlstr_copy_and_escape>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r12
call   1030 <strlen@plt>
mov    %rax,%r14
lea    0x1(%rax),%rdi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,%r15
test   %r14,%r14
je     11a4 <stlstr_copy_and_escape+0x74>
xor    %ebx,%ebx
mov    %r15,%r13
jmp    1186 <stlstr_copy_and_escape+0x56>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl (%r12,%rbx,1),%eax
mov    %al,0x0(%r13)
add    $0x1,%rbx
add    $0x1,%r13
cmp    %r14,%rbx
jae    11a4 <stlstr_copy_and_escape+0x74>
movzbl (%r12,%rbx,1),%eax
cmp    $0x5c,%al
jne    1175 <stlstr_copy_and_escape+0x45>
movsbl 0x1(%r12,%rbx,1),%edi
call   1050 <stlstr_escape_for@plt>
cmp    $0x1,%al
jle    1170 <stlstr_copy_and_escape+0x40>
add    $0x1,%rbx
jmp    1175 <stlstr_copy_and_escape+0x45>
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @stlstr_copy_and_escape(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = call i64 @strlen(i8* %8)
store i64 %9, i64* %3
%10 = load i64, i64* %3
%11 = add i64 %10, 1
%12 = call i8* @calloc(i64 %11, i32 1)
store i8* %12, i8** %6
store i64 0, i64* %5
store i64 0, i64* %4
br label %13
13: ; preds = %58, %1
%14 = load i64, i64* %4
%15 = load i64, i64* %3
%16 = icmp ult i64 %14, %15
br i1 %16, label %17, label %63
17: ; preds = %13
%18 = load i8*, i8** %2
%19 = load i64, i64* %4
%20 = getelementptr inbounds i8, i8* %18, i64 %19
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 92
br i1 %23, label %24, label %32
24: ; preds = %17
%25 = load i8*, i8** %2
%26 = load i64, i64* %4
%27 = getelementptr inbounds i8, i8* %25, i64 %26
%28 = load i8, i8* %27
%29 = load i8*, i8** %6
%30 = load i64, i64* %5
%31 = getelementptr inbounds i8, i8* %29, i64 %30
store i8 %28, i8* %31
br label %58
32: ; preds = %17
%33 = load i8*, i8** %2
%34 = load i64, i64* %4
%35 = add i64 %34, 1
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = load i8, i8* %36
%38 = call signext i8 @stlstr_escape_for(i8 signext %37)
store i8 %38, i8* %7
%39 = load i8, i8* %7
%40 = sext i8 %39 to i32
%41 = icmp sgt i32 %40, 1
br i1 %41, label %42, label %49
42: ; preds = %32
%43 = load i64, i64* %4
%44 = add i64 %43, 1
store i64 %44, i64* %4
%45 = load i8, i8* %7
%46 = load i8*, i8** %6
%47 = load i64, i64* %5
%48 = getelementptr inbounds i8, i8* %46, i64 %47
store i8 %45, i8* %48
br label %57
49: ; preds = %32
%50 = load i8*, i8** %2
%51 = load i64, i64* %4
%52 = getelementptr inbounds i8, i8* %50, i64 %51
%53 = load i8, i8* %52
%54 = load i8*, i8** %6
%55 = load i64, i64* %5
%56 = getelementptr inbounds i8, i8* %54, i64 %55
store i8 %53, i8* %56
br label %57
57: ; preds = %49, %42
br label %58
58: ; preds = %57, %24
%59 = load i64, i64* %4
%60 = add i64 %59, 1
store i64 %60, i64* %4
%61 = load i64, i64* %5
%62 = add i64 %61, 1
store i64 %62, i64* %5
br label %13
63: ; preds = %13
%64 = load i8*, i8** %6
ret i8* %64
}

LLVM IR (-O2):
define i8* @stlstr_copy_and_escape(i8* nocapture %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = add i64 %2, 1
%4 = tail call i8* @calloc(i64 %3, i32 1)
%5 = icmp eq i64 %2, 0
br i1 %5, label %27, label %6
6: ; preds = %1, %20
%7 = phi i64 [ %24, %20 ], [ 0, %1 ]
%8 = phi i64 [ %25, %20 ], [ 0, %1 ]
%9 = getelementptr inbounds i8, i8* %0, i64 %7
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 92
br i1 %11, label %12, label %20
12: ; preds = %6
%13 = add nuw i64 %7, 1
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = load i8, i8* %14
%16 = tail call signext i8 @stlstr_escape_for(i8 signext %15)
%17 = icmp sgt i8 %16, 1
br i1 %17, label %20, label %18
18: ; preds = %12
%19 = load i8, i8* %9
br label %20
20: ; preds = %12, %6, %18
%21 = phi i8 [ %19, %18 ], [ %10, %6 ], [ %16, %12 ]
%22 = phi i64 [ %7, %18 ], [ %7, %6 ], [ %13, %12 ]
%23 = getelementptr inbounds i8, i8* %4, i64 %8
store i8 %21, i8* %23
%24 = add i64 %22, 1
%25 = add i64 %8, 1
%26 = icmp ult i64 %24, %2
br i1 %26, label %6, label %27
27: ; preds = %20, %1
ret i8* %4
}