#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*/ clearerr(int /*<<< orphan*/ *);
size_t fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_t fread(char *, int, long, int /*<<< orphan*/ *);
size_t fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
size_t rename(char const *, char const *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
void ReadFromFile(const char *filename) {
  size_t retval = 0;
  char buffer[128];
  long filesize;
  FILE *fp;
  const char *newfilename = "newname.txt";
  retval = rename(filename, newfilename);
  filename = newfilename;
  fprintf(stdout, "rename returned %d\n", retval);
  fp = fopen(filename, "r");
  if (!fp) {
    fprintf(stderr, "Could not open file %s\n", filename);
    return;
  }
  retval = fseek(fp, 0, SEEK_END);
  fprintf(stdout, "fseek returned %d\n", retval);
  filesize = ftell(fp);
  rewind(fp);
  retval = fread(buffer, sizeof(char), filesize, fp);
  fprintf(stdout, "data read %s\n", buffer);
  fprintf(stdout, "fread returned %d\n", retval);
  if (ferror(fp)) {
    fprintf(stdout, "Error occurred on file\n");
    clearerr(fp);
  }
  retval = fclose(fp);
  fprintf(stdout, "fclose returned %d\n", retval);
}


x86 assembly (clang, -O0):
<ReadFromFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
lea    0xe42(%rip),%rax
mov    %rax,-0xa8(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xa8(%rbp),%rsi
call   10c0 <rename@plt>
mov    %rax,-0x10(%rbp)
mov    -0xa8(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe14(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x8(%rbp),%rdi
lea    0xe16(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0xa0(%rbp)
cmpq   $0x0,-0xa0(%rbp)
jne    1244 <ReadFromFile+0xa4>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdea(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1359 <ReadFromFile+0x1b9>
mov    -0xa0(%rbp),%rdi
mov    0x2d96(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %rax,-0x10(%rbp)
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdc7(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0xa0(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x98(%rbp)
mov    -0xa0(%rbp),%rdi
call   1060 <rewind@plt>
lea    -0x90(%rbp),%rdi
mov    -0x98(%rbp),%rdx
mov    -0xa0(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x10(%rbp)
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
lea    -0x90(%rbp),%rdx
lea    0xd7a(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd6d(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0xa0(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     132e <ReadFromFile+0x18e>
mov    0x2cbe(%rip),%rax
mov    (%rax),%edi
lea    0xd53(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0xa0(%rbp),%rdi
call   1090 <clearerr@plt>
mov    -0xa0(%rbp),%rdi
call   1050 <fclose@plt>
mov    %rax,-0x10(%rbp)
mov    0x2c8b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd34(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadFromFile>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x88,%rsp
lea    0xe4b(%rip),%rbx
mov    %rbx,%rsi
call   10c0 <rename@plt>
mov    0x2e0c(%rip),%r12
mov    (%r12),%edi
lea    0xe3d(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
lea    0xe40(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12c5 <ReadFromFile+0x125>
mov    %rax,%rbx
mov    0x2ded(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    (%r12),%edi
lea    0xe28(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1060 <rewind@plt>
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rax,%r14
mov    (%r12),%edi
lea    0xdfa(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    (%r12),%edi
lea    0xdf3(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     1299 <ReadFromFile+0xf9>
mov    (%r12),%edi
lea    0xde4(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1090 <clearerr@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    (%r12),%edi
lea    0xdda(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
lea    0xd4d(%rip),%rsi
lea    0xd24(%rip),%rdx
xor    %eax,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1070 <fprintf@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"newname.txt\00"
@stdout = global i32 0
@.str.1 = [20 x i8] c"rename returned %d\0A\00"
@.str.2 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.3 = [24 x i8] c"Could not open file %s\0A\00"
@SEEK_END = global i32 0
@.str.4 = [19 x i8] c"fseek returned %d\0A\00"
@.str.5 = [14 x i8] c"data read %s\0A\00"
@.str.6 = [19 x i8] c"fread returned %d\0A\00"
@.str.7 = [24 x i8] c"Error occurred on file\0A\00"
@.str.8 = [20 x i8] c"fclose returned %d\0A\00"
define void @ReadFromFile(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca [128 x i8]
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i8*
store i8* %0, i8** %2
store i64 0, i64* %3
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %7
%8 = load i8*, i8** %2
%9 = load i8*, i8** %7
%10 = call i64 @rename(i8* %8, i8* %9)
store i64 %10, i64* %3
%11 = load i8*, i8** %7
store i8* %11, i8** %2
%12 = load i32, i32* @stdout
%13 = load i64, i64* %3
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i64 %13)
%15 = load i8*, i8** %2
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %16, i32** %6
%17 = load i32*, i32** %6
%18 = icmp ne i32* %17, null
br i1 %18, label %23, label %19
19: ; preds = %1
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %2
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %21)
br label %58
23: ; preds = %1
%24 = load i32*, i32** %6
%25 = load i32, i32* @SEEK_END
%26 = call i64 @fseek(i32* %24, i32 0, i32 %25)
store i64 %26, i64* %3
%27 = load i32, i32* @stdout
%28 = load i64, i64* %3
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i64 %28)
%30 = load i32*, i32** %6
%31 = call i64 @ftell(i32* %30)
store i64 %31, i64* %5
%32 = load i32*, i32** %6
%33 = call i32 @rewind(i32* %32)
%34 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%35 = load i64, i64* %5
%36 = load i32*, i32** %6
%37 = call i64 @fread(i8* %34, i32 1, i64 %35, i32* %36)
store i64 %37, i64* %3
%38 = load i32, i32* @stdout
%39 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* %39)
%41 = load i32, i32* @stdout
%42 = load i64, i64* %3
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i64 %42)
%44 = load i32*, i32** %6
%45 = call i64 @ferror(i32* %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %52
47: ; preds = %23
%48 = load i32, i32* @stdout
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
%50 = load i32*, i32** %6
%51 = call i32 @clearerr(i32* %50)
br label %52
52: ; preds = %47, %23
%53 = load i32*, i32** %6
%54 = call i64 @fclose(i32* %53)
store i64 %54, i64* %3
%55 = load i32, i32* @stdout
%56 = load i64, i64* %3
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0), i64 %56)
br label %58
58: ; preds = %52, %19
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"newname.txt\00"
@stdout = global i32 0
@.str.1 = [20 x i8] c"rename returned %d\0A\00"
@.str.2 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.3 = [24 x i8] c"Could not open file %s\0A\00"
@SEEK_END = global i32 0
@.str.4 = [19 x i8] c"fseek returned %d\0A\00"
@.str.5 = [14 x i8] c"data read %s\0A\00"
@.str.6 = [19 x i8] c"fread returned %d\0A\00"
@.str.7 = [24 x i8] c"Error occurred on file\0A\00"
@.str.8 = [20 x i8] c"fclose returned %d\0A\00"
define void @ReadFromFile(i8* nocapture %0) {
%2 = alloca [128 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%4 = tail call i64 @rename(i8* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @stdout
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i64 %4)
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %34
12: ; preds = %1
%13 = load i32, i32* @SEEK_END
%14 = tail call i64 @fseek(i32* %7, i32 0, i32 %13)
%15 = load i32, i32* @stdout
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i64 %14)
%17 = tail call i64 @ftell(i32* %7)
%18 = tail call i32 @rewind(i32* %7)
%19 = call i64 @fread(i8* %3, i32 1, i64 %17, i32* %7)
%20 = load i32, i32* @stdout
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* %3)
%22 = load i32, i32* @stdout
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i64 %19)
%24 = call i64 @ferror(i32* %7)
%25 = icmp eq i64 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %12
%27 = load i32, i32* @stdout
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
%29 = call i32 @clearerr(i32* %7)
br label %30
30: ; preds = %26, %12
%31 = call i64 @fclose(i32* %7)
%32 = load i32, i32* @stdout
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0), i64 %31)
br label %34
34: ; preds = %30, %9
ret void
}