#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
char *realloc(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char const *);
void quote(char **buf, const char *s) {
  char tmp[strlen(s) * 8 + 1];
  char *out = tmp;
  for (; *s != '\0'; s++) {
    unsigned char ch = (unsigned char)*s;
    if (ch == '\\' || ch == '\"') {
      *out++ = '\\';
      *out++ = ch;
    } else if (ch < ' ') {
      sprintf(out, "\\u%04x", ch);
      out = out + strlen(out);
    } else {
      *out++ = ch;
    }
  }
  *out = '\0';
  *buf = realloc(*buf, strlen(*buf) + strlen(tmp) + 1);
  if (*buf == NULL) {
    perror("realloc");
    exit(EXIT_FAILURE);
  }
  strcat(*buf, tmp);
}


x86 assembly (clang, -O0):
<quote>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    %ecx,%eax
lea    0x1(,%rax,8),%eax
mov    %eax,%ecx
mov    %rsp,%rax
mov    %rax,-0x18(%rbp)
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rax
sub    %rdx,%rax
mov    %rax,-0x38(%rbp)
mov    %rax,%rsp
mov    %rcx,-0x20(%rbp)
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1281 <quote+0x121>
mov    -0x10(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x29(%rbp)
movzbl -0x29(%rbp),%eax
cmp    $0x5c,%eax
je     11e0 <quote+0x80>
movzbl -0x29(%rbp),%eax
cmp    $0x22,%eax
jne    120b <quote+0xab>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x5c,(%rax)
mov    -0x29(%rbp),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
jmp    126b <quote+0x10b>
movzbl -0x29(%rbp),%eax
cmp    $0x20,%eax
jge    1252 <quote+0xf2>
mov    -0x28(%rbp),%rdi
lea    0xddd(%rip),%rsi
movzbl -0x29(%rbp),%edx
call   1070 <sprintf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    1266 <quote+0x106>
mov    -0x29(%rbp),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
jmp    126b <quote+0x10b>
jmp    1270 <quote+0x110>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11ad <quote+0x4d>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,-0x4c(%rbp)
call   1030 <strlen@plt>
mov    -0x4c(%rbp),%esi
mov    -0x48(%rbp),%rdi
add    %eax,%esi
add    $0x1,%esi
call   1040 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    12ee <quote+0x18e>
lea    0xd2c(%rip),%rdi
call   1050 <perror@plt>
mov    0x2cf9(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x38(%rbp),%rsi
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1060 <strcat@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<quote>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %rdi,%r14
mov    %rsi,%rdi
call   1030 <strlen@plt>
lea    0x1(,%rax,8),%eax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%r15
sub    %rax,%r15
mov    %r15,%rsp
lea    0xe65(%rip),%r13
mov    %r15,%rbx
jmp    11ae <quote+0x4e>
movb   $0x5c,(%rbx)
mov    %al,0x1(%rbx)
add    $0x2,%rbx
add    $0x1,%r12
movzbl (%r12),%eax
cmp    $0x22,%al
je     11a0 <quote+0x40>
cmp    $0x5c,%al
je     11a0 <quote+0x40>
test   %al,%al
je     11f0 <quote+0x90>
cmp    $0x1f,%al
ja     11e4 <quote+0x84>
movzbl %al,%edx
mov    %rbx,%rdi
mov    %r13,%rsi
call   1070 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
add    %rax,%rbx
add    $0x1,%r12
jmp    11ae <quote+0x4e>
mov    %al,(%rbx)
add    $0x1,%rbx
add    $0x1,%r12
jmp    11ae <quote+0x4e>
movb   $0x0,(%rbx)
mov    (%r14),%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%r12d
mov    %r15,%rdi
call   1030 <strlen@plt>
lea    (%r12,%rax,1),%esi
add    $0x1,%esi
mov    %rbx,%rdi
call   1040 <realloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     123a <quote+0xda>
mov    %rax,%rdi
mov    %r15,%rsi
call   1060 <strcat@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc6(%rip),%rdi
call   1050 <perror@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"\\u%04x\00"
@.str.1 = [8 x i8] c"realloc\00"
@EXIT_FAILURE = global i32 0
define void @quote(i8** %0, i8* %1) {
%3 = alloca i8**
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8
store i8** %0, i8*** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %4
%10 = call i32 @strlen(i8* %9)
%11 = mul nsw i32 %10, 8
%12 = add nsw i32 %11, 1
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %5
%15 = alloca i8, i64 %13
store i64 %13, i64* %6
store i8* %15, i8** %7
br label %16
16: ; preds = %56, %2
%17 = load i8*, i8** %4
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %59
21: ; preds = %16
%22 = load i8*, i8** %4
%23 = load i8, i8* %22
store i8 %23, i8* %8
%24 = load i8, i8* %8
%25 = zext i8 %24 to i32
%26 = icmp eq i32 %25, 92
br i1 %26, label %31, label %27
27: ; preds = %21
%28 = load i8, i8* %8
%29 = zext i8 %28 to i32
%30 = icmp eq i32 %29, 34
br i1 %30, label %31, label %37
31: ; preds = %27, %21
%32 = load i8*, i8** %7
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %7
store i8 92, i8* %32
%34 = load i8, i8* %8
%35 = load i8*, i8** %7
%36 = getelementptr inbounds i8, i8* %35, i32 1
store i8* %36, i8** %7
store i8 %34, i8* %35
br label %55
37: ; preds = %27
%38 = load i8, i8* %8
%39 = zext i8 %38 to i32
%40 = icmp slt i32 %39, 32
br i1 %40, label %41, label %50
41: ; preds = %37
%42 = load i8*, i8** %7
%43 = load i8, i8* %8
%44 = call i32 @sprintf(i8* %42, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 zeroext %43)
%45 = load i8*, i8** %7
%46 = load i8*, i8** %7
%47 = call i32 @strlen(i8* %46)
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %45, i64 %48
store i8* %49, i8** %7
br label %54
50: ; preds = %37
%51 = load i8, i8* %8
%52 = load i8*, i8** %7
%53 = getelementptr inbounds i8, i8* %52, i32 1
store i8* %53, i8** %7
store i8 %51, i8* %52
br label %54
54: ; preds = %50, %41
br label %55
55: ; preds = %54, %31
br label %56
56: ; preds = %55
%57 = load i8*, i8** %4
%58 = getelementptr inbounds i8, i8* %57, i32 1
store i8* %58, i8** %4
br label %16
59: ; preds = %16
%60 = load i8*, i8** %7
store i8 0, i8* %60
%61 = load i8**, i8*** %3
%62 = load i8*, i8** %61
%63 = load i8**, i8*** %3
%64 = load i8*, i8** %63
%65 = call i32 @strlen(i8* %64)
%66 = call i32 @strlen(i8* %15)
%67 = add nsw i32 %65, %66
%68 = add nsw i32 %67, 1
%69 = call i8* @realloc(i8* %62, i32 %68)
%70 = load i8**, i8*** %3
store i8* %69, i8** %70
%71 = load i8**, i8*** %3
%72 = load i8*, i8** %71
%73 = icmp eq i8* %72, null
br i1 %73, label %74, label %78
74: ; preds = %59
%75 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%76 = load i32, i32* @EXIT_FAILURE
%77 = call i32 @exit(i32 %76)
unreachable
78: ; preds = %59
%79 = load i8**, i8*** %3
%80 = load i8*, i8** %79
%81 = call i32 @strcat(i8* %80, i8* %15)
%82 = load i8*, i8** %5
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"\\u%04x\00"
@.str.1 = [8 x i8] c"realloc\00"
@EXIT_FAILURE = global i32 0
define void @quote(i8** nocapture %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = shl nsw i32 %3, 3
%5 = or i32 %4, 1
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
br label %8
8: ; preds = %24, %2
%9 = phi i8* [ %7, %2 ], [ %25, %24 ]
%10 = phi i8* [ %1, %2 ], [ %26, %24 ]
%11 = load i8, i8* %10
switch i8 %11, label %15 [
i8 0, label %27
i8 92, label %12
i8 34, label %12
]
12: ; preds = %8, %8
%13 = getelementptr inbounds i8, i8* %9, i64 1
store i8 92, i8* %9
%14 = getelementptr inbounds i8, i8* %9, i64 2
store i8 %11, i8* %13
br label %24
15: ; preds = %8
%16 = icmp ult i8 %11, 32
br i1 %16, label %17, label %22
17: ; preds = %15
%18 = call i32 @sprintf(i8* dereferenceable(1) %9, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 zeroext %11)
%19 = call i32 @strlen(i8* %9)
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %9, i64 %20
br label %24
22: ; preds = %15
%23 = getelementptr inbounds i8, i8* %9, i64 1
store i8 %11, i8* %9
br label %24
24: ; preds = %17, %22, %12
%25 = phi i8* [ %14, %12 ], [ %21, %17 ], [ %23, %22 ]
%26 = getelementptr inbounds i8, i8* %10, i64 1
br label %8
27: ; preds = %8
store i8 0, i8* %9
%28 = load i8*, i8** %0
%29 = call i32 @strlen(i8* %28)
%30 = call i32 @strlen(i8* %7)
%31 = add i32 %29, 1
%32 = add i32 %31, %30
%33 = call i8* @realloc(i8* %28, i32 %32)
store i8* %33, i8** %0
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %39
35: ; preds = %27
%36 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %27
%40 = call i32 @strcat(i8* %33, i8* %7)
ret void
}