#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 countChars(char *, char);
int /*<<< orphan*/ escCopy(char *, char *, char, char);
char *needMem(int);
int strlen(char *);
char *makeQuotedString(char *in, char quoteChar) {
  int newSize = 2 + strlen(in) + countChars(in, quoteChar);
  char *out = needMem(newSize + 1);
  out[0] = quoteChar;
  escCopy(in, out + 1, quoteChar, '\\');
  out[newSize - 1] = quoteChar;
  return out;
}


x86 assembly (clang, -O0):
<makeQuotedString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x2,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
movsbl -0x9(%rbp),%esi
call   1060 <countChars@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
add    $0x1,%edi
call   1050 <needMem@plt>
mov    %rax,-0x18(%rbp)
mov    -0x9(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
mov    $0x5c,%ecx
movsbl -0x9(%rbp),%edx
call   1040 <escCopy@plt>
mov    -0x9(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<makeQuotedString>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
mov    %ebp,%esi
call   1060 <countChars@plt>
lea    (%rbx,%rax,1),%r15d
add    $0x2,%r15d
lea    (%rbx,%rax,1),%edi
add    $0x3,%edi
call   1050 <needMem@plt>
mov    %rax,%rbx
mov    %bpl,(%rax)
lea    0x1(%rax),%rsi
mov    %r14,%rdi
mov    %ebp,%edx
mov    $0x5c,%ecx
call   1040 <escCopy@plt>
movslq %r15d,%rax
mov    %bpl,-0x1(%rax,%rbx,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @makeQuotedString(i8* %0, i8 signext %1) {
%3 = alloca i8*
%4 = alloca i8
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
store i8 %1, i8* %4
%7 = load i8*, i8** %3
%8 = call i32 @strlen(i8* %7)
%9 = add nsw i32 2, %8
%10 = load i8*, i8** %3
%11 = load i8, i8* %4
%12 = call i32 @countChars(i8* %10, i8 signext %11)
%13 = add nsw i32 %9, %12
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = add nsw i32 %14, 1
%16 = call i8* @needMem(i32 %15)
store i8* %16, i8** %6
%17 = load i8, i8* %4
%18 = load i8*, i8** %6
%19 = getelementptr inbounds i8, i8* %18, i64 0
store i8 %17, i8* %19
%20 = load i8*, i8** %3
%21 = load i8*, i8** %6
%22 = getelementptr inbounds i8, i8* %21, i64 1
%23 = load i8, i8* %4
%24 = call i32 @escCopy(i8* %20, i8* %22, i8 signext %23, i8 signext 92)
%25 = load i8, i8* %4
%26 = load i8*, i8** %6
%27 = load i32, i32* %5
%28 = sub nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %26, i64 %29
store i8 %25, i8* %30
%31 = load i8*, i8** %6
ret i8* %31
}

LLVM IR (-O2):
define i8* @makeQuotedString(i8* %0, i8 signext %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = add nsw i32 %3, 2
%5 = tail call i32 @countChars(i8* %0, i8 signext %1)
%6 = add nsw i32 %4, %5
%7 = add nsw i32 %6, 1
%8 = tail call i8* @needMem(i32 %7)
store i8 %1, i8* %8
%9 = getelementptr inbounds i8, i8* %8, i64 1
%10 = tail call i32 @escCopy(i8* %0, i8* %9, i8 signext %1, i8 signext 92)
%11 = add nsw i32 %6, -1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %8, i64 %12
store i8 %1, i8* %13
ret i8* %8
}