#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*/ strcat(char *, char *);
char *strchr(char *, char);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char *);
char *strip_xterm(char *s) {
  static char buf[8192];
  char *ch, *p;
  if (!(ch = strstr(s, "\033[38;5;")))
    return s;
  do {
    p = strchr(ch + strlen("\033[38;5;"), 'm') + 1;
    strncpy(buf, s, ch - s);
    buf[ch - s] = 0;
    strcat(buf + (ch - s), p);
    s = buf;
  } while ((ch = strstr(s, "\033[38;5;")));
  if (!(ch = strstr(s, "\033[48;5;")))
    return s;
  do {
    p = strchr(ch + strlen("\033[48;5;"), 'm') + 1;
    strncpy(buf, s, ch - s);
    buf[ch - s] = 0;
    strcat(buf + (ch - s), p);
    s = buf;
  } while ((ch = strstr(s, "\033[48;5;")));
  return (buf);
}


x86 assembly (clang, -O0):
<strip_xterm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1187 <strip_xterm+0x37>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1319 <strip_xterm+0x1c9>
jmp    118c <strip_xterm+0x3c>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    0xe65(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%rdi
cltq
add    %rax,%rdi
mov    $0x6d,%esi
call   1050 <strchr@plt>
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
lea    0x2e8d(%rip),%rdi
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rax,%rcx
lea    0x2e76(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
lea    0x2e60(%rip),%rdi
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
call   1060 <strcat@plt>
lea    0x2e4d(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xdde(%rip),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    118c <strip_xterm+0x3c>
mov    -0x10(%rbp),%rdi
lea    0xdc8(%rip),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1260 <strip_xterm+0x110>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1319 <strip_xterm+0x1c9>
jmp    1265 <strip_xterm+0x115>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    0xd94(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
cltq
add    %rax,%rdi
mov    $0x6d,%esi
call   1050 <strchr@plt>
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
lea    0x2db4(%rip),%rdi
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rax,%rcx
lea    0x2d9d(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
lea    0x2d87(%rip),%rdi
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
call   1060 <strcat@plt>
lea    0x2d74(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xd0d(%rip),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1265 <strip_xterm+0x115>
lea    0x2d4b(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strip_xterm>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r12
lea    0xe9d(%rip),%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     12af <strip_xterm+0x15f>
mov    %rax,%rbx
lea    0xe85(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
movslq %eax,%rdi
add    %rbx,%rdi
mov    $0x6d,%esi
call   1050 <strchr@plt>
lea    0x1(%rax),%r13
sub    %r12,%rbx
lea    0x2ebf(%rip),%r15
mov    %r15,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
lea    (%r15,%rbx,1),%rdi
movb   $0x0,(%rbx,%r15,1)
mov    %r13,%rsi
call   1060 <strcat@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     122d <strip_xterm+0xdd>
mov    %rax,%rbx
lea    0xe27(%rip),%r14
lea    0x2e80(%rip),%r12
mov    %r14,%rdi
call   1040 <strlen@plt>
movslq %eax,%rdi
add    %rbx,%rdi
mov    $0x6d,%esi
call   1050 <strchr@plt>
lea    0x1(%rax),%r13
mov    %ebx,%edx
sub    %r12d,%edx
mov    %r12,%rdi
mov    %r12,%rsi
call   1030 <strncpy@plt>
movb   $0x0,(%rbx)
mov    %rbx,%rdi
mov    %r13,%rsi
call   1060 <strcat@plt>
mov    %r12,%rdi
mov    %r14,%rsi
call   1070 <strstr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11e0 <strip_xterm+0x90>
lea    0xdd4(%rip),%rsi
mov    %r15,%rdi
call   1070 <strstr@plt>
test   %rax,%rax
je     12b2 <strip_xterm+0x162>
mov    %rax,%rbx
lea    0xdbd(%rip),%r14
lea    0x2e0e(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
call   1040 <strlen@plt>
movslq %eax,%rdi
add    %rbx,%rdi
mov    $0x6d,%esi
call   1050 <strchr@plt>
lea    0x1(%rax),%r12
mov    %ebx,%edx
sub    %r15d,%edx
mov    %r15,%rdi
mov    %r15,%rsi
call   1030 <strncpy@plt>
movb   $0x0,(%rbx)
mov    %rbx,%rdi
mov    %r12,%rsi
call   1060 <strcat@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1070 <strstr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1260 <strip_xterm+0x110>
jmp    12b2 <strip_xterm+0x162>
mov    %r12,%r15
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@strip_xterm.buf = internal global [8192 x i8] zeroinitializer
@.str = [8 x i8] c"\1B[38;5;\00"
@.str.1 = [8 x i8] c"\1B[48;5;\00"
define i8* @strip_xterm(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i8* @strstr(i8* %6, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i8* %7, i8** %4
%8 = icmp ne i8* %7, null
br i1 %8, label %11, label %9
9: ; preds = %1
%10 = load i8*, i8** %3
store i8* %10, i8** %2
br label %86
11: ; preds = %1
br label %12
12: ; preds = %41, %11
%13 = load i8*, i8** %4
%14 = call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = call i8* @strchr(i8* %16, i8 signext 109)
%18 = getelementptr inbounds i8, i8* %17, i64 1
store i8* %18, i8** %5
%19 = load i8*, i8** %3
%20 = load i8*, i8** %4
%21 = load i8*, i8** %3
%22 = ptrtoint i8* %20 to i64
%23 = ptrtoint i8* %21 to i64
%24 = sub i64 %22, %23
%25 = trunc i64 %24 to i32
%26 = call i32 @strncpy(i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @strip_xterm.buf, i64 0, i64 0), i8* %19, i32 %25)
%27 = load i8*, i8** %4
%28 = load i8*, i8** %3
%29 = ptrtoint i8* %27 to i64
%30 = ptrtoint i8* %28 to i64
%31 = sub i64 %29, %30
%32 = getelementptr inbounds [8192 x i8], [8192 x i8]* @strip_xterm.buf, i64 0, i64 %31
store i8 0, i8* %32
%33 = load i8*, i8** %4
%34 = load i8*, i8** %3
%35 = ptrtoint i8* %33 to i64
%36 = ptrtoint i8* %34 to i64
%37 = sub i64 %35, %36
%38 = getelementptr inbounds i8, i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @strip_xterm.buf, i64 0, i64 0), i64 %37
%39 = load i8*, i8** %5
%40 = call i32 @strcat(i8* %38, i8* %39)
store i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @strip_xterm.buf, i64 0, i64 0), i8** %3
br label %41
41: ; preds = %12
%42 = load i8*, i8** %3
%43 = call i8* @strstr(i8* %42, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i8* %43, i8** %4
%44 = icmp ne i8* %43, null
br i1 %44, label %12, label %45
45: ; preds = %41
%46 = load i8*, i8** %3
%47 = call i8* @strstr(i8* %46, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i8* %47, i8** %4
%48 = icmp ne i8* %47, null
br i1 %48, label %51, label %49
49: ; preds = %45
%50 = load i8*, i8** %3
store i8* %50, i8** %2
br label %86
51: ; preds = %45
br label %52
52: ; preds = %81, %51
%53 = load i8*, i8** %4
%54 = call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %53, i64 %55
%57 = call i8* @strchr(i8* %56, i8 signext 109)
%58 = getelementptr inbounds i8, i8* %57, i64 1
store i8* %58, i8** %5
%59 = load i8*, i8** %3
%60 = load i8*, i8** %4
%61 = load i8*, i8** %3
%62 = ptrtoint i8* %60 to i64
%63 = ptrtoint i8* %61 to i64
%64 = sub i64 %62, %63
%65 = trunc i64 %64 to i32
%66 = call i32 @strncpy(i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @strip_xterm.buf, i64 0, i64 0), i8* %59, i32 %65)
%67 = load i8*, i8** %4
%68 = load i8*, i8** %3
%69 = ptrtoint i8* %67 to i64
%70 = ptrtoint i8* %68 to i64
%71 = sub i64 %69, %70
%72 = getelementptr inbounds [8192 x i8], [8192 x i8]* @strip_xterm.buf, i64 0, i64 %71
store i8 0, i8* %72
%73 = load i8*, i8** %4
%74 = load i8*, i8** %3
%75 = ptrtoint i8* %73 to i64
%76 = ptrtoint i8* %74 to i64
%77 = sub i64 %75, %76
%78 = getelementptr inbounds i8, i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @strip_xterm.buf, i64 0, i64 0), i64 %77
%79 = load i8*, i8** %5
%80 = call i32 @strcat(i8* %78, i8* %79)
store i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @strip_xterm.buf, i64 0, i64 0), i8** %3
br label %81
81: ; preds = %52
%82 = load i8*, i8** %3
%83 = call i8* @strstr(i8* %82, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i8* %83, i8** %4
%84 = icmp ne i8* %83, null
br i1 %84, label %52, label %85
85: ; preds = %81
store i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @strip_xterm.buf, i64 0, i64 0), i8** %2
br label %86
86: ; preds = %85, %49, %9
%87 = load i8*, i8** %2
ret i8* %87
}

LLVM IR (-O2):
@strip_xterm.buf = internal global [8192 x i8] zeroinitializer
@.str = [8 x i8] c"\1B[38;5;\00"
@.str.1 = [8 x i8] c"\1B[48;5;\00"
define i8* @strip_xterm(i8* %0) {
%2 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i8* %2, null
br i1 %3, label %52, label %4
4: ; preds = %1
%5 = tail call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%6 = sext i32 %5 to i64
%7 = getelementptr inbounds i8, i8* %2, i64 %6
%8 = tail call i8* @strchr(i8* dereferenceable(1) %7, i8 signext 109)
%9 = getelementptr inbounds i8, i8* %8, i64 1
%10 = ptrtoint i8* %2 to i64
%11 = ptrtoint i8* %0 to i64
%12 = sub i64 %10, %11
%13 = trunc i64 %12 to i32
%14 = tail call i32 @strncpy(i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @strip_xterm.buf, i64 0, i64 0), i8* %0, i32 %13)
%15 = getelementptr inbounds [8192 x i8], [8192 x i8]* @strip_xterm.buf, i64 0, i64 %12
store i8 0, i8* %15
%16 = tail call i32 @strcat(i8* %15, i8* %9)
%17 = tail call i8* @strstr(i8* dereferenceable(1) getelementptr inbounds ([8192 x i8], [8192 x i8]* @strip_xterm.buf, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%18 = icmp eq i8* %17, null
br i1 %18, label %34, label %19
19: ; preds = %4, %19
%20 = phi i8* [ %32, %19 ], [ %17, %4 ]
%21 = tail call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = tail call i8* @strchr(i8* dereferenceable(1) %23, i8 signext 109)
%25 = getelementptr inbounds i8, i8* %24, i64 1
%26 = ptrtoint i8* %20 to i64
%27 = sub i64 %26, ptrtoint ([8192 x i8]* @strip_xterm.buf to i64)
%28 = trunc i64 %27 to i32
%29 = tail call i32 @strncpy(i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @strip_xterm.buf, i64 0, i64 0), i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @strip_xterm.buf, i64 0, i64 0), i32 %28)
%30 = getelementptr inbounds [8192 x i8], [8192 x i8]* @strip_xterm.buf, i64 0, i64 %27
store i8 0, i8* %30
%31 = tail call i32 @strcat(i8* %30, i8* %25)
%32 = tail call i8* @strstr(i8* dereferenceable(1) getelementptr inbounds ([8192 x i8], [8192 x i8]* @strip_xterm.buf, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %19
34: ; preds = %19, %4
%35 = tail call i8* @strstr(i8* dereferenceable(1) getelementptr inbounds ([8192 x i8], [8192 x i8]* @strip_xterm.buf, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%36 = icmp eq i8* %35, null
br i1 %36, label %52, label %37
37: ; preds = %34, %37
%38 = phi i8* [ %50, %37 ], [ %35, %34 ]
%39 = tail call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
%42 = tail call i8* @strchr(i8* dereferenceable(1) %41, i8 signext 109)
%43 = getelementptr inbounds i8, i8* %42, i64 1
%44 = ptrtoint i8* %38 to i64
%45 = sub i64 %44, ptrtoint ([8192 x i8]* @strip_xterm.buf to i64)
%46 = trunc i64 %45 to i32
%47 = tail call i32 @strncpy(i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @strip_xterm.buf, i64 0, i64 0), i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @strip_xterm.buf, i64 0, i64 0), i32 %46)
%48 = getelementptr inbounds [8192 x i8], [8192 x i8]* @strip_xterm.buf, i64 0, i64 %45
store i8 0, i8* %48
%49 = tail call i32 @strcat(i8* %48, i8* %43)
%50 = tail call i8* @strstr(i8* dereferenceable(1) getelementptr inbounds ([8192 x i8], [8192 x i8]* @strip_xterm.buf, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%51 = icmp eq i8* %50, null
br i1 %51, label %52, label %37
52: ; preds = %37, %34, %1
%53 = phi i8* [ %0, %1 ], [ getelementptr inbounds ([8192 x i8], [8192 x i8]* @strip_xterm.buf, i64 0, i64 0), %34 ], [ getelementptr inbounds ([8192 x i8], [8192 x i8]* @strip_xterm.buf, i64 0, i64 0), %37 ]
ret i8* %53
}