#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 */
scalar_t__ malloc(int);
int strlen(char *);
char *escape_string_for_regex(char *s) {
  char *ss = (char *)malloc((strlen(s) + 1) * 2), *sss = ss;
  for (; *s; s++) {
    switch (*s) {
    case '(':
    case ')':
    case '[':
    case ']':
    case '-':
    case '^':
    case '*':
    case '?':
    case '+':
      *ss++ = '\\';
    default:
      *ss++ = *s;
      break;
    }
  }
  *ss = 0;
  return sss;
}


x86 assembly (clang, -O0):
<escape_string_for_regex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
shl    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     11fc <escape_string_for_regex+0xdc>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x1c(%rbp)
add    $0xffffffd8,%eax
sub    $0x4,%eax
jb     11bd <escape_string_for_regex+0x9d>
jmp    1176 <escape_string_for_regex+0x56>
mov    -0x1c(%rbp),%eax
sub    $0x2d,%eax
je     11bd <escape_string_for_regex+0x9d>
jmp    1187 <escape_string_for_regex+0x67>
mov    -0x1c(%rbp),%eax
sub    $0x3f,%eax
je     11bd <escape_string_for_regex+0x9d>
jmp    1198 <escape_string_for_regex+0x78>
mov    -0x1c(%rbp),%eax
sub    $0x5b,%eax
je     11bd <escape_string_for_regex+0x9d>
jmp    11a9 <escape_string_for_regex+0x89>
mov    -0x1c(%rbp),%eax
add    $0xffffffa3,%eax
sub    $0x1,%eax
ja     11cf <escape_string_for_regex+0xaf>
jmp    11bd <escape_string_for_regex+0x9d>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x5c,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
jmp    11eb <escape_string_for_regex+0xcb>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    114e <escape_string_for_regex+0x2e>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape_string_for_regex>:
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
lea    (%rax,%rax,1),%edi
add    $0x2,%edi
call   1040 <malloc@plt>
mov    (%rbx),%dl
test   %dl,%dl
je     117f <escape_string_for_regex+0x5f>
add    $0x1,%rbx
movabs $0x6800000080002f,%rsi
mov    %rax,%rcx
jmp    1161 <escape_string_for_regex+0x41>
nopl   (%rax)
mov    %dl,(%rcx)
add    $0x1,%rcx
movzbl (%rbx),%edx
add    $0x1,%rbx
test   %dl,%dl
je     1182 <escape_string_for_regex+0x62>
movsbl %dl,%edi
add    $0xffffffd8,%edi
cmp    $0x36,%edi
ja     1150 <escape_string_for_regex+0x30>
bt     %rdi,%rsi
jae    1150 <escape_string_for_regex+0x30>
movb   $0x5c,(%rcx)
add    $0x1,%rcx
movzbl -0x1(%rbx),%edx
jmp    1150 <escape_string_for_regex+0x30>
mov    %rax,%rcx
movb   $0x0,(%rcx)
pop    %rbx
ret

LLVM IR (-O0):
define i8* @escape_string_for_regex(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32 @strlen(i8* %5)
%7 = add nsw i32 %6, 1
%8 = mul nsw i32 %7, 2
%9 = call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %3
%11 = load i8*, i8** %3
store i8* %11, i8** %4
br label %12
12: ; preds = %29, %1
%13 = load i8*, i8** %2
%14 = load i8, i8* %13
%15 = icmp ne i8 %14, 0
br i1 %15, label %16, label %32
16: ; preds = %12
%17 = load i8*, i8** %2
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
switch i32 %19, label %23 [
i32 40, label %20
i32 41, label %20
i32 91, label %20
i32 93, label %20
i32 45, label %20
i32 94, label %20
i32 42, label %20
i32 63, label %20
i32 43, label %20
]
20: ; preds = %16, %16, %16, %16, %16, %16, %16, %16, %16
%21 = load i8*, i8** %3
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %3
store i8 92, i8* %21
br label %23
23: ; preds = %16, %20
%24 = load i8*, i8** %2
%25 = load i8, i8* %24
%26 = load i8*, i8** %3
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %3
store i8 %25, i8* %26
br label %28
28: ; preds = %23
br label %29
29: ; preds = %28
%30 = load i8*, i8** %2
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %2
br label %12
32: ; preds = %12
%33 = load i8*, i8** %3
store i8 0, i8* %33
%34 = load i8*, i8** %4
ret i8* %34
}

LLVM IR (-O2):
define i8* @escape_string_for_regex(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = shl i32 %2, 1
%4 = add i32 %3, 2
%5 = tail call i64 @malloc(i32 %4)
%6 = inttoptr i64 %5 to i8*
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %24, label %9
9: ; preds = %1, %17
%10 = phi i8 [ %22, %17 ], [ %7, %1 ]
%11 = phi i8* [ %21, %17 ], [ %0, %1 ]
%12 = phi i8* [ %20, %17 ], [ %6, %1 ]
%13 = sext i8 %10 to i32
switch i32 %13, label %17 [
i32 40, label %14
i32 41, label %14
i32 91, label %14
i32 93, label %14
i32 45, label %14
i32 94, label %14
i32 42, label %14
i32 63, label %14
i32 43, label %14
]
14: ; preds = %9, %9, %9, %9, %9, %9, %9, %9, %9
%15 = getelementptr inbounds i8, i8* %12, i64 1
store i8 92, i8* %12
%16 = load i8, i8* %11
br label %17
17: ; preds = %9, %14
%18 = phi i8 [ %10, %9 ], [ %16, %14 ]
%19 = phi i8* [ %12, %9 ], [ %15, %14 ]
%20 = getelementptr inbounds i8, i8* %19, i64 1
store i8 %18, i8* %19
%21 = getelementptr inbounds i8, i8* %11, i64 1
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 0
br i1 %23, label %24, label %9
24: ; preds = %17, %1
%25 = phi i8* [ %6, %1 ], [ %20, %17 ]
store i8 0, i8* %25
ret i8* %6
}