main(argc, argv) char **argv;
{
  int child, waitchild;
  int status;
  child = fork();
  if (child == -1) {
    printf("fork returned -1\n");
    return 1;
  }
  if (child) {
    do {
      waitchild = wait(&status);
      if (waitchild == -1) {
        printf("No children\n");
        return 1;
      }
    } while (waitchild != child);
    if (argc <= 1 && status != (8 << 8)) {
      printf("incorrect status return\n");
      return 2;
    }
  } else {
    if (argc > 1)
      pause();
    return 8;
  }
  printf("fork/wait ok\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1184 <main+0x44>
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    123e <main+0xfe>
cmpl   $0x0,-0x14(%rbp)
je     120c <main+0xcc>
jmp    1193 <main+0x53>
lea    -0x1c(%rbp),%rdi
mov    $0x0,%al
call   1050 <wait@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11c5 <main+0x85>
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    123e <main+0xfe>
jmp    11ca <main+0x8a>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    1193 <main+0x53>
cmpl   $0x1,-0x8(%rbp)
jg     1207 <main+0xc7>
cmpl   $0x800,-0x1c(%rbp)
je     1207 <main+0xc7>
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    123e <main+0xfe>
jmp    1229 <main+0xe9>
cmpl   $0x1,-0x8(%rbp)
jle    121d <main+0xdd>
mov    $0x0,%al
call   1040 <pause@plt>
movl   $0x8,-0x4(%rbp)
jmp    123e <main+0xfe>
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
xor    %eax,%eax
call   1060 <fork@plt>
cmp    $0xffffffff,%eax
je     1171 <main+0x31>
mov    %eax,%ebp
test   %eax,%eax
jne    117a <main+0x3a>
mov    $0x8,%ebp
cmp    $0x2,%r14d
jl     11c7 <main+0x87>
xor    %eax,%eax
call   1040 <pause@plt>
jmp    11c7 <main+0x87>
lea    0xeb9(%rip),%rdi
jmp    11bd <main+0x7d>
lea    0xc(%rsp),%rbx
nop
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <wait@plt>
cmp    $0xffffffff,%eax
je     11b6 <main+0x76>
cmp    %ebp,%eax
jne    1180 <main+0x40>
cmp    $0x1,%r14d
jg     11d2 <main+0x92>
cmpl   $0x800,0xc(%rsp)
je     11d2 <main+0x92>
lea    0xe63(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%ebp
jmp    11c7 <main+0x87>
lea    0xe68(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe27(%rip),%rdi
call   1030 <puts@plt>
xor    %ebp,%ebp
jmp    11c7 <main+0x87>

LLVM IR (-O0):
@.str = [18 x i8] c"fork returned -1\0A\00"
@.str.1 = [13 x i8] c"No children\0A\00"
@.str.2 = [25 x i8] c"incorrect status return\0A\00"
@.str.3 = [14 x i8] c"fork/wait ok\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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (...) @fork()
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %46
14: ; preds = %2
%15 = load i32, i32* %6
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %38
17: ; preds = %14
br label %18
18: ; preds = %25, %17
%19 = call i32 (i32*, ...) bitcast (i32 (...)* @wait to i32 (i32*, ...)*)(i32* %8)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %46
24: ; preds = %18
br label %25
25: ; preds = %24
%26 = load i32, i32* %7
%27 = load i32, i32* %6
%28 = icmp ne i32 %26, %27
br i1 %28, label %18, label %29
29: ; preds = %25
%30 = load i32, i32* %4
%31 = icmp sle i32 %30, 1
br i1 %31, label %32, label %37
32: ; preds = %29
%33 = load i32, i32* %8
%34 = icmp ne i32 %33, 2048
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %3
br label %46
37: ; preds = %32, %29
br label %44
38: ; preds = %14
%39 = load i32, i32* %4
%40 = icmp sgt i32 %39, 1
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 (...) @pause()
br label %43
43: ; preds = %41, %38
store i32 8, i32* %3
br label %46
44: ; preds = %37
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %46
46: ; preds = %44, %43, %35, %22, %12
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@str = [13 x i8] c"fork/wait ok\00"
@str.4 = [24 x i8] c"incorrect status return\00"
@str.5 = [12 x i8] c"No children\00"
@str.6 = [17 x i8] c"fork returned -1\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 (...) @fork()
switch i32 %5, label %8 [
i32 -1, label %6
i32 0, label %22
]
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.6, i64 0, i64 0))
br label %28
8: ; preds = %2, %13
%9 = call i32 (i32*, ...) bitcast (i32 (...)* @wait to i32 (i32*, ...)*)(i32* %3)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.5, i64 0, i64 0))
br label %28
13: ; preds = %8
%14 = icmp eq i32 %9, %5
br i1 %14, label %15, label %8
15: ; preds = %13
%16 = icmp slt i32 %0, 2
%17 = load i32, i32* %3
%18 = icmp ne i32 %17, 2048
%19 = select i1 %16, i1 %18, i1 false
br i1 %19, label %20, label %26
20: ; preds = %15
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.4, i64 0, i64 0))
br label %28
22: ; preds = %2
%23 = icmp sgt i32 %0, 1
br i1 %23, label %24, label %28
24: ; preds = %22
%25 = tail call i32 (...) @pause()
br label %28
26: ; preds = %15
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %28
28: ; preds = %22, %24, %26, %20, %11, %6
%29 = phi i32 [ 1, %6 ], [ 1, %11 ], [ 2, %20 ], [ 0, %26 ], [ 8, %24 ], [ 8, %22 ]
ret i32 %29
}