#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 const *find_violation(char const *, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *get_escaped_name(const char *fn) {
  const char *end = fn + strlen(fn);
  char *escaped_name = 0;
  if (find_violation(fn, 0) != end) {
    escaped_name =
        (char *)malloc(sizeof(char) * (1 + strlen(fn) * strlen("#(000)")));
    char *escp = escaped_name;
    const char *ep = fn;
    const char *last;
    while (end != ep) {
      last = ep;
      ep = find_violation(last, 1);
      strncpy(escp, last, ep - last);
      escp += ep - last;
      *escp = 0;
      if (ep == end)
        break;
      sprintf(escp, "#(%d)", (unsigned char)*ep);
      escp += strlen(escp);
      ep++;
    }
  }
  return escaped_name;
}


x86 assembly (clang, -O0):
<get_escaped_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1050 <find_violation@plt>
cmp    -0x10(%rbp),%rax
je     1293 <get_escaped_name+0x143>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x3c(%rbp)
lea    0xe53(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
imul   %ecx,%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     128e <get_escaped_name+0x13e>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    $0x1,%esi
call   1050 <find_violation@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    %rcx,%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    124d <get_escaped_name+0xfd>
jmp    128e <get_escaped_name+0x13e>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
lea    0xdab(%rip),%rsi
movzbl (%rax),%edx
call   1070 <sprintf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11de <get_escaped_name+0x8e>
jmp    1293 <get_escaped_name+0x143>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_escaped_name>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
call   1040 <strlen@plt>
mov    %eax,%r15d
movslq %eax,%r14
add    %r12,%r14
xor    %ebx,%ebx
mov    %r12,%rdi
xor    %esi,%esi
call   1050 <find_violation@plt>
cmp    %r14,%rax
je     121c <get_escaped_name+0xcc>
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
lea    0xe6e(%rip),%rdi
call   1040 <strlen@plt>
imul   %eax,%ebp
add    $0x1,%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
test   %r15d,%r15d
je     121c <get_escaped_name+0xcc>
lea    0xe54(%rip),%r15
mov    %rbx,(%rsp)
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    $0x1,%esi
call   1050 <find_violation@plt>
mov    %rax,%r13
mov    %rax,%rbp
sub    %r12,%rbp
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
movb   $0x0,(%rbx,%rbp,1)
cmp    %r14,%r13
je     1218 <get_escaped_name+0xc8>
add    %rbp,%rbx
movzbl 0x0(%r13),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <sprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
add    %rax,%rbx
add    $0x1,%r13
mov    %r13,%r12
cmp    %r13,%r14
jne    11c0 <get_escaped_name+0x70>
mov    (%rsp),%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"#(000)\00"
@.str.1 = [6 x i8] c"#(%d)\00"
define i8* @get_escaped_name(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = load i8*, i8** %2
%10 = call i32 @strlen(i8* %9)
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %8, i64 %11
store i8* %12, i8** %3
store i8* null, i8** %4
%13 = load i8*, i8** %2
%14 = call i8* @find_violation(i8* %13, i32 0)
%15 = load i8*, i8** %3
%16 = icmp ne i8* %14, %15
br i1 %16, label %17, label %72
17: ; preds = %1
%18 = load i8*, i8** %2
%19 = call i32 @strlen(i8* %18)
%20 = call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%21 = mul nsw i32 %19, %20
%22 = add nsw i32 1, %21
%23 = sext i32 %22 to i64
%24 = mul i64 1, %23
%25 = trunc i64 %24 to i32
%26 = call i64 @malloc(i32 %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %4
%28 = load i8*, i8** %4
store i8* %28, i8** %5
%29 = load i8*, i8** %2
store i8* %29, i8** %6
br label %30
30: ; preds = %59, %17
%31 = load i8*, i8** %3
%32 = load i8*, i8** %6
%33 = icmp ne i8* %31, %32
br i1 %33, label %34, label %71
34: ; preds = %30
%35 = load i8*, i8** %6
store i8* %35, i8** %7
%36 = load i8*, i8** %7
%37 = call i8* @find_violation(i8* %36, i32 1)
store i8* %37, i8** %6
%38 = load i8*, i8** %5
%39 = load i8*, i8** %7
%40 = load i8*, i8** %6
%41 = load i8*, i8** %7
%42 = ptrtoint i8* %40 to i64
%43 = ptrtoint i8* %41 to i64
%44 = sub i64 %42, %43
%45 = trunc i64 %44 to i32
%46 = call i32 @strncpy(i8* %38, i8* %39, i32 %45)
%47 = load i8*, i8** %6
%48 = load i8*, i8** %7
%49 = ptrtoint i8* %47 to i64
%50 = ptrtoint i8* %48 to i64
%51 = sub i64 %49, %50
%52 = load i8*, i8** %5
%53 = getelementptr inbounds i8, i8* %52, i64 %51
store i8* %53, i8** %5
%54 = load i8*, i8** %5
store i8 0, i8* %54
%55 = load i8*, i8** %6
%56 = load i8*, i8** %3
%57 = icmp eq i8* %55, %56
br i1 %57, label %58, label %59
58: ; preds = %34
br label %71
59: ; preds = %34
%60 = load i8*, i8** %5
%61 = load i8*, i8** %6
%62 = load i8, i8* %61
%63 = call i32 @sprintf(i8* %60, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %62)
%64 = load i8*, i8** %5
%65 = call i32 @strlen(i8* %64)
%66 = load i8*, i8** %5
%67 = sext i32 %65 to i64
%68 = getelementptr inbounds i8, i8* %66, i64 %67
store i8* %68, i8** %5
%69 = load i8*, i8** %6
%70 = getelementptr inbounds i8, i8* %69, i32 1
store i8* %70, i8** %6
br label %30
71: ; preds = %58, %30
br label %72
72: ; preds = %71, %1
%73 = load i8*, i8** %4
ret i8* %73
}

LLVM IR (-O2):
@.str = [7 x i8] c"#(000)\00"
@.str.1 = [6 x i8] c"#(%d)\00"
define i8* @get_escaped_name(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = sext i32 %2 to i64
%4 = getelementptr inbounds i8, i8* %0, i64 %3
%5 = tail call i8* @find_violation(i8* %0, i32 0)
%6 = icmp eq i8* %5, %4
br i1 %6, label %34, label %7
7: ; preds = %1
%8 = tail call i32 @strlen(i8* %0)
%9 = tail call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%10 = mul nsw i32 %9, %8
%11 = add nsw i32 %10, 1
%12 = tail call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
%14 = icmp eq i32 %2, 0
br i1 %14, label %34, label %15
15: ; preds = %7, %26
%16 = phi i8* [ %32, %26 ], [ %0, %7 ]
%17 = phi i8* [ %31, %26 ], [ %13, %7 ]
%18 = tail call i8* @find_violation(i8* %16, i32 1)
%19 = ptrtoint i8* %18 to i64
%20 = ptrtoint i8* %16 to i64
%21 = sub i64 %19, %20
%22 = trunc i64 %21 to i32
%23 = tail call i32 @strncpy(i8* %17, i8* %16, i32 %22)
%24 = getelementptr inbounds i8, i8* %17, i64 %21
store i8 0, i8* %24
%25 = icmp eq i8* %18, %4
br i1 %25, label %34, label %26
26: ; preds = %15
%27 = load i8, i8* %18
%28 = tail call i32 @sprintf(i8* %24, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %27)
%29 = tail call i32 @strlen(i8* %24)
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %24, i64 %30
%32 = getelementptr inbounds i8, i8* %18, i64 1
%33 = icmp eq i8* %4, %32
br i1 %33, label %34, label %15
34: ; preds = %15, %26, %7, %1
%35 = phi i8* [ null, %1 ], [ %13, %7 ], [ %13, %26 ], [ %13, %15 ]
ret i8* %35
}