#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 */
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ Q_strncpyz(char *, char *, int);
int /*<<< orphan*/ Scr_AddString(char *);
int /*<<< orphan*/ Scr_Error(char *);
int Scr_GetNumParam();
char *Scr_GetString(int /*<<< orphan*/);
void GScr_StripColors() {
  char buffer[2048];
  if (Scr_GetNumParam() != 1) {
    Scr_Error("Usage: StrColorStrip(<string>)\n");
  }
  char *string = Scr_GetString(0);
  int i;
  Q_strncpyz(buffer, string, sizeof(buffer));
  for (i = 1; buffer[i]; i++) {
    if (buffer[i - 1] == '^' && buffer[i] >= '0' && buffer[i] <= '9') {
      buffer[i] = '^';
    }
  }
  Scr_AddString(buffer);
}


x86 assembly (clang, -O0):
<GScr_StripColors>:
push   %rbp
mov    %rsp,%rbp
sub    $0x810,%rsp
mov    $0x0,%al
call   1030 <Scr_GetNumParam@plt>
cmp    $0x1,%eax
je     1177 <GScr_StripColors+0x27>
lea    0xe8e(%rip),%rdi
call   1050 <Scr_Error@plt>
xor    %edi,%edi
call   1040 <Scr_GetString@plt>
mov    %rax,-0x808(%rbp)
lea    -0x800(%rbp),%rdi
mov    -0x808(%rbp),%rsi
mov    $0x800,%edx
call   1070 <Q_strncpyz@plt>
movl   $0x1,-0x80c(%rbp)
movslq -0x80c(%rbp),%rax
cmpb   $0x0,-0x800(%rbp,%rax,1)
je     1230 <GScr_StripColors+0xe0>
mov    -0x80c(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x800(%rbp,%rax,1),%eax
ff
cmp    $0x5e,%eax
jne    1217 <GScr_StripColors+0xc7>
movslq -0x80c(%rbp),%rax
movsbl -0x800(%rbp,%rax,1),%eax
ff
cmp    $0x30,%eax
jl     1217 <GScr_StripColors+0xc7>
movslq -0x80c(%rbp),%rax
movsbl -0x800(%rbp,%rax,1),%eax
ff
cmp    $0x39,%eax
jg     1217 <GScr_StripColors+0xc7>
movslq -0x80c(%rbp),%rax
movb   $0x5e,-0x800(%rbp,%rax,1)
5e
jmp    121c <GScr_StripColors+0xcc>
mov    -0x80c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x80c(%rbp)
jmp    11a7 <GScr_StripColors+0x57>
lea    -0x800(%rbp),%rdi
call   1060 <Scr_AddString@plt>
add    $0x810,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GScr_StripColors>:
sub    $0x808,%rsp
xor    %eax,%eax
call   1030 <Scr_GetNumParam@plt>
cmp    $0x1,%eax
je     116f <GScr_StripColors+0x1f>
lea    0xe96(%rip),%rdi
call   1050 <Scr_Error@plt>
xor    %edi,%edi
call   1040 <Scr_GetString@plt>
mov    %rsp,%rdi
mov    %rax,%rsi
mov    $0x800,%edx
call   1070 <Q_strncpyz@plt>
mov    0x1(%rsp),%cl
test   %cl,%cl
je     11c8 <GScr_StripColors+0x78>
lea    0x1(%rsp),%rdx
mov    $0x2,%eax
jmp    11b2 <GScr_StripColors+0x62>
nopw   0x0(%rax,%rax,1)
mov    %eax,%ecx
lea    (%rsp,%rcx,1),%rdx
movzbl (%rsp,%rcx,1),%ecx
add    $0x1,%rax
test   %cl,%cl
je     11c8 <GScr_StripColors+0x78>
cmpb   $0x5e,-0x2(%rsp,%rax,1)
jne    11a0 <GScr_StripColors+0x50>
cmp    $0x30,%cl
jl     11a0 <GScr_StripColors+0x50>
cmp    $0x39,%cl
jg     11a0 <GScr_StripColors+0x50>
movb   $0x5e,(%rdx)
jmp    11a0 <GScr_StripColors+0x50>
mov    %rsp,%rdi
call   1060 <Scr_AddString@plt>
add    $0x808,%rsp
ret

LLVM IR (-O0):
@.str = [32 x i8] c"Usage: StrColorStrip(<string>)\0A\00"
define void @GScr_StripColors() {
%1 = alloca [2048 x i8]
%2 = alloca i8*
%3 = alloca i32
%4 = call i32 (...) @Scr_GetNumParam()
%5 = icmp ne i32 %4, 1
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @Scr_Error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
%9 = call i8* @Scr_GetString(i32 0)
store i8* %9, i8** %2
%10 = getelementptr inbounds [2048 x i8], [2048 x i8]* %1, i64 0, i64 0
%11 = load i8*, i8** %2
%12 = call i32 @Q_strncpyz(i8* %10, i8* %11, i32 2048)
store i32 1, i32* %3
br label %13
13: ; preds = %46, %8
%14 = load i32, i32* %3
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [2048 x i8], [2048 x i8]* %1, i64 0, i64 %15
%17 = load i8, i8* %16
%18 = icmp ne i8 %17, 0
br i1 %18, label %19, label %49
19: ; preds = %13
%20 = load i32, i32* %3
%21 = sub nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [2048 x i8], [2048 x i8]* %1, i64 0, i64 %22
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 94
br i1 %26, label %27, label %45
27: ; preds = %19
%28 = load i32, i32* %3
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [2048 x i8], [2048 x i8]* %1, i64 0, i64 %29
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp sge i32 %32, 48
br i1 %33, label %34, label %45
34: ; preds = %27
%35 = load i32, i32* %3
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [2048 x i8], [2048 x i8]* %1, i64 0, i64 %36
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp sle i32 %39, 57
br i1 %40, label %41, label %45
41: ; preds = %34
%42 = load i32, i32* %3
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [2048 x i8], [2048 x i8]* %1, i64 0, i64 %43
store i8 94, i8* %44
br label %45
45: ; preds = %41, %34, %27, %19
br label %46
46: ; preds = %45
%47 = load i32, i32* %3
%48 = add nsw i32 %47, 1
store i32 %48, i32* %3
br label %13
49: ; preds = %13
%50 = getelementptr inbounds [2048 x i8], [2048 x i8]* %1, i64 0, i64 0
%51 = call i32 @Scr_AddString(i8* %50)
ret void
}

LLVM IR (-O2):
@.str = [32 x i8] c"Usage: StrColorStrip(<string>)\0A\00"
define void @GScr_StripColors() {
%1 = alloca [2048 x i8]
%2 = getelementptr inbounds [2048 x i8], [2048 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @Scr_GetNumParam()
%4 = icmp eq i32 %3, 1
br i1 %4, label %7, label %5
5: ; preds = %0
%6 = tail call i32 @Scr_Error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %0
%8 = tail call i8* @Scr_GetString(i32 0)
%9 = call i32 @Q_strncpyz(i8* %2, i8* %8, i32 2048)
%10 = getelementptr inbounds [2048 x i8], [2048 x i8]* %1, i64 0, i64 1
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 0
br i1 %12, label %32, label %13
13: ; preds = %7, %26
%14 = phi i64 [ %27, %26 ], [ 1, %7 ]
%15 = phi i8 [ %30, %26 ], [ %11, %7 ]
%16 = phi i8* [ %29, %26 ], [ %10, %7 ]
%17 = add nsw i64 %14, -1
%18 = getelementptr inbounds [2048 x i8], [2048 x i8]* %1, i64 0, i64 %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 94
%21 = icmp sgt i8 %15, 47
%22 = select i1 %20, i1 %21, i1 false
%23 = icmp slt i8 %15, 58
%24 = select i1 %22, i1 %23, i1 false
br i1 %24, label %25, label %26
25: ; preds = %13
store i8 94, i8* %16
br label %26
26: ; preds = %13, %25
%27 = add nuw i64 %14, 1
%28 = and i64 %27, 4294967295
%29 = getelementptr inbounds [2048 x i8], [2048 x i8]* %1, i64 0, i64 %28
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 0
br i1 %31, label %32, label %13
32: ; preds = %26, %7
%33 = call i32 @Scr_AddString(i8* %2)
ret void
}