#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 */

/* Variables and functions */
scalar_t__ EBADF;
int STDERR_FILENO;
int /*<<< orphan*/ cctools_debug(long long, char *, int /*<<< orphan*/);
int close(int);
scalar_t__ errno;
int fd_max();
int /*<<< orphan*/ strerror(scalar_t__);
int fd_nonstd_close(void) {
  int fd, max = fd_max();
  for (fd = STDERR_FILENO + 1; fd < max; fd++) {
    if (close(fd) == -1 && errno != EBADF) {
      cctools_debug((1LL << 3), "could not close open file descriptor: %s",
                    strerror(errno));
      return errno;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<fd_nonstd_close>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1050 <fd_max@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e7f(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11df <fd_nonstd_close+0x9f>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
cmp    $0xffffffff,%eax
jne    11cc <fd_nonstd_close+0x8c>
mov    0x2e5b(%rip),%rax
mov    (%rax),%rax
mov    0x2e41(%rip),%rcx
cmp    (%rcx),%rax
je     11cc <fd_nonstd_close+0x8c>
mov    0x2e41(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    %eax,%edx
mov    $0x8,%edi
lea    0xe4b(%rip),%rsi
call   1040 <cctools_debug@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
jmp    11e6 <fd_nonstd_close+0xa6>
jmp    11d1 <fd_nonstd_close+0x91>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1161 <fd_nonstd_close+0x21>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_nonstd_close>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <fd_max@plt>
mov    %eax,%ebp
mov    0x2e7d(%rip),%rax
mov    (%rax),%ebx
add    $0x1,%ebx
cmp    %ebp,%ebx
jge    11b7 <fd_nonstd_close+0x77>
mov    0x2e75(%rip),%r15
mov    0x2e5e(%rip),%r12
jmp    1187 <fd_nonstd_close+0x47>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%ebx
cmp    %ebx,%ebp
je     11b7 <fd_nonstd_close+0x77>
mov    %ebx,%edi
call   1030 <close@plt>
cmp    $0xffffffff,%eax
jne    1180 <fd_nonstd_close+0x40>
mov    (%r15),%rdi
cmp    (%r12),%rdi
je     1180 <fd_nonstd_close+0x40>
call   1060 <strerror@plt>
lea    0xe58(%rip),%rsi
mov    $0x8,%edi
mov    %eax,%edx
call   1040 <cctools_debug@plt>
mov    (%r15),%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@STDERR_FILENO = global i32 0
@errno = global i64 0
@EBADF = global i64 0
@.str = [41 x i8] c"could not close open file descriptor: %s\00"
define i32 @fd_nonstd_close() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = call i32 (...) @fd_max()
store i32 %4, i32* %3
%5 = load i32, i32* @STDERR_FILENO
%6 = add nsw i32 %5, 1
store i32 %6, i32* %2
br label %7
7: ; preds = %26, %0
%8 = load i32, i32* %2
%9 = load i32, i32* %3
%10 = icmp slt i32 %8, %9
br i1 %10, label %11, label %29
11: ; preds = %7
%12 = load i32, i32* %2
%13 = call i32 @close(i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %25
15: ; preds = %11
%16 = load i64, i64* @errno
%17 = load i64, i64* @EBADF
%18 = icmp ne i64 %16, %17
br i1 %18, label %19, label %25
19: ; preds = %15
%20 = load i64, i64* @errno
%21 = call i32 @strerror(i64 %20)
%22 = call i32 @cctools_debug(i64 8, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = load i64, i64* @errno
%24 = trunc i64 %23 to i32
store i32 %24, i32* %1
br label %30
25: ; preds = %15, %11
br label %26
26: ; preds = %25
%27 = load i32, i32* %2
%28 = add nsw i32 %27, 1
store i32 %28, i32* %2
br label %7
29: ; preds = %7
store i32 0, i32* %1
br label %30
30: ; preds = %29, %19
%31 = load i32, i32* %1
ret i32 %31
}

LLVM IR (-O2):
@STDERR_FILENO = global i32 0
@errno = global i64 0
@EBADF = global i64 0
@.str = [41 x i8] c"could not close open file descriptor: %s\00"
define i32 @fd_nonstd_close() {
%1 = tail call i32 (...) @fd_max()
%2 = load i32, i32* @STDERR_FILENO
%3 = add nsw i32 %2, 1
%4 = icmp slt i32 %3, %1
br i1 %4, label %5, label %21
5: ; preds = %0, %18
%6 = phi i32 [ %19, %18 ], [ %3, %0 ]
%7 = tail call i32 @close(i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %18
9: ; preds = %5
%10 = load i64, i64* @errno
%11 = load i64, i64* @EBADF
%12 = icmp eq i64 %10, %11
br i1 %12, label %18, label %13
13: ; preds = %9
%14 = tail call i32 @strerror(i64 %10)
%15 = tail call i32 @cctools_debug(i64 8, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = load i64, i64* @errno
%17 = trunc i64 %16 to i32
br label %21
18: ; preds = %5, %9
%19 = add i32 %6, 1
%20 = icmp eq i32 %19, %1
br i1 %20, label %21, label %5
21: ; preds = %18, %0, %13
%22 = phi i32 [ %17, %13 ], [ 0, %0 ], [ 0, %18 ]
ret i32 %22
}