#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 */
size_t get_char_width_esc(char const *);
int /*<<< orphan*/ memcpy(char *, char const *, size_t const);
char *strdup(char const *);
char *esc_remove(const char str[]) {
  char *no_esc = strdup(str);
  if (no_esc != NULL) {
    char *p = no_esc;
    while (*str != '\0') {
      const size_t char_width_esc = get_char_width_esc(str);
      if (*str != '\033') {
        memcpy(p, str, char_width_esc);
        p += char_width_esc;
      }
      str += char_width_esc;
    }
    *p = '\0';
  }
  return no_esc;
}


x86 assembly (clang, -O0):
<esc_remove>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strdup@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11be <esc_remove+0x8e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11b7 <esc_remove+0x87>
mov    -0x8(%rbp),%rdi
call   1030 <get_char_width_esc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x1b,%eax
je     11a6 <esc_remove+0x76>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    115c <esc_remove+0x2c>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<esc_remove>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1050 <strdup@plt>
mov    %rax,%r14
test   %rax,%rax
je     1197 <esc_remove+0x67>
cmpb   $0x0,(%rbx)
mov    %r14,%r15
je     1193 <esc_remove+0x63>
mov    %r14,%r15
jmp    1170 <esc_remove+0x40>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rax
add    %rbx,%rax
cmpb   $0x0,(%rbx,%r12,1)
mov    %rax,%rbx
je     1193 <esc_remove+0x63>
mov    %rbx,%rdi
call   1030 <get_char_width_esc@plt>
mov    %rax,%r12
cmpb   $0x1b,(%rbx)
je     1160 <esc_remove+0x30>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1040 <memcpy@plt>
add    %r12,%r15
jmp    1160 <esc_remove+0x30>
movb   $0x0,(%r15)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @esc_remove(i8* %0) {
%2 = tail call i8* @strdup(i8* %0)
%3 = icmp eq i8* %2, null
br i1 %3, label %23, label %4
4: ; preds = %1
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 0
br i1 %6, label %21, label %7
7: ; preds = %4, %16
%8 = phi i8* [ %18, %16 ], [ %0, %4 ]
%9 = phi i8* [ %17, %16 ], [ %2, %4 ]
%10 = tail call i64 @get_char_width_esc(i8* %8)
%11 = load i8, i8* %8
%12 = icmp eq i8 %11, 27
br i1 %12, label %16, label %13
13: ; preds = %7
%14 = tail call i32 @memcpy(i8* %9, i8* %8, i64 %10)
%15 = getelementptr inbounds i8, i8* %9, i64 %10
br label %16
16: ; preds = %13, %7
%17 = phi i8* [ %15, %13 ], [ %9, %7 ]
%18 = getelementptr inbounds i8, i8* %8, i64 %10
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 0
br i1 %20, label %21, label %7
21: ; preds = %16, %4
%22 = phi i8* [ %2, %4 ], [ %17, %16 ]
store i8 0, i8* %22
br label %23
23: ; preds = %21, %1
ret i8* %2
}