#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 */
char *strdup(char const *);
size_t strlen(char const *);
int /*<<< orphan*/ strrepl(char *, char *, char *);
int /*<<< orphan*/ utf8_tolower(char **);
char *canonicalize(const char *n) {
  char *result;
  size_t len = strlen(n);
  bool quoted = len > 0 && n[0] == '"' && n[len - 1] == '"';
  int off = quoted ? 1 : 0;
  result = strdup(n + off);
  if (!result)
    return NULL;
  result[len - 2 * off] = '\0';
  utf8_tolower(&result);
  strrepl(result, "\\\\", "\\");
  strrepl(result, "\\n", "_");
  strrepl(result, "\\r", "_");
  strrepl(result, "\n", "_");
  strrepl(result, "\r", "_");
  strrepl(result, " ", "_");
  return result;
}


x86 assembly (clang, -O0):
<canonicalize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x29(%rbp)
jbe    1197 <canonicalize+0x57>
mov    -0x10(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x22,%ecx
mov    %al,-0x29(%rbp)
jne    1197 <canonicalize+0x57>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x22,%eax
sete   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x28(%rbp),%rax
add    %rax,%rdi
call   1060 <strdup@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e1 <canonicalize+0xa1>
movq   $0x0,-0x8(%rbp)
jmp    1294 <canonicalize+0x154>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%edx
shl    $0x1,%edx
movslq %edx,%rdx
sub    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
lea    -0x18(%rbp),%rdi
call   1030 <utf8_tolower@plt>
mov    -0x18(%rbp),%rdi
lea    0xdf3(%rip),%rsi
lea    0xded(%rip),%rdx
call   1050 <strrepl@plt>
mov    -0x18(%rbp),%rdi
lea    0xddf(%rip),%rsi
lea    0xddb(%rip),%rdx
call   1050 <strrepl@plt>
mov    -0x18(%rbp),%rdi
lea    0xdcd(%rip),%rsi
lea    0xdc4(%rip),%rdx
call   1050 <strrepl@plt>
mov    -0x18(%rbp),%rdi
lea    0xdb9(%rip),%rsi
lea    0xdad(%rip),%rdx
call   1050 <strrepl@plt>
mov    -0x18(%rbp),%rdi
lea    0xda4(%rip),%rsi
lea    0xd96(%rip),%rdx
call   1050 <strrepl@plt>
mov    -0x18(%rbp),%rdi
lea    0xd8f(%rip),%rsi
lea    0xd7f(%rip),%rdx
call   1050 <strrepl@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<canonicalize>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %rax,%r14
xor    %r15d,%r15d
test   %rax,%rax
je     116e <canonicalize+0x2e>
cmpb   $0x22,(%rbx)
jne    116e <canonicalize+0x2e>
xor    %r15d,%r15d
cmpb   $0x22,-0x1(%r14,%rbx,1)
sete   %r15b
mov    %r15d,%eax
add    %rax,%rbx
mov    %rbx,%rdi
call   1060 <strdup@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     1229 <canonicalize+0xe9>
add    %r15d,%r15d
sub    %r15,%r14
movb   $0x0,(%rax,%r14,1)
lea    0x8(%rsp),%rdi
call   1030 <utf8_tolower@plt>
mov    0x8(%rsp),%rdi
lea    0xe55(%rip),%rsi
lea    0xe4f(%rip),%rdx
call   1050 <strrepl@plt>
mov    0x8(%rsp),%rdi
lea    0xe40(%rip),%rsi
lea    0xe3c(%rip),%rbx
mov    %rbx,%rdx
call   1050 <strrepl@plt>
mov    0x8(%rsp),%rdi
lea    0xe2a(%rip),%rsi
mov    %rbx,%rdx
call   1050 <strrepl@plt>
mov    0x8(%rsp),%rdi
lea    0xe19(%rip),%rsi
mov    %rbx,%rdx
call   1050 <strrepl@plt>
mov    0x8(%rsp),%rdi
lea    0xe07(%rip),%rsi
mov    %rbx,%rdx
call   1050 <strrepl@plt>
mov    0x8(%rsp),%rdi
lea    0xdf5(%rip),%rsi
mov    %rbx,%rdx
call   1050 <strrepl@plt>
mov    0x8(%rsp),%rax
jmp    122b <canonicalize+0xeb>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"\\\\\00"
@.str.1 = [2 x i8] c"\\\00"
@.str.2 = [3 x i8] c"\\n\00"
@.str.3 = [2 x i8] c"_\00"
@.str.4 = [3 x i8] c"\\r\00"
@.str.5 = [2 x i8] c"\0A\00"
@.str.6 = [2 x i8] c"\0D\00"
@.str.7 = [2 x i8] c" \00"
define i8* @canonicalize(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i64 @strlen(i8* %8)
store i64 %9, i64* %5
%10 = load i64, i64* %5
%11 = icmp ugt i64 %10, 0
br i1 %11, label %12, label %26
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = getelementptr inbounds i8, i8* %13, i64 0
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 34
br i1 %17, label %18, label %26
18: ; preds = %12
%19 = load i8*, i8** %3
%20 = load i64, i64* %5
%21 = sub i64 %20, 1
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 34
br label %26
26: ; preds = %18, %12, %1
%27 = phi i1 [ false, %12 ], [ false, %1 ], [ %25, %18 ]
%28 = zext i1 %27 to i32
store i32 %28, i32* %6
%29 = load i32, i32* %6
%30 = icmp ne i32 %29, 0
%31 = zext i1 %30 to i64
%32 = select i1 %30, i32 1, i32 0
store i32 %32, i32* %7
%33 = load i8*, i8** %3
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = call i8* @strdup(i8* %36)
store i8* %37, i8** %4
%38 = load i8*, i8** %4
%39 = icmp ne i8* %38, null
br i1 %39, label %41, label %40
40: ; preds = %26
store i8* null, i8** %2
br label %63
41: ; preds = %26
%42 = load i8*, i8** %4
%43 = load i64, i64* %5
%44 = load i32, i32* %7
%45 = mul nsw i32 2, %44
%46 = sext i32 %45 to i64
%47 = sub i64 %43, %46
%48 = getelementptr inbounds i8, i8* %42, i64 %47
store i8 0, i8* %48
%49 = call i32 @utf8_tolower(i8** %4)
%50 = load i8*, i8** %4
%51 = call i32 @strrepl(i8* %50, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%52 = load i8*, i8** %4
%53 = call i32 @strrepl(i8* %52, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%54 = load i8*, i8** %4
%55 = call i32 @strrepl(i8* %54, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%56 = load i8*, i8** %4
%57 = call i32 @strrepl(i8* %56, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%58 = load i8*, i8** %4
%59 = call i32 @strrepl(i8* %58, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%60 = load i8*, i8** %4
%61 = call i32 @strrepl(i8* %60, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%62 = load i8*, i8** %4
store i8* %62, i8** %2
br label %63
63: ; preds = %41, %40
%64 = load i8*, i8** %2
ret i8* %64
}

LLVM IR (-O2):
@.str = [3 x i8] c"\\\\\00"
@.str.1 = [2 x i8] c"\\\00"
@.str.2 = [3 x i8] c"\\n\00"
@.str.3 = [2 x i8] c"_\00"
@.str.4 = [3 x i8] c"\\r\00"
@.str.5 = [2 x i8] c"\0A\00"
@.str.6 = [2 x i8] c"\0D\00"
@.str.7 = [2 x i8] c" \00"
define i8* @canonicalize(i8* nocapture %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = icmp eq i64 %4, 0
br i1 %5, label %15, label %6
6: ; preds = %1
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 34
br i1 %8, label %9, label %15
9: ; preds = %6
%10 = add i64 %4, -1
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 34
%14 = zext i1 %13 to i32
br label %15
15: ; preds = %9, %6, %1
%16 = phi i32 [ 0, %6 ], [ 0, %1 ], [ %14, %9 ]
%17 = zext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %0, i64 %17
%19 = tail call i8* @strdup(i8* %18)
store i8* %19, i8** %2
%20 = icmp eq i8* %19, null
br i1 %20, label %40, label %21
21: ; preds = %15
%22 = shl nuw nsw i32 %16, 1
%23 = zext i32 %22 to i64
%24 = sub i64 %4, %23
%25 = getelementptr inbounds i8, i8* %19, i64 %24
store i8 0, i8* %25
%26 = call i32 @utf8_tolower(i8** %2)
%27 = load i8*, i8** %2
%28 = call i32 @strrepl(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%29 = load i8*, i8** %2
%30 = call i32 @strrepl(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%31 = load i8*, i8** %2
%32 = call i32 @strrepl(i8* %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%33 = load i8*, i8** %2
%34 = call i32 @strrepl(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%35 = load i8*, i8** %2
%36 = call i32 @strrepl(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%37 = load i8*, i8** %2
%38 = call i32 @strrepl(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%39 = load i8*, i8** %2
br label %40
40: ; preds = %15, %21
%41 = phi i8* [ %39, %21 ], [ null, %15 ]
ret i8* %41
}