#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 *malloc(scalar_t__);
scalar_t__ strlen(char const *);
char *strpbrk(char const *, char *);
char *strdup_escape(const char *str) {
  const char *input = str;
  char *p, *ret;
  int escapes;
  if (!strlen(str))
    return NULL;
  escapes = 0;
  while ((input = strpbrk(input, "\\\"")) != NULL) {
    escapes++;
    input++;
  }
  p = ret = malloc(strlen(str) + escapes + 1);
  while (*str) {
    if (*str == '\\' || *str == '\"')
      *p++ = '\\';
    *p++ = *str++;
  }
  *p = '\0';
  return ret;
}


x86 assembly (clang, -O0):
<strdup_escape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%rax
jne    1164 <strdup_escape+0x34>
movq   $0x0,-0x8(%rbp)
jmp    123c <strdup_escape+0x10c>
movl   $0x0,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe8a(%rip),%rsi
call   1050 <strpbrk@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11a3 <strdup_escape+0x73>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    116b <strdup_escape+0x3b>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rdi
movslq -0x2c(%rbp),%rax
add    %rax,%rdi
add    $0x1,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     122d <strdup_escape+0xfd>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5c,%eax
je     11f4 <strdup_escape+0xc4>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x22,%eax
jne    1206 <strdup_escape+0xd6>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x5c,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
jmp    11c7 <strdup_escape+0x97>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strdup_escape>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    (%rdi),%bl
test   %bl,%bl
je     11df <strdup_escape+0xaf>
mov    %rdi,%r12
lea    0xeb4(%rip),%rsi
call   1050 <strpbrk@plt>
mov    $0x1,%r14d
test   %rax,%rax
je     1188 <strdup_escape+0x58>
lea    0xe9d(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%rax
mov    %rax,%rdi
mov    %r15,%rsi
call   1050 <strpbrk@plt>
add    $0x1,%r14
test   %rax,%rax
jne    1170 <strdup_escape+0x40>
mov    %r12,%rdi
call   1030 <strlen@plt>
add    %rax,%r14
mov    %r14,%rdi
call   1040 <malloc@plt>
add    $0x1,%r12
mov    %rax,%rcx
jmp    11cc <strdup_escape+0x9c>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movb   $0x5c,(%rcx)
add    $0x1,%rcx
movzbl -0x1(%r12),%ebx
mov    %bl,(%rcx)
add    $0x1,%rcx
movzbl (%r12),%ebx
add    $0x1,%r12
cmp    $0x22,%bl
je     11b0 <strdup_escape+0x80>
cmp    $0x5c,%bl
je     11b0 <strdup_escape+0x80>
test   %bl,%bl
jne    11bd <strdup_escape+0x8d>
movb   $0x0,(%rcx)
jmp    11e1 <strdup_escape+0xb1>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"\\\22\00"
define noalias i8* @strdup_escape(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 0
br i1 %3, label %36, label %4
4: ; preds = %1
%5 = tail call i8* @strpbrk(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i8* %5, null
br i1 %6, label %17, label %7
7: ; preds = %4, %7
%8 = phi i8* [ %12, %7 ], [ %5, %4 ]
%9 = phi i32 [ %10, %7 ], [ 0, %4 ]
%10 = add nuw nsw i32 %9, 1
%11 = getelementptr inbounds i8, i8* %8, i64 1
%12 = tail call i8* @strpbrk(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %7
14: ; preds = %7
%15 = add nuw i32 %9, 2
%16 = zext i32 %15 to i64
br label %17
17: ; preds = %14, %4
%18 = phi i64 [ 1, %4 ], [ %16, %14 ]
%19 = tail call i64 @strlen(i8* %0)
%20 = add i64 %18, %19
%21 = tail call i8* @malloc(i64 %20)
br label %22
22: ; preds = %29, %17
%23 = phi i8 [ %2, %17 ], [ %34, %29 ]
%24 = phi i8* [ %0, %17 ], [ %32, %29 ]
%25 = phi i8* [ %21, %17 ], [ %33, %29 ]
switch i8 %23, label %29 [
i8 0, label %35
i8 92, label %26
i8 34, label %26
]
26: ; preds = %22, %22
%27 = getelementptr inbounds i8, i8* %25, i64 1
store i8 92, i8* %25
%28 = load i8, i8* %24
br label %29
29: ; preds = %22, %26
%30 = phi i8 [ %28, %26 ], [ %23, %22 ]
%31 = phi i8* [ %27, %26 ], [ %25, %22 ]
%32 = getelementptr inbounds i8, i8* %24, i64 1
%33 = getelementptr inbounds i8, i8* %31, i64 1
store i8 %30, i8* %31
%34 = load i8, i8* %32
br label %22
35: ; preds = %22
store i8 0, i8* %25
br label %36
36: ; preds = %1, %35
%37 = phi i8* [ %21, %35 ], [ null, %1 ]
ret i8* %37
}