#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 */
scalar_t__ EBADF;
scalar_t__ errno;
int fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *op_freopen(char const *, char const *,
                               int /*<<< orphan*/ *);
FILE *freopen(const char *path, const char *mode, FILE *stream) {
  int retval;
  do {
  } while (0);
  retval = fclose(stream);
  if (retval < 0 && errno != EBADF)
    return NULL;
  return op_freopen(path, mode, stream);
}


x86 assembly (clang, -O0):
<freopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
jmp    1139 <freopen+0x19>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1176 <freopen+0x56>
mov    0x2e8a(%rip),%rax
mov    (%rax),%rax
mov    0x2e78(%rip),%rcx
cmp    (%rcx),%rax
je     1176 <freopen+0x56>
movq   $0x0,-0x8(%rbp)
jmp    118b <freopen+0x6b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <op_freopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<freopen>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rdx,%rdi
call   1030 <fclose@plt>
test   %eax,%eax
jns    1150 <freopen+0x30>
mov    0x2e9f(%rip),%rax
mov    (%rax),%rax
mov    0x2e8d(%rip),%rcx
cmp    (%rcx),%rax
jne    1163 <freopen+0x43>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <op_freopen@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@EBADF = global i64 0
define i32* @freopen(i8* %0, i8* %1, i32* %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
br label %9
9: ; preds = %3
br label %10
10: ; preds = %9
%11 = load i32*, i32** %7
%12 = call i32 @fclose(i32* %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %10
%16 = load i64, i64* @errno
%17 = load i64, i64* @EBADF
%18 = icmp ne i64 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %15
store i32* null, i32** %4
br label %25
20: ; preds = %15, %10
%21 = load i8*, i8** %5
%22 = load i8*, i8** %6
%23 = load i32*, i32** %7
%24 = call i32* @op_freopen(i8* %21, i8* %22, i32* %23)
store i32* %24, i32** %4
br label %25
25: ; preds = %20, %19
%26 = load i32*, i32** %4
ret i32* %26
}

LLVM IR (-O2):
@errno = global i64 0
@EBADF = global i64 0
define i32* @freopen(i8* %0, i8* %1, i32* %2) {
%4 = tail call i32 @fclose(i32* %2)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = load i64, i64* @errno
%8 = load i64, i64* @EBADF
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %12
10: ; preds = %6, %3
%11 = tail call i32* @op_freopen(i8* %0, i8* %1, i32* %2)
br label %12
12: ; preds = %6, %10
%13 = phi i32* [ %11, %10 ], [ null, %6 ]
ret i32* %13
}