#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(size_t);
int /*<<< orphan*/ strcat(char *, char *);
size_t strcspn(char const *, char *);
int strlen(char const *);
int /*<<< orphan*/ strncat(char *, char const *, int);
const char *sym_escape_string_value(const char *in) {
  const char *p;
  size_t reslen;
  char *res;
  size_t l;
  reslen = strlen(in) + strlen("\"\"") + 1;
  p = in;
  for (;;) {
    l = strcspn(p, "\"\\");
    p += l;
    if (p[0] == '\0')
      break;
    reslen++;
    p++;
  }
  res = malloc(reslen);
  res[0] = '\0';
  strcat(res, "\"");
  p = in;
  for (;;) {
    l = strcspn(p, "\"\\");
    strncat(res, p, l);
    p += l;
    if (p[0] == '\0')
      break;
    strcat(res, "\\");
    strncat(res, p++, 1);
  }
  strcat(res, "\"");
  return res;
}


x86 assembly (clang, -O0):
<sym_escape_string_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x2c(%rbp)
lea    0xe91(%rip),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cltq
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe6c(%rip),%rsi
call   1050 <strcspn@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11c1 <sym_escape_string_value+0x71>
jmp    11de <sym_escape_string_value+0x8e>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    118c <sym_escape_string_value+0x3c>
mov    -0x18(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
lea    0xe04(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xdee(%rip),%rsi
call   1050 <strcspn@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    %eax,%edx
call   1040 <strncat@plt>
mov    -0x28(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1252 <sym_escape_string_value+0x102>
jmp    1284 <sym_escape_string_value+0x134>
mov    -0x20(%rbp),%rdi
lea    0xda7(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    %rsi,%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    $0x1,%edx
call   1040 <strncat@plt>
jmp    120a <sym_escape_string_value+0xba>
mov    -0x20(%rbp),%rdi
lea    0xd72(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sym_escape_string_value>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %eax,%ebx
lea    0xe94(%rip),%rdi
call   1030 <strlen@plt>
add    %ebx,%eax
add    $0x1,%eax
movslq %eax,%r14
lea    0xe83(%rip),%rsi
mov    %r15,%rdi
call   1050 <strcspn@plt>
cmpb   $0x0,(%r15,%rax,1)
je     11bc <sym_escape_string_value+0x6c>
mov    %rax,%rbx
add    %r15,%rbx
lea    0xe67(%rip),%r12
nopl   0x0(%rax)
add    $0x1,%r14
lea    0x1(%rbx),%rdi
mov    %r12,%rsi
call   1050 <strcspn@plt>
cmpb   $0x0,0x1(%rbx,%rax,1)
lea    0x1(%rbx,%rax,1),%rbx
jne    11a0 <sym_escape_string_value+0x50>
mov    %r14,%rdi
call   1060 <malloc@plt>
mov    %rax,%r14
movb   $0x0,(%rax)
lea    0xe30(%rip),%rsi
mov    %rax,%rdi
call   1070 <strcat@plt>
lea    0xe23(%rip),%rsi
mov    %r15,%rdi
call   1050 <strcspn@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1040 <strncat@plt>
cmpb   $0x0,(%r15,%rbx,1)
je     1257 <sym_escape_string_value+0x107>
add    %rbx,%r15
lea    0xdfb(%rip),%r12
lea    0xdf3(%rip),%r13
mov    %r14,%rdi
mov    %r12,%rsi
call   1070 <strcat@plt>
lea    0x1(%r15),%rbx
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
call   1040 <strncat@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1050 <strcspn@plt>
mov    %rax,%rbp
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <strncat@plt>
cmpb   $0x0,0x1(%r15,%rbp,1)
lea    0x1(%r15,%rbp,1),%r15
jne    1210 <sym_escape_string_value+0xc0>
lea    0xda3(%rip),%rsi
mov    %r14,%rdi
call   1070 <strcat@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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