#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;
scalar_t__ ENFILE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *);
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int mkstemp(char *);
unsigned int nitems(int *);
int open(char *, int /*<<< orphan*/);
int pipe(int *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ unlink(char *);
int main(void) {
  char template[] = "pipe.XXXXXXXXXX";
  int lastfd, pipes[10000], returnval;
  unsigned int i;
  lastfd = -1;
  if (mkstemp(template) == -1)
    err(1, "mkstemp failed");
  for (i = 0; i < nitems(pipes); i++) {
    returnval = open(template, O_RDONLY);
    if (returnval == -1 && (errno == ENFILE || errno == EMFILE))
      break;
    else
      lastfd = returnval;
  }
  for (i = 0; i < 1000; i++) {
    returnval = pipe(&pipes[i]);
  }
  close(lastfd);
  for (i = 0; i < 1000; i++) {
    returnval = pipe(&pipes[i]);
  }
  printf("PASS\n");
  unlink(template);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x9c80,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe57(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe54(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0xffffffff,-0x24(%rbp)
lea    -0x20(%rbp),%rdi
call   1090 <mkstemp@plt>
cmp    $0xffffffff,%eax
jne    11e2 <main+0x52>
mov    $0x1,%edi
lea    0xe33(%rip),%rsi
call   1080 <err@plt>
movl   $0x0,-0x9c78(%rbp)
mov    -0x9c78(%rbp),%eax
mov    %eax,-0x9c7c(%rbp)
lea    -0x9c70(%rbp),%rdi
call   1070 <nitems@plt>
mov    %eax,%ecx
mov    -0x9c7c(%rbp),%eax
cmp    %ecx,%eax
jae    1294 <main+0x104>
lea    -0x20(%rbp),%rdi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x9c74(%rbp)
cmpl   $0xffffffff,-0x9c74(%rbp)
jne    1272 <main+0xe2>
mov    0x2d98(%rip),%rax
mov    (%rax),%rax
mov    0x2d86(%rip),%rcx
cmp    (%rcx),%rax
je     126d <main+0xdd>
mov    0x2d7e(%rip),%rax
mov    (%rax),%rax
mov    0x2d5c(%rip),%rcx
cmp    (%rcx),%rax
jne    1272 <main+0xe2>
jmp    1294 <main+0x104>
mov    -0x9c74(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    1280 <main+0xf0>
mov    -0x9c78(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x9c78(%rbp)
jmp    11ec <main+0x5c>
movl   $0x0,-0x9c78(%rbp)
cmpl   $0x3e8,-0x9c78(%rbp)
jae    12e1 <main+0x151>
mov    -0x9c78(%rbp),%eax
lea    -0x9c70(%rbp),%rdi
shl    $0x2,%rax
add    %rax,%rdi
call   1060 <pipe@plt>
mov    %eax,-0x9c74(%rbp)
mov    -0x9c78(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x9c78(%rbp)
jmp    129e <main+0x10e>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x9c78(%rbp)
cmpl   $0x3e8,-0x9c78(%rbp)
jae    1336 <main+0x1a6>
mov    -0x9c78(%rbp),%eax
lea    -0x9c70(%rbp),%rdi
shl    $0x2,%rax
add    %rax,%rdi
call   1060 <pipe@plt>
mov    %eax,-0x9c74(%rbp)
mov    -0x9c78(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x9c78(%rbp)
jmp    12f3 <main+0x163>
lea    0xce2(%rip),%rdi
call   1040 <printf@plt>
lea    -0x20(%rbp),%rdi
call   1030 <unlink@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x9c58,%rsp
movaps 0xe58(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    %rsp,%rdi
call   1090 <mkstemp@plt>
cmp    $0xffffffff,%eax
jne    11ca <main+0x3a>
lea    0xe50(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
lea    0x10(%rsp),%rdi
call   1070 <nitems@plt>
mov    $0xffffffff,%r12d
test   %eax,%eax
jne    1245 <main+0xb5>
xor    %ebx,%ebx
lea    (%rsp,%rbx,1),%rdi
add    $0x10,%rdi
call   1060 <pipe@plt>
add    $0x4,%rbx
cmp    $0xfa0,%rbx
jne    11e0 <main+0x50>
mov    %r12d,%edi
call   1050 <close@plt>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    (%rsp,%rbx,1),%rdi
add    $0x10,%rdi
call   1060 <pipe@plt>
add    $0x4,%rbx
cmp    $0xfa0,%rbx
jne    1210 <main+0x80>
lea    0xdee(%rip),%rdi
call   1040 <puts@plt>
mov    %rsp,%rdi
call   1030 <unlink@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    $0xffffffff,%r12d
xor    %ebx,%ebx
mov    0x2d94(%rip),%r13
mov    %rsp,%r14
lea    0x10(%rsp),%r15
jmp    1276 <main+0xe6>
xchg   %ax,%ax
add    $0x1,%ebx
mov    %r15,%rdi
call   1070 <nitems@plt>
mov    %ebp,%r12d
cmp    %eax,%ebx
jae    11de <main+0x4e>
mov    0x0(%r13),%esi
mov    %r14,%rdi
call   10a0 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1260 <main+0xd0>
mov    0x2d48(%rip),%rax
mov    (%rax),%rax
mov    0x2d36(%rip),%rcx
cmp    (%rcx),%rax
je     11de <main+0x4e>
mov    0x2d16(%rip),%rcx
cmp    (%rcx),%rax
jne    1260 <main+0xd0>
jmp    11de <main+0x4e>

LLVM IR (-O0):
@__const.main.template = [16 x i8] c"pipe.XXXXXXXXXX\00"
@.str = [15 x i8] c"mkstemp failed\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@ENFILE = global i64 0
@EMFILE = global i64 0
@.str.1 = [6 x i8] c"PASS\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [16 x i8]
%3 = alloca i32
%4 = alloca [10000 x i32]
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = bitcast [16 x i8]* %2 to i8*
store i32 -1, i32* %3
%8 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%9 = call i32 @mkstemp(i8* %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @err(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %0
store i32 0, i32* %6
br label %14
14: ; preds = %37, %13
%15 = load i32, i32* %6
%16 = getelementptr inbounds [10000 x i32], [10000 x i32]* %4, i64 0, i64 0
%17 = call i32 @nitems(i32* %16)
%18 = icmp ult i32 %15, %17
br i1 %18, label %19, label %40
19: ; preds = %14
%20 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %5
%23 = load i32, i32* %5
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %34
25: ; preds = %19
%26 = load i64, i64* @errno
%27 = load i64, i64* @ENFILE
%28 = icmp eq i64 %26, %27
br i1 %28, label %33, label %29
29: ; preds = %25
%30 = load i64, i64* @errno
%31 = load i64, i64* @EMFILE
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %29, %25
br label %40
34: ; preds = %29, %19
%35 = load i32, i32* %5
store i32 %35, i32* %3
br label %36
36: ; preds = %34
br label %37
37: ; preds = %36
%38 = load i32, i32* %6
%39 = add i32 %38, 1
store i32 %39, i32* %6
br label %14
40: ; preds = %33, %14
store i32 0, i32* %6
br label %41
41: ; preds = %49, %40
%42 = load i32, i32* %6
%43 = icmp ult i32 %42, 1000
br i1 %43, label %44, label %52
44: ; preds = %41
%45 = load i32, i32* %6
%46 = zext i32 %45 to i64
%47 = getelementptr inbounds [10000 x i32], [10000 x i32]* %4, i64 0, i64 %46
%48 = call i32 @pipe(i32* %47)
store i32 %48, i32* %5
br label %49
49: ; preds = %44
%50 = load i32, i32* %6
%51 = add i32 %50, 1
store i32 %51, i32* %6
br label %41
52: ; preds = %41
%53 = load i32, i32* %3
%54 = call i32 @close(i32 %53)
store i32 0, i32* %6
br label %55
55: ; preds = %63, %52
%56 = load i32, i32* %6
%57 = icmp ult i32 %56, 1000
br i1 %57, label %58, label %66
58: ; preds = %55
%59 = load i32, i32* %6
%60 = zext i32 %59 to i64
%61 = getelementptr inbounds [10000 x i32], [10000 x i32]* %4, i64 0, i64 %60
%62 = call i32 @pipe(i32* %61)
store i32 %62, i32* %5
br label %63
63: ; preds = %58
%64 = load i32, i32* %6
%65 = add i32 %64, 1
store i32 %65, i32* %6
br label %55
66: ; preds = %55
%67 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%68 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%69 = call i32 @unlink(i8* %68)
%70 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@__const.main.template = [16 x i8] c"pipe.XXXXXXXXXX\00"
@.str = [15 x i8] c"mkstemp failed\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@ENFILE = global i64 0
@EMFILE = global i64 0
@str = [5 x i8] c"PASS\00"
define i32 @main() {
%1 = alloca [16 x i8]
%2 = alloca [10000 x i32]
%3 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%4 = bitcast [10000 x i32]* %2 to i8*
%5 = call i32 @mkstemp(i8* %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @err(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %0
%10 = getelementptr inbounds [10000 x i32], [10000 x i32]* %2, i64 0, i64 0
%11 = call i32 @nitems(i32* %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %30, label %13
13: ; preds = %9, %26
%14 = phi i32 [ %27, %26 ], [ 0, %9 ]
%15 = phi i32 [ %17, %26 ], [ -1, %9 ]
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %3, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %26
19: ; preds = %13
%20 = load i64, i64* @errno
%21 = load i64, i64* @ENFILE
%22 = icmp eq i64 %20, %21
%23 = load i64, i64* @EMFILE
%24 = icmp eq i64 %20, %23
%25 = select i1 %22, i1 true, i1 %24
br i1 %25, label %30, label %26
26: ; preds = %19, %13
%27 = add nuw i32 %14, 1
%28 = call i32 @nitems(i32* %10)
%29 = icmp ult i32 %27, %28
br i1 %29, label %13, label %30
30: ; preds = %26, %19, %9
%31 = phi i32 [ -1, %9 ], [ %15, %19 ], [ %17, %26 ]
br label %32
32: ; preds = %30, %32
%33 = phi i64 [ 0, %30 ], [ %36, %32 ]
%34 = getelementptr inbounds [10000 x i32], [10000 x i32]* %2, i64 0, i64 %33
%35 = call i32 @pipe(i32* %34)
%36 = add nuw nsw i64 %33, 1
%37 = icmp eq i64 %36, 1000
br i1 %37, label %38, label %32
38: ; preds = %32
%39 = call i32 @close(i32 %31)
br label %40
40: ; preds = %38, %40
%41 = phi i64 [ 0, %38 ], [ %44, %40 ]
%42 = getelementptr inbounds [10000 x i32], [10000 x i32]* %2, i64 0, i64 %41
%43 = call i32 @pipe(i32* %42)
%44 = add nuw nsw i64 %41, 1
%45 = icmp eq i64 %44, 1000
br i1 %45, label %46, label %40
46: ; preds = %40
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
%48 = call i32 @unlink(i8* %3)
%49 = call i32 @exit(i32 0)
unreachable
}