#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 /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ err(int, char *, int);
scalar_t__ errno;
int /*<<< orphan*/ errx(int, char *, int);
int fcntl(int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
void check_stdio(void) {
  for (int i = 0; i <= 2; i++) {
    errno = 0;
    if (fcntl(i, F_GETFD) == -1) {
      if (errno == EBADF) {
        if (open("/dev/null", 0) != i)
          errx(2, "fd %d not available; aborting", i);
      } else {
        err(2, "fcntl on fd %d", i);
      }
    }
  }
}


x86 assembly (clang, -O0):
<check_stdio>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
cmpl   $0x2,-0x4(%rbp)
jg     11f7 <check_stdio+0xb7>
mov    0x2e80(%rip),%rax
movq   $0x0,(%rax)
mov    -0x4(%rbp),%edi
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11e4 <check_stdio+0xa4>
mov    0x2e58(%rip),%rax
mov    (%rax),%rax
mov    0x2e3e(%rip),%rcx
cmp    (%rcx),%rax
jne    11cb <check_stdio+0x8b>
lea    0xe5e(%rip),%rdi
xor    %esi,%esi
call   1060 <open@plt>
cmp    -0x4(%rbp),%eax
je     11c6 <check_stdio+0x86>
mov    -0x4(%rbp),%edx
mov    $0x2,%edi
lea    0xe49(%rip),%rsi
call   1040 <errx@plt>
jmp    11df <check_stdio+0x9f>
mov    -0x4(%rbp),%edx
mov    $0x2,%edi
lea    0xe4e(%rip),%rsi
call   1050 <err@plt>
jmp    11e4 <check_stdio+0xa4>
jmp    11e9 <check_stdio+0xa9>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    114f <check_stdio+0xf>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_stdio>:
push   %r14
push   %rbx
push   %rax
mov    0x2e95(%rip),%rbx
movq   $0x0,(%rbx)
mov    0x2e7f(%rip),%r14
mov    (%r14),%esi
xor    %edi,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11b1 <check_stdio+0x71>
mov    (%rbx),%rax
mov    0x2e5e(%rip),%rcx
cmp    (%rcx),%rax
jne    119e <check_stdio+0x5e>
lea    0xe82(%rip),%rdi
xor    %esi,%esi
call   1060 <open@plt>
test   %eax,%eax
je     11b1 <check_stdio+0x71>
lea    0xe7a(%rip),%rsi
mov    $0x2,%edi
xor    %edx,%edx
call   1040 <errx@plt>
jmp    11b1 <check_stdio+0x71>
lea    0xe83(%rip),%rsi
mov    $0x2,%edi
xor    %edx,%edx
call   1050 <err@plt>
movq   $0x0,(%rbx)
mov    (%r14),%esi
mov    $0x1,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    121a <check_stdio+0xda>
mov    (%rbx),%rax
mov    0x2dfc(%rip),%rcx
cmp    (%rcx),%rax
jne    1204 <check_stdio+0xc4>
lea    0xe20(%rip),%rdi
xor    %esi,%esi
call   1060 <open@plt>
cmp    $0x1,%eax
je     121a <check_stdio+0xda>
lea    0xe17(%rip),%rsi
mov    $0x2,%edi
mov    $0x1,%edx
call   1040 <errx@plt>
jmp    121a <check_stdio+0xda>
lea    0xe1d(%rip),%rsi
mov    $0x2,%edi
mov    $0x1,%edx
call   1050 <err@plt>
movq   $0x0,(%rbx)
mov    (%r14),%esi
mov    $0x2,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     123b <check_stdio+0xfb>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    (%rbx),%rax
mov    0x2d8b(%rip),%rcx
cmp    (%rcx),%rax
jne    127a <check_stdio+0x13a>
lea    0xdaf(%rip),%rdi
xor    %esi,%esi
call   1060 <open@plt>
cmp    $0x2,%eax
je     1233 <check_stdio+0xf3>
lea    0xda6(%rip),%rsi
mov    $0x2,%edi
mov    $0x2,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <errx@plt>
lea    0xda7(%rip),%rsi
mov    $0x2,%edi
mov    $0x2,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <err@plt>

LLVM IR (-O0):
@errno = global i64 0
@F_GETFD = global i32 0
@EBADF = global i64 0
@.str = [10 x i8] c"/dev/null\00"
@.str.1 = [30 x i8] c"fd %d not available; aborting\00"
@.str.2 = [15 x i8] c"fcntl on fd %d\00"
define void @check_stdio() {
%1 = alloca i32
store i32 0, i32* %1
br label %2
2: ; preds = %27, %0
%3 = load i32, i32* %1
%4 = icmp sle i32 %3, 2
br i1 %4, label %5, label %30
5: ; preds = %2
store i64 0, i64* @errno
%6 = load i32, i32* %1
%7 = load i32, i32* @F_GETFD
%8 = call i32 @fcntl(i32 %6, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %26
10: ; preds = %5
%11 = load i64, i64* @errno
%12 = load i64, i64* @EBADF
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %22
14: ; preds = %10
%15 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0)
%16 = load i32, i32* %1
%17 = icmp ne i32 %15, %16
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i32, i32* %1
%20 = call i32 @errx(i32 2, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %18, %14
br label %25
22: ; preds = %10
%23 = load i32, i32* %1
%24 = call i32 @err(i32 2, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %22, %21
br label %26
26: ; preds = %25, %5
br label %27
27: ; preds = %26
%28 = load i32, i32* %1
%29 = add nsw i32 %28, 1
store i32 %29, i32* %1
br label %2
30: ; preds = %2
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@F_GETFD = global i32 0
@EBADF = global i64 0
@.str = [10 x i8] c"/dev/null\00"
@.str.1 = [30 x i8] c"fd %d not available; aborting\00"
@.str.2 = [15 x i8] c"fcntl on fd %d\00"
define void @check_stdio() {
store i64 0, i64* @errno
%1 = load i32, i32* @F_GETFD
%2 = tail call i32 @fcntl(i32 0, i32 %1)
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %15
4: ; preds = %0
%5 = load i64, i64* @errno
%6 = load i64, i64* @EBADF
%7 = icmp eq i64 %5, %6
br i1 %7, label %8, label %13
8: ; preds = %4
%9 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0)
%10 = icmp eq i32 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %8
%12 = tail call i32 @errx(i32 2, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 0)
br label %15
13: ; preds = %4
%14 = tail call i32 @err(i32 2, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 0)
br label %15
15: ; preds = %0, %8, %11, %13
store i64 0, i64* @errno
%16 = load i32, i32* @F_GETFD
%17 = tail call i32 @fcntl(i32 1, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %30
19: ; preds = %15
%20 = load i64, i64* @errno
%21 = load i64, i64* @EBADF
%22 = icmp eq i64 %20, %21
br i1 %22, label %25, label %23
23: ; preds = %19
%24 = tail call i32 @err(i32 2, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 1)
br label %30
25: ; preds = %19
%26 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0)
%27 = icmp eq i32 %26, 1
br i1 %27, label %30, label %28
28: ; preds = %25
%29 = tail call i32 @errx(i32 2, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 1)
br label %30
30: ; preds = %28, %25, %23, %15
store i64 0, i64* @errno
%31 = load i32, i32* @F_GETFD
%32 = tail call i32 @fcntl(i32 2, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %45
34: ; preds = %30
%35 = load i64, i64* @errno
%36 = load i64, i64* @EBADF
%37 = icmp eq i64 %35, %36
br i1 %37, label %40, label %38
38: ; preds = %34
%39 = tail call i32 @err(i32 2, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 2)
br label %45
40: ; preds = %34
%41 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0)
%42 = icmp eq i32 %41, 2
br i1 %42, label %45, label %43
43: ; preds = %40
%44 = tail call i32 @errx(i32 2, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 2)
br label %45
45: ; preds = %43, %40, %38, %30
ret void
}