#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_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int main(int argc, const char *argv[]) {
  FILE *fd = NULL;
  if (argc < 2) {
    fprintf(stderr, "%s [FILE]\n", argv[0]);
    return 1;
  }
  if ((fd = fopen(argv[1], "r+")) == NULL) {
    perror("Cannot open file\n");
    return 1;
  }
  fseek(fd, 0, SEEK_END);
  long int psz = ftell(fd);
  rewind(fd);
  char *buff = (char *)malloc(sizeof(char) * psz);
  if (!buff) {
    fputs("Memory error", stderr);
    fclose(fd);
    return 1;
  }
  fread(buff, 1, psz, fd);
  if (buff[psz - 1] != '\n') {
    fflush(fd);
    fwrite("\n", 1, 1, fd);
  }
  free(buff);
  fclose(fd);
  return 0;
}


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)
movq   $0x0,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1220 <main+0x50>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdf1(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1334 <main+0x164>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xddc(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    125a <main+0x8a>
lea    0xdc5(%rip),%rdi
call   10e0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1334 <main+0x164>
mov    -0x18(%rbp),%rdi
mov    0x2d83(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1090 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x20(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12cc <main+0xfc>
mov    0x2d2f(%rip),%rax
mov    (%rax),%esi
lea    0xd6e(%rip),%rdi
call   1070 <fputs@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    1334 <main+0x164>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     131b <main+0x14b>
mov    -0x18(%rbp),%rdi
call   10b0 <fflush@plt>
mov    -0x18(%rbp),%rcx
lea    0xcfa(%rip),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   10f0 <fwrite@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
cmp    $0x1,%edi
jg     11f7 <main+0x27>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xe13(%rip),%rsi
call   1090 <fprintf@plt>
jmp    12aa <main+0xda>
mov    0x8(%rsi),%rdi
lea    0xe09(%rip),%rsi
call   10e0 <fopen@plt>
test   %rax,%rax
je     12b5 <main+0xe5>
mov    %rax,%rbx
mov    0x2dce(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10d0 <fseek@plt>
mov    %rbx,%rdi
call   10a0 <ftell@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1060 <rewind@plt>
mov    %r15d,%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     128d <main+0xbd>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmpb   $0xa,-0x1(%r15,%r14,1)
je     1279 <main+0xa9>
mov    %rbx,%rdi
call   10c0 <fflush@plt>
mov    $0xa,%edi
mov    %rbx,%rsi
call   1080 <fputc@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
jmp    12af <main+0xdf>
mov    0x2d44(%rip),%rax
mov    (%rax),%esi
lea    0xd83(%rip),%rdi
call   1070 <fputs@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd52(%rip),%rdi
call   10f0 <perror@plt>
jmp    12aa <main+0xda>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [11 x i8] c"%s [FILE]\0A\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [18 x i8] c"Cannot open file\0A\00"
@SEEK_END = global i32 0
@.str.3 = [13 x i8] c"Memory error\00"
@.str.4 = [2 x i8] c"\0A\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 i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32* null, i32** %6
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 1, i32* %3
br label %67
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %6
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %67
25: ; preds = %17
%26 = load i32*, i32** %6
%27 = load i32, i32* @SEEK_END
%28 = call i32 @fseek(i32* %26, i32 0, i32 %27)
%29 = load i32*, i32** %6
%30 = call i64 @ftell(i32* %29)
store i64 %30, i64* %7
%31 = load i32*, i32** %6
%32 = call i32 @rewind(i32* %31)
%33 = load i64, i64* %7
%34 = mul i64 1, %33
%35 = trunc i64 %34 to i32
%36 = call i64 @malloc(i32 %35)
%37 = inttoptr i64 %36 to i8*
store i8* %37, i8** %8
%38 = load i8*, i8** %8
%39 = icmp ne i8* %38, null
br i1 %39, label %45, label %40
40: ; preds = %25
%41 = load i32, i32* @stderr
%42 = call i32 @fputs(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %41)
%43 = load i32*, i32** %6
%44 = call i32 @fclose(i32* %43)
store i32 1, i32* %3
br label %67
45: ; preds = %25
%46 = load i8*, i8** %8
%47 = load i64, i64* %7
%48 = load i32*, i32** %6
%49 = call i32 @fread(i8* %46, i32 1, i64 %47, i32* %48)
%50 = load i8*, i8** %8
%51 = load i64, i64* %7
%52 = sub nsw i64 %51, 1
%53 = getelementptr inbounds i8, i8* %50, i64 %52
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp ne i32 %55, 10
br i1 %56, label %57, label %62
57: ; preds = %45
%58 = load i32*, i32** %6
%59 = call i32 @fflush(i32* %58)
%60 = load i32*, i32** %6
%61 = call i32 @fwrite(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 1, i32 1, i32* %60)
br label %62
62: ; preds = %57, %45
%63 = load i8*, i8** %8
%64 = call i32 @free(i8* %63)
%65 = load i32*, i32** %6
%66 = call i32 @fclose(i32* %65)
store i32 0, i32* %3
br label %67
67: ; preds = %62, %40, %23, %11
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [11 x i8] c"%s [FILE]\0A\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [18 x i8] c"Cannot open file\0A\00"
@SEEK_END = global i32 0
@.str.3 = [13 x i8] c"Memory error\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %40
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %40
15: ; preds = %8
%16 = load i32, i32* @SEEK_END
%17 = tail call i32 @fseek(i32* %11, i32 0, i32 %16)
%18 = tail call i64 @ftell(i32* %11)
%19 = tail call i32 @rewind(i32* %11)
%20 = trunc i64 %18 to i32
%21 = tail call i64 @malloc(i32 %20)
%22 = inttoptr i64 %21 to i8*
%23 = icmp eq i64 %21, 0
br i1 %23, label %24, label %28
24: ; preds = %15
%25 = load i32, i32* @stderr
%26 = tail call i32 @fputs(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = tail call i32 @fclose(i32* %11)
br label %40
28: ; preds = %15
%29 = tail call i32 @fread(i8* %22, i32 1, i64 %18, i32* %11)
%30 = add nsw i64 %18, -1
%31 = getelementptr inbounds i8, i8* %22, i64 %30
%32 = load i8, i8* %31
%33 = icmp eq i8 %32, 10
br i1 %33, label %37, label %34
34: ; preds = %28
%35 = tail call i32 @fflush(i32* %11)
%36 = tail call i32 @fputc(i32 10, i32* %11)
br label %37
37: ; preds = %34, %28
%38 = tail call i32 @free(i8* %22)
%39 = tail call i32 @fclose(i32* %11)
br label %40
40: ; preds = %24, %37, %13, %4
%41 = phi i32 [ 1, %4 ], [ 1, %13 ], [ 0, %37 ], [ 1, %24 ]
ret i32 %41
}