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

/* Variables and functions */
char *format_ansi(char);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ putchar(char);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stderr;
int main() {
  char buf[4096];
  int eaten = 0;
  int len;
  while ((len = read(0, buf, sizeof(buf))) > 0) {
    int begpos = 0;
    int pos;
    if (eaten) {
      printf("%s", format_ansi(buf[0]));
      begpos = 1;
      eaten = 0;
    }
    while (len > begpos && buf[len - 1] == '%') {
      eaten++;
      len--;
    }
    if (!(eaten % 2)) {
      len += eaten;
      eaten = 0;
    } else {
      len += (eaten - 1);
      eaten = 1;
    }
    pos = begpos;
    while (pos < len) {
      if (buf[pos] == '%') {
        pos++;
        printf("%s", format_ansi(buf[pos]));
        pos++;
      } else {
        putchar(buf[pos]);
        pos++;
      }
    }
  }
  printf("\n");
  if (eaten)
    fprintf(stderr, "BAD LOG-FILE? ended with: '%%'\n");
  return eaten;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x1014(%rbp)
lea    -0x1010(%rbp),%rsi
xor    %edi,%edi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x1018(%rbp)
cmp    $0x0,%eax
jle    134c <main+0x1fc>
movl   $0x0,-0x101c(%rbp)
cmpl   $0x0,-0x1014(%rbp)
je     11d6 <main+0x86>
movsbl -0x1010(%rbp),%edi
call   1070 <format_ansi@plt>
mov    %rax,%rsi
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x101c(%rbp)
movl   $0x0,-0x1014(%rbp)
jmp    11db <main+0x8b>
mov    -0x1018(%rbp),%ecx
xor    %eax,%eax
cmp    -0x101c(%rbp),%ecx
mov    %al,-0x1021(%rbp)
jle    1214 <main+0xc4>
mov    -0x1018(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x1010(%rbp,%rax,1),%eax
ff
cmp    $0x25,%eax
sete   %al
mov    %al,-0x1021(%rbp)
mov    -0x1021(%rbp),%al
test   $0x1,%al
jne    1227 <main+0xd7>
jmp    124a <main+0xfa>
mov    -0x1014(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1014(%rbp)
mov    -0x1018(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1018(%rbp)
jmp    11db <main+0x8b>
mov    -0x1014(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1282 <main+0x132>
mov    -0x1014(%rbp),%eax
add    -0x1018(%rbp),%eax
mov    %eax,-0x1018(%rbp)
movl   $0x0,-0x1014(%rbp)
jmp    12a1 <main+0x151>
mov    -0x1014(%rbp),%eax
sub    $0x1,%eax
add    -0x1018(%rbp),%eax
mov    %eax,-0x1018(%rbp)
movl   $0x1,-0x1014(%rbp)
mov    -0x101c(%rbp),%eax
mov    %eax,-0x1020(%rbp)
mov    -0x1020(%rbp),%eax
cmp    -0x1018(%rbp),%eax
jge    1347 <main+0x1f7>
movslq -0x1020(%rbp),%rax
movsbl -0x1010(%rbp,%rax,1),%eax
ff
cmp    $0x25,%eax
jne    131f <main+0x1cf>
mov    -0x1020(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1020(%rbp)
movslq -0x1020(%rbp),%rax
movsbl -0x1010(%rbp,%rax,1),%edi
ff
call   1070 <format_ansi@plt>
mov    %rax,%rsi
lea    0xcfc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1020(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1020(%rbp)
jmp    1342 <main+0x1f2>
movslq -0x1020(%rbp),%rax
movsbl -0x1010(%rbp,%rax,1),%edi
ff
call   1030 <putchar@plt>
mov    -0x1020(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1020(%rbp)
jmp    12ad <main+0x15d>
jmp    116c <main+0x1c>
lea    0xcce(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x1014(%rbp)
je     137c <main+0x22c>
mov    0x2c72(%rip),%rax
mov    (%rax),%edi
lea    0xc8c(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x1014(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
xor    %r14d,%r14d
mov    %rsp,%rsi
xor    %edi,%edi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    12ba <main+0x16a>
mov    %eax,%r13d
lea    0xe7b(%rip),%r15
mov    %rsp,%r14
xor    %r12d,%r12d
jmp    11aa <main+0x5a>
nopl   (%rax)
xor    %edi,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,%r13d
test   %eax,%eax
jle    128e <main+0x13e>
mov    $0x0,%ebp
test   %r12d,%r12d
je     11cf <main+0x7f>
movsbl (%rsp),%edi
call   1070 <format_ansi@plt>
mov    %r15,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%ebp
mov    %r13d,%r12d
sub    %ebp,%r12d
jbe    1220 <main+0xd0>
mov    %r13d,%r13d
mov    %ebp,%ecx
mov    %rcx,%rax
shl    $0x20,%rax
sub    %rcx,%rax
add    %r13,%rax
mov    $0xffffffffffffffff,%rdx
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    (%rdx,%r13,1),%esi
cmpb   $0x25,(%rsp,%rsi,1)
jne    1230 <main+0xe0>
add    $0x1,%rcx
add    $0xffffffffffffffff,%rdx
cmp    %ecx,%eax
jne    1200 <main+0xb0>
mov    %ebp,%r13d
jmp    1236 <main+0xe6>
nopl   0x0(%rax,%rax,1)
xor    %r12d,%r12d
jmp    1236 <main+0xe6>
cs nopw 0x0(%rax,%rax,1)
nop
sub    %ecx,%r13d
mov    %ecx,%r12d
add    %r12d,%r13d
and    $0x1,%r12d
sub    %r12d,%r13d
jmp    125b <main+0x10b>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsbl %al,%edi
call   1030 <putchar@plt>
add    $0x1,%ebp
cmp    %r13d,%ebp
jge    1190 <main+0x40>
movslq %ebp,%rbx
movzbl (%rsp,%rbx,1),%eax
cmp    $0x25,%al
jne    1250 <main+0x100>
movsbl 0x1(%rsp,%rbx,1),%edi
add    $0x1,%rbx
call   1070 <format_ansi@plt>
mov    %r15,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%ebp
jmp    1258 <main+0x108>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %r14d,%r14d
test   %r12d,%r12d
je     12c4 <main+0x174>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
lea    0xd53(%rip),%rsi
call   1060 <fprintf@plt>
mov    %r12d,%r14d
jmp    12c4 <main+0x174>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r14d,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@.str.1 = [2 x i8] c"\0A\00"
@stderr = global i32 0
@.str.2 = [32 x i8] c"BAD LOG-FILE? ended with: '%%'\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [4096 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %3
br label %7
7: ; preds = %85, %0
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%9 = call i32 @read(i32 0, i8* %8, i32 4096)
store i32 %9, i32* %4
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %86
11: ; preds = %7
store i32 0, i32* %5
%12 = load i32, i32* %3
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %11
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%16 = load i8, i8* %15
%17 = call i8* @format_ansi(i8 signext %16)
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 1, i32* %5
store i32 0, i32* %3
br label %19
19: ; preds = %14, %11
br label %20
20: ; preds = %34, %19
%21 = load i32, i32* %4
%22 = load i32, i32* %5
%23 = icmp sgt i32 %21, %22
br i1 %23, label %24, label %32
24: ; preds = %20
%25 = load i32, i32* %4
%26 = sub nsw i32 %25, 1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 37
br label %32
32: ; preds = %24, %20
%33 = phi i1 [ false, %20 ], [ %31, %24 ]
br i1 %33, label %34, label %39
34: ; preds = %32
%35 = load i32, i32* %3
%36 = add nsw i32 %35, 1
store i32 %36, i32* %3
%37 = load i32, i32* %4
%38 = add nsw i32 %37, -1
store i32 %38, i32* %4
br label %20
39: ; preds = %32
%40 = load i32, i32* %3
%41 = srem i32 %40, 2
%42 = icmp ne i32 %41, 0
br i1 %42, label %47, label %43
43: ; preds = %39
%44 = load i32, i32* %3
%45 = load i32, i32* %4
%46 = add nsw i32 %45, %44
store i32 %46, i32* %4
store i32 0, i32* %3
br label %52
47: ; preds = %39
%48 = load i32, i32* %3
%49 = sub nsw i32 %48, 1
%50 = load i32, i32* %4
%51 = add nsw i32 %50, %49
store i32 %51, i32* %4
store i32 1, i32* %3
br label %52
52: ; preds = %47, %43
%53 = load i32, i32* %5
store i32 %53, i32* %6
br label %54
54: ; preds = %84, %52
%55 = load i32, i32* %6
%56 = load i32, i32* %4
%57 = icmp slt i32 %55, %56
br i1 %57, label %58, label %85
58: ; preds = %54
%59 = load i32, i32* %6
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %60
%62 = load i8, i8* %61
%63 = sext i8 %62 to i32
%64 = icmp eq i32 %63, 37
br i1 %64, label %65, label %76
65: ; preds = %58
%66 = load i32, i32* %6
%67 = add nsw i32 %66, 1
store i32 %67, i32* %6
%68 = load i32, i32* %6
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %69
%71 = load i8, i8* %70
%72 = call i8* @format_ansi(i8 signext %71)
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %72)
%74 = load i32, i32* %6
%75 = add nsw i32 %74, 1
store i32 %75, i32* %6
br label %84
76: ; preds = %58
%77 = load i32, i32* %6
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %78
%80 = load i8, i8* %79
%81 = call i32 @putchar(i8 signext %80)
%82 = load i32, i32* %6
%83 = add nsw i32 %82, 1
store i32 %83, i32* %6
br label %84
84: ; preds = %76, %65
br label %54
85: ; preds = %54
br label %7
86: ; preds = %7
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%88 = load i32, i32* %3
%89 = icmp ne i32 %88, 0
br i1 %89, label %90, label %93
90: ; preds = %86
%91 = load i32, i32* @stderr
%92 = call i32 @fprintf(i32 %91, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
br label %93
93: ; preds = %90, %86
%94 = load i32, i32* %3
ret i32 %94
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@stderr = global i32 0
@.str.2 = [32 x i8] c"BAD LOG-FILE? ended with: '%%'\0A\00"
define i32 @main() {
%1 = alloca [4096 x i8]
%2 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%3 = call i32 @read(i32 0, i8* %2, i32 4096)
%4 = icmp sgt i32 %3, 0
br i1 %4, label %10, label %5
5: ; preds = %0
%6 = tail call i32 bitcast (i32 (i8)* @putchar to i32 (i32)*)(i32 10)
br label %70
7: ; preds = %60, %37
%8 = call i32 @read(i32 0, i8* %2, i32 4096)
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %64
10: ; preds = %0, %7
%11 = phi i32 [ %8, %7 ], [ %3, %0 ]
%12 = phi i32 [ %40, %7 ], [ 0, %0 ]
%13 = icmp eq i32 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %10
%15 = load i8, i8* %2
%16 = tail call i8* @format_ansi(i8 signext %15)
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %16)
br label %18
18: ; preds = %14, %10
%19 = phi i32 [ 1, %14 ], [ 0, %10 ]
%20 = icmp ugt i32 %11, %19
br i1 %20, label %21, label %37
21: ; preds = %18
%22 = zext i32 %11 to i64
%23 = sub nuw nsw i32 %11, %19
br label %24
24: ; preds = %21, %32
%25 = phi i64 [ %22, %21 ], [ %27, %32 ]
%26 = phi i32 [ 0, %21 ], [ %33, %32 ]
%27 = add nsw i64 %25, -1
%28 = and i64 %27, 4294967295
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 %28
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 37
br i1 %31, label %32, label %35
32: ; preds = %24
%33 = add nuw i32 %26, 1
%34 = icmp eq i32 %33, %23
br i1 %34, label %37, label %24
35: ; preds = %24
%36 = trunc i64 %25 to i32
br label %37
37: ; preds = %32, %35, %18
%38 = phi i32 [ 0, %18 ], [ %26, %35 ], [ %23, %32 ]
%39 = phi i32 [ %11, %18 ], [ %36, %35 ], [ %19, %32 ]
%40 = and i32 %38, 1
%41 = and i32 %38, 1
%42 = add i32 %39, %38
%43 = sub i32 %42, %41
%44 = icmp slt i32 %19, %43
br i1 %44, label %45, label %7
45: ; preds = %37, %60
%46 = phi i32 [ %62, %60 ], [ %19, %37 ]
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 %47
%49 = load i8, i8* %48
%50 = icmp eq i8 %49, 37
br i1 %50, label %51, label %58
51: ; preds = %45
%52 = add nsw i32 %46, 1
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 %53
%55 = load i8, i8* %54
%56 = tail call i8* @format_ansi(i8 signext %55)
%57 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %56)
br label %60
58: ; preds = %45
%59 = tail call i32 @putchar(i8 signext %49)
br label %60
60: ; preds = %58, %51
%61 = phi i32 [ %52, %51 ], [ %46, %58 ]
%62 = add nsw i32 %61, 1
%63 = icmp slt i32 %62, %43
br i1 %63, label %45, label %7
64: ; preds = %7
%65 = tail call i32 bitcast (i32 (i8)* @putchar to i32 (i32)*)(i32 10)
%66 = icmp eq i32 %40, 0
br i1 %66, label %70, label %67
67: ; preds = %64
%68 = load i32, i32* @stderr
%69 = tail call i32 @fprintf(i32 %68, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
br label %70
70: ; preds = %5, %67, %64
%71 = phi i32 [ 0, %5 ], [ %40, %67 ], [ 0, %64 ]
ret i32 %71
}