#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*/ _SC_OPEN_MAX;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int fstat(int, struct stat *);
int getdtablesize();
int /*<<< orphan*/ reopen_devnull(int);
int sysconf(int /*<<< orphan*/);
void sancus_sanitize_files(void) {
  int fd, fds;
  struct stat st;
  if ((fds = getdtablesize()) == -1)
    fds = sysconf(_SC_OPEN_MAX);
  for (fd = 3; fd < fds; fd++)
    close(fd);
  for (fd = 0; fd < 3; fd++)
    if (fstat(fd, &st) == -1 && (errno != EBADF || !reopen_devnull(fd)))
      abort();
}


x86 assembly (clang, -O0):
<sancus_sanitize_files>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1080 <getdtablesize@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    118c <sancus_sanitize_files+0x2c>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %eax,-0x8(%rbp)
movl   $0x3,-0x4(%rbp)
mov    -0x4(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11b5 <sancus_sanitize_files+0x55>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1193 <sancus_sanitize_files+0x33>
movl   $0x0,-0x4(%rbp)
cmpl   $0x3,-0x4(%rbp)
jge    1220 <sancus_sanitize_files+0xc0>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0xffffffff,%eax
jne    120d <sancus_sanitize_files+0xad>
mov    0x2df6(%rip),%rax
mov    (%rax),%rax
mov    0x2de4(%rip),%rcx
cmp    (%rcx),%rax
jne    1206 <sancus_sanitize_files+0xa6>
mov    -0x4(%rbp),%edi
call   1050 <reopen_devnull@plt>
cmp    $0x0,%eax
jne    120d <sancus_sanitize_files+0xad>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    1212 <sancus_sanitize_files+0xb2>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11bc <sancus_sanitize_files+0x5c>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sancus_sanitize_files>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1080 <getdtablesize@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1178 <sancus_sanitize_files+0x18>
cmp    $0x4,%ebx
jge    118d <sancus_sanitize_files+0x2d>
jmp    11ae <sancus_sanitize_files+0x4e>
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %eax,%ebx
cmp    $0x4,%ebx
jl     11ae <sancus_sanitize_files+0x4e>
mov    $0x3,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%edi
call   1040 <close@plt>
add    $0x1,%ebp
cmp    %ebp,%ebx
jne    11a0 <sancus_sanitize_files+0x40>
mov    %rsp,%rsi
xor    %edi,%edi
call   1070 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11de <sancus_sanitize_files+0x7e>
mov    0x2e14(%rip),%rax
mov    (%rax),%rax
mov    0x2e02(%rip),%rcx
cmp    (%rcx),%rax
jne    1251 <sancus_sanitize_files+0xf1>
xor    %edi,%edi
call   1050 <reopen_devnull@plt>
test   %eax,%eax
je     1251 <sancus_sanitize_files+0xf1>
mov    %rsp,%rsi
mov    $0x1,%edi
call   1070 <fstat@plt>
cmp    $0xffffffff,%eax
jne    1214 <sancus_sanitize_files+0xb4>
mov    0x2de1(%rip),%rax
mov    (%rax),%rax
mov    0x2dcf(%rip),%rcx
cmp    (%rcx),%rax
jne    1251 <sancus_sanitize_files+0xf1>
mov    $0x1,%edi
call   1050 <reopen_devnull@plt>
test   %eax,%eax
je     1251 <sancus_sanitize_files+0xf1>
mov    %rsp,%rsi
mov    $0x2,%edi
call   1070 <fstat@plt>
cmp    $0xffffffff,%eax
jne    124a <sancus_sanitize_files+0xea>
mov    0x2dab(%rip),%rax
mov    (%rax),%rax
mov    0x2d99(%rip),%rcx
cmp    (%rcx),%rax
jne    1251 <sancus_sanitize_files+0xf1>
mov    $0x2,%edi
call   1050 <reopen_devnull@plt>
test   %eax,%eax
je     1251 <sancus_sanitize_files+0xf1>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
xor    %eax,%eax
call   1030 <abort@plt>

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

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