#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 errno;
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *freopen(char *, char *, int /*<<< orphan*/ *);
int handle_ptr(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
FILE *freopen_test(char *file, char *type, FILE *stream) {
  FILE *newfp = NULL;
  int save_errno;
  int passed;
  printf("freopen(): file=%s, type=%s, stream=0x%p\n",
         (((file) == NULL) ? "(null)" : (file)),
         (((type) == NULL) ? "(null)" : (type)), stream);
  fflush(NULL);
  passed = handle_ptr(newfp = freopen(file, type, stream));
  save_errno = errno;
  switch (passed) {
  case -1:
    printf("\tFailed Phase 1: returned 0x%p, expected 0x%pn", newfp, stream);
    fflush(NULL);
    break;
  case 0:
    printf("\t\tnewfp =  0x%p\n", newfp);
    fflush(NULL);
    if (newfp != stream) {
      printf("\tFailed Phase 1: returned 0x%p, expected 0x%p\n", newfp, stream);
      fflush(NULL);
    } else {
      printf("\tSucceeded Phase 1\n");
      fflush(NULL);
    }
    break;
  case -2:
    printf("\tFailed Phase 1: undefined return code 0x%p\n", newfp);
    fflush(NULL);
    break;
  default:
    printf("Internal syscalltester error: passed = %d\n", passed);
    fflush(NULL);
    break;
  }
  errno = save_errno;
  return newfp;
}


x86 assembly (clang, -O0):
<freopen_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1177 <freopen_test+0x37>
lea    0xebc(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    117f <freopen_test+0x3f>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a2 <freopen_test+0x62>
lea    0xe91(%rip),%rax
mov    %rax,-0x40(%rbp)
jmp    11aa <freopen_test+0x6a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe43(%rip),%rdi
xor    %eax,%eax
mov    %eax,-0x48(%rbp)
call   1030 <printf@plt>
mov    -0x48(%rbp),%eax
mov    %eax,%edi
call   1060 <fflush@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <freopen@plt>
mov    %rax,%rdi
mov    %rdi,-0x20(%rbp)
call   1050 <handle_ptr@plt>
mov    %eax,-0x28(%rbp)
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x44(%rbp)
sub    $0xfffffffe,%eax
je     12bf <freopen_test+0x17f>
jmp    1211 <freopen_test+0xd1>
mov    -0x44(%rbp),%eax
sub    $0xffffffff,%eax
je     1232 <freopen_test+0xf2>
jmp    1222 <freopen_test+0xe2>
mov    -0x44(%rbp),%eax
test   %eax,%eax
je     1256 <freopen_test+0x116>
jmp    12df <freopen_test+0x19f>
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <fflush@plt>
jmp    12f9 <freopen_test+0x1b9>
mov    -0x20(%rbp),%rsi
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <fflush@plt>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     12a3 <freopen_test+0x163>
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <fflush@plt>
jmp    12ba <freopen_test+0x17a>
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <fflush@plt>
jmp    12f9 <freopen_test+0x1b9>
mov    -0x20(%rbp),%rsi
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <fflush@plt>
jmp    12f9 <freopen_test+0x1b9>
mov    -0x28(%rbp),%esi
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <fflush@plt>
mov    -0x24(%rbp),%ecx
mov    0x2cdd(%rip),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<freopen_test>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
test   %rdi,%rdi
lea    0xec6(%rip),%rax
mov    %rdi,%rcx
cmove  %rax,%rcx
mov    %rdx,%r15
mov    %rsi,%rbx
test   %rsi,%rsi
cmovne %rsi,%rax
lea    0xe81(%rip),%rdi
mov    %rcx,%rsi
mov    %rax,%rdx
mov    %r15,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <fflush@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1050 <freopen@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1060 <handle_ptr@plt>
mov    0x2e2a(%rip),%rbx
mov    (%rbx),%ebp
cmp    $0xfffffffe,%eax
je     11cf <freopen_test+0x7f>
test   %eax,%eax
je     11e2 <freopen_test+0x92>
cmp    $0xffffffff,%eax
jne    1215 <freopen_test+0xc5>
lea    0xe64(%rip),%rdi
jmp    1206 <freopen_test+0xb6>
lea    0xeca(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1233 <freopen_test+0xe3>
lea    0xe77(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <fflush@plt>
cmp    %r15,%r14
je     1227 <freopen_test+0xd7>
lea    0xe6b(%rip),%rdi
mov    %r14,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1233 <freopen_test+0xe3>
lea    0xeb1(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1233 <freopen_test+0xe3>
lea    0xeca(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1070 <fflush@plt>
mov    %ebp,(%rbx)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [42 x i8] c"freopen(): file=%s, type=%s, stream=0x%p\0A\00"
@.str.1 = [7 x i8] c"(null)\00"
@errno = global i32 0
@.str.2 = [47 x i8] c"\09Failed Phase 1: returned 0x%p, expected 0x%pn\00"
@.str.3 = [17 x i8] c"\09\09newfp = 0x%p\0A\00"
@.str.4 = [47 x i8] c"\09Failed Phase 1: returned 0x%p, expected 0x%p\0A\00"
@.str.5 = [20 x i8] c"\09Succeeded Phase 1\0A\00"
@.str.6 = [45 x i8] c"\09Failed Phase 1: undefined return code 0x%p\0A\00"
@.str.7 = [43 x i8] c"Internal syscalltester error: passed = %d\0A\00"
define i32* @freopen_test(i8* %0, i8* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32* %2, i32** %6
store i32* null, i32** %7
%10 = load i8*, i8** %4
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %13
12: ; preds = %3
br label %15
13: ; preds = %3
%14 = load i8*, i8** %4
br label %15
15: ; preds = %13, %12
%16 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %12 ], [ %14, %13 ]
%17 = load i8*, i8** %5
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %20
19: ; preds = %15
br label %22
20: ; preds = %15
%21 = load i8*, i8** %5
br label %22
22: ; preds = %20, %19
%23 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %19 ], [ %21, %20 ]
%24 = load i32*, i32** %6
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %16, i8* %23, i32* %24)
%26 = call i32 @fflush(i32* null)
%27 = load i8*, i8** %4
%28 = load i8*, i8** %5
%29 = load i32*, i32** %6
%30 = call i32* @freopen(i8* %27, i8* %28, i32* %29)
store i32* %30, i32** %7
%31 = call i32 @handle_ptr(i32* %30)
store i32 %31, i32* %9
%32 = load i32, i32* @errno
store i32 %32, i32* %8
%33 = load i32, i32* %9
switch i32 %33, label %59 [
i32 -1, label %34
i32 0, label %39
i32 -2, label %55
]
34: ; preds = %22
%35 = load i32*, i32** %7
%36 = load i32*, i32** %6
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32* %35, i32* %36)
%38 = call i32 @fflush(i32* null)
br label %63
39: ; preds = %22
%40 = load i32*, i32** %7
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32* %40)
%42 = call i32 @fflush(i32* null)
%43 = load i32*, i32** %7
%44 = load i32*, i32** %6
%45 = icmp ne i32* %43, %44
br i1 %45, label %46, label %51
46: ; preds = %39
%47 = load i32*, i32** %7
%48 = load i32*, i32** %6
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i32* %47, i32* %48)
%50 = call i32 @fflush(i32* null)
br label %54
51: ; preds = %39
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%53 = call i32 @fflush(i32* null)
br label %54
54: ; preds = %51, %46
br label %63
55: ; preds = %22
%56 = load i32*, i32** %7
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0), i32* %56)
%58 = call i32 @fflush(i32* null)
br label %63
59: ; preds = %22
%60 = load i32, i32* %9
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i32 %60)
%62 = call i32 @fflush(i32* null)
br label %63
63: ; preds = %59, %55, %54, %34
%64 = load i32, i32* %8
store i32 %64, i32* @errno
%65 = load i32*, i32** %7
ret i32* %65
}

LLVM IR (-O2):
@.str = [42 x i8] c"freopen(): file=%s, type=%s, stream=0x%p\0A\00"
@.str.1 = [7 x i8] c"(null)\00"
@errno = global i32 0
@.str.2 = [47 x i8] c"\09Failed Phase 1: returned 0x%p, expected 0x%pn\00"
@.str.3 = [17 x i8] c"\09\09newfp = 0x%p\0A\00"
@.str.4 = [47 x i8] c"\09Failed Phase 1: returned 0x%p, expected 0x%p\0A\00"
@.str.6 = [45 x i8] c"\09Failed Phase 1: undefined return code 0x%p\0A\00"
@.str.7 = [43 x i8] c"Internal syscalltester error: passed = %d\0A\00"
@str = [19 x i8] c"\09Succeeded Phase 1\00"
define i32* @freopen_test(i8* %0, i8* %1, i32* %2) {
%4 = icmp eq i8* %0, null
%5 = select i1 %4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %0
%6 = icmp eq i8* %1, null
%7 = select i1 %6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %5, i8* %7, i32* %2)
%9 = tail call i32 @fflush(i32* null)
%10 = tail call i32* @freopen(i8* %0, i8* %1, i32* %2)
%11 = tail call i32 @handle_ptr(i32* %10)
%12 = load i32, i32* @errno
switch i32 %11, label %25 [
i32 -1, label %13
i32 0, label %15
i32 -2, label %23
]
13: ; preds = %3
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32* %10, i32* %2)
br label %27
15: ; preds = %3
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32* %10)
%17 = tail call i32 @fflush(i32* null)
%18 = icmp eq i32* %10, %2
br i1 %18, label %21, label %19
19: ; preds = %15
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i32* %10, i32* %2)
br label %27
21: ; preds = %15
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %27
23: ; preds = %3
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0), i32* %10)
br label %27
25: ; preds = %3
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i32 %11)
br label %27
27: ; preds = %19, %21, %25, %23, %13
%28 = tail call i32 @fflush(i32* null)
store i32 %12, i32* @errno
ret i32* %10
}