#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*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ freopen(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  FILE *fp;
  if (argc < 2) {
    fprintf(stdout, "usage:%s filename\n", argv[0]);
    return -1;
  }
  if ((fp = fopen(argv[1], "r+")) == NULL) {
    perror("fopen");
    return -1;
  }
  printf("before freopen\n");
  freopen(argv[1], "w", stdout);
  printf("after freopen\n");
  freopen("/dev/tty", "w", stdout);
  printf("end main\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1198 <main+0x48>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe79(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1236 <main+0xe6>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe6c(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11d2 <main+0x82>
lea    0xe55(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1236 <main+0xe6>
lea    0xe43(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2df3(%rip),%rax
mov    (%rax),%edx
lea    0xe36(%rip),%rsi
call   1040 <freopen@plt>
lea    0xe2c(%rip),%rdi
call   1030 <printf@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
lea    0xe26(%rip),%rdi
lea    0xe0e(%rip),%rsi
call   1040 <freopen@plt>
lea    0xe1c(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     117b <main+0x2b>
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe91(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    11e3 <main+0x93>
mov    0x8(%rbx),%rdi
lea    0xe8d(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11eb <main+0x9b>
lea    0xe90(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rbx),%rdi
mov    0x2e39(%rip),%rbx
mov    (%rbx),%edx
lea    0xe6c(%rip),%r14
mov    %r14,%rsi
call   1040 <freopen@plt>
lea    0xe77(%rip),%rdi
call   1030 <puts@plt>
mov    (%rbx),%edx
lea    0xe51(%rip),%rdi
mov    %r14,%rsi
call   1040 <freopen@plt>
lea    0xe68(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe24(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
jmp    11e3 <main+0x93>

LLVM IR (-O0):
@stdout = global i32 0
@.str = [19 x i8] c"usage:%s filename\0A\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [16 x i8] c"before freopen\0A\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [15 x i8] c"after freopen\0A\00"
@.str.6 = [9 x i8] c"/dev/tty\00"
@.str.7 = [10 x i8] c"end main\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 2
br i1 %8, label %9, label %15
9: ; preds = %2
%10 = load i32, i32* @stdout
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %13)
store i32 -1, i32* %3
br label %34
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %6
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %34
23: ; preds = %15
%24 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = load i32, i32* @stdout
%29 = call i32 @freopen(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 %28)
%30 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%31 = load i32, i32* @stdout
%32 = call i32 @freopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 %31)
%33 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %3
br label %34
34: ; preds = %23, %21, %9
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [19 x i8] c"usage:%s filename\0A\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.4 = [2 x i8] c"w\00"
@.str.6 = [9 x i8] c"/dev/tty\00"
@str = [15 x i8] c"before freopen\00"
@str.8 = [14 x i8] c"after freopen\00"
@str.9 = [9 x i8] c"end main\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* @stdout
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %24
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 ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %24
15: ; preds = %8
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%17 = load i8*, i8** %9
%18 = load i32, i32* @stdout
%19 = tail call i32 @freopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 %18)
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.8, i64 0, i64 0))
%21 = load i32, i32* @stdout
%22 = tail call i32 @freopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 %21)
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.9, i64 0, i64 0))
br label %24
24: ; preds = %15, %13, %4
%25 = phi i32 [ -1, %4 ], [ -1, %13 ], [ 0, %15 ]
ret i32 %25
}