#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*/ O_RDONLY;
int /*<<< orphan*/ _SC_OPEN_MAX;
int /*<<< orphan*/ abort();
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
char *malloc(scalar_t__);
scalar_t__ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
int sysconf(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int i;
  int filemax;
  filemax = sysconf(_SC_OPEN_MAX);
  char *fn = malloc(strlen(argv[0]) + 2);
  if (fn == NULL)
    abort();
  strcpy(fn, "/");
  strcat(fn, argv[0]);
  for (i = 0; i < filemax + 1; i++) {
    if (open(fn, O_RDONLY) < 0) {
      if (i < filemax - 3 - 1) {
        fprintf(stderr, "i: %d\n", i);
        abort();
      }
      if (errno != EMFILE) {
        perror("open");
        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    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   10b0 <sysconf@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
add    $0x2,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1205 <main+0x55>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x20(%rbp),%rdi
lea    0xdf0(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
call   10c0 <strcat@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    12d1 <main+0x121>
mov    -0x20(%rbp),%rdi
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
cmp    $0x0,%rax
jge    12be <main+0x10e>
mov    -0x14(%rbp),%eax
mov    -0x18(%rbp),%ecx
sub    $0x3,%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    128c <main+0xdc>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xd82(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    0x2d35(%rip),%rax
mov    (%rax),%rax
mov    0x2d1b(%rip),%rcx
cmp    (%rcx),%rax
je     12b9 <main+0x109>
lea    0xd5c(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    12d8 <main+0x128>
jmp    12c3 <main+0x113>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    122c <main+0x7c>
mov    $0x0,%al
call   1030 <abort@plt>
lea    0xd2f(%rip),%rdi
call   1060 <printf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbp
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
call   10b0 <sysconf@plt>
mov    %eax,%r14d
mov    0x0(%rbp),%rdi
call   1060 <strlen@plt>
lea    0x2(%rax),%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     1238 <main+0x88>
mov    %rax,%r15
lea    0xe13(%rip),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
mov    0x0(%rbp),%rsi
mov    %r15,%rdi
call   10c0 <strcat@plt>
test   %r14d,%r14d
js     1238 <main+0x88>
lea    0x1(%r14),%ebx
xor    %ebp,%ebp
mov    0x2dd5(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r12),%esi
mov    %r15,%rdi
call   1090 <open@plt>
test   %rax,%rax
js     123f <main+0x8f>
add    $0x1,%ebp
cmp    %ebp,%ebx
jne    1220 <main+0x70>
xor    %eax,%eax
call   1030 <abort@plt>
add    $0xfffffffc,%r14d
cmp    %r14d,%ebp
jge    1266 <main+0xb6>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
lea    0xdaa(%rip),%rsi
mov    %ebp,%edx
call   1070 <fprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%rax
mov    0x2d41(%rip),%rcx
cmp    (%rcx),%rax
jne    128f <main+0xdf>
lea    0xd8b(%rip),%rdi
call   1050 <puts@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
lea    0xd73(%rip),%rdi
call   10a0 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@_SC_OPEN_MAX = global i32 0
@.str = [2 x i8] c"/\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [7 x i8] c"i: %d\0A\00"
@errno = global i64 0
@EMFILE = global i64 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [6 x i8] c"pass\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @_SC_OPEN_MAX
%10 = call i32 @sysconf(i32 %9)
store i32 %10, i32* %7
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = call i64 @strlen(i8* %13)
%15 = add nsw i64 %14, 2
%16 = call i8* @malloc(i64 %15)
store i8* %16, i8** %8
%17 = load i8*, i8** %8
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 (...) @abort()
unreachable
21: ; preds = %2
%22 = load i8*, i8** %8
%23 = call i32 @strcpy(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%24 = load i8*, i8** %8
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 0
%27 = load i8*, i8** %26
%28 = call i32 @strcat(i8* %24, i8* %27)
store i32 0, i32* %6
br label %29
29: ; preds = %59, %21
%30 = load i32, i32* %6
%31 = load i32, i32* %7
%32 = add nsw i32 %31, 1
%33 = icmp slt i32 %30, %32
br i1 %33, label %34, label %62
34: ; preds = %29
%35 = load i8*, i8** %8
%36 = load i32, i32* @O_RDONLY
%37 = call i64 @open(i8* %35, i32 %36)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %58
39: ; preds = %34
%40 = load i32, i32* %6
%41 = load i32, i32* %7
%42 = sub nsw i32 %41, 3
%43 = sub nsw i32 %42, 1
%44 = icmp slt i32 %40, %43
br i1 %44, label %45, label %50
45: ; preds = %39
%46 = load i32, i32* @stderr
%47 = load i32, i32* %6
%48 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %47)
%49 = call i32 (...) @abort()
unreachable
50: ; preds = %39
%51 = load i64, i64* @errno
%52 = load i64, i64* @EMFILE
%53 = icmp ne i64 %51, %52
br i1 %53, label %54, label %57
54: ; preds = %50
%55 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%56 = call i32 (...) @abort()
unreachable
57: ; preds = %50
br label %64
58: ; preds = %34
br label %59
59: ; preds = %58
%60 = load i32, i32* %6
%61 = add nsw i32 %60, 1
store i32 %61, i32* %6
br label %29
62: ; preds = %29
%63 = call i32 (...) @abort()
unreachable
64: ; preds = %57
%65 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%66 = call i32 @exit(i32 0)
unreachable
}

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