#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__ isalnum(char);
char *malloc(scalar_t__);
scalar_t__ strlen(char *);
char *escapename(char *filename) {
  char *result = malloc(strlen(filename) + 1);
  if (result) {
    char *c = result;
    while (*filename) {
      if (isalnum(*filename) || *filename == '.' || *filename == '/' ||
          *filename == '$')
        *c++ = *filename;
      else
        *c++ = '_';
      ++filename;
    }
    *c++ = '\0';
  }
  return result;
}


x86 assembly (clang, -O0):
<escapename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     120c <escapename+0xdc>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     11fa <escapename+0xca>
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isalnum@plt>
cmp    $0x0,%rax
jne    11bb <escapename+0x8b>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
je     11bb <escapename+0x8b>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     11bb <escapename+0x8b>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x24,%eax
jne    11d7 <escapename+0xa7>
mov    -0x8(%rbp),%rax
mov    (%rax),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    11e9 <escapename+0xb9>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x5f,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1168 <escapename+0x38>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escapename>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
call   1030 <strlen@plt>
lea    0x1(%rax),%rdi
call   1050 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     11a9 <escapename+0x79>
mov    (%r15),%al
mov    %r14,%rbx
test   %al,%al
je     11a6 <escapename+0x76>
add    $0x1,%r15
movabs $0xc01000000000,%r12
mov    %r14,%rbx
nopl   0x0(%rax)
movsbl %al,%edi
call   1040 <isalnum@plt>
movzbl -0x1(%r15),%ecx
test   %rax,%rax
jne    1192 <escapename+0x62>
mov    $0x5f,%al
cmp    $0x2f,%cl
ja     1194 <escapename+0x64>
movzbl %cl,%edx
bt     %rdx,%r12
jae    1194 <escapename+0x64>
mov    %ecx,%eax
mov    %al,(%rbx)
add    $0x1,%rbx
movzbl (%r15),%eax
add    $0x1,%r15
test   %al,%al
jne    1170 <escapename+0x40>
movb   $0x0,(%rbx)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @escapename(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i64 @strlen(i8* %5)
%7 = add nsw i64 %6, 1
%8 = call i8* @malloc(i64 %7)
store i8* %8, i8** %3
%9 = load i8*, i8** %3
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %51
11: ; preds = %1
%12 = load i8*, i8** %3
store i8* %12, i8** %4
br label %13
13: ; preds = %45, %11
%14 = load i8*, i8** %2
%15 = load i8, i8* %14
%16 = icmp ne i8 %15, 0
br i1 %16, label %17, label %48
17: ; preds = %13
%18 = load i8*, i8** %2
%19 = load i8, i8* %18
%20 = call i64 @isalnum(i8 signext %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %37, label %22
22: ; preds = %17
%23 = load i8*, i8** %2
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 46
br i1 %26, label %37, label %27
27: ; preds = %22
%28 = load i8*, i8** %2
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 47
br i1 %31, label %37, label %32
32: ; preds = %27
%33 = load i8*, i8** %2
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 36
br i1 %36, label %37, label %42
37: ; preds = %32, %27, %22, %17
%38 = load i8*, i8** %2
%39 = load i8, i8* %38
%40 = load i8*, i8** %4
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %4
store i8 %39, i8* %40
br label %45
42: ; preds = %32
%43 = load i8*, i8** %4
%44 = getelementptr inbounds i8, i8* %43, i32 1
store i8* %44, i8** %4
store i8 95, i8* %43
br label %45
45: ; preds = %42, %37
%46 = load i8*, i8** %2
%47 = getelementptr inbounds i8, i8* %46, i32 1
store i8* %47, i8** %2
br label %13
48: ; preds = %13
%49 = load i8*, i8** %4
%50 = getelementptr inbounds i8, i8* %49, i32 1
store i8* %50, i8** %4
store i8 0, i8* %49
br label %51
51: ; preds = %48, %1
%52 = load i8*, i8** %3
ret i8* %52
}

LLVM IR (-O2):
define noalias i8* @escapename(i8* nocapture %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = add nsw i64 %2, 1
%4 = tail call i8* @malloc(i64 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %26, label %6
6: ; preds = %1
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %24, label %9
9: ; preds = %6, %18
%10 = phi i8 [ %22, %18 ], [ %7, %6 ]
%11 = phi i8* [ %20, %18 ], [ %4, %6 ]
%12 = phi i8* [ %21, %18 ], [ %0, %6 ]
%13 = tail call i64 @isalnum(i8 signext %10)
%14 = icmp eq i64 %13, 0
%15 = load i8, i8* %12
br i1 %14, label %16, label %17
16: ; preds = %9
switch i8 %15, label %18 [
i8 46, label %17
i8 47, label %17
i8 36, label %17
]
17: ; preds = %9, %16, %16, %16
br label %18
18: ; preds = %16, %17
%19 = phi i8 [ %15, %17 ], [ 95, %16 ]
%20 = getelementptr inbounds i8, i8* %11, i64 1
store i8 %19, i8* %11
%21 = getelementptr inbounds i8, i8* %12, i64 1
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 0
br i1 %23, label %24, label %9
24: ; preds = %18, %6
%25 = phi i8* [ %4, %6 ], [ %20, %18 ]
store i8 0, i8* %25
br label %26
26: ; preds = %24, %1
ret i8* %4
}