char *security_escape_character_array(char *destination, char *source,
                                      char *character_array) {
  char *character_array_anchor = character_array;
  char *destination_anchor = destination;
  while (*source) {
    character_array = character_array_anchor;
    while (*character_array) {
      if (*source == *character_array) {
        *destination++ = '\\';
        break;
      }
      character_array++;
    }
    *destination++ = *source++;
  }
  *destination = '\0';
  return destination_anchor;
}


x86 assembly (clang, -O0):
<security_escape_character_array>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     11a7 <security_escape_character_array+0xa7>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     1180 <security_escape_character_array+0x80>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
mov    -0x18(%rbp),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    116f <security_escape_character_array+0x6f>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x5c,(%rax)
jmp    1180 <security_escape_character_array+0x80>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1135 <security_escape_character_array+0x35>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
jmp    1120 <security_escape_character_array+0x20>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<security_escape_character_array>:
mov    %rdi,%rax
mov    (%rsi),%r10b
mov    %rdi,%r9
test   %r10b,%r10b
je     1162 <security_escape_character_array+0x62>
lea    0x1(%rdx),%r8
mov    %rax,%r9
jmp    113f <security_escape_character_array+0x3f>
nopw   0x0(%rax,%rax,1)
movb   $0x5c,(%r9)
add    $0x1,%r9
mov    (%rsi),%r10b
mov    %r10b,(%r9)
add    $0x1,%r9
mov    0x1(%rsi),%r10b
add    $0x1,%rsi
test   %r10b,%r10b
je     1162 <security_escape_character_array+0x62>
mov    (%rdx),%cl
mov    %r8,%rdi
test   %cl,%cl
je     112b <security_escape_character_array+0x2b>
nopl   0x0(%rax,%rax,1)
cmp    %cl,%r10b
je     1120 <security_escape_character_array+0x20>
movzbl (%rdi),%ecx
add    $0x1,%rdi
test   %cl,%cl
jne    1150 <security_escape_character_array+0x50>
jmp    112b <security_escape_character_array+0x2b>
movb   $0x0,(%r9)
ret

LLVM IR (-O0):
define i8* @security_escape_character_array(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%9 = load i8*, i8** %6
store i8* %9, i8** %7
%10 = load i8*, i8** %4
store i8* %10, i8** %8
br label %11
11: ; preds = %35, %3
%12 = load i8*, i8** %5
%13 = load i8, i8* %12
%14 = icmp ne i8 %13, 0
br i1 %14, label %15, label %41
15: ; preds = %11
%16 = load i8*, i8** %7
store i8* %16, i8** %6
br label %17
17: ; preds = %32, %15
%18 = load i8*, i8** %6
%19 = load i8, i8* %18
%20 = icmp ne i8 %19, 0
br i1 %20, label %21, label %35
21: ; preds = %17
%22 = load i8*, i8** %5
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = load i8*, i8** %6
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %24, %27
br i1 %28, label %29, label %32
29: ; preds = %21
%30 = load i8*, i8** %4
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %4
store i8 92, i8* %30
br label %35
32: ; preds = %21
%33 = load i8*, i8** %6
%34 = getelementptr inbounds i8, i8* %33, i32 1
store i8* %34, i8** %6
br label %17
35: ; preds = %29, %17
%36 = load i8*, i8** %5
%37 = getelementptr inbounds i8, i8* %36, i32 1
store i8* %37, i8** %5
%38 = load i8, i8* %36
%39 = load i8*, i8** %4
%40 = getelementptr inbounds i8, i8* %39, i32 1
store i8* %40, i8** %4
store i8 %38, i8* %39
br label %11
41: ; preds = %11
%42 = load i8*, i8** %4
store i8 0, i8* %42
%43 = load i8*, i8** %8
ret i8* %43
}

LLVM IR (-O2):
define i8* @security_escape_character_array(i8* returned writeonly %0, i8* nocapture %1, i8* nocapture %2) {
%4 = load i8, i8* %1
%5 = icmp eq i8 %4, 0
br i1 %5, label %30, label %6
6: ; preds = %3, %23
%7 = phi i8 [ %28, %23 ], [ %4, %3 ]
%8 = phi i8* [ %27, %23 ], [ %0, %3 ]
%9 = phi i8* [ %26, %23 ], [ %1, %3 ]
%10 = load i8, i8* %2
%11 = icmp eq i8 %10, 0
br i1 %11, label %23, label %15
12: ; preds = %15
%13 = load i8, i8* %19
%14 = icmp eq i8 %13, 0
br i1 %14, label %23, label %15
15: ; preds = %6, %12
%16 = phi i8 [ %13, %12 ], [ %10, %6 ]
%17 = phi i8* [ %19, %12 ], [ %2, %6 ]
%18 = icmp eq i8 %7, %16
%19 = getelementptr inbounds i8, i8* %17, i64 1
br i1 %18, label %20, label %12
20: ; preds = %15
%21 = getelementptr inbounds i8, i8* %8, i64 1
store i8 92, i8* %8
%22 = load i8, i8* %9
br label %23
23: ; preds = %12, %6, %20
%24 = phi i8 [ %22, %20 ], [ %7, %6 ], [ %7, %12 ]
%25 = phi i8* [ %21, %20 ], [ %8, %6 ], [ %8, %12 ]
%26 = getelementptr inbounds i8, i8* %9, i64 1
%27 = getelementptr inbounds i8, i8* %25, i64 1
store i8 %24, i8* %25
%28 = load i8, i8* %26
%29 = icmp eq i8 %28, 0
br i1 %29, label %30, label %6
30: ; preds = %23, %3
%31 = phi i8* [ %0, %3 ], [ %27, %23 ]
store i8 0, i8* %31
ret i8* %0
}