#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 strlen(char const *);
int /*<<< orphan*/ xfree(char *);
scalar_t__ xmalloc(int);
char *xstrdup(char *);
char *strenquote(const char *str, const char quot_char) {
  int len;
  const char *cin;
  char *cout;
  char *tmp;
  if (str == NULL)
    cin = "";
  else
    cin = (char *)str;
  len = strlen(cin);
  cout = tmp = (char *)xmalloc((len * 2) + 3);
  *cout++ = quot_char;
  while (*cin) {
    *cout++ = *cin;
    if (*cin++ == quot_char)
      *cout++ = quot_char;
  }
  *cout++ = quot_char;
  *cout = '\0';
  cout = xstrdup(tmp);
  xfree(tmp);
  return cout;
}


x86 assembly (clang, -O0):
<strenquote>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    116d <strenquote+0x2d>
lea    0xe9c(%rip),%rax
mov    %rax,-0x18(%rbp)
jmp    1175 <strenquote+0x35>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
shl    $0x1,%edi
add    $0x3,%edi
call   1050 <xmalloc@plt>
mov    %rax,-0x28(%rbp)
mov    %rax,-0x20(%rbp)
mov    -0x9(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     1206 <strenquote+0xc6>
mov    -0x18(%rbp),%rax
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movsbl (%rax),%eax
movsbl -0x9(%rbp),%ecx
cmp    %ecx,%eax
jne    1201 <strenquote+0xc1>
mov    -0x9(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
jmp    11ab <strenquote+0x6b>
mov    -0x9(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
call   1060 <xstrdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <xfree@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strenquote>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
test   %rdi,%rdi
lea    0xeb0(%rip),%rbx
cmovne %rdi,%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    (%rax,%rax,1),%edi
add    $0x3,%edi
call   1050 <xmalloc@plt>
mov    %rax,%r14
mov    %bpl,(%rax)
add    $0x1,%rax
mov    (%rbx),%cl
test   %cl,%cl
je     11a1 <strenquote+0x61>
add    $0x1,%rbx
jmp    1193 <strenquote+0x53>
nopl   (%rax)
mov    %bpl,0x1(%rax)
add    $0x2,%rax
movzbl (%rbx),%ecx
add    $0x1,%rbx
test   %cl,%cl
je     11a1 <strenquote+0x61>
mov    %cl,(%rax)
cmp    %bpl,-0x1(%rbx)
je     1180 <strenquote+0x40>
add    $0x1,%rax
jmp    1188 <strenquote+0x48>
mov    %bpl,(%rax)
movb   $0x0,0x1(%rax)
mov    %r14,%rdi
call   1060 <xstrdup@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <xfree@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define i8* @strenquote(i8* %0, i8 signext %1) {
%3 = icmp eq i8* %0, null
%4 = select i1 %3, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* %0
%5 = tail call i32 @strlen(i8* %4)
%6 = shl nsw i32 %5, 1
%7 = add nsw i32 %6, 3
%8 = tail call i64 @xmalloc(i32 %7)
%9 = inttoptr i64 %8 to i8*
%10 = getelementptr inbounds i8, i8* %9, i64 1
store i8 %1, i8* %9
%11 = load i8, i8* %4
%12 = icmp eq i8 %11, 0
br i1 %12, label %27, label %13
13: ; preds = %2, %23
%14 = phi i8 [ %25, %23 ], [ %11, %2 ]
%15 = phi i8* [ %18, %23 ], [ %4, %2 ]
%16 = phi i8* [ %24, %23 ], [ %10, %2 ]
%17 = getelementptr inbounds i8, i8* %16, i64 1
store i8 %14, i8* %16
%18 = getelementptr inbounds i8, i8* %15, i64 1
%19 = load i8, i8* %15
%20 = icmp eq i8 %19, %1
br i1 %20, label %21, label %23
21: ; preds = %13
%22 = getelementptr inbounds i8, i8* %16, i64 2
store i8 %1, i8* %17
br label %23
23: ; preds = %21, %13
%24 = phi i8* [ %22, %21 ], [ %17, %13 ]
%25 = load i8, i8* %18
%26 = icmp eq i8 %25, 0
br i1 %26, label %27, label %13
27: ; preds = %23, %2
%28 = phi i8* [ %10, %2 ], [ %24, %23 ]
%29 = getelementptr inbounds i8, i8* %28, i64 1
store i8 %1, i8* %28
store i8 0, i8* %29
%30 = tail call i8* @xstrdup(i8* %9)
%31 = tail call i32 @xfree(i8* %9)
ret i8* %30
}