#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);
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
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);
    } else {
      ptr[i] = pid;
    }
  }
  for (int j = 0; j != n; ++j) {
    waitpid(ptr[j], NULL, 0);
  }
  free(ptr);
  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    128d <main+0x10d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <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     1246 <main+0xc6>
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   1090 <sleep@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x28(%rbp),%rax
mov    %eax,%edx
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    1238 <main+0xb8>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11e2 <main+0x62>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     127d <main+0xfd>
mov    -0x20(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    124d <main+0xcd>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    $0x1,%edi
jg     11a0 <main+0x20>
lea    0xe6c(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%eax
jmp    1210 <main+0x90>
mov    0x8(%rsi),%rdi
call   1070 <atoi@plt>
mov    %eax,%r14d
lea    0x0(,%r14,4),%edi
call   1050 <malloc@plt>
mov    %rax,%r15
test   %r14d,%r14d
je     1206 <main+0x86>
mov    %r14d,%r12d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     121c <main+0x9c>
mov    %eax,(%r15,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    11d0 <main+0x50>
test   %r14d,%r14d
je     1206 <main+0x86>
xor    %ebx,%ebx
mov    (%r15,%rbx,4),%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    11f0 <main+0x70>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %r15,%rdi
call   1030 <free@plt>
not    %ebx
add    %r14d,%ebx
mov    %ebx,%edi
call   1090 <sleep@plt>
xor    %edi,%edi
call   1080 <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
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %71
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @atoi(i8* %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = sext i32 %20 to i64
%22 = mul i64 4, %21
%23 = trunc i64 %22 to i32
%24 = call i32* @malloc(i32 %23)
store i32* %24, i32** %7
store i32 0, i32* %9
br label %25
25: ; preds = %50, %15
%26 = load i32, i32* %9
%27 = load i32, i32* %6
%28 = icmp ne i32 %26, %27
br i1 %28, label %29, label %53
29: ; preds = %25
%30 = call i64 (...) @fork()
store i64 %30, i64* %8
%31 = load i64, i64* %8
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %42
33: ; preds = %29
%34 = load i32*, i32** %7
%35 = call i32 @free(i32* %34)
%36 = load i32, i32* %6
%37 = load i32, i32* %9
%38 = sub nsw i32 %36, %37
%39 = sub nsw i32 %38, 1
%40 = call i32 @sleep(i32 %39)
%41 = call i32 @exit(i32 0)
unreachable
42: ; preds = %29
%43 = load i64, i64* %8
%44 = trunc i64 %43 to i32
%45 = load i32*, i32** %7
%46 = load i32, i32* %9
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32, i32* %45, i64 %47
store i32 %44, i32* %48
br label %49
49: ; preds = %42
br label %50
50: ; preds = %49
%51 = load i32, i32* %9
%52 = add nsw i32 %51, 1
store i32 %52, i32* %9
br label %25
53: ; preds = %25
store i32 0, i32* %10
br label %54
54: ; preds = %65, %53
%55 = load i32, i32* %10
%56 = load i32, i32* %6
%57 = icmp ne i32 %55, %56
br i1 %57, label %58, label %68
58: ; preds = %54
%59 = load i32*, i32** %7
%60 = load i32, i32* %10
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i32, i32* %59, i64 %61
%63 = load i32, i32* %62
%64 = call i32 @waitpid(i32 %63, i32* null, i32 0)
br label %65
65: ; preds = %58
%66 = load i32, i32* %10
%67 = add nsw i32 %66, 1
store i32 %67, i32* %10
br label %54
68: ; preds = %54
%69 = load i32*, i32** %7
%70 = call i32 @free(i32* %69)
store i32 0, i32* %3
br label %71
71: ; preds = %68, %13
%72 = load i32, i32* %3
ret i32 %72
}

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 %43
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 %34, label %13
13: ; preds = %6
%14 = zext i32 %9 to i64
br label %18
15: ; preds = %29
br i1 %12, label %34, label %16
16: ; preds = %15
%17 = zext i32 %9 to i64
br label %36
18: ; preds = %13, %29
%19 = phi i64 [ 0, %13 ], [ %32, %29 ]
%20 = tail call i64 (...) @fork()
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %18
%23 = trunc i64 %19 to i32
%24 = tail call i32 @free(i32* %11)
%25 = xor i32 %23, -1
%26 = add i32 %9, %25
%27 = tail call i32 @sleep(i32 %26)
%28 = tail call i32 @exit(i32 0)
unreachable
29: ; preds = %18
%30 = trunc i64 %20 to i32
%31 = getelementptr inbounds i32, i32* %11, i64 %19
store i32 %30, i32* %31
%32 = add nuw nsw i64 %19, 1
%33 = icmp eq i64 %32, %14
br i1 %33, label %15, label %18
34: ; preds = %36, %6, %15
%35 = tail call i32 @free(i32* %11)
br label %43
36: ; preds = %16, %36
%37 = phi i64 [ 0, %16 ], [ %41, %36 ]
%38 = getelementptr inbounds i32, i32* %11, i64 %37
%39 = load i32, i32* %38
%40 = tail call i32 @waitpid(i32 %39, i32* null, i32 0)
%41 = add nuw nsw i64 %37, 1
%42 = icmp eq i64 %41, %17
br i1 %42, label %34, label %36
43: ; preds = %34, %4
%44 = phi i32 [ -1, %4 ], [ 0, %34 ]
ret i32 %44
}