#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*/ FILE;

/* Variables and functions */
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ output_css_class_for(char *, int /*<<< orphan*/ *);
scalar_t__ strcmp(char *, char *);
char *strtok(char *, char *);
int /*<<< orphan*/ warn(char *, char *);
void parse_colour_code(FILE *in, FILE *out) {
  char command_sequence[12 + 1];
  int i = 0;
  int c = 0;
  char *tok = NULL;
  memset(command_sequence, 0, 12 + 1);
  while (i < 12 && c != 'm') {
    c = fgetc(in);
    command_sequence[i++] = c;
  }
  if (c != 'm') {
    warn("parse_colour_code: ignoring unfinished command sequence: %s\n",
         command_sequence);
  } else {
    if (strcmp(command_sequence, "m") == 0) {
      fputs("</span>", out);
    } else {
      command_sequence[i - 1] = 0;
      fputs("<span class=\"", out);
      for ((tok = strtok(command_sequence, ";")); tok;
           tok = strtok(NULL, ";")) {
        output_css_class_for(tok, out);
      }
      fputs("\">", out);
    }
  }
}


x86 assembly (clang, -O0):
<parse_colour_code>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x1d(%rbp),%rdi
xor    %esi,%esi
mov    $0xd,%edx
call   1050 <memset@plt>
xor    %eax,%eax
cmpl   $0xc,-0x24(%rbp)
mov    %al,-0x31(%rbp)
jge    11bf <parse_colour_code+0x4f>
cmpl   $0x6d,-0x28(%rbp)
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    11cf <parse_colour_code+0x5f>
jmp    11f6 <parse_colour_code+0x86>
mov    -0x8(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %al,%cl
mov    -0x24(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x24(%rbp)
cltq
mov    %cl,-0x1d(%rbp,%rax,1)
jmp    11a6 <parse_colour_code+0x36>
cmpl   $0x6d,-0x28(%rbp)
je     1215 <parse_colour_code+0xa5>
lea    -0x1d(%rbp),%rsi
lea    0xdf5(%rip),%rdi
call   1030 <warn@plt>
jmp    12bb <parse_colour_code+0x14b>
lea    -0x1d(%rbp),%rdi
lea    0xe1d(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1244 <parse_colour_code+0xd4>
mov    -0x10(%rbp),%rsi
lea    0xe05(%rip),%rdi
call   1040 <fputs@plt>
jmp    12b6 <parse_colour_code+0x146>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x1d(%rbp,%rax,1)
mov    -0x10(%rbp),%rsi
lea    0xdeb(%rip),%rdi
call   1040 <fputs@plt>
lea    -0x1d(%rbp),%rdi
lea    0xde9(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12a6 <parse_colour_code+0x136>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <output_css_class_for@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xdbd(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x30(%rbp)
jmp    1275 <parse_colour_code+0x105>
mov    -0x10(%rbp),%rsi
lea    0xda6(%rip),%rdi
call   1040 <fputs@plt>
jmp    12bb <parse_colour_code+0x14b>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_colour_code>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xb(%rsp),%rdi
xor    %r12d,%r12d
xor    %esi,%esi
mov    $0xd,%edx
call   1040 <memset@plt>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rbx
mov    %r15,%rdi
call   1050 <fgetc@plt>
add    $0x1,%r12
mov    %al,0xb(%rsp,%rbx,1)
cmp    $0xa,%rbx
ja     11be <parse_colour_code+0x4e>
cmp    $0x6d,%eax
jne    11a0 <parse_colour_code+0x30>
cmp    $0x6d,%eax
je     11d9 <parse_colour_code+0x69>
lea    0xe36(%rip),%rdi
lea    0xb(%rsp),%rsi
call   1030 <warn@plt>
jmp    1271 <parse_colour_code+0x101>
lea    0xe5d(%rip),%rsi
lea    0xb(%rsp),%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1258 <parse_colour_code+0xe8>
movb   $0x0,0xa(%rsp,%r12,1)
lea    0xe4b(%rip),%rdi
mov    $0xd,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1090 <fwrite@plt>
lea    0xe40(%rip),%rsi
lea    0xb(%rsp),%rdi
call   1080 <strtok@plt>
test   %rax,%rax
je     124a <parse_colour_code+0xda>
lea    0xe2a(%rip),%rbx
nopl   0x0(%rax,%rax,1)
mov    %rax,%rdi
mov    %r14,%rsi
call   1070 <output_css_class_for@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1080 <strtok@plt>
test   %rax,%rax
jne    1230 <parse_colour_code+0xc0>
lea    0xe06(%rip),%rdi
mov    $0x2,%esi
jmp    1264 <parse_colour_code+0xf4>
lea    0xde0(%rip),%rdi
mov    $0x7,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1090 <fwrite@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [61 x i8] c"parse_colour_code: ignoring unfinished command sequence: %s\0A\00"
@.str.1 = [2 x i8] c"m\00"
@.str.2 = [8 x i8] c"</span>\00"
@.str.3 = [14 x i8] c"<span class=\22\00"
@.str.4 = [2 x i8] c";\00"
@.str.5 = [3 x i8] c"\22>\00"
define void @parse_colour_code(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca [13 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i32* %0, i32** %3
store i32* %1, i32** %4
store i32 0, i32* %6
store i32 0, i32* %7
store i8* null, i8** %8
%9 = getelementptr inbounds [13 x i8], [13 x i8]* %5, i64 0, i64 0
%10 = call i32 @memset(i8* %9, i32 0, i32 13)
br label %11
11: ; preds = %19, %2
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 12
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* %7
%16 = icmp ne i32 %15, 109
br label %17
17: ; preds = %14, %11
%18 = phi i1 [ false, %11 ], [ %16, %14 ]
br i1 %18, label %19, label %28
19: ; preds = %17
%20 = load i32*, i32** %3
%21 = call i32 @fgetc(i32* %20)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = trunc i32 %22 to i8
%24 = load i32, i32* %6
%25 = add nsw i32 %24, 1
store i32 %25, i32* %6
%26 = sext i32 %24 to i64
%27 = getelementptr inbounds [13 x i8], [13 x i8]* %5, i64 0, i64 %26
store i8 %23, i8* %27
br label %11
28: ; preds = %17
%29 = load i32, i32* %7
%30 = icmp ne i32 %29, 109
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = getelementptr inbounds [13 x i8], [13 x i8]* %5, i64 0, i64 0
%33 = call i32 @warn(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0), i8* %32)
br label %63
34: ; preds = %28
%35 = getelementptr inbounds [13 x i8], [13 x i8]* %5, i64 0, i64 0
%36 = call i64 @strcmp(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = load i32*, i32** %4
%40 = call i32 @fputs(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32* %39)
br label %62
41: ; preds = %34
%42 = load i32, i32* %6
%43 = sub nsw i32 %42, 1
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [13 x i8], [13 x i8]* %5, i64 0, i64 %44
store i8 0, i8* %45
%46 = load i32*, i32** %4
%47 = call i32 @fputs(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32* %46)
%48 = getelementptr inbounds [13 x i8], [13 x i8]* %5, i64 0, i64 0
%49 = call i8* @strtok(i8* %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i8* %49, i8** %8
br label %50
50: ; preds = %57, %41
%51 = load i8*, i8** %8
%52 = icmp ne i8* %51, null
br i1 %52, label %53, label %59
53: ; preds = %50
%54 = load i8*, i8** %8
%55 = load i32*, i32** %4
%56 = call i32 @output_css_class_for(i8* %54, i32* %55)
br label %57
57: ; preds = %53
%58 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i8* %58, i8** %8
br label %50
59: ; preds = %50
%60 = load i32*, i32** %4
%61 = call i32 @fputs(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %60)
br label %62
62: ; preds = %59, %38
br label %63
63: ; preds = %62, %31
ret void
}

LLVM IR (-O2):
@.str = [61 x i8] c"parse_colour_code: ignoring unfinished command sequence: %s\0A\00"
@.str.1 = [2 x i8] c"m\00"
@.str.2 = [8 x i8] c"</span>\00"
@.str.3 = [14 x i8] c"<span class=\22\00"
@.str.4 = [2 x i8] c";\00"
@.str.5 = [3 x i8] c"\22>\00"
define void @parse_colour_code(i32* nocapture %0, i32* %1) {
%3 = alloca [13 x i8]
%4 = getelementptr inbounds [13 x i8], [13 x i8]* %3, i64 0, i64 0
%5 = call i32 @memset(i8* %4, i32 0, i32 13)
br label %6
6: ; preds = %2, %6
%7 = phi i64 [ 0, %2 ], [ %10, %6 ]
%8 = call i32 @fgetc(i32* %0)
%9 = trunc i32 %8 to i8
%10 = add nuw nsw i64 %7, 1
%11 = getelementptr inbounds [13 x i8], [13 x i8]* %3, i64 0, i64 %7
store i8 %9, i8* %11
%12 = icmp ult i64 %7, 11
%13 = icmp ne i32 %8, 109
%14 = and i1 %12, %13
br i1 %14, label %6, label %15
15: ; preds = %6
br i1 %13, label %16, label %18
16: ; preds = %15
%17 = call i32 @warn(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0), i8* %4)
br label %35
18: ; preds = %15
%19 = call i64 @strcmp(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i64 @fwrite(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i64 7, i64 1, i32* %1)
br label %35
23: ; preds = %18
%24 = getelementptr inbounds [13 x i8], [13 x i8]* %3, i64 0, i64 %7
store i8 0, i8* %24
%25 = call i64 @fwrite(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i64 13, i64 1, i32* %1)
%26 = call i8* @strtok(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%27 = icmp eq i8* %26, null
br i1 %27, label %33, label %28
28: ; preds = %23, %28
%29 = phi i8* [ %31, %28 ], [ %26, %23 ]
%30 = call i32 @output_css_class_for(i8* %29, i32* %1)
%31 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %28
33: ; preds = %28, %23
%34 = call i64 @fwrite(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i64 2, i64 1, i32* %1)
br label %35
35: ; preds = %21, %33, %16
ret void
}