#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 */
char *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
char *malloc(int);
int random();
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
scalar_t__ strcmp(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncmp(char *, char *, int);
int main() {
  int inPage = 0;
  int inGoodMood = 0;
  int page_count = 0;
  char *buffer;
  char *r;
  int template_title_len = strlen("    <title>Template:");
  int title_len = strlen("    <title>");
  buffer = malloc((5 * 1024 * 1024) * sizeof(*buffer));
  while (1) {
    r = fgets(buffer, (5 * 1024 * 1024), stdin);
    if (r == NULL)
      return 0;
    if (!strncmp(buffer, "    <title>Template:", template_title_len)) {
      inPage = 1;
      inGoodMood = 1;
      fputs("  <page>\n", stdout);
    } else {
      if (!strncmp(buffer, "    <title>", title_len)) {
        inPage = 1;
        if (5000 == -1 || page_count < 5000) {
          inGoodMood = ((random() % 500) == 1);
        } else {
          inGoodMood = 0;
        }
        if (inGoodMood) {
          page_count++;
          fputs("  <page>\n", stdout);
        }
      }
    }
    if ((inPage && inGoodMood) || (!inPage)) {
      if (strcmp(buffer, "  <page>\n")) {
        fputs(buffer, stdout);
      }
    }
    if (!strcmp(buffer, "  </page>\n")) {
      inPage = 0;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
lea    0xe65(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
lea    0xe6b(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    $0x500000,%edi
call   1090 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e1d(%rip),%rax
mov    (%rax),%edx
mov    $0x500000,%esi
call   1060 <fgets@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ee <main+0x7e>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%edx
lea    0xe04(%rip),%rsi
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1232 <main+0xc2>
movl   $0x1,-0x8(%rbp)
movl   $0x1,-0xc(%rbp)
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
lea    0xdf9(%rip),%rdi
call   1050 <fputs@plt>
jmp    12bd <main+0x14d>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%edx
lea    0xdd5(%rip),%rsi
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    12b8 <main+0x148>
movl   $0x1,-0x8(%rbp)
cmpl   $0x1388,-0x10(%rbp)
jge    1284 <main+0x114>
mov    $0x0,%al
call   1080 <random@plt>
mov    $0x1f4,%ecx
cltd
idiv   %ecx
cmp    $0x1,%edx
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0xc(%rbp)
jmp    128b <main+0x11b>
movl   $0x0,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     12b3 <main+0x143>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    0x2d33(%rip),%rax
mov    (%rax),%esi
lea    0xd73(%rip),%rdi
call   1050 <fputs@plt>
jmp    12b8 <main+0x148>
jmp    12bd <main+0x14d>
cmpl   $0x0,-0x8(%rbp)
je     12d1 <main+0x161>
cmpl   $0x0,-0xc(%rbp)
jne    12db <main+0x16b>
cmpl   $0x0,-0x8(%rbp)
jne    130c <main+0x19c>
mov    -0x18(%rbp),%rdi
lea    0xd3b(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1307 <main+0x197>
mov    -0x18(%rbp),%rdi
mov    0x2cd8(%rip),%rax
mov    (%rax),%esi
call   1050 <fputs@plt>
jmp    130c <main+0x19c>
mov    -0x18(%rbp),%rdi
lea    0xd14(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    132d <main+0x1bd>
movl   $0x0,-0x8(%rbp)
jmp    11c0 <main+0x50>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe7e(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
lea    0xe85(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%r14d
mov    $0x500000,%edi
call   1090 <malloc@plt>
mov    %rax,%rbp
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
mov    $0x500000,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     12f8 <main+0x188>
lea    0xe60(%rip),%r13
xor    %r12d,%r12d
xor    %r15d,%r15d
movl   $0x0,0x4(%rsp)
jmp    1216 <main+0xa6>
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %r13,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
mov    $0x0,%eax
cmove  %eax,%r12d
mov    0x2dea(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
mov    $0x500000,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     12f8 <main+0x188>
mov    %rbp,%rdi
lea    0xde0(%rip),%rsi
mov    %ebx,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
je     1290 <main+0x120>
mov    %rbp,%rdi
lea    0xde0(%rip),%rsi
mov    %r14d,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    12b1 <main+0x141>
mov    $0x1,%r12d
xor    %r15d,%r15d
cmpl   $0x1387,0x4(%rsp)
jg     12b1 <main+0x141>
xor    %r15d,%r15d
xor    %eax,%eax
call   1080 <random@plt>
cltq
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x25,%rcx
add    %edx,%ecx
imul   $0x1f4,%ecx,%ecx
sub    %ecx,%eax
cmp    $0x1,%eax
jne    12b1 <main+0x141>
addl   $0x1,0x4(%rsp)
cs nopw 0x0(%rax,%rax,1)
mov    0x2d41(%rip),%rax
mov    (%rax),%esi
lea    0xd81(%rip),%rdi
call   1050 <fputs@plt>
mov    $0x1,%r15d
mov    $0x1,%r12d
test   %r12d,%r12d
sete   %al
test   %r15d,%r15d
sete   %cl
test   %r12d,%r12d
je     12ca <main+0x15a>
or     %cl,%al
jne    11e0 <main+0x70>
mov    %rbp,%rdi
lea    0xd4d(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     11e0 <main+0x70>
mov    0x2cef(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1050 <fputs@plt>
jmp    11e0 <main+0x70>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c" <title>Template:\00"
@.str.1 = [12 x i8] c" <title>\00"
@stdin = global i32 0
@.str.2 = [10 x i8] c" <page>\0A\00"
@stdout = global i32 0
@.str.3 = [11 x i8] c" </page>\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
store i32 0, i32* %3
store i32 0, i32* %4
%9 = call i32 @strlen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 %9, i32* %7
%10 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 %10, i32* %8
%11 = call i8* @malloc(i32 5242880)
store i8* %11, i8** %5
br label %12
12: ; preds = %0, %74
%13 = load i8*, i8** %5
%14 = load i32, i32* @stdin
%15 = call i8* @fgets(i8* %13, i32 5242880, i32 %14)
store i8* %15, i8** %6
%16 = load i8*, i8** %6
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %19
18: ; preds = %12
ret i32 0
19: ; preds = %12
%20 = load i8*, i8** %5
%21 = load i32, i32* %7
%22 = call i32 @strncmp(i8* %20, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %19
store i32 1, i32* %2
store i32 1, i32* %3
%25 = load i32, i32* @stdout
%26 = call i32 @fputs(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %25)
br label %51
27: ; preds = %19
%28 = load i8*, i8** %5
%29 = load i32, i32* %8
%30 = call i32 @strncmp(i8* %28, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %50, label %32
32: ; preds = %27
store i32 1, i32* %2
%33 = load i32, i32* %4
%34 = icmp slt i32 %33, 5000
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = call i32 (...) @random()
%37 = srem i32 %36, 500
%38 = icmp eq i32 %37, 1
%39 = zext i1 %38 to i32
store i32 %39, i32* %3
br label %41
40: ; preds = %32
store i32 0, i32* %3
br label %41
41: ; preds = %40, %35
%42 = load i32, i32* %3
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %41
%45 = load i32, i32* %4
%46 = add nsw i32 %45, 1
store i32 %46, i32* %4
%47 = load i32, i32* @stdout
%48 = call i32 @fputs(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %47)
br label %49
49: ; preds = %44, %41
br label %50
50: ; preds = %49, %27
br label %51
51: ; preds = %50, %24
%52 = load i32, i32* %2
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %51
%55 = load i32, i32* %3
%56 = icmp ne i32 %55, 0
br i1 %56, label %60, label %57
57: ; preds = %54, %51
%58 = load i32, i32* %2
%59 = icmp ne i32 %58, 0
br i1 %59, label %69, label %60
60: ; preds = %57, %54
%61 = load i8*, i8** %5
%62 = call i64 @strcmp(i8* %61, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %68
64: ; preds = %60
%65 = load i8*, i8** %5
%66 = load i32, i32* @stdout
%67 = call i32 @fputs(i8* %65, i32 %66)
br label %68
68: ; preds = %64, %60
br label %69
69: ; preds = %68, %57
%70 = load i8*, i8** %5
%71 = call i64 @strcmp(i8* %70, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%72 = icmp ne i64 %71, 0
br i1 %72, label %74, label %73
73: ; preds = %69
store i32 0, i32* %2
br label %74
74: ; preds = %73, %69
br label %12
}

LLVM IR (-O2):
@.str = [21 x i8] c" <title>Template:\00"
@.str.1 = [12 x i8] c" <title>\00"
@stdin = global i32 0
@.str.2 = [10 x i8] c" <page>\0A\00"
@stdout = global i32 0
@.str.3 = [11 x i8] c" </page>\0A\00"
define i32 @main() {
%1 = tail call i32 @strlen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%2 = tail call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%3 = tail call dereferenceable_or_null(5242880) i8* @malloc(i32 5242880)
%4 = load i32, i32* @stdin
%5 = tail call i8* @fgets(i8* %3, i32 5242880, i32 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %8
7: ; preds = %44, %0
ret i32 0
8: ; preds = %0, %44
%9 = phi i32 [ %47, %44 ], [ 0, %0 ]
%10 = phi i32 [ %31, %44 ], [ 0, %0 ]
%11 = phi i32 [ %30, %44 ], [ 0, %0 ]
%12 = tail call i32 @strncmp(i8* %3, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %1)
%13 = icmp eq i32 %12, 0
br i1 %13, label %25, label %14
14: ; preds = %8
%15 = tail call i32 @strncmp(i8* %3, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %2)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %29
17: ; preds = %14
%18 = icmp slt i32 %11, 5000
br i1 %18, label %19, label %29
19: ; preds = %17
%20 = tail call i32 (...) @random()
%21 = srem i32 %20, 500
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %29
23: ; preds = %19
%24 = add nsw i32 %11, 1
br label %25
25: ; preds = %8, %23
%26 = phi i32 [ %24, %23 ], [ %11, %8 ]
%27 = load i32, i32* @stdout
%28 = tail call i32 @fputs(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %25, %17, %14, %19
%30 = phi i32 [ %11, %14 ], [ %11, %19 ], [ %11, %17 ], [ %26, %25 ]
%31 = phi i32 [ %10, %14 ], [ 0, %19 ], [ 0, %17 ], [ 1, %25 ]
%32 = phi i32 [ %9, %14 ], [ 1, %19 ], [ 1, %17 ], [ 1, %25 ]
%33 = icmp ne i32 %32, 0
%34 = icmp eq i32 %31, 0
%35 = xor i1 %33, true
%36 = select i1 %35, i1 true, i1 %34
%37 = and i1 %33, %36
br i1 %37, label %44, label %38
38: ; preds = %29
%39 = tail call i64 @strcmp(i8* %3, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%40 = icmp eq i64 %39, 0
br i1 %40, label %44, label %41
41: ; preds = %38
%42 = load i32, i32* @stdout
%43 = tail call i32 @fputs(i8* %3, i32 %42)
br label %44
44: ; preds = %38, %41, %29
%45 = tail call i64 @strcmp(i8* %3, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%46 = icmp eq i64 %45, 0
%47 = select i1 %46, i32 0, i32 %32
%48 = load i32, i32* @stdin
%49 = tail call i8* @fgets(i8* %3, i32 5242880, i32 %48)
%50 = icmp eq i8* %49, null
br i1 %50, label %7, label %8
}