#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 scalar_t__ int64_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EXIT_SUCCESS;
int /*<<< orphan*/ *Fopen(char *, char *);
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fseeko(int /*<<< orphan*/ *, scalar_t__, int /*<<< orphan*/);
scalar_t__ ftello(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  FILE *IN = Fopen(argv[argc - 1], "r");
  int64_t i, size;
  if (argc != 2) {
    fprintf(stderr, "Reverse huge file using very low memory.\n");
    fprintf(stderr, "Usage: %s file\n", argv[0]);
    return EXIT_SUCCESS;
  }
  fseeko(IN, 0, SEEK_END);
  size = ftello(IN);
  i = 0;
  while (i++ < size) {
    fseeko(IN, -i, SEEK_END);
    printf("%c", fgetc(IN));
  }
  fclose(IN);
  return EXIT_SUCCESS;
}


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)
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xe62(%rip),%rsi
call   1090 <Fopen@plt>
mov    %rax,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11f7 <main+0x87>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe4d(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1286 <main+0x116>
mov    -0x18(%rbp),%rdi
mov    0x2de6(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1080 <fseeko@plt>
mov    -0x18(%rbp),%rdi
call   1030 <ftello@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
cmp    -0x28(%rbp),%rax
jge    1271 <main+0x101>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
sub    -0x20(%rbp),%rsi
mov    0x2d9a(%rip),%rax
mov    (%rax),%edx
call   1080 <fseeko@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,%esi
lea    0xdd5(%rip),%rdi
call   1050 <printf@plt>
jmp    1222 <main+0xb2>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2d4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
movslq %edi,%rbx
mov    -0x8(%rsi,%rbx,8),%rdi
lea    0xe75(%rip),%rsi
call   1090 <Fopen@plt>
cmp    $0x2,%ebx
jne    120d <main+0x9d>
mov    %rax,%r15
mov    0x2e49(%rip),%r13
mov    0x0(%r13),%edx
mov    %rax,%rdi
xor    %esi,%esi
call   1080 <fseeko@plt>
mov    %r15,%rdi
call   1030 <ftello@plt>
test   %rax,%rax
jle    1203 <main+0x93>
mov    %rax,%r14
mov    $0xffffffffffffffff,%rbx
lea    0xe71(%rip),%r12
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13),%edx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1080 <fseeko@plt>
mov    %r15,%rdi
call   1060 <fgetc@plt>
mov    %r12,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
lea    (%r14,%rbx,1),%rax
add    $0xffffffffffffffff,%rax
add    $0xffffffffffffffff,%rbx
cmp    $0xffffffffffffffff,%rax
jne    11d0 <main+0x60>
mov    %r15,%rdi
call   1040 <fclose@plt>
jmp    1237 <main+0xc7>
mov    0x2dc4(%rip),%rbx
mov    (%rbx),%edi
lea    0xde5(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    (%rbx),%edi
mov    (%r14),%rdx
lea    0xdfc(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2d92(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [42 x i8] c"Reverse huge file using very low memory.\0A\00"
@.str.2 = [16 x i8] c"Usage: %s file\0A\00"
@EXIT_SUCCESS = global i32 0
@SEEK_END = global i32 0
@.str.3 = [3 x i8] c"%c\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i8**, i8*** %5
%10 = load i32, i32* %4
%11 = sub nsw i32 %10, 1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8*, i8** %9, i64 %12
%14 = load i8*, i8** %13
%15 = call i32* @Fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %6
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 2
br i1 %17, label %18, label %27
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* @stderr
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 0
%24 = load i8*, i8** %23
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %24)
%26 = load i32, i32* @EXIT_SUCCESS
store i32 %26, i32* %3
br label %51
27: ; preds = %2
%28 = load i32*, i32** %6
%29 = load i32, i32* @SEEK_END
%30 = call i32 @fseeko(i32* %28, i64 0, i32 %29)
%31 = load i32*, i32** %6
%32 = call i64 @ftello(i32* %31)
store i64 %32, i64* %8
store i64 0, i64* %7
br label %33
33: ; preds = %38, %27
%34 = load i64, i64* %7
%35 = add nsw i64 %34, 1
store i64 %35, i64* %7
%36 = load i64, i64* %8
%37 = icmp slt i64 %34, %36
br i1 %37, label %38, label %47
38: ; preds = %33
%39 = load i32*, i32** %6
%40 = load i64, i64* %7
%41 = sub nsw i64 0, %40
%42 = load i32, i32* @SEEK_END
%43 = call i32 @fseeko(i32* %39, i64 %41, i32 %42)
%44 = load i32*, i32** %6
%45 = call i32 @fgetc(i32* %44)
%46 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %45)
br label %33
47: ; preds = %33
%48 = load i32*, i32** %6
%49 = call i32 @fclose(i32* %48)
%50 = load i32, i32* @EXIT_SUCCESS
store i32 %50, i32* %3
br label %51
51: ; preds = %47, %18
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [42 x i8] c"Reverse huge file using very low memory.\0A\00"
@.str.2 = [16 x i8] c"Usage: %s file\0A\00"
@EXIT_SUCCESS = global i32 0
@SEEK_END = global i32 0
@.str.3 = [3 x i8] c"%c\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = add nsw i32 %0, -1
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds i8*, i8** %1, i64 %4
%6 = load i8*, i8** %5
%7 = tail call i32* @Fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32 %0, 2
br i1 %8, label %15, label %9
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %1
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %13)
br label %31
15: ; preds = %2
%16 = load i32, i32* @SEEK_END
%17 = tail call i32 @fseeko(i32* %7, i64 0, i32 %16)
%18 = tail call i64 @ftello(i32* %7)
%19 = icmp sgt i64 %18, 0
br i1 %19, label %20, label %29
20: ; preds = %15, %20
%21 = phi i64 [ %22, %20 ], [ 0, %15 ]
%22 = add nuw nsw i64 %21, 1
%23 = xor i64 %21, -1
%24 = load i32, i32* @SEEK_END
%25 = tail call i32 @fseeko(i32* %7, i64 %23, i32 %24)
%26 = tail call i32 @fgetc(i32* %7)
%27 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%28 = icmp eq i64 %22, %18
br i1 %28, label %29, label %20
29: ; preds = %20, %15
%30 = tail call i32 @fclose(i32* %7)
br label %31
31: ; preds = %29, %9
%32 = load i32, i32* @EXIT_SUCCESS
ret i32 %32
}