#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 scalar_t__ pid_t;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ free(int *);
int *malloc(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
scalar_t__ wait(int *);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("the number of args is wrong!");
    return -1;
  }
  int n = atoi(argv[1]);
  int *ptr = malloc(sizeof(int) * n);
  pid_t pid;
  for (int i = 0; i != n; ++i) {
    pid = fork();
    if (pid == 0) {
      free(ptr);
      sleep(n - i - 1);
      exit(0);
    }
    while (wait((int *)0) != pid)
      ;
  }
  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   $0x2,-0x8(%rbp)
jge    11b8 <main+0x38>
lea    0xe59(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1259 <main+0xd9>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     1252 <main+0xd2>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1222 <main+0xa2>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%edi
sub    -0x2c(%rbp),%edi
sub    $0x1,%edi
call   1080 <sleep@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    1227 <main+0xa7>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
cmp    -0x28(%rbp),%rax
je     123f <main+0xbf>
jmp    1227 <main+0xa7>
jmp    1244 <main+0xc4>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11e2 <main+0x62>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x1,%edi
jg     119f <main+0x1f>
lea    0xe6d(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%eax
jmp    11f6 <main+0x76>
mov    0x8(%rsi),%rdi
call   1060 <atoi@plt>
mov    %eax,%r15d
lea    0x0(,%r15,4),%edi
call   1050 <malloc@plt>
test   %r15d,%r15d
je     11f4 <main+0x74>
mov    %rax,%r14
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1201 <main+0x81>
nop
xor    %edi,%edi
call   1090 <wait@plt>
cmp    %rbx,%rax
jne    11e0 <main+0x60>
add    $0x1,%ebp
cmp    %ebp,%r15d
jne    11d0 <main+0x50>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
call   1030 <free@plt>
not    %ebp
add    %ebp,%r15d
mov    %r15d,%edi
call   1080 <sleep@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [29 x i8] c"the number of args is wrong!\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 i64
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %52
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @atoi(i8* %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = sext i32 %19 to i64
%21 = mul i64 4, %20
%22 = trunc i64 %21 to i32
%23 = call i32* @malloc(i32 %22)
store i32* %23, i32** %7
store i32 0, i32* %9
br label %24
24: ; preds = %48, %14
%25 = load i32, i32* %9
%26 = load i32, i32* %6
%27 = icmp ne i32 %25, %26
br i1 %27, label %28, label %51
28: ; preds = %24
%29 = call i64 (...) @fork()
store i64 %29, i64* %8
%30 = load i64, i64* %8
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %41
32: ; preds = %28
%33 = load i32*, i32** %7
%34 = call i32 @free(i32* %33)
%35 = load i32, i32* %6
%36 = load i32, i32* %9
%37 = sub nsw i32 %35, %36
%38 = sub nsw i32 %37, 1
%39 = call i32 @sleep(i32 %38)
%40 = call i32 @exit(i32 0)
unreachable
41: ; preds = %28
br label %42
42: ; preds = %46, %41
%43 = call i64 @wait(i32* null)
%44 = load i64, i64* %8
%45 = icmp ne i64 %43, %44
br i1 %45, label %46, label %47
46: ; preds = %42
br label %42
47: ; preds = %42
br label %48
48: ; preds = %47
%49 = load i32, i32* %9
%50 = add nsw i32 %49, 1
store i32 %50, i32* %9
br label %24
51: ; preds = %24
store i32 0, i32* %3
br label %52
52: ; preds = %51, %12
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@.str = [29 x i8] c"the number of args is wrong!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %29
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
%10 = shl i32 %9, 2
%11 = tail call i32* @malloc(i32 %10)
%12 = icmp eq i32 %9, 0
br i1 %12, label %29, label %13
13: ; preds = %6, %26
%14 = phi i32 [ %27, %26 ], [ 0, %6 ]
%15 = tail call i64 (...) @fork()
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %13
%18 = tail call i32 @free(i32* %11)
%19 = xor i32 %14, -1
%20 = add i32 %9, %19
%21 = tail call i32 @sleep(i32 %20)
%22 = tail call i32 @exit(i32 0)
unreachable
23: ; preds = %13, %23
%24 = tail call i64 @wait(i32* null)
%25 = icmp eq i64 %24, %15
br i1 %25, label %26, label %23
26: ; preds = %23
%27 = add nuw nsw i32 %14, 1
%28 = icmp eq i32 %9, %27
br i1 %28, label %29, label %13
29: ; preds = %26, %6, %4
%30 = phi i32 [ -1, %4 ], [ 0, %6 ], [ 0, %26 ]
ret i32 %30
}