#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 *, void *);
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int Q_ColorCharCount(const char *s, int byteofs) {
  char c;
  const char *end = s + byteofs;
  int charcount = 0;
  while (s < end) {
    int gc = Q_GrabCharFromColorString(&s, &c, ((void *)0));
    if (gc == 1)
      charcount++;
    else if (gc == 2)
      ;
    else if (gc == 0)
      break;
    else
      assert(0);
  }
  return charcount;
}


x86 assembly (clang, -O0):
<Q_ColorCharCount>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11b3 <Q_ColorCharCount+0x93>
lea    -0x8(%rbp),%rdi
lea    -0xd(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <Q_GrabCharFromColorString@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x1,-0x20(%rbp)
jne    117f <Q_ColorCharCount+0x5f>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ae <Q_ColorCharCount+0x8e>
cmpl   $0x2,-0x20(%rbp)
jne    118e <Q_ColorCharCount+0x6e>
jmp    11a9 <Q_ColorCharCount+0x89>
cmpl   $0x0,-0x20(%rbp)
jne    119d <Q_ColorCharCount+0x7d>
jmp    11b3 <Q_ColorCharCount+0x93>
xor    %edi,%edi
call   1040 <assert@plt>
jmp    11a9 <Q_ColorCharCount+0x89>
jmp    11ae <Q_ColorCharCount+0x8e>
jmp    1145 <Q_ColorCharCount+0x25>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Q_ColorCharCount>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,0x10(%rsp)
test   %esi,%esi
jle    117f <Q_ColorCharCount+0x5f>
mov    %rdi,%rbx
movslq %esi,%rax
add    %rax,%rbx
xor    %r14d,%r14d
lea    0x10(%rsp),%r15
lea    0xf(%rsp),%r12
jmp    115b <Q_ColorCharCount+0x3b>
nopl   0x0(%rax)
add    $0x1,%r14d
cmp    %rbx,0x10(%rsp)
jae    1182 <Q_ColorCharCount+0x62>
mov    %r15,%rdi
mov    %r12,%rsi
xor    %edx,%edx
call   1030 <Q_GrabCharFromColorString@plt>
cmp    $0x1,%eax
je     1150 <Q_ColorCharCount+0x30>
cmp    $0x2,%eax
je     1154 <Q_ColorCharCount+0x34>
test   %eax,%eax
je     1182 <Q_ColorCharCount+0x62>
xor    %edi,%edi
call   1040 <assert@plt>
jmp    1154 <Q_ColorCharCount+0x34>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i32 @Q_ColorCharCount(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8
store i8* %0, i8** %3
%5 = sext i32 %1 to i64
%6 = getelementptr inbounds i8, i8* %0, i64 %5
%7 = icmp sgt i32 %1, 0
br i1 %7, label %8, label %19
8: ; preds = %2, %15
%9 = phi i32 [ %16, %15 ], [ 0, %2 ]
%10 = call i32 @Q_GrabCharFromColorString(i8** %3, i8* %4, i8* null)
switch i32 %10, label %13 [
i32 1, label %11
i32 2, label %15
i32 0, label %19
]
11: ; preds = %8
%12 = add nsw i32 %9, 1
br label %15
13: ; preds = %8
%14 = call i32 @assert(i32 0)
br label %15
15: ; preds = %11, %13, %8
%16 = phi i32 [ %12, %11 ], [ %9, %13 ], [ %9, %8 ]
%17 = load i8*, i8** %3
%18 = icmp ult i8* %17, %6
br i1 %18, label %8, label %19
19: ; preds = %15, %8, %2
%20 = phi i32 [ 0, %2 ], [ %9, %8 ], [ %16, %15 ]
ret i32 %20
}