#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 *);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  pid_t pid;
  int n;
  if (argc != 2) {
    printf("Zła ilość argumentów! ./a.out [n]\n");
    return 1;
  }
  n = atoi(argv[1]);
  printf("%d ", getpid());
  int i = 0;
  while (i < n) {
    pid = fork();
    if (pid == 0) {
      --n;
      i = 0;
      printf("%d ", getpid());
    } else {
      ++i;
      int status;
      while (-1 == waitpid(pid, &status, 0))
        ;
    }
  }
  printf("\n");
  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)
je     118a <main+0x3a>
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1251 <main+0x101>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    123c <main+0xec>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1206 <main+0xb6>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1237 <main+0xe7>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x24(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1232 <main+0xe2>
jmp    120f <main+0xbf>
jmp    1237 <main+0xe7>
jmp    11b8 <main+0x68>
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
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   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    1217 <main+0xa7>
mov    0x8(%rsi),%rdi
call   1080 <atoi@plt>
mov    %eax,%r12d
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe5f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
test   %r12d,%r12d
jle    120b <main+0x9b>
lea    0xe4a(%rip),%r15
lea    0x4(%rsp),%rbx
xor    %r13d,%r13d
jmp    11df <main+0x6f>
add    $0xffffffff,%r12d
xor    %r13d,%r13d
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
cmp    %r12d,%r13d
jge    120b <main+0x9b>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     11c0 <main+0x50>
mov    %rax,%rbp
xchg   %ax,%ax
mov    %rbp,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    $0xffffffff,%eax
je     11f0 <main+0x80>
add    $0x1,%r13d
cmp    %r12d,%r13d
jl     11df <main+0x6f>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    1229 <main+0xb9>
lea    0xde6(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [39 x i8] c"Z\C5\82a ilo\C5\9B\C4\87 argument\C3\B3w! ./a.out [n]\0A\00"
@.str.1 = [4 x i8] c"%d \00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%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 ne i32 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %46
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* %7
%19 = call i32 (...) @getpid()
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %19)
store i32 0, i32* %8
br label %21
21: ; preds = %43, %14
%22 = load i32, i32* %8
%23 = load i32, i32* %7
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %44
25: ; preds = %21
%26 = call i64 (...) @fork()
store i64 %26, i64* %6
%27 = load i64, i64* %6
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %25
%30 = load i32, i32* %7
%31 = add nsw i32 %30, -1
store i32 %31, i32* %7
store i32 0, i32* %8
%32 = call i32 (...) @getpid()
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %32)
br label %43
34: ; preds = %25
%35 = load i32, i32* %8
%36 = add nsw i32 %35, 1
store i32 %36, i32* %8
br label %37
37: ; preds = %41, %34
%38 = load i64, i64* %6
%39 = call i32 @waitpid(i64 %38, i32* %9, i32 0)
%40 = icmp eq i32 -1, %39
br i1 %40, label %41, label %42
41: ; preds = %37
br label %37
42: ; preds = %37
br label %43
43: ; preds = %42, %29
br label %21
44: ; preds = %21
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %46
46: ; preds = %44, %12
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@.str.1 = [4 x i8] c"%d \00"
@str = [38 x i8] c"Z\C5\82a ilo\C5\9B\C4\87 argument\C3\B3w! ./a.out [n]\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = icmp eq i32 %0, 2
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
br label %37
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @atoi(i8* %9)
%11 = tail call i32 (...) @getpid()
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = icmp sgt i32 %10, 0
br i1 %13, label %14, label %35
14: ; preds = %7
%15 = bitcast i32* %3 to i8*
br label %16
16: ; preds = %14, %31
%17 = phi i32 [ 0, %14 ], [ %33, %31 ]
%18 = phi i32 [ %10, %14 ], [ %32, %31 ]
%19 = call i64 (...) @fork()
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = add nsw i32 %18, -1
%23 = call i32 (...) @getpid()
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %23)
br label %31
25: ; preds = %16
br label %26
26: ; preds = %26, %25
%27 = call i32 @waitpid(i64 %19, i32* %3, i32 0)
%28 = icmp eq i32 %27, -1
br i1 %28, label %26, label %29
29: ; preds = %26
%30 = add nsw i32 %17, 1
br label %31
31: ; preds = %29, %21
%32 = phi i32 [ %22, %21 ], [ %18, %29 ]
%33 = phi i32 [ 0, %21 ], [ %30, %29 ]
%34 = icmp slt i32 %33, %32
br i1 %34, label %16, label %35
35: ; preds = %31, %7
%36 = call i32 @putchar(i32 10)
br label %37
37: ; preds = %35, %5
%38 = phi i32 [ 1, %5 ], [ 0, %35 ]
ret i32 %38
}