#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*/ exit(int);
scalar_t__ fclose(int /*<<< orphan*/);
int /*<<< orphan*/ *freopen(char const *, char *, int /*<<< orphan*/);
scalar_t__ fwrite(char const *, int, scalar_t__, int /*<<< orphan*/);
char getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
scalar_t__ strlen(char const *);
int main(void) {
  const char *fname = "freopen.dat";
  const char tsttxt[] =
      "A random line of text, used to test correct freopen etc.\n";
  FILE *instream;
  FILE *old_stderr;
  char c1;
  old_stderr = freopen(fname, "w+", stderr);
  if (old_stderr == NULL ||
      fwrite(tsttxt, 1, strlen(tsttxt), stderr) != strlen(tsttxt) ||
      fclose(stderr) != 0) {
    printf("fail\n");
    exit(1);
  }
  instream = freopen(fname, "r", stdin);
  if (instream == NULL) {
    printf("fail\n");
    exit(1);
  }
  c1 = getc(instream);
  if (c1 != 'A') {
    printf("fail\n");
    exit(1);
  }
  printf("pass\n");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe67(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xe78(%rip),%rsi
mov    $0x3a,%edx
call   1070 <memcpy@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
lea    0xe46(%rip),%rsi
call   1060 <freopen@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
je     123a <main+0xba>
lea    -0x50(%rbp),%rax
mov    %rax,-0x78(%rbp)
lea    -0x50(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x78(%rbp),%rdi
mov    %rax,%rdx
mov    0x2ddf(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1090 <fwrite@plt>
mov    %rax,-0x70(%rbp)
lea    -0x50(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x70(%rbp),%rax
cmp    %rcx,%rax
jne    123a <main+0xba>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     1250 <main+0xd0>
lea    0xdce(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    0x2d8d(%rip),%rax
mov    (%rax),%edx
lea    0xdb1(%rip),%rsi
call   1060 <freopen@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    128e <main+0x10e>
lea    0xd90(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x58(%rbp),%rdi
call   10a0 <getc@plt>
mov    %al,-0x61(%rbp)
movsbl -0x61(%rbp),%eax
cmp    $0x41,%eax
je     12bd <main+0x13d>
lea    0xd61(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xd53(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x40,%rsp
movups 0xeca(%rip),%xmm0
movups %xmm0,0x2a(%rsp)
movaps 0xeb4(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe98(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe7c(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    0x2e29(%rip),%r15
mov    (%r15),%edx
lea    0xe47(%rip),%rdi
lea    0xe4c(%rip),%rsi
call   1060 <freopen@plt>
test   %rax,%rax
je     1245 <main+0xd5>
mov    %rsp,%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    (%r15),%ecx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
call   1080 <fwrite@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1050 <strlen@plt>
cmp    %rax,%rbx
jne    1245 <main+0xd5>
mov    (%r15),%edi
call   1040 <fclose@plt>
test   %rax,%rax
jne    1245 <main+0xd5>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edx
lea    0xdeb(%rip),%rdi
lea    0xdf3(%rip),%rsi
call   1060 <freopen@plt>
test   %rax,%rax
je     1245 <main+0xd5>
mov    %rax,%rdi
call   1090 <getc@plt>
cmp    $0x41,%al
jne    1245 <main+0xd5>
lea    0xdd8(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1070 <exit@plt>
lea    0xdca(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"freopen.dat\00"
@__const.main.tsttxt = [58 x i8] c"A random line of text, used to test correct freopen etc.\0A\00"
@.str.1 = [3 x i8] c"w+\00"
@stderr = global i32 0
@.str.2 = [6 x i8] c"fail\0A\00"
@.str.3 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.4 = [6 x i8] c"pass\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca [58 x i8]
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i8
store i32 0, i32* %1
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %2
%7 = bitcast [58 x i8]* %3 to i8*
%8 = load i8*, i8** %2
%9 = load i32, i32* @stderr
%10 = call i32* @freopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %9)
store i32* %10, i32** %5
%11 = load i32*, i32** %5
%12 = icmp eq i32* %11, null
br i1 %12, label %26, label %13
13: ; preds = %0
%14 = getelementptr inbounds [58 x i8], [58 x i8]* %3, i64 0, i64 0
%15 = getelementptr inbounds [58 x i8], [58 x i8]* %3, i64 0, i64 0
%16 = call i64 @strlen(i8* %15)
%17 = load i32, i32* @stderr
%18 = call i64 @fwrite(i8* %14, i32 1, i64 %16, i32 %17)
%19 = getelementptr inbounds [58 x i8], [58 x i8]* %3, i64 0, i64 0
%20 = call i64 @strlen(i8* %19)
%21 = icmp ne i64 %18, %20
br i1 %21, label %26, label %22
22: ; preds = %13
%23 = load i32, i32* @stderr
%24 = call i64 @fclose(i32 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %22, %13, %0
%27 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %22
%30 = load i8*, i8** %2
%31 = load i32, i32* @stdin
%32 = call i32* @freopen(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %31)
store i32* %32, i32** %4
%33 = load i32*, i32** %4
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %38
35: ; preds = %29
%36 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %29
%39 = load i32*, i32** %4
%40 = call signext i8 @getc(i32* %39)
store i8 %40, i8* %6
%41 = load i8, i8* %6
%42 = sext i8 %41 to i32
%43 = icmp ne i32 %42, 65
br i1 %43, label %44, label %47
44: ; preds = %38
%45 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %38
%48 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%49 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [12 x i8] c"freopen.dat\00"
@__const.main.tsttxt = [58 x i8] c"A random line of text, used to test correct freopen etc.\0A\00"
@.str.1 = [3 x i8] c"w+\00"
@stderr = global i32 0
@.str.3 = [2 x i8] c"r\00"
@stdin = global i32 0
@str = [5 x i8] c"pass\00"
@str.7 = [5 x i8] c"fail\00"
define i32 @main() {
%1 = alloca [58 x i8]
%2 = getelementptr inbounds [58 x i8], [58 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @stderr
%4 = tail call i32* @freopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %3)
%5 = icmp eq i32* %4, null
br i1 %5, label %16, label %6
6: ; preds = %0
%7 = call i64 @strlen(i8* %2)
%8 = load i32, i32* @stderr
%9 = call i64 @fwrite(i8* %2, i32 1, i64 %7, i32 %8)
%10 = call i64 @strlen(i8* %2)
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %16
12: ; preds = %6
%13 = load i32, i32* @stderr
%14 = call i64 @fclose(i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %12, %6, %0
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.7, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %12
%20 = load i32, i32* @stdin
%21 = call i32* @freopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.7, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %19
%27 = call signext i8 @getc(i32* %21)
%28 = icmp eq i8 %27, 65
br i1 %28, label %32, label %29
29: ; preds = %26
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.7, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %26
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
%34 = call i32 @exit(i32 0)
unreachable
}