#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int atoi(char *);
int errno;
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
char *strerror(int);
int wait(int *);
int main(int argc, char *argv[]) {
  if (argc != 3) {
    fprintf(stderr, "testlimit nbprocess nbopen\n");
  } else {
    int nbproc = atoi(argv[1]);
    int nbopen = atoi(argv[2]);
    int i;
    int status;
    for (i = 0; i < nbproc; i++) {
      if (fork() == 0) {
        int j;
        for (j = 0; j < nbopen; j++) {
          FILE *fin = fopen("/proc/self/status", "r");
          if (fin == NULL) {
            fprintf(stderr, "Can't open %d (%s)\n", errno, strerror(errno));
            break;
          }
        }
        printf("%d open files, sleeping\n", j);
        sleep(100);
        _exit(0);
      }
    }
    while (wait(&status) != -1)
      ;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11cc <main+0x3c>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    12ee <main+0x15e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12cd <main+0x13d>
mov    $0x0,%al
call   10b0 <fork@plt>
cmp    $0x0,%rax
jne    12ba <main+0x12a>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    129a <main+0x10a>
lea    0xdf2(%rip),%rdi
lea    0xdfd(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1287 <main+0xf7>
mov    0x2d94(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d80(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    -0x34(%rbp),%edx
mov    %rax,%rcx
lea    0xdb5(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    129a <main+0x10a>
jmp    128c <main+0xfc>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1217 <main+0x87>
mov    -0x24(%rbp),%esi
lea    0xda0(%rip),%rdi
call   1040 <printf@plt>
mov    $0x64,%edi
call   1090 <sleep@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
jmp    12bf <main+0x12f>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11f3 <main+0x63>
jmp    12d2 <main+0x142>
lea    -0x20(%rbp),%rdi
call   10a0 <wait@plt>
cmp    $0xffffffff,%eax
je     12e9 <main+0x159>
jmp    12d2 <main+0x142>
jmp    12ee <main+0x15e>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x3,%edi
jne    11f1 <main+0x61>
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
call   1070 <atoi@plt>
mov    %eax,%ebx
mov    0x10(%rbp),%rdi
call   1070 <atoi@plt>
test   %ebx,%ebx
jle    11e0 <main+0x50>
mov    %eax,%ebp
nopl   0x0(%rax)
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     1215 <main+0x85>
add    $0xffffffff,%ebx
jne    11c0 <main+0x30>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    0x4(%rsp),%rdi
call   10a0 <wait@plt>
cmp    $0xffffffff,%eax
jne    11e0 <main+0x50>
jmp    1208 <main+0x78>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0xdff(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebx,%ebx
test   %ebp,%ebp
jle    127b <main+0xeb>
xor    %ebx,%ebx
lea    0xdf8(%rip),%r14
lea    0xe03(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     124b <main+0xbb>
add    $0x1,%ebx
cmp    %ebx,%ebp
jne    1230 <main+0xa0>
mov    %ebp,%ebx
jmp    127b <main+0xeb>
mov    0x2d8e(%rip),%rax
mov    (%rax),%r14d
mov    0x2d7c(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1080 <strerror@plt>
lea    0xdc4(%rip),%rsi
mov    %r14d,%edi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xdc2(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
mov    $0x64,%edi
call   1090 <sleep@plt>
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"testlimit nbprocess nbopen\0A\00"
@.str.1 = [18 x i8] c"/proc/self/status\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [20 x i8] c"Can't open %d (%s)\0A\00"
@errno = global i32 0
@.str.4 = [25 x i8] c"%d open files, sleeping\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 i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 3
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %67
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @atoi(i8* %20)
store i32 %21, i32* %6
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 2
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %7
store i32 0, i32* %8
br label %26
26: ; preds = %58, %17
%27 = load i32, i32* %8
%28 = load i32, i32* %6
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %61
30: ; preds = %26
%31 = call i64 (...) @fork()
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %57
33: ; preds = %30
store i32 0, i32* %10
br label %34
34: ; preds = %49, %33
%35 = load i32, i32* %10
%36 = load i32, i32* %7
%37 = icmp slt i32 %35, %36
br i1 %37, label %38, label %52
38: ; preds = %34
%39 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %39, i32** %11
%40 = load i32*, i32** %11
%41 = icmp eq i32* %40, null
br i1 %41, label %42, label %48
42: ; preds = %38
%43 = load i32, i32* @stderr
%44 = load i32, i32* @errno
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %44, i8* %46)
br label %52
48: ; preds = %38
br label %49
49: ; preds = %48
%50 = load i32, i32* %10
%51 = add nsw i32 %50, 1
store i32 %51, i32* %10
br label %34
52: ; preds = %42, %34
%53 = load i32, i32* %10
%54 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %53)
%55 = call i32 @sleep(i32 100)
%56 = call i32 @_exit(i32 0)
unreachable
57: ; preds = %30
br label %58
58: ; preds = %57
%59 = load i32, i32* %8
%60 = add nsw i32 %59, 1
store i32 %60, i32* %8
br label %26
61: ; preds = %26
br label %62
62: ; preds = %65, %61
%63 = call i32 @wait(i32* %9)
%64 = icmp ne i32 %63, -1
br i1 %64, label %65, label %66
65: ; preds = %62
br label %62
66: ; preds = %62
br label %67
67: ; preds = %66, %14
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"testlimit nbprocess nbopen\0A\00"
@.str.1 = [18 x i8] c"/proc/self/status\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [20 x i8] c"Can't open %d (%s)\0A\00"
@errno = global i32 0
@.str.4 = [25 x i8] c"%d open files, sleeping\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = icmp eq i32 %0, 3
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %48
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @atoi(i8* %10)
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = bitcast i32* %3 to i8*
%16 = icmp sgt i32 %11, 0
br i1 %16, label %20, label %17
17: ; preds = %18, %8
br label %44
18: ; preds = %20
%19 = icmp eq i32 %24, %11
br i1 %19, label %17, label %20
20: ; preds = %8, %18
%21 = phi i32 [ %24, %18 ], [ 0, %8 ]
%22 = tail call i64 (...) @fork()
%23 = icmp eq i64 %22, 0
%24 = add nuw nsw i32 %21, 1
br i1 %23, label %25, label %18
25: ; preds = %20
%26 = icmp sgt i32 %14, 0
br i1 %26, label %27, label %39
27: ; preds = %25, %36
%28 = phi i32 [ %37, %36 ], [ 0, %25 ]
%29 = tail call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %36
31: ; preds = %27
%32 = load i32, i32* @stderr
%33 = load i32, i32* @errno
%34 = tail call i8* @strerror(i32 %33)
%35 = tail call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %33, i8* %34)
br label %39
36: ; preds = %27
%37 = add nuw nsw i32 %28, 1
%38 = icmp eq i32 %37, %14
br i1 %38, label %39, label %27
39: ; preds = %36, %25, %31
%40 = phi i32 [ %28, %31 ], [ 0, %25 ], [ %14, %36 ]
%41 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %40)
%42 = tail call i32 @sleep(i32 100)
%43 = tail call i32 @_exit(i32 0)
unreachable
44: ; preds = %17, %44
%45 = call i32 @wait(i32* %3)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %44
47: ; preds = %44
br label %48
48: ; preds = %47, %5
ret i32 0
}