#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 */
int /*<<< orphan*/ memmove(char *, char *, int);
char *strchr(char *, char);
int strlen(char *);
char *COM_Purge(char *s, int *np) {
  char *t;
  char *p;
  int n;
  n = strlen(s);
  t = s + n + 1;
  p = s;
  while ((p = strchr(p, '\033'))) {
    memmove(p, &p[1], t - p - 1);
    n--;
  }
  if (np)
    (*np) = n;
  return s;
}


x86 assembly (clang, -O0):
<COM_Purge>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x1b,%esi
call   1040 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11b3 <COM_Purge+0x83>
mov    -0x20(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
call   1050 <memmove@plt>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    1167 <COM_Purge+0x37>
cmpq   $0x0,-0x10(%rbp)
je     11c7 <COM_Purge+0x97>
mov    -0x24(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<COM_Purge>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    %r15,%rdi
mov    $0x1b,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     119b <COM_Purge+0x6b>
mov    %rax,%rbx
movslq %ebp,%rax
lea    (%r15,%rax,1),%r12
add    $0x1,%r12
cs nopw 0x0(%rax,%rax,1)
nop
lea    0x1(%rbx),%rsi
mov    %ebx,%edx
not    %edx
add    %r12d,%edx
mov    %rbx,%rdi
call   1050 <memmove@plt>
add    $0xffffffff,%ebp
mov    %rbx,%rdi
mov    $0x1b,%esi
call   1040 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1170 <COM_Purge+0x40>
test   %r14,%r14
je     11a3 <COM_Purge+0x73>
mov    %ebp,(%r14)
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @COM_Purge(i8* returned %0, i32* writeonly %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 27)
%5 = icmp eq i8* %4, null
br i1 %5, label %23, label %6
6: ; preds = %2
%7 = sext i32 %3 to i64
%8 = getelementptr inbounds i8, i8* %0, i64 %7
%9 = getelementptr inbounds i8, i8* %8, i64 1
%10 = ptrtoint i8* %9 to i64
br label %11
11: ; preds = %6, %11
%12 = phi i8* [ %4, %6 ], [ %21, %11 ]
%13 = phi i32 [ %3, %6 ], [ %20, %11 ]
%14 = getelementptr inbounds i8, i8* %12, i64 1
%15 = ptrtoint i8* %12 to i64
%16 = xor i64 %15, -1
%17 = add i64 %16, %10
%18 = trunc i64 %17 to i32
%19 = tail call i32 @memmove(i8* %12, i8* %14, i32 %18)
%20 = add nsw i32 %13, -1
%21 = tail call i8* @strchr(i8* dereferenceable(1) %12, i8 signext 27)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %11
23: ; preds = %11, %2
%24 = phi i32 [ %3, %2 ], [ %20, %11 ]
%25 = icmp eq i32* %1, null
br i1 %25, label %27, label %26
26: ; preds = %23
store i32 %24, i32* %1
br label %27
27: ; preds = %26, %23
ret i8* %0
}