#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 EXIT_FAILURE;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stdout;
int main() {
  FILE *file = fopen("Textfile.txt", "rw+");
  if (file == NULL)
    return EXIT_FAILURE;
  int id = 0;
  fseek(file, 0L, SEEK_END);
  long size = ftell(file);
  rewind(file);
  fprintf(stdout, "File size: %ld\n", size);
  while (ftell(file) < size) {
    fwrite(&id, sizeof(int), 1, file);
    fseek(file, 1024 - sizeof(int), SEEK_CUR);
    id++;
  }
  fprintf(stdout, "File end id: %d\n", id);
  fclose(file);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
lea    0xe80(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b2 <main+0x42>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1275 <main+0x105>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e24(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <rewind@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    %eax,%edx
lea    0xe18(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1060 <ftell@plt>
cmp    -0x20(%rbp),%rax
jge    124d <main+0xdd>
mov    -0x10(%rbp),%rcx
lea    -0x14(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1090 <fwrite@plt>
mov    -0x10(%rbp),%rdi
mov    0x2dad(%rip),%rax
mov    (%rax),%edx
mov    $0x3fc,%esi
call   1070 <fseek@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11fe <main+0x8e>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xdc1(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe7e(%rip),%rdi
lea    0xe84(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     126f <main+0xff>
mov    %rax,%rbx
movl   $0x0,0x4(%rsp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1040 <rewind@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
mov    %r12d,%edx
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    $0x0,%ebp
cmp    %r12,%rax
jge    124e <main+0xde>
lea    0x4(%rsp),%r15
mov    0x2ddf(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
mov    0x0(%r13),%edx
mov    %rbx,%rdi
mov    $0x3fc,%esi
call   1070 <fseek@plt>
mov    0x4(%rsp),%ebp
add    $0x1,%ebp
mov    %ebp,0x4(%rsp)
mov    %rbx,%rdi
call   1060 <ftell@plt>
cmp    %r12,%rax
jl     1210 <main+0xa0>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
lea    0xdc3(%rip),%rsi
mov    %ebp,%edx
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    1279 <main+0x109>
mov    0x2d5a(%rip),%rax
mov    (%rax),%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"Textfile.txt\00"
@.str.1 = [4 x i8] c"rw+\00"
@EXIT_FAILURE = global i32 0
@SEEK_END = global i32 0
@stdout = global i32 0
@.str.2 = [16 x i8] c"File size: %ld\0A\00"
@SEEK_CUR = global i32 0
@.str.3 = [17 x i8] c"File end id: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i64
store i32 0, i32* %1
%5 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %2
%6 = load i32*, i32** %2
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = load i32, i32* @EXIT_FAILURE
store i32 %9, i32* %1
br label %41
10: ; preds = %0
store i32 0, i32* %3
%11 = load i32*, i32** %2
%12 = load i32, i32* @SEEK_END
%13 = call i32 @fseek(i32* %11, i32 0, i32 %12)
%14 = load i32*, i32** %2
%15 = call i64 @ftell(i32* %14)
store i64 %15, i64* %4
%16 = load i32*, i32** %2
%17 = call i32 @rewind(i32* %16)
%18 = load i32, i32* @stdout
%19 = load i64, i64* %4
%20 = trunc i64 %19 to i32
%21 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %27, %10
%23 = load i32*, i32** %2
%24 = call i64 @ftell(i32* %23)
%25 = load i64, i64* %4
%26 = icmp slt i64 %24, %25
br i1 %26, label %27, label %35
27: ; preds = %22
%28 = load i32*, i32** %2
%29 = call i32 @fwrite(i32* %3, i32 4, i32 1, i32* %28)
%30 = load i32*, i32** %2
%31 = load i32, i32* @SEEK_CUR
%32 = call i32 @fseek(i32* %30, i32 1020, i32 %31)
%33 = load i32, i32* %3
%34 = add nsw i32 %33, 1
store i32 %34, i32* %3
br label %22
35: ; preds = %22
%36 = load i32, i32* @stdout
%37 = load i32, i32* %3
%38 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %37)
%39 = load i32*, i32** %2
%40 = call i32 @fclose(i32* %39)
store i32 0, i32* %1
br label %41
41: ; preds = %35, %8
%42 = load i32, i32* %1
ret i32 %42
}

LLVM IR (-O2):
@.str = [13 x i8] c"Textfile.txt\00"
@.str.1 = [4 x i8] c"rw+\00"
@EXIT_FAILURE = global i32 0
@SEEK_END = global i32 0
@stdout = global i32 0
@.str.2 = [16 x i8] c"File size: %ld\0A\00"
@SEEK_CUR = global i32 0
@.str.3 = [17 x i8] c"File end id: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = load i32, i32* @EXIT_FAILURE
br label %30
6: ; preds = %0
%7 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %2, i32 0, i32 %8)
%10 = tail call i64 @ftell(i32* %2)
%11 = tail call i32 @rewind(i32* %2)
%12 = load i32, i32* @stdout
%13 = trunc i64 %10 to i32
%14 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = tail call i64 @ftell(i32* %2)
%16 = icmp slt i64 %15, %10
br i1 %16, label %17, label %25
17: ; preds = %6, %17
%18 = call i32 @fwrite(i32* %1, i32 4, i32 1, i32* %2)
%19 = load i32, i32* @SEEK_CUR
%20 = tail call i32 @fseek(i32* %2, i32 1020, i32 %19)
%21 = load i32, i32* %1
%22 = add nsw i32 %21, 1
store i32 %22, i32* %1
%23 = tail call i64 @ftell(i32* %2)
%24 = icmp slt i64 %23, %10
br i1 %24, label %17, label %25
25: ; preds = %17, %6
%26 = phi i32 [ 0, %6 ], [ %22, %17 ]
%27 = load i32, i32* @stdout
%28 = tail call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%29 = tail call i32 @fclose(i32* %2)
br label %30
30: ; preds = %25, %4
%31 = phi i32 [ %5, %4 ], [ 0, %25 ]
ret i32 %31
}