#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__ EMFILE;
int /*<<< orphan*/ _SC_OPEN_MAX;
int /*<<< orphan*/ abort();
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int sysconf(int /*<<< orphan*/);
int main(void) {
  int i;
  int filemax;
  filemax = sysconf(_SC_OPEN_MAX);
  for (i = 0; i < filemax + 1; i++) {
    int pip[2];
    if (pipe(pip) != 0) {
      if (i < filemax / 2 - 3 - 1) {
        fprintf(stderr, "i: %d\n", i);
        abort();
      }
      if (errno != EMFILE) {
        perror("pipe");
        abort();
      }
      goto ok;
    }
  }
  abort();
ok:
  printf("pass\n");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    1243 <main+0xd3>
lea    -0x14(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
je     1230 <main+0xc0>
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
sub    $0x3,%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11fe <main+0x8e>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe0e(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%rax
mov    0x2db1(%rip),%rcx
cmp    (%rcx),%rax
je     122b <main+0xbb>
lea    0xde8(%rip),%rdi
call   1070 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    124a <main+0xda>
jmp    1235 <main+0xc5>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1197 <main+0x27>
mov    $0x0,%al
call   1030 <abort@plt>
lea    0xdbb(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
test   %eax,%eax
js     11b4 <main+0x44>
mov    %eax,%r14d
lea    0x1(%r14),%ebp
xor    %ebx,%ebx
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
call   1050 <pipe@plt>
test   %rax,%rax
jne    11bb <main+0x4b>
add    $0x1,%ebx
cmp    %ebx,%ebp
jne    11a0 <main+0x30>
xor    %eax,%eax
call   1030 <abort@plt>
mov    %r14d,%eax
shr    $0x1f,%eax
add    %r14d,%eax
sar    %eax
add    $0xfffffffc,%eax
cmp    %eax,%ebx
jge    11eb <main+0x7b>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
mov    %ebx,%edx
call   1060 <fprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%rax
mov    0x2dc4(%rip),%rcx
cmp    (%rcx),%rax
jne    1214 <main+0xa4>
lea    0xe04(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xdec(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@_SC_OPEN_MAX = global i32 0
@stderr = global i32 0
@.str = [7 x i8] c"i: %d\0A\00"
@errno = global i64 0
@EMFILE = global i64 0
@.str.1 = [5 x i8] c"pipe\00"
@.str.2 = [6 x i8] c"pass\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [2 x i32]
store i32 0, i32* %1
%5 = load i32, i32* @_SC_OPEN_MAX
%6 = call i32 @sysconf(i32 %5)
store i32 %6, i32* %3
store i32 0, i32* %2
br label %7
7: ; preds = %37, %0
%8 = load i32, i32* %2
%9 = load i32, i32* %3
%10 = add nsw i32 %9, 1
%11 = icmp slt i32 %8, %10
br i1 %11, label %12, label %40
12: ; preds = %7
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%14 = call i64 @pipe(i32* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %36
16: ; preds = %12
%17 = load i32, i32* %2
%18 = load i32, i32* %3
%19 = sdiv i32 %18, 2
%20 = sub nsw i32 %19, 3
%21 = sub nsw i32 %20, 1
%22 = icmp slt i32 %17, %21
br i1 %22, label %23, label %28
23: ; preds = %16
%24 = load i32, i32* @stderr
%25 = load i32, i32* %2
%26 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = call i32 (...) @abort()
unreachable
28: ; preds = %16
%29 = load i64, i64* @errno
%30 = load i64, i64* @EMFILE
%31 = icmp ne i64 %29, %30
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 (...) @abort()
unreachable
35: ; preds = %28
br label %42
36: ; preds = %12
br label %37
37: ; preds = %36
%38 = load i32, i32* %2
%39 = add nsw i32 %38, 1
store i32 %39, i32* %2
br label %7
40: ; preds = %7
%41 = call i32 (...) @abort()
unreachable
42: ; preds = %35
%43 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%44 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@_SC_OPEN_MAX = global i32 0
@stderr = global i32 0
@.str = [7 x i8] c"i: %d\0A\00"
@errno = global i64 0
@EMFILE = global i64 0
@.str.1 = [5 x i8] c"pipe\00"
@str = [5 x i8] c"pass\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = load i32, i32* @_SC_OPEN_MAX
%3 = tail call i32 @sysconf(i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %30, label %5
5: ; preds = %0
%6 = bitcast [2 x i32]* %1 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
br label %8
8: ; preds = %5, %27
%9 = phi i32 [ 0, %5 ], [ %28, %27 ]
%10 = call i64 @pipe(i32* %7)
%11 = icmp eq i64 %10, 0
br i1 %11, label %27, label %12
12: ; preds = %8
%13 = sdiv i32 %3, 2
%14 = add nsw i32 %13, -4
%15 = icmp slt i32 %9, %14
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %9)
%19 = call i32 (...) @abort()
unreachable
20: ; preds = %12
%21 = load i64, i64* @errno
%22 = load i64, i64* @EMFILE
%23 = icmp eq i64 %21, %22
br i1 %23, label %32, label %24
24: ; preds = %20
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 (...) @abort()
unreachable
27: ; preds = %8
%28 = add nuw i32 %9, 1
%29 = icmp eq i32 %9, %3
br i1 %29, label %30, label %8
30: ; preds = %27, %0
%31 = call i32 (...) @abort()
unreachable
32: ; preds = %20
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
%34 = call i32 @exit(i32 0)
unreachable
}