#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 */
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int atoi(char *);
int /*<<< orphan*/ errno;
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
void *malloc(int);
int /*<<< orphan*/ memset(void *, int, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int wait(int *);
int main(int argc, char *argv[]) {
  if (argc != 3) {
    fprintf(stderr, "testlimit nbprocess malloc_size\n");
  } else {
    int nbproc = atoi(argv[1]);
    int size = atoi(argv[2]);
    int i;
    int status;
    for (i = 0; i < nbproc; i++) {
      if (fork() == 0) {
        void *pt = malloc(size);
        if (pt == NULL) {
          fprintf(stderr, "Child %d: can't malloc (%s)\n", i, strerror(errno));
        } else {
          memset(pt, 1, size);
        }
        sleep(100);
        _exit(0);
      }
    }
    while (wait(&status) != -1)
      ;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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    12b9 <main+0x129>
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    1298 <main+0x108>
mov    $0x0,%al
call   10b0 <fork@plt>
cmp    $0x0,%rax
jne    1285 <main+0xf5>
mov    -0x18(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1263 <main+0xd3>
mov    0x2db2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    -0x2c(%rbp),%edx
mov    %rax,%rcx
lea    0xdca(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1274 <main+0xe4>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    $0x1,%esi
call   1040 <memset@plt>
mov    $0x64,%edi
call   1090 <sleep@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
jmp    128a <main+0xfa>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11f3 <main+0x63>
jmp    129d <main+0x10d>
lea    -0x20(%rbp),%rdi
call   10a0 <wait@plt>
cmp    $0xffffffff,%eax
je     12b4 <main+0x124>
jmp    129d <main+0x10d>
jmp    12b9 <main+0x129>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x3,%edi
jne    11ef <main+0x5f>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   1070 <atoi@plt>
mov    %eax,%ebp
mov    0x10(%rbx),%rdi
call   1070 <atoi@plt>
test   %ebp,%ebp
jle    11d3 <main+0x43>
mov    %eax,%r14d
xor    %ebx,%ebx
nopl   (%rax)
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     1211 <main+0x81>
add    $0x1,%ebx
cmp    %ebx,%ebp
jne    11c0 <main+0x30>
lea    0xc(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10a0 <wait@plt>
cmp    $0xffffffff,%eax
jne    11e0 <main+0x50>
jmp    1206 <main+0x76>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
lea    0xe01(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %r14d,%edi
call   1060 <malloc@plt>
test   %rax,%rax
jne    124c <main+0xbc>
mov    0x2dbb(%rip),%rax
mov    (%rax),%ebp
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xde5(%rip),%rsi
mov    %ebp,%edi
mov    %ebx,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    125c <main+0xcc>
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
call   1040 <memset@plt>
mov    $0x64,%edi
call   1090 <sleep@plt>
xor    %edi,%edi
call   1030 <_exit@plt>

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [33 x i8] c"testlimit nbprocess malloc_size\0A\00"
@.str.1 = [29 x i8] c"Child %d: can't malloc (%s)\0A\00"
@errno = global i32 0
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 ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %42
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 %17, label %37
17: ; preds = %8, %34
%18 = phi i32 [ %35, %34 ], [ 0, %8 ]
%19 = tail call i64 (...) @fork()
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %34
21: ; preds = %17
%22 = tail call i8* @malloc(i32 %14)
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %29
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = tail call i8* @strerror(i32 %26)
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %18, i8* %27)
br label %31
29: ; preds = %21
%30 = tail call i32 @memset(i8* %22, i32 1, i32 %14)
br label %31
31: ; preds = %29, %24
%32 = tail call i32 @sleep(i32 100)
%33 = tail call i32 @_exit(i32 0)
unreachable
34: ; preds = %17
%35 = add nuw nsw i32 %18, 1
%36 = icmp eq i32 %35, %11
br i1 %36, label %37, label %17
37: ; preds = %34, %8
br label %38
38: ; preds = %37, %38
%39 = call i32 @wait(i32* %3)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %38
41: ; preds = %38
br label %42
42: ; preds = %41, %5
ret i32 0
}