#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 Q_GrabCharFromColorString(char const **, char *, int *);
int /*<<< orphan*/ assert(int /*<<< orphan*/);
const char *Q_ColorStringTerminator(const char *str, int finalcolor) {
  char c;
  int lastcolor =
          (((('7') - '0') < 10) && ((('7') - '0') >= 0) ? (('7') - '0') : 7),
      colorindex;
  const char *s = str;
  while (1) {
    int gc = Q_GrabCharFromColorString(&s, &c, &colorindex);
    if (gc == 1)
      ;
    else if (gc == 2)
      lastcolor = colorindex;
    else if (gc == 0)
      break;
    else
      assert(0);
  }
  if (lastcolor == finalcolor)
    return "";
  else {
    int escapecount = 0;
    static char buf[4];
    char *p = buf;
    while (--s >= str)
      if (*s == '^')
        escapecount++;
      else
        break;
    if (escapecount & 1)
      *p++ = '^';
    *p++ = '^';
    *p++ = '0' + finalcolor;
    *p++ = '\0';
    return buf;
  }
}


x86 assembly (clang, -O0):
<Q_ColorStringTerminator>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x7,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    -0x28(%rbp),%rdi
lea    -0x15(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1030 <Q_GrabCharFromColorString@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x1,-0x2c(%rbp)
jne    1161 <Q_ColorStringTerminator+0x41>
jmp    1196 <Q_ColorStringTerminator+0x76>
cmpl   $0x2,-0x2c(%rbp)
jne    1176 <Q_ColorStringTerminator+0x56>
mov    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1191 <Q_ColorStringTerminator+0x71>
cmpl   $0x0,-0x2c(%rbp)
jne    1185 <Q_ColorStringTerminator+0x65>
jmp    119b <Q_ColorStringTerminator+0x7b>
xor    %edi,%edi
call   1040 <assert@plt>
jmp    1191 <Q_ColorStringTerminator+0x71>
jmp    1196 <Q_ColorStringTerminator+0x76>
jmp    113e <Q_ColorStringTerminator+0x1e>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    11b7 <Q_ColorStringTerminator+0x97>
lea    0xe52(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1270 <Q_ColorStringTerminator+0x150>
movl   $0x0,-0x30(%rbp)
lea    0x2e6c(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
cmp    -0x10(%rbp),%rax
jb     1207 <Q_ColorStringTerminator+0xe7>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5e,%eax
jne    11fd <Q_ColorStringTerminator+0xdd>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1202 <Q_ColorStringTerminator+0xe2>
jmp    1207 <Q_ColorStringTerminator+0xe7>
jmp    11c9 <Q_ColorStringTerminator+0xa9>
mov    -0x30(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1228 <Q_ColorStringTerminator+0x108>
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movb   $0x5e,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movb   $0x5e,(%rax)
mov    -0x14(%rbp),%eax
add    $0x30,%eax
mov    %al,%cl
mov    -0x38(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x38(%rbp)
mov    %cl,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movb   $0x0,(%rax)
lea    0x2dc5(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Q_ColorStringTerminator>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
mov    %rdi,0x8(%rsp)
mov    $0x7,%r13d
lea    0x8(%rsp),%r12
lea    0x7(%rsp),%rbx
lea    0x14(%rsp),%rbp
xchg   %ax,%ax
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <Q_GrabCharFromColorString@plt>
cmp    $0x1,%eax
je     1150 <Q_ColorStringTerminator+0x30>
test   %eax,%eax
je     117c <Q_ColorStringTerminator+0x5c>
cmp    $0x2,%eax
je     1175 <Q_ColorStringTerminator+0x55>
xor    %edi,%edi
call   1040 <assert@plt>
jmp    1150 <Q_ColorStringTerminator+0x30>
mov    0x14(%rsp),%r13d
jmp    1150 <Q_ColorStringTerminator+0x30>
cmp    %r14d,%r13d
jne    118a <Q_ColorStringTerminator+0x6a>
lea    0xe78(%rip),%rax
jmp    11eb <Q_ColorStringTerminator+0xcb>
mov    0x8(%rsp),%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,0x8(%rsp)
lea    0x2e92(%rip),%rax
mov    %rax,%rsi
cmp    %r15,%rcx
jb     11dc <Q_ColorStringTerminator+0xbc>
xor    %edx,%edx
nopl   0x0(%rax)
cmpb   $0x5e,(%rcx)
jne    11c6 <Q_ColorStringTerminator+0xa6>
add    $0x1,%edx
add    $0xffffffffffffffff,%rcx
mov    %rcx,0x8(%rsp)
cmp    %r15,%rcx
jae    11b0 <Q_ColorStringTerminator+0x90>
mov    %rax,%rsi
test   $0x1,%dl
je     11dc <Q_ColorStringTerminator+0xbc>
movb   $0x5e,0x2e5c(%rip)
lea    0x2e56(%rip),%rsi
movb   $0x5e,(%rsi)
add    $0x30,%r14b
mov    %r14b,0x1(%rsi)
movb   $0x0,0x2(%rsi)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@Q_ColorStringTerminator.buf = internal global [4 x i8] zeroinitializer
define i8* @Q_ColorStringTerminator(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 7, i32* %7
%13 = load i8*, i8** %4
store i8* %13, i8** %9
br label %14
14: ; preds = %2, %32
%15 = call i32 @Q_GrabCharFromColorString(i8** %9, i8* %6, i32* %8)
store i32 %15, i32* %10
%16 = load i32, i32* %10
%17 = icmp eq i32 %16, 1
br i1 %17, label %18, label %19
18: ; preds = %14
br label %32
19: ; preds = %14
%20 = load i32, i32* %10
%21 = icmp eq i32 %20, 2
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = load i32, i32* %8
store i32 %23, i32* %7
br label %31
24: ; preds = %19
%25 = load i32, i32* %10
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %24
br label %33
28: ; preds = %24
%29 = call i32 @assert(i32 0)
br label %30
30: ; preds = %28
br label %31
31: ; preds = %30, %22
br label %32
32: ; preds = %31, %18
br label %14
33: ; preds = %27
%34 = load i32, i32* %7
%35 = load i32, i32* %5
%36 = icmp eq i32 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %33
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %3
br label %71
38: ; preds = %33
store i32 0, i32* %11
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @Q_ColorStringTerminator.buf, i64 0, i64 0), i8** %12
br label %39
39: ; preds = %53, %38
%40 = load i8*, i8** %9
%41 = getelementptr inbounds i8, i8* %40, i32 -1
store i8* %41, i8** %9
%42 = load i8*, i8** %4
%43 = icmp uge i8* %41, %42
br i1 %43, label %44, label %54
44: ; preds = %39
%45 = load i8*, i8** %9
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp eq i32 %47, 94
br i1 %48, label %49, label %52
49: ; preds = %44
%50 = load i32, i32* %11
%51 = add nsw i32 %50, 1
store i32 %51, i32* %11
br label %53
52: ; preds = %44
br label %54
53: ; preds = %49
br label %39
54: ; preds = %52, %39
%55 = load i32, i32* %11
%56 = and i32 %55, 1
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %54
%59 = load i8*, i8** %12
%60 = getelementptr inbounds i8, i8* %59, i32 1
store i8* %60, i8** %12
store i8 94, i8* %59
br label %61
61: ; preds = %58, %54
%62 = load i8*, i8** %12
%63 = getelementptr inbounds i8, i8* %62, i32 1
store i8* %63, i8** %12
store i8 94, i8* %62
%64 = load i32, i32* %5
%65 = add nsw i32 48, %64
%66 = trunc i32 %65 to i8
%67 = load i8*, i8** %12
%68 = getelementptr inbounds i8, i8* %67, i32 1
store i8* %68, i8** %12
store i8 %66, i8* %67
%69 = load i8*, i8** %12
%70 = getelementptr inbounds i8, i8* %69, i32 1
store i8* %70, i8** %12
store i8 0, i8* %69
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @Q_ColorStringTerminator.buf, i64 0, i64 0), i8** %3
br label %71
71: ; preds = %61, %37
%72 = load i8*, i8** %3
ret i8* %72
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@Q_ColorStringTerminator.buf = internal global [4 x i8] zeroinitializer
define i8* @Q_ColorStringTerminator(i8* %0, i32 %1) {
%3 = alloca i8
%4 = alloca i32
%5 = alloca i8*
%6 = bitcast i32* %4 to i8*
%7 = bitcast i8** %5 to i8*
store i8* %0, i8** %5
br label %8
8: ; preds = %12, %2
%9 = phi i32 [ %13, %12 ], [ 7, %2 ]
br label %10
10: ; preds = %16, %8
%11 = call i32 @Q_GrabCharFromColorString(i8** %5, i8* %3, i32* %4)
switch i32 %11, label %14 [
i32 1, label %16
i32 2, label %12
i32 0, label %17
]
12: ; preds = %10
%13 = load i32, i32* %4
br label %8
14: ; preds = %10
%15 = call i32 @assert(i32 0)
br label %16
16: ; preds = %14, %10
br label %10
17: ; preds = %10
%18 = icmp eq i32 %9, %1
br i1 %18, label %43, label %19
19: ; preds = %17
%20 = load i8*, i8** %5
%21 = getelementptr inbounds i8, i8* %20, i64 -1
store i8* %21, i8** %5
%22 = icmp ult i8* %21, %0
br i1 %22, label %37, label %23
23: ; preds = %19, %28
%24 = phi i8* [ %30, %28 ], [ %21, %19 ]
%25 = phi i32 [ %29, %28 ], [ 0, %19 ]
%26 = load i8, i8* %24
%27 = icmp eq i8 %26, 94
br i1 %27, label %28, label %32
28: ; preds = %23
%29 = add nuw nsw i32 %25, 1
%30 = getelementptr inbounds i8, i8* %24, i64 -1
store i8* %30, i8** %5
%31 = icmp ult i8* %30, %0
br i1 %31, label %32, label %23
32: ; preds = %28, %23
%33 = phi i32 [ %29, %28 ], [ %25, %23 ]
%34 = and i32 %33, 1
%35 = icmp eq i32 %34, 0
br i1 %35, label %37, label %36
36: ; preds = %32
store i8 94, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @Q_ColorStringTerminator.buf, i64 0, i64 0)
br label %37
37: ; preds = %19, %36, %32
%38 = phi i8* [ getelementptr inbounds ([4 x i8], [4 x i8]* @Q_ColorStringTerminator.buf, i64 0, i64 1), %36 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @Q_ColorStringTerminator.buf, i64 0, i64 0), %32 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @Q_ColorStringTerminator.buf, i64 0, i64 0), %19 ]
%39 = getelementptr inbounds i8, i8* %38, i64 1
store i8 94, i8* %38
%40 = trunc i32 %1 to i8
%41 = add i8 %40, 48
%42 = getelementptr inbounds i8, i8* %38, i64 2
store i8 %41, i8* %39
store i8 0, i8* %42
br label %43
43: ; preds = %17, %37
%44 = phi i8* [ getelementptr inbounds ([4 x i8], [4 x i8]* @Q_ColorStringTerminator.buf, i64 0, i64 0), %37 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %17 ]
ret i8* %44
}