#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 */
struct stat {
  int dummy;
};

/* Variables and functions */
scalar_t__ EBADF;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int fstat(int, struct stat *);
int getdtablesize();
int /*<<< orphan*/ open_devnull(int);
void spc_sanitize_files(void) {
  int fd, fds;
  struct stat st;
  if ((fds = getdtablesize()) == -1)
    fds = 256;
  for (fd = 3; fd < fds; fd++)
    close(fd);
  for (fd = 0; fd < 3; fd++)
    if (fstat(fd, &st) == -1 && (errno != EBADF || !open_devnull(fd)))
      abort();
}


x86 assembly (clang, -O0):
<spc_sanitize_files>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1070 <getdtablesize@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1172 <spc_sanitize_files+0x22>
movl   $0x100,-0x8(%rbp)
movl   $0x3,-0x4(%rbp)
mov    -0x4(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    119b <spc_sanitize_files+0x4b>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1179 <spc_sanitize_files+0x29>
movl   $0x0,-0x4(%rbp)
cmpl   $0x3,-0x4(%rbp)
jge    1206 <spc_sanitize_files+0xb6>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11f3 <spc_sanitize_files+0xa3>
mov    0x2e18(%rip),%rax
mov    (%rax),%rax
mov    0x2e06(%rip),%rcx
cmp    (%rcx),%rax
jne    11ec <spc_sanitize_files+0x9c>
mov    -0x4(%rbp),%edi
call   1040 <open_devnull@plt>
cmp    $0x0,%eax
jne    11f3 <spc_sanitize_files+0xa3>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    11f8 <spc_sanitize_files+0xa8>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11a2 <spc_sanitize_files+0x52>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spc_sanitize_files>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1070 <getdtablesize@plt>
cmp    $0xffffffff,%eax
mov    $0x100,%ebp
cmovne %eax,%ebp
cmp    $0x4,%ebp
jl     117e <spc_sanitize_files+0x2e>
mov    $0x3,%ebx
nop
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x1,%ebx
cmp    %ebx,%ebp
jne    1170 <spc_sanitize_files+0x20>
mov    %rsp,%rsi
xor    %edi,%edi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11ae <spc_sanitize_files+0x5e>
mov    0x2e4c(%rip),%rax
mov    (%rax),%rax
mov    0x2e3a(%rip),%rcx
cmp    (%rcx),%rax
jne    1221 <spc_sanitize_files+0xd1>
xor    %edi,%edi
call   1040 <open_devnull@plt>
test   %eax,%eax
je     1221 <spc_sanitize_files+0xd1>
mov    %rsp,%rsi
mov    $0x1,%edi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11e4 <spc_sanitize_files+0x94>
mov    0x2e19(%rip),%rax
mov    (%rax),%rax
mov    0x2e07(%rip),%rcx
cmp    (%rcx),%rax
jne    1221 <spc_sanitize_files+0xd1>
mov    $0x1,%edi
call   1040 <open_devnull@plt>
test   %eax,%eax
je     1221 <spc_sanitize_files+0xd1>
mov    %rsp,%rsi
mov    $0x2,%edi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
jne    121a <spc_sanitize_files+0xca>
mov    0x2de3(%rip),%rax
mov    (%rax),%rax
mov    0x2dd1(%rip),%rcx
cmp    (%rcx),%rax
jne    1221 <spc_sanitize_files+0xd1>
mov    $0x2,%edi
call   1040 <open_devnull@plt>
test   %eax,%eax
je     1221 <spc_sanitize_files+0xd1>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@EBADF = global i64 0
define void @spc_sanitize_files() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.stat
%4 = call i32 (...) @getdtablesize()
store i32 %4, i32* %2
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %7
6: ; preds = %0
store i32 256, i32* %2
br label %7
7: ; preds = %6, %0
store i32 3, i32* %1
br label %8
8: ; preds = %15, %7
%9 = load i32, i32* %1
%10 = load i32, i32* %2
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %18
12: ; preds = %8
%13 = load i32, i32* %1
%14 = call i32 @close(i32 %13)
br label %15
15: ; preds = %12
%16 = load i32, i32* %1
%17 = add nsw i32 %16, 1
store i32 %17, i32* %1
br label %8
18: ; preds = %8
store i32 0, i32* %1
br label %19
19: ; preds = %37, %18
%20 = load i32, i32* %1
%21 = icmp slt i32 %20, 3
br i1 %21, label %22, label %40
22: ; preds = %19
%23 = load i32, i32* %1
%24 = call i32 @fstat(i32 %23, %struct.stat* %3)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %36
26: ; preds = %22
%27 = load i64, i64* @errno
%28 = load i64, i64* @EBADF
%29 = icmp ne i64 %27, %28
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = load i32, i32* %1
%32 = call i32 @open_devnull(i32 %31)
%33 = icmp ne i32 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %30, %26
%35 = call i32 (...) @abort()
unreachable
36: ; preds = %30, %22
br label %37
37: ; preds = %36
%38 = load i32, i32* %1
%39 = add nsw i32 %38, 1
store i32 %39, i32* %1
br label %19
40: ; preds = %19
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@EBADF = global i64 0
define void @spc_sanitize_files() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = tail call i32 (...) @getdtablesize()
%4 = icmp eq i32 %3, -1
%5 = select i1 %4, i32 256, i32 %3
%6 = icmp sgt i32 %5, 3
br i1 %6, label %10, label %7
7: ; preds = %10, %0
%8 = call i32 @fstat(i32 0, %struct.stat* %1)
%9 = icmp eq i32 %8, -1
br i1 %9, label %15, label %24
10: ; preds = %0, %10
%11 = phi i32 [ %13, %10 ], [ 3, %0 ]
%12 = tail call i32 @close(i32 %11)
%13 = add nuw nsw i32 %11, 1
%14 = icmp eq i32 %13, %5
br i1 %14, label %7, label %10
15: ; preds = %7
%16 = load i64, i64* @errno
%17 = load i64, i64* @EBADF
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = tail call i32 @open_devnull(i32 0)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %41, %37, %31, %27, %19, %15
%23 = tail call i32 (...) @abort()
unreachable
24: ; preds = %7, %19
%25 = call i32 @fstat(i32 1, %struct.stat* %1)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %34
27: ; preds = %24
%28 = load i64, i64* @errno
%29 = load i64, i64* @EBADF
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %22
31: ; preds = %27
%32 = tail call i32 @open_devnull(i32 1)
%33 = icmp eq i32 %32, 0
br i1 %33, label %22, label %34
34: ; preds = %31, %24
%35 = call i32 @fstat(i32 2, %struct.stat* %1)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %44
37: ; preds = %34
%38 = load i64, i64* @errno
%39 = load i64, i64* @EBADF
%40 = icmp eq i64 %38, %39
br i1 %40, label %41, label %22
41: ; preds = %37
%42 = tail call i32 @open_devnull(i32 2)
%43 = icmp eq i32 %42, 0
br i1 %43, label %22, label %44
44: ; preds = %41, %34
ret void
}