#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*/ array_contains(int *, size_t, int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int fstat(int, struct stat *);
int getdtablesize();
int /*<<< orphan*/ open_devnull(int);
unsigned char spc_sanitize_files(int *a, size_t size, int skip) {
  int fd, fds;
  struct stat st;
  if ((fds = getdtablesize()) == -1) {
    fds = 256;
  }
  for (fd = 3; fd < fds; fd++) {
    if ((fd != skip) && !array_contains(a, size, fd)) {
      close(fd);
    }
  }
  for (fd = 0; fd < 3; fd++) {
    if (fstat(fd, &st) == -1 && (errno != EBADF || !open_devnull(fd))) {
      return (0);
    }
  }
  return (1);
}


x86 assembly (clang, -O0):
<spc_sanitize_files>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    $0x0,%al
call   1070 <getdtablesize@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    117d <spc_sanitize_files+0x2d>
movl   $0x100,-0x24(%rbp)
movl   $0x3,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    11d0 <spc_sanitize_files+0x80>
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     11bd <spc_sanitize_files+0x6d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1050 <array_contains@plt>
cmp    $0x0,%eax
jne    11bd <spc_sanitize_files+0x6d>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
jmp    11c2 <spc_sanitize_files+0x72>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1184 <spc_sanitize_files+0x34>
movl   $0x0,-0x20(%rbp)
cmpl   $0x3,-0x20(%rbp)
jge    123d <spc_sanitize_files+0xed>
mov    -0x20(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
jne    122a <spc_sanitize_files+0xda>
mov    0x2de3(%rip),%rax
mov    (%rax),%rax
mov    0x2dd1(%rip),%rcx
cmp    (%rcx),%rax
jne    1221 <spc_sanitize_files+0xd1>
mov    -0x20(%rbp),%edi
call   1030 <open_devnull@plt>
cmp    $0x0,%eax
jne    122a <spc_sanitize_files+0xda>
movb   $0x0,-0x1(%rbp)
jmp    1241 <spc_sanitize_files+0xf1>
jmp    122f <spc_sanitize_files+0xdf>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11d7 <spc_sanitize_files+0x87>
movb   $0x1,-0x1(%rbp)
movzbl -0x1(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spc_sanitize_files>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r12d
mov    %rsi,%r14
mov    %rdi,%r15
xor    %eax,%eax
call   1070 <getdtablesize@plt>
cmp    $0xffffffff,%eax
mov    $0x100,%ebx
cmovne %eax,%ebx
cmp    $0x4,%ebx
jl     11b6 <spc_sanitize_files+0x66>
mov    $0x3,%ebp
jmp    1197 <spc_sanitize_files+0x47>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%ebp
cmp    %ebp,%ebx
je     11b6 <spc_sanitize_files+0x66>
cmp    %ebp,%r12d
je     1190 <spc_sanitize_files+0x40>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1050 <array_contains@plt>
test   %eax,%eax
jne    1190 <spc_sanitize_files+0x40>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    1190 <spc_sanitize_files+0x40>
lea    0x8(%rsp),%rsi
xor    %edi,%edi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
je     11f3 <spc_sanitize_files+0xa3>
lea    0x8(%rsp),%rsi
mov    $0x1,%edi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
je     1218 <spc_sanitize_files+0xc8>
lea    0x8(%rsp),%rsi
mov    $0x2,%edi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
je     123e <spc_sanitize_files+0xee>
mov    $0x1,%bl
jmp    1264 <spc_sanitize_files+0x114>
mov    0x2de6(%rip),%rax
mov    (%rax),%rax
mov    0x2dd4(%rip),%rcx
cmp    (%rcx),%rax
jne    1262 <spc_sanitize_files+0x112>
xor    %ebx,%ebx
xor    %edi,%edi
call   1030 <open_devnull@plt>
test   %eax,%eax
jne    11c7 <spc_sanitize_files+0x77>
jmp    1264 <spc_sanitize_files+0x114>
mov    0x2dc1(%rip),%rax
mov    (%rax),%rax
mov    0x2daf(%rip),%rcx
cmp    (%rcx),%rax
jne    1262 <spc_sanitize_files+0x112>
mov    $0x1,%edi
call   1030 <open_devnull@plt>
test   %eax,%eax
jne    11db <spc_sanitize_files+0x8b>
jmp    1262 <spc_sanitize_files+0x112>
mov    0x2d9b(%rip),%rax
mov    (%rax),%rax
mov    0x2d89(%rip),%rcx
cmp    (%rcx),%rax
jne    1262 <spc_sanitize_files+0x112>
mov    $0x2,%edi
call   1030 <open_devnull@plt>
test   %eax,%eax
jne    11ef <spc_sanitize_files+0x9f>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@EBADF = global i64 0
define zeroext i8 @spc_sanitize_files(i32* %0, i64 %1, i32 %2) {
%4 = alloca i8
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.stat
store i32* %0, i32** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
%11 = call i32 (...) @getdtablesize()
store i32 %11, i32* %9
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 256, i32* %9
br label %14
14: ; preds = %13, %3
store i32 3, i32* %8
br label %15
15: ; preds = %33, %14
%16 = load i32, i32* %8
%17 = load i32, i32* %9
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %36
19: ; preds = %15
%20 = load i32, i32* %8
%21 = load i32, i32* %7
%22 = icmp ne i32 %20, %21
br i1 %22, label %23, label %32
23: ; preds = %19
%24 = load i32*, i32** %5
%25 = load i64, i64* %6
%26 = load i32, i32* %8
%27 = call i32 @array_contains(i32* %24, i64 %25, i32 %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %23
%30 = load i32, i32* %8
%31 = call i32 @close(i32 %30)
br label %32
32: ; preds = %29, %23, %19
br label %33
33: ; preds = %32
%34 = load i32, i32* %8
%35 = add nsw i32 %34, 1
store i32 %35, i32* %8
br label %15
36: ; preds = %15
store i32 0, i32* %8
br label %37
37: ; preds = %54, %36
%38 = load i32, i32* %8
%39 = icmp slt i32 %38, 3
br i1 %39, label %40, label %57
40: ; preds = %37
%41 = load i32, i32* %8
%42 = call i32 @fstat(i32 %41, %struct.stat* %10)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %53
44: ; preds = %40
%45 = load i64, i64* @errno
%46 = load i64, i64* @EBADF
%47 = icmp ne i64 %45, %46
br i1 %47, label %52, label %48
48: ; preds = %44
%49 = load i32, i32* %8
%50 = call i32 @open_devnull(i32 %49)
%51 = icmp ne i32 %50, 0
br i1 %51, label %53, label %52
52: ; preds = %48, %44
store i8 0, i8* %4
br label %58
53: ; preds = %48, %40
br label %54
54: ; preds = %53
%55 = load i32, i32* %8
%56 = add nsw i32 %55, 1
store i32 %56, i32* %8
br label %37
57: ; preds = %37
store i8 1, i8* %4
br label %58
58: ; preds = %57, %52
%59 = load i8, i8* %4
ret i8 %59
}

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