#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*/ close(int);
scalar_t__ errno;
int fstat(int, struct stat *);
int getdtablesize();
int /*<<< orphan*/ open_devnull(int);
int 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)))
      return -1;
  return 0;
}


x86 assembly (clang, -O0):
<sanitize_files>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1060 <getdtablesize@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    1162 <sanitize_files+0x22>
movl   $0x100,-0xc(%rbp)
movl   $0x3,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    118b <sanitize_files+0x4b>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1169 <sanitize_files+0x29>
movl   $0x0,-0x8(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11fb <sanitize_files+0xbb>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1050 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11e8 <sanitize_files+0xa8>
mov    0x2e28(%rip),%rax
mov    (%rax),%rax
mov    0x2e16(%rip),%rcx
cmp    (%rcx),%rax
jne    11dc <sanitize_files+0x9c>
mov    -0x8(%rbp),%edi
call   1030 <open_devnull@plt>
cmp    $0x0,%eax
jne    11e8 <sanitize_files+0xa8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1202 <sanitize_files+0xc2>
jmp    11ed <sanitize_files+0xad>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1192 <sanitize_files+0x52>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sanitize_files>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1060 <getdtablesize@plt>
cmp    $0xffffffff,%eax
mov    $0x100,%ebp
cmovne %eax,%ebp
cmp    $0x4,%ebp
jl     116e <sanitize_files+0x2e>
mov    $0x3,%ebx
nop
mov    %ebx,%edi
call   1040 <close@plt>
add    $0x1,%ebx
cmp    %ebx,%ebp
jne    1160 <sanitize_files+0x20>
mov    %rsp,%rsi
xor    %edi,%edi
call   1050 <fstat@plt>
cmp    $0xffffffff,%eax
je     11a5 <sanitize_files+0x65>
mov    %rsp,%rsi
mov    $0x1,%edi
call   1050 <fstat@plt>
cmp    $0xffffffff,%eax
je     11cd <sanitize_files+0x8d>
mov    %rsp,%rsi
mov    $0x2,%edi
call   1050 <fstat@plt>
cmp    $0xffffffff,%eax
je     11f8 <sanitize_files+0xb8>
xor    %ebx,%ebx
jmp    1221 <sanitize_files+0xe1>
mov    0x2e34(%rip),%rax
mov    (%rax),%rax
mov    0x2e22(%rip),%rcx
mov    $0xffffffff,%ebx
cmp    (%rcx),%rax
jne    1221 <sanitize_files+0xe1>
xor    %edi,%edi
call   1030 <open_devnull@plt>
test   %eax,%eax
jne    117d <sanitize_files+0x3d>
jmp    1221 <sanitize_files+0xe1>
mov    0x2e0c(%rip),%rax
mov    (%rax),%rax
mov    0x2dfa(%rip),%rcx
mov    $0xffffffff,%ebx
cmp    (%rcx),%rax
jne    1221 <sanitize_files+0xe1>
mov    $0x1,%edi
call   1030 <open_devnull@plt>
test   %eax,%eax
jne    118f <sanitize_files+0x4f>
jmp    1221 <sanitize_files+0xe1>
mov    0x2de1(%rip),%rax
mov    (%rax),%rax
mov    0x2dcf(%rip),%rcx
mov    $0xffffffff,%ebx
cmp    (%rcx),%rax
jne    1221 <sanitize_files+0xe1>
mov    $0x2,%edi
call   1030 <open_devnull@plt>
test   %eax,%eax
jne    11a1 <sanitize_files+0x61>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@EBADF = global i64 0
define i32 @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 %22
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 %43
19: ; preds = %15
%20 = tail call i32 @open_devnull(i32 0)
%21 = icmp eq i32 %20, 0
br i1 %21, label %43, label %22
22: ; preds = %7, %19
%23 = call i32 @fstat(i32 1, %struct.stat* %1)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %32
25: ; preds = %22
%26 = load i64, i64* @errno
%27 = load i64, i64* @EBADF
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %43
29: ; preds = %25
%30 = tail call i32 @open_devnull(i32 1)
%31 = icmp eq i32 %30, 0
br i1 %31, label %43, label %32
32: ; preds = %29, %22
%33 = call i32 @fstat(i32 2, %struct.stat* %1)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %42
35: ; preds = %32
%36 = load i64, i64* @errno
%37 = load i64, i64* @EBADF
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %43
39: ; preds = %35
%40 = tail call i32 @open_devnull(i32 2)
%41 = icmp eq i32 %40, 0
br i1 %41, label %43, label %42
42: ; preds = %39, %32
br label %43
43: ; preds = %42, %39, %35, %29, %25, %15, %19
%44 = phi i32 [ -1, %19 ], [ -1, %15 ], [ -1, %25 ], [ -1, %29 ], [ -1, %35 ], [ -1, %39 ], [ 0, %42 ]
ret i32 %44
}