#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*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ *freopen(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdout;
void freopen_t() {
  FILE *fp;
  printf("This text is redirected to stdout\n");
  fp = freopen("file.txt", "w+", stdout);
  printf("This text is redirected to file.txt\n");
  fclose(fp);
  int c;
  fp = fopen("file.txt", "r");
  while (1) {
    c = fgetc(fp);
    if (feof(fp)) {
      break;
    }
    printf("%c", c);
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<freopen_t>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e63(%rip),%rax
mov    (%rax),%edx
lea    0xe9d(%rip),%rdi
lea    0xe9f(%rip),%rsi
call   1050 <freopen@plt>
mov    %rax,-0x8(%rbp)
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe6f(%rip),%rdi
lea    0xe99(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%rax
je     11e8 <freopen_t+0x88>
jmp    11fe <freopen_t+0x9e>
mov    -0xc(%rbp),%esi
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11c4 <freopen_t+0x64>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<freopen_t>:
push   %rbp
push   %rbx
push   %rax
lea    0xe8b(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%edx
lea    0xeb3(%rip),%rbx
lea    0xe6a(%rip),%rsi
mov    %rbx,%rdi
call   1060 <freopen@plt>
mov    %rax,%rbp
lea    0xe7f(%rip),%rdi
call   1040 <puts@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
lea    0xe47(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
nopw   0x0(%rax,%rax,1)
call   1070 <fgetc@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1080 <feof@plt>
test   %rax,%rax
jne    11f0 <freopen_t+0x80>
mov    %ebp,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
jmp    11d0 <freopen_t+0x60>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <fclose@plt>

LLVM IR (-O0):
@.str = [35 x i8] c"This text is redirected to stdout\0A\00"
@.str.1 = [9 x i8] c"file.txt\00"
@.str.2 = [3 x i8] c"w+\00"
@stdout = global i32 0
@.str.3 = [37 x i8] c"This text is redirected to file.txt\0A\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [3 x i8] c"%c\00"
define void @freopen_t() {
%1 = alloca i32*
%2 = alloca i32
%3 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%4 = load i32, i32* @stdout
%5 = call i32* @freopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %4)
store i32* %5, i32** %1
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
%7 = load i32*, i32** %1
%8 = call i32 @fclose(i32* %7)
%9 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %9, i32** %1
br label %10
10: ; preds = %0, %17
%11 = load i32*, i32** %1
%12 = call i32 @fgetc(i32* %11)
store i32 %12, i32* %2
%13 = load i32*, i32** %1
%14 = call i64 @feof(i32* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %10
br label %20
17: ; preds = %10
%18 = load i32, i32* %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %18)
br label %10
20: ; preds = %16
%21 = load i32*, i32** %1
%22 = call i32 @fclose(i32* %21)
ret void
}

LLVM IR (-O2):
@.str.1 = [9 x i8] c"file.txt\00"
@.str.2 = [3 x i8] c"w+\00"
@stdout = global i32 0
@.str.4 = [2 x i8] c"r\00"
@str = [34 x i8] c"This text is redirected to stdout\00"
@str.6 = [36 x i8] c"This text is redirected to file.txt\00"
define void @freopen_t() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%2 = load i32, i32* @stdout
%3 = tail call i32* @freopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %2)
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.6, i64 0, i64 0))
%5 = tail call i32 @fclose(i32* %3)
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%7 = tail call i32 @fgetc(i32* %6)
%8 = tail call i64 @feof(i32* %6)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %16
10: ; preds = %0, %10
%11 = phi i32 [ %13, %10 ], [ %7, %0 ]
%12 = tail call i32 @putchar(i32 %11)
%13 = tail call i32 @fgetc(i32* %6)
%14 = tail call i64 @feof(i32* %6)
%15 = icmp eq i64 %14, 0
br i1 %15, label %10, label %16
16: ; preds = %10, %0
%17 = tail call i32 @fclose(i32* %6)
ret void
}