#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 WEXITSTATUS(int);
int WIFEXITED(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
scalar_t__ wait(int *);
int main() {
  pid_t pid, cpid;
  int status;
  printf("S1\n");
  pid = fork();
  if (pid == 0) {
    sleep(1);
    printf("S2-child\n");
    exit(0);
  } else {
    sleep(3);
    printf("S2-father\n");
    wait(NULL);
  }
  printf("S4\n");
  pid = fork();
  if (pid == 0) {
    int status1 = 5;
    sleep(4);
    printf("S5-child\n");
    printf("CHILD: fpid: %d; mypid: %d; mystatus: %d\n", getppid(), pid,
           status1);
    status = -34;
    exit(status1);
  } else {
    sleep(1);
    printf("S5-father\n");
    cpid = wait(&status);
    printf("FATHER: mypid=%d; cpid: %d; status: %x; ", getpid(), cpid, status);
    printf("WIFEXITED: %d; WEXITSTATUS: %d\n", WIFEXITED(status),
           WEXITSTATUS(status));
  }
  sleep(4);
  printf("S7\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11e2 <main+0x52>
mov    $0x1,%edi
call   1090 <sleep@plt>
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    $0x3,%edi
call   1090 <sleep@plt>
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1273 <main+0xe3>
movl   $0x5,-0x20(%rbp)
mov    $0x4,%edi
call   1090 <sleep@plt>
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1070 <getppid@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%ecx
lea    0xdca(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffde,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
call   1080 <exit@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x1c(%rbp),%rdi
call   10a0 <wait@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
lea    0xdad(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1060 <WIFEXITED@plt>
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    -0x24(%rbp),%esi
mov    %eax,%edx
lea    0xdb0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x4,%edi
call   1090 <sleep@plt>
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xec7(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
je     127f <main+0xdf>
mov    $0x3,%edi
call   10a0 <sleep@plt>
lea    0xea4(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10b0 <wait@plt>
lea    0xe9b(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
je     129c <main+0xfc>
mov    $0x1,%edi
call   10a0 <sleep@plt>
lea    0xe78(%rip),%rdi
call   1030 <puts@plt>
lea    0xc(%rsp),%rdi
call   10b0 <wait@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    0xc(%rsp),%ecx
lea    0xdfb(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rsp),%edi
call   1070 <WIFEXITED@plt>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
lea    0xdfd(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x4,%edi
call   10a0 <sleep@plt>
lea    0xe1b(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    $0x1,%edi
call   10a0 <sleep@plt>
lea    0xe09(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    $0x4,%edi
call   10a0 <sleep@plt>
lea    0xde3(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <getppid@plt>
lea    0xd40(%rip),%rdi
mov    %eax,%esi
xor    %edx,%edx
mov    $0x5,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x5,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"S1\0A\00"
@.str.1 = [10 x i8] c"S2-child\0A\00"
@.str.2 = [11 x i8] c"S2-father\0A\00"
@.str.3 = [4 x i8] c"S4\0A\00"
@.str.4 = [10 x i8] c"S5-child\0A\00"
@.str.5 = [42 x i8] c"CHILD: fpid: %d; mypid: %d; mystatus: %d\0A\00"
@.str.6 = [11 x i8] c"S5-father\0A\00"
@.str.7 = [41 x i8] c"FATHER: mypid=%d; cpid: %d; status: %x; \00"
@.str.8 = [32 x i8] c"WIFEXITED: %d; WEXITSTATUS: %d\0A\00"
@.str.9 = [4 x i8] c"S7\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%7 = call i64 (...) @fork()
store i64 %7, i64* %2
%8 = load i64, i64* %2
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = call i32 @sleep(i32 1)
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 0)
unreachable
14: ; preds = %0
%15 = call i32 @sleep(i32 3)
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%17 = call i64 @wait(i32* null)
br label %18
18: ; preds = %14
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%20 = call i64 (...) @fork()
store i64 %20, i64* %2
%21 = load i64, i64* %2
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %32
23: ; preds = %18
store i32 5, i32* %5
%24 = call i32 @sleep(i32 4)
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%26 = call i32 (...) @getppid()
%27 = load i64, i64* %2
%28 = load i32, i32* %5
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i32 %26, i64 %27, i32 %28)
store i32 -34, i32* %4
%30 = load i32, i32* %5
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %18
%33 = call i32 @sleep(i32 1)
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
%35 = call i64 @wait(i32* %4)
store i64 %35, i64* %3
%36 = call i32 (...) @getpid()
%37 = load i64, i64* %3
%38 = load i32, i32* %4
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.7, i64 0, i64 0), i32 %36, i64 %37, i32 %38)
%40 = load i32, i32* %4
%41 = call i32 @WIFEXITED(i32 %40)
%42 = load i32, i32* %4
%43 = call i32 @WEXITSTATUS(i32 %42)
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0), i32 %41, i32 %43)
br label %45
45: ; preds = %32
%46 = call i32 @sleep(i32 4)
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.9, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str.5 = [42 x i8] c"CHILD: fpid: %d; mypid: %d; mystatus: %d\0A\00"
@.str.7 = [41 x i8] c"FATHER: mypid=%d; cpid: %d; status: %x; \00"
@.str.8 = [32 x i8] c"WIFEXITED: %d; WEXITSTATUS: %d\0A\00"
@str = [3 x i8] c"S1\00"
@str.10 = [10 x i8] c"S2-father\00"
@str.11 = [3 x i8] c"S4\00"
@str.12 = [10 x i8] c"S5-father\00"
@str.13 = [3 x i8] c"S7\00"
@str.14 = [9 x i8] c"S5-child\00"
@str.15 = [9 x i8] c"S2-child\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str, i64 0, i64 0))
%4 = tail call i64 (...) @fork()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %0
%7 = tail call i32 @sleep(i32 1)
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.15, i64 0, i64 0))
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %0
%11 = tail call i32 @sleep(i32 3)
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.10, i64 0, i64 0))
%13 = tail call i64 @wait(i32* null)
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str.11, i64 0, i64 0))
%15 = tail call i64 (...) @fork()
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %10
%18 = tail call i32 @sleep(i32 4)
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.14, i64 0, i64 0))
%20 = tail call i32 (...) @getppid()
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i32 %20, i64 0, i32 5)
%22 = tail call i32 @exit(i32 5)
unreachable
23: ; preds = %10
%24 = tail call i32 @sleep(i32 1)
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.12, i64 0, i64 0))
%26 = call i64 @wait(i32* %1)
%27 = call i32 (...) @getpid()
%28 = load i32, i32* %1
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.7, i64 0, i64 0), i32 %27, i64 %26, i32 %28)
%30 = load i32, i32* %1
%31 = call i32 @WIFEXITED(i32 %30)
%32 = load i32, i32* %1
%33 = call i32 @WEXITSTATUS(i32 %32)
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0), i32 %31, i32 %33)
%35 = call i32 @sleep(i32 4)
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str.13, i64 0, i64 0))
ret i32 0
}