#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
scalar_t__ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int main() {
  FILE *f;
  size_t i, j;
  char buf[4096];
  f = fopen("task1.tsko", "rb");
  i = 0;
  while (!feof(f)) {
    i += fread(buf + i, 1, 1, f);
  }
  buf[i] = 0;
  fclose(f);
  f = fopen("task1.tsko", "wb");
  fprintf(f, "%c", 0xe9);
  fprintf(f, "%d", (int)(4096 - i));
  for (j = 5; j < 4096 - i; j++) {
    fprintf(f, "%c", 0xe5);
  }
  fwrite(buf, 1, i, f);
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe87(%rip),%rdi
lea    0xe8b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11af <main+0x4f>
jmp    11d7 <main+0x77>
lea    -0x1020(%rbp),%rdi
add    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    1191 <main+0x31>
mov    -0x18(%rbp),%rax
movb   $0x0,-0x1020(%rbp,%rax,1)
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe0d(%rip),%rdi
lea    0xe14(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe03(%rip),%rsi
mov    $0xe9,%edx
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rdi
mov    $0x1000,%eax
sub    -0x18(%rbp),%rax
mov    %eax,%edx
lea    0xde6(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x5,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    $0x1000,%ecx
sub    -0x18(%rbp),%rcx
cmp    %rcx,%rax
jae    1277 <main+0x117>
mov    -0x10(%rbp),%rdi
lea    0xdb5(%rip),%rsi
mov    $0xe5,%edx
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    123b <main+0xdb>
lea    -0x1020(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1008,%rsp
lea    0xe7b(%rip),%rdi
lea    0xe7f(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1070 <feof@plt>
xor    %r12d,%r12d
test   %eax,%eax
jne    11d5 <main+0x65>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    (%rsp,%r12,1),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
add    %rax,%r12
mov    %r14,%rdi
call   1070 <feof@plt>
test   %eax,%eax
je     11b0 <main+0x40>
movb   $0x0,(%rsp,%r12,1)
mov    %r14,%rdi
call   1040 <fclose@plt>
lea    0xe17(%rip),%rdi
lea    0xe1e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r14
mov    $0xe9,%edi
mov    %rax,%rsi
call   1050 <fputc@plt>
mov    $0x1000,%r15d
sub    %r12,%r15
lea    0xdfc(%rip),%rsi
mov    %r14,%rdi
mov    %r15d,%edx
call   1060 <fprintf@plt>
cmp    $0x6,%r15
jb     1242 <main+0xd2>
lea    -0xffb(%r12),%rbx
ff
xchg   %ax,%ax
mov    $0xe5,%edi
mov    %r14,%rsi
call   1050 <fputc@plt>
inc    %rbx
jne    1230 <main+0xc0>
mov    %rsp,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %r14,%rcx
call   1090 <fwrite@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [11 x i8] c"task1.tsko\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [3 x i8] c"wb\00"
@.str.3 = [3 x i8] c"%c\00"
@.str.4 = [3 x i8] c"%d\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i64
%4 = alloca i64
%5 = alloca [4096 x i8]
store i32 0, i32* %1
%6 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %2
store i64 0, i64* %3
br label %7
7: ; preds = %12, %0
%8 = load i32*, i32** %2
%9 = call i32 @feof(i32* %8)
%10 = icmp ne i32 %9, 0
%11 = xor i1 %10, true
br i1 %11, label %12, label %20
12: ; preds = %7
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%14 = load i64, i64* %3
%15 = getelementptr inbounds i8, i8* %13, i64 %14
%16 = load i32*, i32** %2
%17 = call i64 @fread(i8* %15, i32 1, i32 1, i32* %16)
%18 = load i64, i64* %3
%19 = add i64 %18, %17
store i64 %19, i64* %3
br label %7
20: ; preds = %7
%21 = load i64, i64* %3
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 %21
store i8 0, i8* %22
%23 = load i32*, i32** %2
%24 = call i32 @fclose(i32* %23)
%25 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %25, i32** %2
%26 = load i32*, i32** %2
%27 = call i32 @fprintf(i32* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 233)
%28 = load i32*, i32** %2
%29 = load i64, i64* %3
%30 = sub i64 4096, %29
%31 = trunc i64 %30 to i32
%32 = call i32 @fprintf(i32* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %31)
store i64 5, i64* %4
br label %33
33: ; preds = %41, %20
%34 = load i64, i64* %4
%35 = load i64, i64* %3
%36 = sub i64 4096, %35
%37 = icmp ult i64 %34, %36
br i1 %37, label %38, label %44
38: ; preds = %33
%39 = load i32*, i32** %2
%40 = call i32 @fprintf(i32* %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 229)
br label %41
41: ; preds = %38
%42 = load i64, i64* %4
%43 = add i64 %42, 1
store i64 %43, i64* %4
br label %33
44: ; preds = %33
%45 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%46 = load i64, i64* %3
%47 = load i32*, i32** %2
%48 = call i32 @fwrite(i8* %45, i32 1, i64 %46, i32* %47)
%49 = load i32*, i32** %2
%50 = call i32 @fclose(i32* %49)
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"task1.tsko\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [3 x i8] c"wb\00"
@.str.4 = [3 x i8] c"%d\00"
define i32 @main() {
%1 = alloca [4096 x i8]
%2 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%4 = tail call i32 @feof(i32* %3)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %13
6: ; preds = %0, %6
%7 = phi i64 [ %10, %6 ], [ 0, %0 ]
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 %7
%9 = call i64 @fread(i8* %8, i32 1, i32 1, i32* %3)
%10 = add i64 %9, %7
%11 = tail call i32 @feof(i32* %3)
%12 = icmp eq i32 %11, 0
br i1 %12, label %6, label %13
13: ; preds = %6, %0
%14 = phi i64 [ 0, %0 ], [ %10, %6 ]
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 %14
store i8 0, i8* %15
%16 = tail call i32 @fclose(i32* %3)
%17 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%18 = tail call i32 @fputc(i32 233, i32* %17)
%19 = sub i64 4096, %14
%20 = trunc i64 %19 to i32
%21 = tail call i32 @fprintf(i32* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %20)
%22 = icmp ugt i64 %19, 5
br i1 %22, label %23, label %28
23: ; preds = %13, %23
%24 = phi i64 [ %26, %23 ], [ 5, %13 ]
%25 = tail call i32 @fputc(i32 229, i32* %17)
%26 = add nuw i64 %24, 1
%27 = icmp eq i64 %26, %19
br i1 %27, label %28, label %23
28: ; preds = %23, %13
%29 = call i32 @fwrite(i8* %2, i32 1, i64 %14, i32* %17)
%30 = tail call i32 @fclose(i32* %17)
ret i32 0
}