#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 expect_ptr(int /*<<< orphan*/, int /*<<< orphan*/ *);
int expect_vap(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int expect_zng(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fclose_test(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen_test(char *, char *);
int /*<<< orphan*/ *freopen_test(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ testbreak();
int /*<<< orphan*/ unlink_test(char *);
int /*<<< orphan*/ xtmpnam(char *);
int BasicFreopen(void) {
  int block = 0;
  FILE *fp;
  FILE *fp2;
  int passed;
  char tf[8192] = {0};
  xtmpnam(tf);
  testbreak();
  passed = expect_ptr(0, fp = fopen_test(tf, "w+"));
  if (passed != 0) {
    printf("\tFailed Phase 2: This call did something unexpected.\n");
    fflush(NULL);
    block = -1;
  };
  if (passed == -1) {
    printf("\tFail Phase 2: Aborting test because of catastrophic failure\n");
    fflush(NULL);
    return passed;
  };
  passed = expect_vap(fp, fp2 = freopen_test(tf, "ad", fp));
  if (passed != 0) {
    printf("\tFailed Phase 2: This call did something unexpected.\n");
    fflush(NULL);
    block = -1;
  };
  if (passed == -1) {
    printf("\tFail Phase 2: Aborting test because of catastrophic failure\n");
    fflush(NULL);
    return passed;
  };
  passed = expect_zng(0, fclose_test(fp2));
  if (passed != 0) {
    printf("\tFailed Phase 2: This call did something unexpected.\n");
    fflush(NULL);
    block = -1;
  };
  passed = expect_zng(0, unlink_test(tf));
  if (passed != 0) {
    printf("\tFailed Phase 2: This call did something unexpected.\n");
    fflush(NULL);
    block = -1;
  };
  testbreak();
  return block;
}


x86 assembly (clang, -O0):
<BasicFreopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2030,%rsp
movl   $0x0,-0x8(%rbp)
lea    -0x2020(%rbp),%rdi
xor    %esi,%esi
mov    $0x2000,%edx
call   1070 <memset@plt>
lea    -0x2020(%rbp),%rdi
call   1050 <xtmpnam@plt>
mov    $0x0,%al
call   10b0 <testbreak@plt>
lea    -0x2020(%rbp),%rdi
lea    0xdfa(%rip),%rsi
call   10c0 <fopen_test@plt>
mov    %rax,%rsi
mov    %rsi,-0x10(%rbp)
xor    %edi,%edi
call   1040 <expect_ptr@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1242 <BasicFreopen+0x82>
lea    0xdd6(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <fflush@plt>
movl   $0xffffffff,-0x8(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    126c <BasicFreopen+0xac>
lea    0xde6(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <fflush@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    137a <BasicFreopen+0x1ba>
mov    -0x10(%rbp),%rax
mov    %rax,-0x2028(%rbp)
lea    -0x2020(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xdee(%rip),%rsi
call   1030 <freopen_test@plt>
mov    -0x2028(%rbp),%rdi
mov    %rax,%rsi
mov    %rsi,-0x18(%rbp)
call   10d0 <expect_vap@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     12ca <BasicFreopen+0x10a>
lea    0xd4e(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <fflush@plt>
movl   $0xffffffff,-0x8(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12f4 <BasicFreopen+0x134>
lea    0xd5e(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <fflush@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    137a <BasicFreopen+0x1ba>
mov    -0x18(%rbp),%rdi
call   1080 <fclose_test@plt>
mov    %eax,%esi
xor    %edi,%edi
call   1090 <expect_zng@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     132f <BasicFreopen+0x16f>
lea    0xce9(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <fflush@plt>
movl   $0xffffffff,-0x8(%rbp)
lea    -0x2020(%rbp),%rdi
call   10e0 <unlink_test@plt>
mov    %eax,%esi
xor    %edi,%edi
call   1090 <expect_zng@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     136d <BasicFreopen+0x1ad>
lea    0xcab(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <fflush@plt>
movl   $0xffffffff,-0x8(%rbp)
mov    $0x0,%al
call   10b0 <testbreak@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BasicFreopen>:
push   %rbp
push   %r14
push   %rbx
sub    $0x2000,%rsp
mov    %rsp,%rbx
xor    %r14d,%r14d
mov    $0x2000,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
mov    %rbx,%rdi
call   1060 <xtmpnam@plt>
xor    %eax,%eax
call   10b0 <testbreak@plt>
lea    0xe0a(%rip),%rsi
mov    %rbx,%rdi
call   10c0 <fopen_test@plt>
mov    %rax,%rbx
xor    %edi,%edi
mov    %rax,%rsi
call   1050 <expect_ptr@plt>
test   %eax,%eax
je     1233 <BasicFreopen+0x73>
mov    %eax,%ebp
lea    0xe2b(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10a0 <fflush@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%ebp
je     12dc <BasicFreopen+0x11c>
lea    0xdc9(%rip),%rsi
mov    %rsp,%rdi
mov    %rbx,%rdx
call   1030 <freopen_test@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %rax,%rsi
call   10d0 <expect_vap@plt>
test   %eax,%eax
je     1277 <BasicFreopen+0xb7>
mov    %eax,%ebx
lea    0xde3(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10a0 <fflush@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%ebx
je     12dc <BasicFreopen+0x11c>
mov    %rbp,%rdi
call   1080 <fclose_test@plt>
xor    %edi,%edi
mov    %eax,%esi
call   1090 <expect_zng@plt>
test   %eax,%eax
je     12a5 <BasicFreopen+0xe5>
lea    0xdb0(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10a0 <fflush@plt>
mov    $0xffffffff,%r14d
mov    %rsp,%rdi
call   10e0 <unlink_test@plt>
xor    %edi,%edi
mov    %eax,%esi
call   1090 <expect_zng@plt>
test   %eax,%eax
je     12d3 <BasicFreopen+0x113>
lea    0xd82(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10a0 <fflush@plt>
mov    $0xffffffff,%r14d
xor    %eax,%eax
call   10b0 <testbreak@plt>
jmp    12f5 <BasicFreopen+0x135>
lea    0xd23(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10a0 <fflush@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x2000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"w+\00"
@.str.1 = [54 x i8] c"\09Failed Phase 2: This call did something unexpected.\0A\00"
@.str.2 = [62 x i8] c"\09Fail Phase 2: Aborting test because of catastrophic failure\0A\00"
@.str.3 = [3 x i8] c"ad\00"
define i32 @BasicFreopen() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca [8192 x i8]
store i32 0, i32* %2
%7 = bitcast [8192 x i8]* %6 to i8*
%8 = getelementptr inbounds [8192 x i8], [8192 x i8]* %6, i64 0, i64 0
%9 = call i32 @xtmpnam(i8* %8)
%10 = call i32 (...) @testbreak()
%11 = getelementptr inbounds [8192 x i8], [8192 x i8]* %6, i64 0, i64 0
%12 = call i32* @fopen_test(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %3
%13 = call i32 @expect_ptr(i32 0, i32* %12)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %0
%17 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @fflush(i32* null)
store i32 -1, i32* %2
br label %19
19: ; preds = %16, %0
%20 = load i32, i32* %5
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = call i32 @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @fflush(i32* null)
%25 = load i32, i32* %5
store i32 %25, i32* %1
br label %65
26: ; preds = %19
%27 = load i32*, i32** %3
%28 = getelementptr inbounds [8192 x i8], [8192 x i8]* %6, i64 0, i64 0
%29 = load i32*, i32** %3
%30 = call i32* @freopen_test(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %29)
store i32* %30, i32** %4
%31 = call i32 @expect_vap(i32* %27, i32* %30)
store i32 %31, i32* %5
%32 = load i32, i32* %5
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %26
%35 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0))
%36 = call i32 @fflush(i32* null)
store i32 -1, i32* %2
br label %37
37: ; preds = %34, %26
%38 = load i32, i32* %5
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %44
40: ; preds = %37
%41 = call i32 @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.2, i64 0, i64 0))
%42 = call i32 @fflush(i32* null)
%43 = load i32, i32* %5
store i32 %43, i32* %1
br label %65
44: ; preds = %37
%45 = load i32*, i32** %4
%46 = call i32 @fclose_test(i32* %45)
%47 = call i32 @expect_zng(i32 0, i32 %46)
store i32 %47, i32* %5
%48 = load i32, i32* %5
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %44
%51 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0))
%52 = call i32 @fflush(i32* null)
store i32 -1, i32* %2
br label %53
53: ; preds = %50, %44
%54 = getelementptr inbounds [8192 x i8], [8192 x i8]* %6, i64 0, i64 0
%55 = call i32 @unlink_test(i8* %54)
%56 = call i32 @expect_zng(i32 0, i32 %55)
store i32 %56, i32* %5
%57 = load i32, i32* %5
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %53
%60 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0))
%61 = call i32 @fflush(i32* null)
store i32 -1, i32* %2
br label %62
62: ; preds = %59, %53
%63 = call i32 (...) @testbreak()
%64 = load i32, i32* %2
store i32 %64, i32* %1
br label %65
65: ; preds = %62, %40, %22
%66 = load i32, i32* %1
ret i32 %66
}

LLVM IR (-O2):
@.str = [3 x i8] c"w+\00"
@.str.3 = [3 x i8] c"ad\00"
@str.7 = [61 x i8] c"\09Fail Phase 2: Aborting test because of catastrophic failure\00"
@str.8 = [53 x i8] c"\09Failed Phase 2: This call did something unexpected.\00"
define i32 @BasicFreopen() {
%1 = alloca [8192 x i8]
%2 = getelementptr inbounds [8192 x i8], [8192 x i8]* %1, i64 0, i64 0
%3 = call i32 @xtmpnam(i8* %2)
%4 = call i32 (...) @testbreak()
%5 = call i32* @fopen_test(i8* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @expect_ptr(i32 0, i32* %5)
%7 = icmp eq i32 %6, 0
br i1 %7, label %15, label %8
8: ; preds = %0
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str.8, i64 0, i64 0))
%10 = call i32 @fflush(i32* null)
%11 = icmp eq i32 %6, -1
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @str.7, i64 0, i64 0))
%14 = call i32 @fflush(i32* null)
br label %46
15: ; preds = %0, %8
%16 = phi i32 [ -1, %8 ], [ 0, %0 ]
%17 = call i32* @freopen_test(i8* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %5)
%18 = call i32 @expect_vap(i32* %5, i32* %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %27, label %20
20: ; preds = %15
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str.8, i64 0, i64 0))
%22 = call i32 @fflush(i32* null)
%23 = icmp eq i32 %18, -1
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @str.7, i64 0, i64 0))
%26 = call i32 @fflush(i32* null)
br label %46
27: ; preds = %15, %20
%28 = phi i32 [ -1, %20 ], [ %16, %15 ]
%29 = call i32 @fclose_test(i32* %17)
%30 = call i32 @expect_zng(i32 0, i32 %29)
%31 = icmp eq i32 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %27
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str.8, i64 0, i64 0))
%34 = call i32 @fflush(i32* null)
br label %35
35: ; preds = %32, %27
%36 = phi i32 [ -1, %32 ], [ %28, %27 ]
%37 = call i32 @unlink_test(i8* %2)
%38 = call i32 @expect_zng(i32 0, i32 %37)
%39 = icmp eq i32 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %35
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str.8, i64 0, i64 0))
%42 = call i32 @fflush(i32* null)
br label %43
43: ; preds = %40, %35
%44 = phi i32 [ -1, %40 ], [ %36, %35 ]
%45 = call i32 (...) @testbreak()
br label %46
46: ; preds = %43, %24, %12
%47 = phi i32 [ -1, %12 ], [ -1, %24 ], [ %44, %43 ]
ret i32 %47
}