#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;
int EOF;
scalar_t__ EPIPE;
scalar_t__ __fpending(int /*<<< orphan*/ *);
scalar_t__ errno;
scalar_t__ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int close_stream(FILE *stream) {
  const int some_pending = (__fpending(stream) != 0);
  const int prev_fail = (ferror(stream) != 0);
  const int fclose_fail = (fclose(stream) != 0);
  if (prev_fail || (fclose_fail && (some_pending || errno != EBADF))) {
    if (!fclose_fail && !(errno == EPIPE))
      errno = 0;
    return EOF;
  }
  return 0;
}


x86 assembly (clang, -O0):
<close_stream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <__fpending@plt>
cmp    $0x0,%rax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
cmp    $0x0,%rax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    11bc <close_stream+0x8c>
cmpl   $0x0,-0x1c(%rbp)
je     11ff <close_stream+0xcf>
cmpl   $0x0,-0x14(%rbp)
jne    11bc <close_stream+0x8c>
mov    0x2e27(%rip),%rax
mov    (%rax),%rax
mov    0x2e15(%rip),%rcx
cmp    (%rcx),%rax
je     11ff <close_stream+0xcf>
cmpl   $0x0,-0x1c(%rbp)
jne    11ee <close_stream+0xbe>
mov    0x2e03(%rip),%rax
mov    (%rax),%rax
mov    0x2e01(%rip),%rcx
cmp    (%rcx),%rax
je     11ee <close_stream+0xbe>
mov    0x2de9(%rip),%rax
movq   $0x0,(%rax)
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1206 <close_stream+0xd6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<close_stream>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1030 <__fpending@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <ferror@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %rax,%rcx
test   %r15,%r15
je     117f <close_stream+0x4f>
test   %rcx,%rcx
jne    11a1 <close_stream+0x71>
mov    0x2e69(%rip),%rax
mov    (%rax),%rcx
mov    0x2e67(%rip),%rdx
cmp    (%rdx),%rcx
je     11a1 <close_stream+0x71>
movq   $0x0,(%rax)
jmp    11a1 <close_stream+0x71>
xor    %eax,%eax
test   %rcx,%rcx
je     11aa <close_stream+0x7a>
test   %r14,%r14
jne    11a1 <close_stream+0x71>
mov    0x2e3e(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e2c(%rip),%rdx
cmp    (%rdx),%rcx
je     11aa <close_stream+0x7a>
mov    0x2e40(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@EBADF = global i64 0
@EPIPE = global i64 0
@EOF = global i32 0
define i32 @close_stream(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %3
%7 = load i32*, i32** %3
%8 = call i64 @__fpending(i32* %7)
%9 = icmp ne i64 %8, 0
%10 = zext i1 %9 to i32
store i32 %10, i32* %4
%11 = load i32*, i32** %3
%12 = call i64 @ferror(i32* %11)
%13 = icmp ne i64 %12, 0
%14 = zext i1 %13 to i32
store i32 %14, i32* %5
%15 = load i32*, i32** %3
%16 = call i64 @fclose(i32* %15)
%17 = icmp ne i64 %16, 0
%18 = zext i1 %17 to i32
store i32 %18, i32* %6
%19 = load i32, i32* %5
%20 = icmp ne i32 %19, 0
br i1 %20, label %31, label %21
21: ; preds = %1
%22 = load i32, i32* %6
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %41
24: ; preds = %21
%25 = load i32, i32* %4
%26 = icmp ne i32 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %24
%28 = load i64, i64* @errno
%29 = load i64, i64* @EBADF
%30 = icmp ne i64 %28, %29
br i1 %30, label %31, label %41
31: ; preds = %27, %24, %1
%32 = load i32, i32* %6
%33 = icmp ne i32 %32, 0
br i1 %33, label %39, label %34
34: ; preds = %31
%35 = load i64, i64* @errno
%36 = load i64, i64* @EPIPE
%37 = icmp eq i64 %35, %36
br i1 %37, label %39, label %38
38: ; preds = %34
store i64 0, i64* @errno
br label %39
39: ; preds = %38, %34, %31
%40 = load i32, i32* @EOF
store i32 %40, i32* %2
br label %42
41: ; preds = %27, %21
store i32 0, i32* %2
br label %42
42: ; preds = %41, %39
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
@errno = global i64 0
@EBADF = global i64 0
@EPIPE = global i64 0
@EOF = global i32 0
define i32 @close_stream(i32* %0) {
%2 = tail call i64 @__fpending(i32* %0)
%3 = icmp eq i64 %2, 0
%4 = tail call i64 @ferror(i32* %0)
%5 = icmp eq i64 %4, 0
%6 = tail call i64 @fclose(i32* %0)
%7 = icmp eq i64 %6, 0
br i1 %5, label %8, label %14
8: ; preds = %1
br i1 %7, label %22, label %9
9: ; preds = %8
br i1 %3, label %10, label %20
10: ; preds = %9
%11 = load i64, i64* @errno
%12 = load i64, i64* @EBADF
%13 = icmp eq i64 %11, %12
br i1 %13, label %22, label %20
14: ; preds = %1
br i1 %7, label %15, label %20
15: ; preds = %14
%16 = load i64, i64* @errno
%17 = load i64, i64* @EPIPE
%18 = icmp eq i64 %16, %17
br i1 %18, label %20, label %19
19: ; preds = %15
store i64 0, i64* @errno
br label %20
20: ; preds = %9, %10, %19, %15, %14
%21 = load i32, i32* @EOF
br label %22
22: ; preds = %8, %10, %20
%23 = phi i32 [ %21, %20 ], [ 0, %10 ], [ 0, %8 ]
ret i32 %23
}