#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*/ free(char *);
char *malloc(int);
int strlen(char const *);
void escape_backslashes(const char *instr, char **outstr) {
  int out_idx = 0;
  if (*outstr != NULL)
    free(*outstr);
  *outstr = malloc(2 * strlen(instr));
  while (*instr != '\0') {
    if (*instr == '\\') {
      *(*outstr + out_idx) = '\\';
      out_idx++;
    }
    *(*outstr + out_idx) = *instr;
    out_idx++;
    instr++;
  }
  *(*outstr + out_idx) = '\0';
}


x86 assembly (clang, -O0):
<escape_backslashes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     1161 <escape_backslashes+0x31>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
shl    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11e4 <escape_backslashes+0xb4>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5c,%eax
jne    11b6 <escape_backslashes+0x86>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x14(%rbp),%rcx
movb   $0x5c,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%dl
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    117e <escape_backslashes+0x4e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape_backslashes>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    (%rsi),%rdi
test   %rdi,%rdi
je     1147 <escape_backslashes+0x17>
call   1030 <free@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    (%rax,%rax,1),%edi
call   1050 <malloc@plt>
mov    %rax,(%r14)
xor    %eax,%eax
jmp    117e <escape_backslashes+0x4e>
xchg   %ax,%ax
mov    (%r14),%rcx
cltq
movb   $0x5c,(%rcx,%rax,1)
add    $0x1,%eax
movzbl (%rbx),%ecx
mov    (%r14),%rdx
cltq
mov    %cl,(%rdx,%rax,1)
add    $0x1,%eax
add    $0x1,%rbx
movzbl (%rbx),%ecx
cmp    $0x5c,%cl
je     1160 <escape_backslashes+0x30>
test   %cl,%cl
jne    116f <escape_backslashes+0x3f>
mov    (%r14),%rcx
cltq
movb   $0x0,(%rcx,%rax,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define void @escape_backslashes(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i32
store i8* %0, i8** %3
store i8** %1, i8*** %4
store i32 0, i32* %5
%6 = load i8**, i8*** %4
%7 = load i8*, i8** %6
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i8**, i8*** %4
%11 = load i8*, i8** %10
%12 = call i32 @free(i8* %11)
br label %13
13: ; preds = %9, %2
%14 = load i8*, i8** %3
%15 = call i32 @strlen(i8* %14)
%16 = mul nsw i32 2, %15
%17 = call i8* @malloc(i32 %16)
%18 = load i8**, i8*** %4
store i8* %17, i8** %18
br label %19
19: ; preds = %37, %13
%20 = load i8*, i8** %3
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %49
24: ; preds = %19
%25 = load i8*, i8** %3
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 92
br i1 %28, label %29, label %37
29: ; preds = %24
%30 = load i8**, i8*** %4
%31 = load i8*, i8** %30
%32 = load i32, i32* %5
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
store i8 92, i8* %34
%35 = load i32, i32* %5
%36 = add nsw i32 %35, 1
store i32 %36, i32* %5
br label %37
37: ; preds = %29, %24
%38 = load i8*, i8** %3
%39 = load i8, i8* %38
%40 = load i8**, i8*** %4
%41 = load i8*, i8** %40
%42 = load i32, i32* %5
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
store i8 %39, i8* %44
%45 = load i32, i32* %5
%46 = add nsw i32 %45, 1
store i32 %46, i32* %5
%47 = load i8*, i8** %3
%48 = getelementptr inbounds i8, i8* %47, i32 1
store i8* %48, i8** %3
br label %19
49: ; preds = %19
%50 = load i8**, i8*** %4
%51 = load i8*, i8** %50
%52 = load i32, i32* %5
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
store i8 0, i8* %54
ret void
}

LLVM IR (-O2):
define void @escape_backslashes(i8* %0, i8** nocapture %1) {
%3 = load i8*, i8** %1
%4 = icmp eq i8* %3, null
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @free(i8* %3)
br label %7
7: ; preds = %5, %2
%8 = tail call i32 @strlen(i8* %0)
%9 = shl nsw i32 %8, 1
%10 = tail call i8* @malloc(i32 %9)
store i8* %10, i8** %1
br label %11
11: ; preds = %21, %7
%12 = phi i8* [ %0, %7 ], [ %28, %21 ]
%13 = phi i32 [ 0, %7 ], [ %27, %21 ]
%14 = load i8, i8* %12
switch i8 %14, label %21 [
i8 0, label %29
i8 92, label %15
]
15: ; preds = %11
%16 = load i8*, i8** %1
%17 = sext i32 %13 to i64
%18 = getelementptr inbounds i8, i8* %16, i64 %17
store i8 92, i8* %18
%19 = add nsw i32 %13, 1
%20 = load i8, i8* %12
br label %21
21: ; preds = %11, %15
%22 = phi i8 [ %20, %15 ], [ %14, %11 ]
%23 = phi i32 [ %19, %15 ], [ %13, %11 ]
%24 = load i8*, i8** %1
%25 = sext i32 %23 to i64
%26 = getelementptr inbounds i8, i8* %24, i64 %25
store i8 %22, i8* %26
%27 = add nsw i32 %23, 1
%28 = getelementptr inbounds i8, i8* %12, i64 1
br label %11
29: ; preds = %11
%30 = load i8*, i8** %1
%31 = sext i32 %13 to i64
%32 = getelementptr inbounds i8, i8* %30, i64 %31
store i8 0, i8* %32
ret void
}