#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 countchar(char *, char);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
char *strdup(char *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
char *strstr(char *, char *);
char *shquote(char *string) {
  if (strlen(string) == 0)
    return strdup("''");
  if (strstr(string, "'") == NULL) {
    char *dupstring = malloc(strlen(string) + 3);
    sprintf(dupstring, "'%s'", string);
    return dupstring;
  }
  int new_len = strlen(string) + 5 * countchar(string, '\'');
  char *new_string = malloc(new_len + 1);
  memset(new_string, 0, new_len + 1);
  char *current = string;
  char *prev = current;
  current = strstr(current, "'");
  while (current) {
    strncat(new_string, prev, current - prev);
    strncat(new_string, "'\"'\"'", 5);
    prev = current + 1;
    current = strstr(current + 1, "'");
  }
  strcat(new_string, prev);
  return new_string;
}


x86 assembly (clang, -O0):
<shquote>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%eax
jne    11c3 <shquote+0x33>
lea    0xe4b(%rip),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    12f8 <shquote+0x168>
mov    -0x10(%rbp),%rdi
lea    0xe3e(%rip),%rsi
call   10b0 <strstr@plt>
cmp    $0x0,%rax
jne    1215 <shquote+0x85>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
add    $0x3,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe00(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12f8 <shquote+0x168>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x27,%esi
call   1030 <countchar@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
imul   $0x5,%ecx,%ecx
add    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xd95(%rip),%rsi
call   10b0 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12e3 <shquote+0x153>
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1060 <strncat@plt>
mov    -0x28(%rbp),%rdi
lea    0xd58(%rip),%rsi
mov    $0x5,%edx
call   1060 <strncat@plt>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
lea    0xd37(%rip),%rsi
call   10b0 <strstr@plt>
mov    %rax,-0x30(%rbp)
jmp    1280 <shquote+0xf0>
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shquote>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
call   1040 <strlen@plt>
test   %eax,%eax
je     1269 <shquote+0xd9>
mov    %r12,%rdi
mov    $0x27,%esi
call   1050 <strchr@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %eax,%r14d
test   %rbx,%rbx
je     1280 <shquote+0xf0>
mov    %r12,%rdi
mov    $0x27,%esi
call   1030 <countchar@plt>
lea    (%rax,%rax,4),%eax
lea    (%r14,%rax,1),%ebx
add    $0x1,%ebx
mov    %ebx,%edi
call   1080 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1060 <memset@plt>
mov    %r12,%rdi
mov    $0x27,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     125c <shquote+0xcc>
mov    %rax,%rbx
lea    0xdf3(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edx
sub    %r12d,%edx
mov    %r14,%rdi
mov    %r12,%rsi
call   1070 <strncat@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x5,%edx
call   1070 <strncat@plt>
mov    %rbx,%r12
add    $0x1,%r12
mov    %r12,%rdi
mov    $0x27,%esi
call   1050 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1220 <shquote+0x90>
mov    %r14,%rdi
mov    %r12,%rsi
call   1090 <strcat@plt>
jmp    12a1 <shquote+0x111>
lea    0xd90(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    10b0 <strdup@plt>
add    $0x3,%r14d
mov    %r14d,%edi
call   1080 <malloc@plt>
mov    %rax,%r14
lea    0xd6d(%rip),%rsi
mov    %rax,%rdi
mov    %r12,%rdx
call   10a0 <sprintf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"''\00"
@.str.2 = [5 x i8] c"'%s'\00"
@.str.3 = [6 x i8] c"'\22'\22'\00"
define i8* @shquote(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp eq i32 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call dereferenceable_or_null(3) i8* @strdup(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %38
6: ; preds = %1
%7 = tail call i8* @strchr(i8* dereferenceable(1) %0, i32 39)
%8 = icmp eq i8* %7, null
%9 = tail call i32 @strlen(i8* %0)
br i1 %8, label %10, label %14
10: ; preds = %6
%11 = add nsw i32 %9, 3
%12 = tail call i8* @malloc(i32 %11)
%13 = tail call i32 @sprintf(i8* dereferenceable(1) %12, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %38
14: ; preds = %6
%15 = tail call i32 @countchar(i8* %0, i8 signext 39)
%16 = mul nsw i32 %15, 5
%17 = add i32 %9, 1
%18 = add i32 %17, %16
%19 = tail call i8* @malloc(i32 %18)
%20 = tail call i32 @memset(i8* %19, i32 0, i32 %18)
%21 = tail call i8* @strchr(i8* dereferenceable(1) %0, i32 39)
%22 = icmp eq i8* %21, null
br i1 %22, label %35, label %23
23: ; preds = %14, %23
%24 = phi i8* [ %32, %23 ], [ %0, %14 ]
%25 = phi i8* [ %33, %23 ], [ %21, %14 ]
%26 = ptrtoint i8* %25 to i64
%27 = ptrtoint i8* %24 to i64
%28 = sub i64 %26, %27
%29 = trunc i64 %28 to i32
%30 = tail call i32 @strncat(i8* %19, i8* %24, i32 %29)
%31 = tail call i32 @strncat(i8* %19, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 5)
%32 = getelementptr inbounds i8, i8* %25, i64 1
%33 = tail call i8* @strchr(i8* %32, i32 39)
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %23
35: ; preds = %23, %14
%36 = phi i8* [ %0, %14 ], [ %32, %23 ]
%37 = tail call i32 @strcat(i8* %19, i8* %36)
br label %38
38: ; preds = %35, %10, %4
%39 = phi i8* [ %5, %4 ], [ %12, %10 ], [ %19, %35 ]
ret i8* %39
}