#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*/ fail_and_notify();
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);
  if (!ret)
    fail_and_notify();
  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   1040 <strlen@plt>
cmp    $0x0,%rax
jne    1174 <strdup_escape+0x34>
movq   $0x0,-0x8(%rbp)
jmp    1263 <strdup_escape+0x123>
movl   $0x0,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe7a(%rip),%rsi
call   1060 <strpbrk@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11b3 <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    117b <strdup_escape+0x3b>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
movslq -0x2c(%rbp),%rax
add    %rax,%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e9 <strdup_escape+0xa9>
mov    $0x0,%al
call   1030 <fail_and_notify@plt>
jmp    11ee <strdup_escape+0xae>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1254 <strdup_escape+0x114>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5c,%eax
je     121b <strdup_escape+0xdb>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x22,%eax
jne    122d <strdup_escape+0xed>
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    11ee <strdup_escape+0xae>
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   %rbx
cmpb   $0x0,(%rdi)
je     11da <strdup_escape+0x9a>
mov    %rdi,%rbx
lea    0xea8(%rip),%rsi
call   1060 <strpbrk@plt>
mov    $0x1,%r14d
test   %rax,%rax
je     1188 <strdup_escape+0x48>
lea    0xe91(%rip),%r15
nop
add    $0x1,%rax
mov    %rax,%rdi
mov    %r15,%rsi
call   1060 <strpbrk@plt>
add    $0x1,%r14
test   %rax,%rax
jne    1170 <strdup_escape+0x30>
mov    %rbx,%rdi
call   1040 <strlen@plt>
add    %rax,%r14
mov    %r14,%rdi
call   1050 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11aa <strdup_escape+0x6a>
xor    %eax,%eax
call   1030 <fail_and_notify@plt>
mov    %r14,%rax
jmp    11c4 <strdup_escape+0x84>
nop
movb   $0x5c,(%rax)
add    $0x1,%rax
movzbl (%rbx),%ecx
add    $0x1,%rbx
mov    %cl,(%rax)
add    $0x1,%rax
movzbl (%rbx),%ecx
cmp    $0x22,%cl
je     11b0 <strdup_escape+0x70>
cmp    $0x5c,%cl
je     11b0 <strdup_escape+0x70>
test   %cl,%cl
jne    11ba <strdup_escape+0x7a>
movb   $0x0,(%rax)
jmp    11dd <strdup_escape+0x9d>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
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 %62
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
%31 = load i8*, i8** %6
%32 = icmp ne i8* %31, null
br i1 %32, label %35, label %33
33: ; preds = %23
%34 = call i32 (...) @fail_and_notify()
br label %35
35: ; preds = %33, %23
br label %36
36: ; preds = %53, %35
%37 = load i8*, i8** %3
%38 = load i8, i8* %37
%39 = icmp ne i8 %38, 0
br i1 %39, label %40, label %59
40: ; preds = %36
%41 = load i8*, i8** %3
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %43, 92
br i1 %44, label %50, label %45
45: ; preds = %40
%46 = load i8*, i8** %3
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp eq i32 %48, 34
br i1 %49, label %50, label %53
50: ; preds = %45, %40
%51 = load i8*, i8** %5
%52 = getelementptr inbounds i8, i8* %51, i32 1
store i8* %52, i8** %5
store i8 92, i8* %51
br label %53
53: ; preds = %50, %45
%54 = load i8*, i8** %3
%55 = getelementptr inbounds i8, i8* %54, i32 1
store i8* %55, i8** %3
%56 = load i8, i8* %54
%57 = load i8*, i8** %5
%58 = getelementptr inbounds i8, i8* %57, i32 1
store i8* %58, i8** %5
store i8 %56, i8* %57
br label %36
59: ; preds = %36
%60 = load i8*, i8** %5
store i8 0, i8* %60
%61 = load i8*, i8** %6
store i8* %61, i8** %2
br label %62
62: ; preds = %59, %12
%63 = load i8*, i8** %2
ret i8* %63
}

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 %39, 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)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = tail call i32 (...) @fail_and_notify()
br label %25
25: ; preds = %23, %17
br label %26
26: ; preds = %25, %33
%27 = phi i8* [ %36, %33 ], [ %0, %25 ]
%28 = phi i8* [ %37, %33 ], [ %21, %25 ]
%29 = load i8, i8* %27
switch i8 %29, label %33 [
i8 0, label %38
i8 92, label %30
i8 34, label %30
]
30: ; preds = %26, %26
%31 = getelementptr inbounds i8, i8* %28, i64 1
store i8 92, i8* %28
%32 = load i8, i8* %27
br label %33
33: ; preds = %26, %30
%34 = phi i8 [ %32, %30 ], [ %29, %26 ]
%35 = phi i8* [ %31, %30 ], [ %28, %26 ]
%36 = getelementptr inbounds i8, i8* %27, i64 1
%37 = getelementptr inbounds i8, i8* %35, i64 1
store i8 %34, i8* %35
br label %26
38: ; preds = %26
store i8 0, i8* %28
br label %39
39: ; preds = %1, %38
%40 = phi i8* [ %21, %38 ], [ null, %1 ]
ret i8* %40
}