#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_SUCCESS;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
scalar_t__ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char);
int /*<<< orphan*/ unlink(char *);
int main(void) {
  FILE *fs = fopen("fileio-fopen-modify.temp", "w");
  fprintf(fs, "Hello world\n");
  fclose(fs);
  fs = fopen("fileio-fopen-modify.temp", "r");
  unlink("fileio-fopen-modify.temp");
  char c;
  while (fread(&c, 1, 1, fs) > 0)
    printf("%c", c);
  fclose(fs);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
lea    0xe9c(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe8a(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xe5a(%rip),%rdi
lea    0xe7b(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    0xe43(%rip),%rdi
call   1030 <unlink@plt>
mov    -0x10(%rbp),%rcx
lea    -0x11(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
cmp    $0x0,%rax
jle    11f5 <main+0x95>
lea    0xe43(%rip),%rdi
movsbl -0x11(%rbp),%esi
call   1060 <printf@plt>
jmp    11c2 <main+0x62>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xe90(%rip),%r14
lea    0xea2(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
mov    %rax,%rbx
lea    0xe92(%rip),%rdi
mov    $0xc,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1080 <fwrite@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xe7e(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <unlink@plt>
lea    0xf(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
test   %rax,%rax
jle    1217 <main+0xb7>
lea    0xe4a(%rip),%r14
lea    0xf(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nop
movsbl 0xf(%rsp),%esi
mov    %r14,%rdi
call   1060 <printf@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
test   %rax,%rax
jg     11f0 <main+0x90>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [25 x i8] c"fileio-fopen-modify.temp\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [13 x i8] c"Hello world\0A\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [3 x i8] c"%c\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i8
store i32 0, i32* %1
%4 = call i32* @fopen(i8* getelementptr inbounds ([25 x i8], [25 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** %2
%5 = load i32*, i32** %2
%6 = call i32 @fprintf(i32* %5, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%7 = load i32*, i32** %2
%8 = call i32 @fclose(i32* %7)
%9 = call i32* @fopen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %9, i32** %2
%10 = call i32 @unlink(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %15, %0
%12 = load i32*, i32** %2
%13 = call i64 @fread(i8* %3, i32 1, i32 1, i32* %12)
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i8, i8* %3
%17 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8 signext %16)
br label %11
18: ; preds = %11
%19 = load i32*, i32** %2
%20 = call i32 @fclose(i32* %19)
%21 = load i32, i32* @EXIT_SUCCESS
ret i32 %21
}

LLVM IR (-O2):
@.str = [25 x i8] c"fileio-fopen-modify.temp\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [13 x i8] c"Hello world\0A\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [3 x i8] c"%c\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i8
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = tail call i64 @fwrite(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i64 12, i64 1, i32* %2)
%4 = tail call i32 @fclose(i32* %2)
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%6 = tail call i32 @unlink(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%7 = call i64 @fread(i8* %1, i32 1, i32 1, i32* %5)
%8 = icmp sgt i64 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %0, %9
%10 = load i8, i8* %1
%11 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8 signext %10)
%12 = call i64 @fread(i8* %1, i32 1, i32 1, i32* %5)
%13 = icmp sgt i64 %12, 0
br i1 %13, label %9, label %14
14: ; preds = %9, %0
%15 = tail call i32 @fclose(i32* %5)
%16 = load i32, i32* @EXIT_SUCCESS
ret i32 %16
}