#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 */
const char *strip_color(const char *str) {
  static char out[5][2048];
  static int i = 0;
  char *res = NULL;
  ++i, i %= 5;
  res = out[i];
  for (const char *pstr = str; *pstr != 0; pstr++) {
    if (*pstr != '~') {
      *res++ = *pstr;
      continue;
    }
    if (*(++pstr) == 0) {
      break;
    }
    if (*pstr == '!') {
      pstr++;
    } else {
      continue;
    }
    if (*pstr == 0) {
      break;
    }
  }
  return out[i];
}


x86 assembly (clang, -O0):
<strip_color>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    0x571a(%rip),%eax
add    $0x1,%eax
mov    %eax,0x5711(%rip)
mov    0x570b(%rip),%eax
mov    $0x5,%ecx
cltd
idiv   %ecx
mov    %edx,0x56fd(%rip)
movslq 0x56f6(%rip),%rcx
lea    0x2eef(%rip),%rax
shl    $0xb,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1202 <strip_color+0x102>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
je     1190 <strip_color+0x90>
mov    -0x18(%rbp),%rax
mov    (%rax),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
jmp    11f1 <strip_color+0xf1>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
jne    11b1 <strip_color+0xb1>
jmp    1202 <strip_color+0x102>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x21,%eax
jne    11d2 <strip_color+0xd2>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11d7 <strip_color+0xd7>
jmp    11f1 <strip_color+0xf1>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11ec <strip_color+0xec>
jmp    1202 <strip_color+0x102>
jmp    11f1 <strip_color+0xf1>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1154 <strip_color+0x54>
movslq 0x5627(%rip),%rcx
lea    0x2e20(%rip),%rax
shl    $0xb,%rcx
add    %rcx,%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<strip_color>:
mov    0x572a(%rip),%eax
mov    %eax,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
imul   $0x66666667,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x21,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,4),%ecx
neg    %ecx
add    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,0x56fe(%rip)
shl    $0xb,%rcx
lea    0x2ef3(%rip),%rax
add    %rcx,%rax
mov    %rax,%rcx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movzbl (%rdi),%edx
cmp    $0x7e,%dl
je     1170 <strip_color+0x70>
test   %dl,%dl
je     11a0 <strip_color+0xa0>
mov    %dl,(%rcx)
add    $0x1,%rcx
add    $0x1,%rdi
jmp    1150 <strip_color+0x50>
nopl   0x0(%rax,%rax,1)
movzbl 0x1(%rdi),%edx
cmp    $0x21,%dl
je     1190 <strip_color+0x90>
test   %dl,%dl
je     11a0 <strip_color+0xa0>
add    $0x1,%rdi
add    $0x1,%rdi
jmp    1150 <strip_color+0x50>
nopw   0x0(%rax,%rax,1)
cmpb   $0x0,0x2(%rdi)
je     11a0 <strip_color+0xa0>
add    $0x2,%rdi
add    $0x1,%rdi
jmp    1150 <strip_color+0x50>
ret

LLVM IR (-O0):
@strip_color.out = internal global [5 x [2048 x i8]] zeroinitializer
@strip_color.i = internal global i32 0
define i8* @strip_color(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %2
store i8* null, i8** %3
%5 = load i32, i32* @strip_color.i
%6 = add nsw i32 %5, 1
store i32 %6, i32* @strip_color.i
%7 = load i32, i32* @strip_color.i
%8 = srem i32 %7, 5
store i32 %8, i32* @strip_color.i
%9 = load i32, i32* @strip_color.i
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds [5 x [2048 x i8]], [5 x [2048 x i8]]* @strip_color.out, i64 0, i64 %10
%12 = getelementptr inbounds [2048 x i8], [2048 x i8]* %11, i64 0, i64 0
store i8* %12, i8** %3
%13 = load i8*, i8** %2
store i8* %13, i8** %4
br label %14
14: ; preds = %52, %1
%15 = load i8*, i8** %4
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %55
19: ; preds = %14
%20 = load i8*, i8** %4
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 126
br i1 %23, label %24, label %29
24: ; preds = %19
%25 = load i8*, i8** %4
%26 = load i8, i8* %25
%27 = load i8*, i8** %3
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %3
store i8 %26, i8* %27
br label %52
29: ; preds = %19
%30 = load i8*, i8** %4
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %4
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %29
br label %55
36: ; preds = %29
%37 = load i8*, i8** %4
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 33
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = load i8*, i8** %4
%43 = getelementptr inbounds i8, i8* %42, i32 1
store i8* %43, i8** %4
br label %45
44: ; preds = %36
br label %52
45: ; preds = %41
%46 = load i8*, i8** %4
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %45
br label %55
51: ; preds = %45
br label %52
52: ; preds = %51, %44, %24
%53 = load i8*, i8** %4
%54 = getelementptr inbounds i8, i8* %53, i32 1
store i8* %54, i8** %4
br label %14
55: ; preds = %50, %35, %14
%56 = load i32, i32* @strip_color.i
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds [5 x [2048 x i8]], [5 x [2048 x i8]]* @strip_color.out, i64 0, i64 %57
%59 = getelementptr inbounds [2048 x i8], [2048 x i8]* %58, i64 0, i64 0
ret i8* %59
}

LLVM IR (-O2):
@strip_color.out = internal global [5 x [2048 x i8]] zeroinitializer
@strip_color.i = internal unnamed_addr global i32 0
define i8* @strip_color(i8* nocapture %0) {
%2 = load i32, i32* @strip_color.i
%3 = add nsw i32 %2, 1
%4 = srem i32 %3, 5
store i32 %4, i32* @strip_color.i
%5 = zext i32 %4 to i64
%6 = getelementptr inbounds [5 x [2048 x i8]], [5 x [2048 x i8]]* @strip_color.out, i64 0, i64 %5, i64 0
br label %7
7: ; preds = %20, %1
%8 = phi i8* [ %6, %1 ], [ %21, %20 ]
%9 = phi i8* [ %0, %1 ], [ %23, %20 ]
%10 = load i8, i8* %9
switch i8 %10, label %11 [
i8 0, label %24
i8 126, label %13
]
11: ; preds = %7
%12 = getelementptr inbounds i8, i8* %8, i64 1
store i8 %10, i8* %8
br label %20
13: ; preds = %7
%14 = getelementptr inbounds i8, i8* %9, i64 1
%15 = load i8, i8* %14
switch i8 %15, label %20 [
i8 0, label %24
i8 33, label %16
]
16: ; preds = %13
%17 = getelementptr inbounds i8, i8* %9, i64 2
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %13, %16, %11
%21 = phi i8* [ %12, %11 ], [ %8, %16 ], [ %8, %13 ]
%22 = phi i8* [ %9, %11 ], [ %17, %16 ], [ %14, %13 ]
%23 = getelementptr inbounds i8, i8* %22, i64 1
br label %7
24: ; preds = %16, %13, %7
ret i8* %6
}