#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 /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ is_sequence(char *);
int /*<<< orphan*/ printf(char *, char *);
int main(int argc, char *argv[]) {
  FILE *fp = fopen("a.bin", "rb+");
  char buffer[6];
  int writer = ftell(fp);
  int reader = writer;
  while (!feof(fp)) {
    fread(buffer, sizeof(char), 6, fp);
    if (is_sequence(buffer)) {
      printf("%s\n", buffer);
      reader = ftell(fp);
    } else {
      fseek(fp, writer, SEEK_SET);
      fwrite(buffer, sizeof(char), 1, fp);
      ++reader;
      writer = ftell(fp);
      fseek(fp, reader, SEEK_SET);
    }
  }
  char end[] = "!";
  fwrite(end, sizeof(char), reader - writer, fp);
  fclose(fp);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe53(%rip),%rdi
lea    0xe52(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11ed <main+0x5d>
jmp    1290 <main+0x100>
lea    -0x1e(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x6,%edx
call   1030 <fread@plt>
lea    -0x1e(%rbp),%rdi
call   1060 <is_sequence@plt>
cmp    $0x0,%rax
je     1238 <main+0xa8>
lea    -0x1e(%rbp),%rsi
lea    0xde8(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,-0x28(%rbp)
jmp    128b <main+0xfb>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    0x2d9a(%rip),%rax
mov    (%rax),%edx
call   1090 <fseek@plt>
lea    -0x1e(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   10b0 <fwrite@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%esi
mov    0x2d5c(%rip),%rax
mov    (%rax),%edx
call   1090 <fseek@plt>
jmp    11cf <main+0x3f>
mov    0xd77(%rip),%ax
mov    %ax,-0x2a(%rbp)
lea    -0x2a(%rbp),%rdi
mov    -0x28(%rbp),%edx
sub    -0x24(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe4e(%rip),%rdi
lea    0xe4d(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <ftell@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1090 <feof@plt>
test   %eax,%eax
je     120e <main+0x6e>
mov    %ebp,%r12d
sub    %r12d,%ebp
movw   $0x21,(%rsp)
mov    %rsp,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   10c0 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x2(%rsp),%r14
mov    0x2dc6(%rip),%r13
lea    0xde9(%rip),%r15
mov    %ebp,%r12d
jmp    1278 <main+0xd8>
cs nopw 0x0(%rax,%rax,1)
mov    0x0(%r13),%edx
mov    %rbx,%rdi
mov    %r12d,%esi
call   10a0 <fseek@plt>
movsbl 0x2(%rsp),%edi
mov    %rbx,%rsi
call   1060 <fputc@plt>
add    $0x1,%ebp
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %eax,%r12d
mov    0x0(%r13),%edx
mov    %rbx,%rdi
mov    %ebp,%esi
call   10a0 <fseek@plt>
mov    %rbx,%rdi
call   1090 <feof@plt>
test   %eax,%eax
jne    11da <main+0x3a>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x6,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %r14,%rdi
call   1070 <is_sequence@plt>
test   %rax,%rax
je     1230 <main+0x90>
mov    %r15,%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %eax,%ebp
jmp    1268 <main+0xc8>

LLVM IR (-O0):
@.str = [6 x i8] c"a.bin\00"
@.str.1 = [4 x i8] c"rb+\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@SEEK_SET = global i32 0
@__const.main.end = [2 x i8] c"!\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [6 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca [2 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = call i32 @ftell(i32* %12)
store i32 %13, i32* %8
%14 = load i32, i32* %8
store i32 %14, i32* %9
br label %15
15: ; preds = %48, %2
%16 = load i32*, i32** %6
%17 = call i32 @feof(i32* %16)
%18 = icmp ne i32 %17, 0
%19 = xor i1 %18, true
br i1 %19, label %20, label %49
20: ; preds = %15
%21 = getelementptr inbounds [6 x i8], [6 x i8]* %7, i64 0, i64 0
%22 = load i32*, i32** %6
%23 = call i32 @fread(i8* %21, i32 1, i32 6, i32* %22)
%24 = getelementptr inbounds [6 x i8], [6 x i8]* %7, i64 0, i64 0
%25 = call i64 @is_sequence(i8* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %20
%28 = getelementptr inbounds [6 x i8], [6 x i8]* %7, i64 0, i64 0
%29 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %28)
%30 = load i32*, i32** %6
%31 = call i32 @ftell(i32* %30)
store i32 %31, i32* %9
br label %48
32: ; preds = %20
%33 = load i32*, i32** %6
%34 = load i32, i32* %8
%35 = load i32, i32* @SEEK_SET
%36 = call i32 @fseek(i32* %33, i32 %34, i32 %35)
%37 = getelementptr inbounds [6 x i8], [6 x i8]* %7, i64 0, i64 0
%38 = load i32*, i32** %6
%39 = call i32 @fwrite(i8* %37, i32 1, i32 1, i32* %38)
%40 = load i32, i32* %9
%41 = add nsw i32 %40, 1
store i32 %41, i32* %9
%42 = load i32*, i32** %6
%43 = call i32 @ftell(i32* %42)
store i32 %43, i32* %8
%44 = load i32*, i32** %6
%45 = load i32, i32* %9
%46 = load i32, i32* @SEEK_SET
%47 = call i32 @fseek(i32* %44, i32 %45, i32 %46)
br label %48
48: ; preds = %32, %27
br label %15
49: ; preds = %15
%50 = bitcast [2 x i8]* %10 to i8*
%51 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
%52 = load i32, i32* %9
%53 = load i32, i32* %8
%54 = sub nsw i32 %52, %53
%55 = load i32*, i32** %6
%56 = call i32 @fwrite(i8* %51, i32 1, i32 %54, i32* %55)
%57 = load i32*, i32** %6
%58 = call i32 @fclose(i32* %57)
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
@.str = [6 x i8] c"a.bin\00"
@.str.1 = [4 x i8] c"rb+\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@SEEK_SET = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [6 x i8]
%4 = alloca i16
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%6 = getelementptr inbounds [6 x i8], [6 x i8]* %3, i64 0, i64 0
%7 = tail call i32 @ftell(i32* %5)
%8 = call i32 @feof(i32* %5)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %34
10: ; preds = %2, %29
%11 = phi i32 [ %31, %29 ], [ %7, %2 ]
%12 = phi i32 [ %30, %29 ], [ %7, %2 ]
%13 = call i32 @fread(i8* %6, i32 1, i32 6, i32* %5)
%14 = call i64 @is_sequence(i8* %6)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %10
%17 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %6)
%18 = call i32 @ftell(i32* %5)
br label %29
19: ; preds = %10
%20 = load i32, i32* @SEEK_SET
%21 = call i32 @fseek(i32* %5, i32 %12, i32 %20)
%22 = load i8, i8* %6
%23 = sext i8 %22 to i32
%24 = call i32 @fputc(i32 %23, i32* %5)
%25 = add nsw i32 %11, 1
%26 = call i32 @ftell(i32* %5)
%27 = load i32, i32* @SEEK_SET
%28 = call i32 @fseek(i32* %5, i32 %25, i32 %27)
br label %29
29: ; preds = %19, %16
%30 = phi i32 [ %12, %16 ], [ %26, %19 ]
%31 = phi i32 [ %18, %16 ], [ %25, %19 ]
%32 = call i32 @feof(i32* %5)
%33 = icmp eq i32 %32, 0
br i1 %33, label %10, label %34
34: ; preds = %29, %2
%35 = phi i32 [ %7, %2 ], [ %30, %29 ]
%36 = phi i32 [ %7, %2 ], [ %31, %29 ]
%37 = bitcast i16* %4 to i8*
store i16 33, i16* %4
%38 = sub nsw i32 %36, %35
%39 = call i32 @fwrite(i8* %37, i32 1, i32 %38, i32* %5)
%40 = call i32 @fclose(i32* %5)
ret i32 0
}