#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 *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, long);
int /*<<< orphan*/ remove(char *);
int main(void) {
  FILE *fout = fopen("fileio-fopen-twice.temp", "w");
  fprintf(fout, "Hello world.");
  FILE *fin = fopen("fileio-fopen-twice.temp", "r");
  printf("fout=%p\n", (void *)fout);
  printf("fin =%p\n", (void *)fin);
  fseek(fin, 0L, SEEK_END);
  long length = ftell(fin);
  printf("File has length %ld\n", length);
  fflush(fout);
  fseek(fin, 0L, SEEK_END);
  length = ftell(fin);
  printf("File has length %ld\n", length);
  fprintf(fout, "  This is a test.");
  fflush(fout);
  fseek(fin, 0L, SEEK_END);
  length = ftell(fin);
  printf("File has length %ld\n", length);
  fclose(fin);
  fclose(fout);
  remove("fileio-fopen-twice.temp");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe71(%rip),%rdi
lea    0xe82(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe70(%rip),%rsi
call   1060 <fprintf@plt>
lea    0xe4a(%rip),%rdi
lea    0xe6a(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe58(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe51(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2df7(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe27(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1080 <fflush@plt>
mov    -0x10(%rbp),%rdi
mov    0x2dbb(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xdeb(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
lea    0xdf0(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x8(%rbp),%rdi
call   1080 <fflush@plt>
mov    -0x10(%rbp),%rdi
mov    0x2d6f(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xd9f(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xd46(%rip),%rdi
call   1030 <remove@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
lea    0xe70(%rip),%r14
lea    0xe81(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
mov    %rax,%r15
lea    0xe71(%rip),%rdi
mov    $0xc,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10a0 <fwrite@plt>
lea    0xe65(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
mov    %rax,%rbx
lea    0xe55(%rip),%rdi
mov    %r15,%rsi
call   1050 <printf@plt>
lea    0xe4f(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    0x2df6(%rip),%r13
mov    0x0(%r13),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
lea    0xe2c(%rip),%r12
mov    %r12,%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    %r15,%rdi
call   1070 <fflush@plt>
mov    0x0(%r13),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %r12,%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
lea    0xe06(%rip),%rdi
mov    $0x11,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   10a0 <fwrite@plt>
mov    %r15,%rdi
call   1070 <fflush@plt>
mov    0x0(%r13),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %r12,%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1030 <remove@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [24 x i8] c"fileio-fopen-twice.temp\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [13 x i8] c"Hello world.\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [9 x i8] c"fout=%p\0A\00"
@.str.5 = [9 x i8] c"fin =%p\0A\00"
@SEEK_END = global i32 0
@.str.6 = [21 x i8] c"File has length %ld\0A\00"
@.str.7 = [18 x i8] c" This is a test.\00"
define i32 @main() {
%1 = alloca i32*
%2 = alloca i32*
%3 = alloca i64
%4 = call i32* @fopen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %1
%5 = load i32*, i32** %1
%6 = call i32 @fprintf(i32* %5, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%7 = call i32* @fopen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %7, i32** %2
%8 = load i32*, i32** %1
%9 = bitcast i32* %8 to i8*
%10 = ptrtoint i8* %9 to i64
%11 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i64 %10)
%12 = load i32*, i32** %2
%13 = bitcast i32* %12 to i8*
%14 = ptrtoint i8* %13 to i64
%15 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i64 %14)
%16 = load i32*, i32** %2
%17 = load i32, i32* @SEEK_END
%18 = call i32 @fseek(i32* %16, i64 0, i32 %17)
%19 = load i32*, i32** %2
%20 = call i64 @ftell(i32* %19)
store i64 %20, i64* %3
%21 = load i64, i64* %3
%22 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i64 %21)
%23 = load i32*, i32** %1
%24 = call i32 @fflush(i32* %23)
%25 = load i32*, i32** %2
%26 = load i32, i32* @SEEK_END
%27 = call i32 @fseek(i32* %25, i64 0, i32 %26)
%28 = load i32*, i32** %2
%29 = call i64 @ftell(i32* %28)
store i64 %29, i64* %3
%30 = load i64, i64* %3
%31 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i64 %30)
%32 = load i32*, i32** %1
%33 = call i32 @fprintf(i32* %32, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0))
%34 = load i32*, i32** %1
%35 = call i32 @fflush(i32* %34)
%36 = load i32*, i32** %2
%37 = load i32, i32* @SEEK_END
%38 = call i32 @fseek(i32* %36, i64 0, i32 %37)
%39 = load i32*, i32** %2
%40 = call i64 @ftell(i32* %39)
store i64 %40, i64* %3
%41 = load i64, i64* %3
%42 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i64 %41)
%43 = load i32*, i32** %2
%44 = call i32 @fclose(i32* %43)
%45 = load i32*, i32** %1
%46 = call i32 @fclose(i32* %45)
%47 = call i32 @remove(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [24 x i8] c"fileio-fopen-twice.temp\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [13 x i8] c"Hello world.\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [9 x i8] c"fout=%p\0A\00"
@.str.5 = [9 x i8] c"fin =%p\0A\00"
@SEEK_END = global i32 0
@.str.6 = [21 x i8] c"File has length %ld\0A\00"
@.str.7 = [18 x i8] c" This is a test.\00"
define i32 @main() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%2 = tail call i64 @fwrite(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i64 12, i64 1, i32* %1)
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%4 = ptrtoint i32* %1 to i64
%5 = tail call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i64 %4)
%6 = ptrtoint i32* %3 to i64
%7 = tail call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i64 %6)
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %3, i64 0, i32 %8)
%10 = tail call i64 @ftell(i32* %3)
%11 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i64 %10)
%12 = tail call i32 @fflush(i32* %1)
%13 = load i32, i32* @SEEK_END
%14 = tail call i32 @fseek(i32* %3, i64 0, i32 %13)
%15 = tail call i64 @ftell(i32* %3)
%16 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i64 %15)
%17 = tail call i64 @fwrite(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), i64 17, i64 1, i32* %1)
%18 = tail call i32 @fflush(i32* %1)
%19 = load i32, i32* @SEEK_END
%20 = tail call i32 @fseek(i32* %3, i64 0, i32 %19)
%21 = tail call i64 @ftell(i32* %3)
%22 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i64 %21)
%23 = tail call i32 @fclose(i32* %3)
%24 = tail call i32 @fclose(i32* %1)
%25 = tail call i32 @remove(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
ret i32 0
}