#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 WEXITSTATUS(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int wait(int *);
int main() {
  int pid, npid;
  if ((pid = fork()) == -1) {
    printf("error!");
  } else if (pid == 0) {
    sleep(10);
    printf("first child\n");
  } else {
    if ((npid = fork() == 0)) {
      sleep(1);
      printf("second child\n");
    } else {
      int status;
      int t = wait(&status);
      printf("parent status=%d t=%d\n", WEXITSTATUS(status), t);
    }
  }
  exit(1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1195 <main+0x35>
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    122f <main+0xcf>
cmpl   $0x0,-0x8(%rbp)
jne    11bc <main+0x5c>
mov    $0xa,%edi
call   1060 <sleep@plt>
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    122a <main+0xca>
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%eax
sete   %al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0xc(%rbp)
test   $0x1,%al
jne    11e1 <main+0x81>
jmp    11fe <main+0x9e>
mov    $0x1,%edi
call   1060 <sleep@plt>
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1225 <main+0xc5>
lea    -0x10(%rbp),%rdi
call   1070 <wait@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %eax,%esi
mov    -0x14(%rbp),%edx
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    122a <main+0xca>
jmp    122f <main+0xcf>
mov    $0x1,%edi
call   1050 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     119d <main+0x2d>
cmp    $0xffffffff,%eax
jne    11b0 <main+0x40>
lea    0xe74(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    $0xa,%edi
call   1070 <sleep@plt>
lea    0xe7d(%rip),%rdi
jmp    11cc <main+0x5c>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
jne    11db <main+0x6b>
mov    $0x1,%edi
call   1070 <sleep@plt>
lea    0xe52(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
lea    0xc(%rsp),%rdi
call   1080 <wait@plt>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
lea    0xe10(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"error!\00"
@.str.1 = [13 x i8] c"first child\0A\00"
@.str.2 = [14 x i8] c"second child\0A\00"
@.str.3 = [23 x i8] c"parent status=%d t=%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 (...) @fork()
store i32 %6, i32* %2
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %31
10: ; preds = %0
%11 = load i32, i32* %2
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = call i32 @sleep(i32 10)
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %30
16: ; preds = %10
%17 = call i32 (...) @fork()
%18 = icmp eq i32 %17, 0
%19 = zext i1 %18 to i32
store i32 %19, i32* %3
br i1 %18, label %20, label %23
20: ; preds = %16
%21 = call i32 @sleep(i32 1)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %29
23: ; preds = %16
%24 = call i32 @wait(i32* %4)
store i32 %24, i32* %5
%25 = load i32, i32* %4
%26 = call i32 @WEXITSTATUS(i32 %25)
%27 = load i32, i32* %5
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %26, i32 %27)
br label %29
29: ; preds = %23, %20
br label %30
30: ; preds = %29, %13
br label %31
31: ; preds = %30, %8
%32 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@.str = [7 x i8] c"error!\00"
@.str.3 = [23 x i8] c"parent status=%d t=%d\0A\00"
@str = [13 x i8] c"second child\00"
@str.4 = [12 x i8] c"first child\00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i32 (...) @fork()
switch i32 %2, label %8 [
i32 -1, label %3
i32 0, label %5
]
3: ; preds = %0
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %20
5: ; preds = %0
%6 = tail call i32 @sleep(i32 10)
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.4, i64 0, i64 0))
br label %20
8: ; preds = %0
%9 = tail call i32 (...) @fork()
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = tail call i32 @sleep(i32 1)
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %20
14: ; preds = %8
%15 = bitcast i32* %1 to i8*
%16 = call i32 @wait(i32* %1)
%17 = load i32, i32* %1
%18 = call i32 @WEXITSTATUS(i32 %17)
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %18, i32 %16)
br label %20
20: ; preds = %5, %14, %11, %3
%21 = call i32 @exit(i32 1)
unreachable
}