#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/, char *);
int /*<<< orphan*/ strcpy(char *, char *);
void print(char *str) {
  char buf[4096];
  char *b = buf;
  static int count = 1;
  memset(buf, 0, sizeof(buf));
  while (*str) {
    if (*str < 20) {
      strcpy(b, "~color");
      b += 6;
      *b++ = *str++ + '0';
      *b++ = '~';
    } else if (*str == '"') {
      *b++ = '\\';
      *b++ = *str++;
    } else {
      *b++ = *str++;
    }
  }
  printf("%d %s\n", count++, buf);
}


x86 assembly (clang, -O0):
<print>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x1010(%rbp),%rax
mov    %rax,-0x1018(%rbp)
lea    -0x1010(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     1274 <print+0x144>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x14,%eax
jge    11ed <print+0xbd>
mov    -0x1018(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x1018(%rbp),%rax
add    $0x6,%rax
mov    %rax,-0x1018(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movsbl (%rax),%eax
add    $0x30,%eax
mov    %al,%cl
mov    -0x1018(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x1018(%rbp)
mov    %cl,(%rax)
mov    -0x1018(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x1018(%rbp)
movb   $0x7e,(%rax)
jmp    126f <print+0x13f>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x22,%eax
jne    1242 <print+0x112>
mov    -0x1018(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x1018(%rbp)
movb   $0x5c,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%cl
mov    -0x1018(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x1018(%rbp)
mov    %cl,(%rax)
jmp    126a <print+0x13a>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%cl
mov    -0x1018(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x1018(%rbp)
mov    %cl,(%rax)
jmp    126f <print+0x13f>
jmp    1160 <print+0x30>
mov    0x2dbe(%rip),%esi
mov    %esi,%eax
add    $0x1,%eax
mov    %eax,0x2db3(%rip)
lea    -0x1010(%rbp),%rdx
lea    0xd74(%rip),%rdi
call   1040 <printf@plt>
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print>:
push   %r15
push   %r14
push   %rbx
sub    $0x1000,%rsp
mov    %rdi,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    (%rbx),%al
test   %al,%al
je     11b9 <print+0x89>
add    $0x1,%rbx
lea    0xe9e(%rip),%r15
jmp    1199 <print+0x69>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
movzbl -0x1(%rbx),%eax
add    $0x30,%al
mov    %al,0x6(%r14)
movb   $0x7e,0x7(%r14)
add    $0x8,%r14
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     11b9 <print+0x89>
cmp    $0x13,%al
jle    1170 <print+0x40>
cmp    $0x22,%al
jne    11b0 <print+0x80>
movw   $0x225c,(%r14)
add    $0x2,%r14
jmp    118e <print+0x5e>
nopl   (%rax)
mov    %al,(%r14)
add    $0x1,%r14
jmp    118e <print+0x5e>
mov    0x2e79(%rip),%esi
lea    0x1(%rsi),%eax
mov    %eax,0x2e70(%rip)
lea    0xe38(%rip),%rdi
mov    %rsp,%rdx
call   1040 <printf@plt>
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@print.count = internal global i32 1
@.str = [7 x i8] c"~color\00"
@.str.1 = [7 x i8] c"%d %s\0A\00"
define void @print(i8* %0) {
%2 = alloca i8*
%3 = alloca [4096 x i8]
%4 = alloca i8*
store i8* %0, i8** %2
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
store i8* %5, i8** %4
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%7 = call i32 @memset(i8* %6, i32 0, i32 4096)
br label %8
8: ; preds = %52, %1
%9 = load i8*, i8** %2
%10 = load i8, i8* %9
%11 = icmp ne i8 %10, 0
br i1 %11, label %12, label %53
12: ; preds = %8
%13 = load i8*, i8** %2
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp slt i32 %15, 20
br i1 %16, label %17, label %32
17: ; preds = %12
%18 = load i8*, i8** %4
%19 = call i32 @strcpy(i8* %18, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%20 = load i8*, i8** %4
%21 = getelementptr inbounds i8, i8* %20, i64 6
store i8* %21, i8** %4
%22 = load i8*, i8** %2
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %2
%24 = load i8, i8* %22
%25 = sext i8 %24 to i32
%26 = add nsw i32 %25, 48
%27 = trunc i32 %26 to i8
%28 = load i8*, i8** %4
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %4
store i8 %27, i8* %28
%30 = load i8*, i8** %4
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %4
store i8 126, i8* %30
br label %52
32: ; preds = %12
%33 = load i8*, i8** %2
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 34
br i1 %36, label %37, label %45
37: ; preds = %32
%38 = load i8*, i8** %4
%39 = getelementptr inbounds i8, i8* %38, i32 1
store i8* %39, i8** %4
store i8 92, i8* %38
%40 = load i8*, i8** %2
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %2
%42 = load i8, i8* %40
%43 = load i8*, i8** %4
%44 = getelementptr inbounds i8, i8* %43, i32 1
store i8* %44, i8** %4
store i8 %42, i8* %43
br label %51
45: ; preds = %32
%46 = load i8*, i8** %2
%47 = getelementptr inbounds i8, i8* %46, i32 1
store i8* %47, i8** %2
%48 = load i8, i8* %46
%49 = load i8*, i8** %4
%50 = getelementptr inbounds i8, i8* %49, i32 1
store i8* %50, i8** %4
store i8 %48, i8* %49
br label %51
51: ; preds = %45, %37
br label %52
52: ; preds = %51, %17
br label %8
53: ; preds = %8
%54 = load i32, i32* @print.count
%55 = add nsw i32 %54, 1
store i32 %55, i32* @print.count
%56 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%57 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %54, i8* %56)
ret void
}

LLVM IR (-O2):
@print.count = internal unnamed_addr global i32 1
@.str = [7 x i8] c"~color\00"
@.str.1 = [7 x i8] c"%d %s\0A\00"
define void @print(i8* nocapture %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = call i32 @memset(i8* %3, i32 0, i32 4096)
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 0
br i1 %6, label %30, label %7
7: ; preds = %1, %25
%8 = phi i8 [ %28, %25 ], [ %5, %1 ]
%9 = phi i8* [ %26, %25 ], [ %3, %1 ]
%10 = phi i8* [ %27, %25 ], [ %0, %1 ]
%11 = icmp slt i8 %8, 20
br i1 %11, label %12, label %19
12: ; preds = %7
%13 = call i32 @strcpy(i8* %9, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = getelementptr inbounds i8, i8* %9, i64 6
%15 = load i8, i8* %10
%16 = add i8 %15, 48
%17 = getelementptr inbounds i8, i8* %9, i64 7
store i8 %16, i8* %14
%18 = getelementptr inbounds i8, i8* %9, i64 8
store i8 126, i8* %17
br label %25
19: ; preds = %7
%20 = icmp eq i8 %8, 34
%21 = getelementptr inbounds i8, i8* %9, i64 1
br i1 %20, label %22, label %24
22: ; preds = %19
store i8 92, i8* %9
%23 = getelementptr inbounds i8, i8* %9, i64 2
store i8 34, i8* %21
br label %25
24: ; preds = %19
store i8 %8, i8* %9
br label %25
25: ; preds = %22, %24, %12
%26 = phi i8* [ %18, %12 ], [ %23, %22 ], [ %21, %24 ]
%27 = getelementptr inbounds i8, i8* %10, i64 1
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 0
br i1 %29, label %30, label %7
30: ; preds = %25, %1
%31 = load i32, i32* @print.count
%32 = add nsw i32 %31, 1
store i32 %32, i32* @print.count
%33 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %31, i8* %3)
ret void
}