#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*/ memmove(char *, char *, long);
char *strchr(char *, char);
long strnlen(char *, int);
int escape_data(char *buffer, int max_field_size) {
  long len = 0;
  char *rp = buffer;
  while ((rp = strchr(rp, '"'))) {
    len = strnlen(rp, max_field_size);
    memmove(rp + 1, rp, len + 1);
    rp += 2;
  }
  len = strnlen(buffer, max_field_size);
  memmove(buffer + 1, buffer, len);
  *buffer = '"';
  *(buffer + len + 1) = '"';
  return 0;
}


x86 assembly (clang, -O0):
<escape_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x22,%esi
call   1030 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11a5 <escape_data+0x75>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <strnlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
add    $0x1,%rdx
call   1050 <memmove@plt>
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
jmp    114f <escape_data+0x1f>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <strnlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
add    $0x1,%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memmove@plt>
mov    -0x8(%rbp),%rax
movb   $0x22,(%rax)
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x22,0x1(%rax,%rcx,1)
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape_data>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
mov    $0x22,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     1183 <escape_data+0x53>
mov    %rax,%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %ebp,%esi
call   1040 <strnlen@plt>
lea    0x1(%rbx),%rdi
lea    0x1(%rax),%rdx
mov    %rbx,%rsi
call   1050 <memmove@plt>
add    $0x2,%rbx
mov    %rbx,%rdi
mov    $0x22,%esi
call   1030 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1150 <escape_data+0x20>
mov    %r14,%rdi
mov    %ebp,%esi
call   1040 <strnlen@plt>
mov    %rax,%rbx
lea    0x1(%r14),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1050 <memmove@plt>
movb   $0x22,(%r14)
movb   $0x22,0x1(%r14,%rbx,1)
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @escape_data(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
store i64 0, i64* %5
%7 = load i8*, i8** %3
store i8* %7, i8** %6
br label %8
8: ; preds = %12, %2
%9 = load i8*, i8** %6
%10 = call i8* @strchr(i8* %9, i8 signext 34)
store i8* %10, i8** %6
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %24
12: ; preds = %8
%13 = load i8*, i8** %6
%14 = load i32, i32* %4
%15 = call i64 @strnlen(i8* %13, i32 %14)
store i64 %15, i64* %5
%16 = load i8*, i8** %6
%17 = getelementptr inbounds i8, i8* %16, i64 1
%18 = load i8*, i8** %6
%19 = load i64, i64* %5
%20 = add nsw i64 %19, 1
%21 = call i32 @memmove(i8* %17, i8* %18, i64 %20)
%22 = load i8*, i8** %6
%23 = getelementptr inbounds i8, i8* %22, i64 2
store i8* %23, i8** %6
br label %8
24: ; preds = %8
%25 = load i8*, i8** %3
%26 = load i32, i32* %4
%27 = call i64 @strnlen(i8* %25, i32 %26)
store i64 %27, i64* %5
%28 = load i8*, i8** %3
%29 = getelementptr inbounds i8, i8* %28, i64 1
%30 = load i8*, i8** %3
%31 = load i64, i64* %5
%32 = call i32 @memmove(i8* %29, i8* %30, i64 %31)
%33 = load i8*, i8** %3
store i8 34, i8* %33
%34 = load i8*, i8** %3
%35 = load i64, i64* %5
%36 = getelementptr inbounds i8, i8* %34, i64 %35
%37 = getelementptr inbounds i8, i8* %36, i64 1
store i8 34, i8* %37
ret i32 0
}

LLVM IR (-O2):
define i32 @escape_data(i8* %0, i32 %1) {
%3 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 34)
%4 = icmp eq i8* %3, null
br i1 %4, label %14, label %5
5: ; preds = %2, %5
%6 = phi i8* [ %12, %5 ], [ %3, %2 ]
%7 = tail call i64 @strnlen(i8* %6, i32 %1)
%8 = getelementptr inbounds i8, i8* %6, i64 1
%9 = add nsw i64 %7, 1
%10 = tail call i32 @memmove(i8* %8, i8* %6, i64 %9)
%11 = getelementptr inbounds i8, i8* %6, i64 2
%12 = tail call i8* @strchr(i8* dereferenceable(1) %11, i8 signext 34)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %5
14: ; preds = %5, %2
%15 = tail call i64 @strnlen(i8* %0, i32 %1)
%16 = getelementptr inbounds i8, i8* %0, i64 1
%17 = tail call i32 @memmove(i8* %16, i8* %0, i64 %15)
store i8 34, i8* %0
%18 = getelementptr inbounds i8, i8* %0, i64 %15
%19 = getelementptr inbounds i8, i8* %18, i64 1
store i8 34, i8* %19
ret i32 0
}