#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 const);
int /*<<< orphan*/ memcpy(char *, char const *, size_t const);
size_t strlen(char const *);
char *strdup_quoted(const char *str) {
  if (str == NULL)
    return NULL;
  const size_t old_length = strlen(str);
  const size_t new_length = old_length + 2;
  char *new_str = malloc(new_length + 1);
  if (new_str == NULL)
    return NULL;
  new_str[0] = '"';
  new_str[new_length - 1] = '"';
  new_str[new_length] = '\0';
  memcpy(new_str + 1, str, old_length);
  return new_str;
}


x86 assembly (clang, -O0):
<strdup_quoted>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1154 <strdup_quoted+0x24>
movq   $0x0,-0x8(%rbp)
jmp    11d6 <strdup_quoted+0xa6>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1196 <strdup_quoted+0x66>
movq   $0x0,-0x8(%rbp)
jmp    11d6 <strdup_quoted+0xa6>
mov    -0x28(%rbp),%rax
movb   $0x22,(%rax)
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x22,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strdup_quoted>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     1175 <strdup_quoted+0x45>
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %rax,%r15
lea    0x3(%rax),%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     1175 <strdup_quoted+0x45>
mov    %rax,%rbx
movb   $0x22,(%rax)
movw   $0x22,0x1(%r15,%rax,1)
mov    %rax,%rdi
add    $0x1,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
jmp    1177 <strdup_quoted+0x47>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @strdup_quoted(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %15, label %3
3: ; preds = %1
%4 = tail call i64 @strlen(i8* %0)
%5 = add i64 %4, 3
%6 = tail call i8* @malloc(i64 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %15, label %8
8: ; preds = %3
%9 = add i64 %4, 2
store i8 34, i8* %6
%10 = add i64 %4, 1
%11 = getelementptr inbounds i8, i8* %6, i64 %10
store i8 34, i8* %11
%12 = getelementptr inbounds i8, i8* %6, i64 %9
store i8 0, i8* %12
%13 = getelementptr inbounds i8, i8* %6, i64 1
%14 = tail call i32 @memcpy(i8* %13, i8* %0, i64 %4)
br label %15
15: ; preds = %8, %3, %1
%16 = phi i8* [ null, %1 ], [ %6, %8 ], [ null, %3 ]
ret i8* %16
}