#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 fork();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int wait(int *);
int main(int argc, char *argv[]) {
  int first_child = fork();
  if (first_child == 0) {
    printf("in first child\n");
    sleep(10);
    printf("terminating first child\n");
    exit(0);
  }
  int second_child = fork();
  if (second_child == 0) {
    printf("in second child\n");
    sleep(30);
    printf("terminating second child\n");
    exit(0);
  }
  printf("in parent, children started\n");
  sleep(20);
  printf("collecting children\n");
  int i;
  int status;
  for (i = 0; i < 2; i++) {
    int pid = wait(&status);
    printf("child %d terminated (first_child=%d second_child=%d)\n", pid,
           first_child, second_child);
  }
  printf("done\n");
  exit(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)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11a7 <main+0x57>
lea    0xe7f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xa,%edi
call   1050 <sleep@plt>
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1040 <exit@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    11e8 <main+0x98>
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1e,%edi
call   1050 <sleep@plt>
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1040 <exit@plt>
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x14,%edi
call   1050 <sleep@plt>
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x2,-0x1c(%rbp)
jge    1250 <main+0x100>
lea    -0x20(%rbp),%rdi
call   1060 <wait@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1215 <main+0xc5>
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1040 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
jne    1194 <main+0x34>
lea    0xf1b(%rip),%rdi
call   1030 <puts@plt>
mov    $0xa,%edi
call   1060 <sleep@plt>
lea    0xf14(%rip),%rdi
jmp    1227 <main+0xc7>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
jne    11c0 <main+0x60>
lea    0xec3(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1e,%edi
call   1060 <sleep@plt>
lea    0xebd(%rip),%rdi
jmp    1227 <main+0xc7>
mov    %eax,%ebp
lea    0xe6d(%rip),%rdi
call   1030 <puts@plt>
mov    $0x14,%edi
call   1060 <sleep@plt>
lea    0xe73(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%r14
mov    %r14,%rdi
call   1070 <wait@plt>
lea    0xe08(%rip),%r15
mov    %r15,%rdi
mov    %eax,%esi
mov    %ebx,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
call   1070 <wait@plt>
mov    %r15,%rdi
mov    %eax,%esi
mov    %ebx,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe3f(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"in first child\0A\00"
@.str.1 = [25 x i8] c"terminating first child\0A\00"
@.str.2 = [17 x i8] c"in second child\0A\00"
@.str.3 = [26 x i8] c"terminating second child\0A\00"
@.str.4 = [29 x i8] c"in parent, children started\0A\00"
@.str.5 = [21 x i8] c"collecting children\0A\00"
@.str.6 = [54 x i8] c"child %d terminated (first_child=%d second_child=%d)\0A\00"
@.str.7 = [6 x i8] c"done\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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 (...) @fork()
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @sleep(i32 10)
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 0)
unreachable
19: ; preds = %2
%20 = call i32 (...) @fork()
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @sleep(i32 30)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%27 = call i32 @exit(i32 0)
unreachable
28: ; preds = %19
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
%30 = call i32 @sleep(i32 20)
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %8
br label %32
32: ; preds = %41, %28
%33 = load i32, i32* %8
%34 = icmp slt i32 %33, 2
br i1 %34, label %35, label %44
35: ; preds = %32
%36 = call i32 @wait(i32* %9)
store i32 %36, i32* %10
%37 = load i32, i32* %10
%38 = load i32, i32* %6
%39 = load i32, i32* %7
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.6, i64 0, i64 0), i32 %37, i32 %38, i32 %39)
br label %41
41: ; preds = %35
%42 = load i32, i32* %8
%43 = add nsw i32 %42, 1
store i32 %43, i32* %8
br label %32
44: ; preds = %32
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%46 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.6 = [54 x i8] c"child %d terminated (first_child=%d second_child=%d)\0A\00"
@str = [28 x i8] c"in parent, children started\00"
@str.8 = [20 x i8] c"collecting children\00"
@str.9 = [5 x i8] c"done\00"
@str.10 = [16 x i8] c"in second child\00"
@str.11 = [25 x i8] c"terminating second child\00"
@str.12 = [15 x i8] c"in first child\00"
@str.13 = [24 x i8] c"terminating first child\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = tail call i32 (...) @fork()
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.12, i64 0, i64 0))
%8 = tail call i32 @sleep(i32 10)
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.13, i64 0, i64 0))
%10 = tail call i32 @exit(i32 0)
unreachable
11: ; preds = %2
%12 = tail call i32 (...) @fork()
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %11
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.10, i64 0, i64 0))
%16 = tail call i32 @sleep(i32 30)
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.11, i64 0, i64 0))
%18 = tail call i32 @exit(i32 0)
unreachable
19: ; preds = %11
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%21 = tail call i32 @sleep(i32 20)
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.8, i64 0, i64 0))
%23 = bitcast i32* %3 to i8*
%24 = call i32 @wait(i32* %3)
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str.6, i64 0, i64 0), i32 %24, i32 %4, i32 %12)
%26 = call i32 @wait(i32* %3)
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str.6, i64 0, i64 0), i32 %26, i32 %4, i32 %12)
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.9, i64 0, i64 0))
%29 = call i32 @exit(i32 0)
unreachable
}