#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 int pid_t;

/* Variables and functions */
scalar_t__ WCOREDUMP(int);
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ exec_ps_cmd();
int fork();
scalar_t__ getpid();
scalar_t__ getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int wait(int *);
int main(int argc, char *argv[]) {
  pid_t pid;
  pid_t wpid;
  int status;
  pid = fork();
  if (pid == -1) {
    perror("fork()");
    return 1;
  } else if (pid == 0) {
    printf("PID %ld: child started, parent is %ld\n", (long)getpid(),
           (long)getppid());
    exec_ps_cmd();
  } else {
    printf("PID %ld: started child PID %ld\n", (long)getpid(), (long)pid);
    wpid = wait(&status);
    if (wpid == -1) {
      perror("wait()");
      return 1;
    } else if (wpid != pid) {
      return 1;
    } else {
      if (WIFEXITED(status)) {
        printf("exited: $? = %d\n", WEXITSTATUS(status));
      } else if (WIFSIGNALED(status)) {
        printf("signal: %d%s\n", WTERMSIG(status),
               (WCOREDUMP(status) ? " with core file." : ""));
      } else {
        printf("stopped\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)
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1202 <main+0x42>
lea    0xe0f(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1352 <main+0x192>
cmpl   $0x0,-0x14(%rbp)
jne    123f <main+0x7f>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   10c0 <getppid@plt>
mov    -0x28(%rbp),%rsi
mov    %rax,%rdx
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <exec_ps_cmd@plt>
jmp    1346 <main+0x186>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,%rsi
movslq -0x14(%rbp),%rdx
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x1c(%rbp),%rdi
call   10d0 <wait@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1289 <main+0xc9>
lea    0xdd6(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1352 <main+0x192>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     12a1 <main+0xe1>
movl   $0x1,-0x4(%rbp)
jmp    1352 <main+0x192>
mov    -0x1c(%rbp),%edi
call   10b0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12d0 <main+0x110>
mov    -0x1c(%rbp),%edi
call   1090 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1337 <main+0x177>
mov    -0x1c(%rbp),%edi
call   1070 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1324 <main+0x164>
mov    -0x1c(%rbp),%edi
call   1080 <WTERMSIG@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <WCOREDUMP@plt>
mov    -0x2c(%rbp),%esi
mov    %rax,%rcx
lea    0xd63(%rip),%rdx
lea    0xd6b(%rip),%rax
cmp    $0x0,%rcx
cmovne %rax,%rdx
lea    0xd4e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1332 <main+0x172>
lea    0xd5a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1337 <main+0x177>
jmp    133c <main+0x17c>
jmp    1341 <main+0x181>
jmp    1346 <main+0x186>
jmp    134b <main+0x18b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %ebx,%ebx
xor    %eax,%eax
call   10f0 <fork@plt>
test   %eax,%eax
je     124d <main+0x7d>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     12d9 <main+0x109>
xor    %eax,%eax
call   1050 <getpid@plt>
movslq %ebp,%rdx
lea    0xe32(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x4(%rsp),%rdi
call   10e0 <wait@plt>
cmp    $0xffffffff,%eax
je     12e2 <main+0x112>
mov    $0x1,%ebx
cmp    %ebp,%eax
jne    1279 <main+0xa9>
mov    0x4(%rsp),%edi
call   10c0 <WIFEXITED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     1282 <main+0xb2>
call   10a0 <WEXITSTATUS@plt>
lea    0xe15(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1279 <main+0xa9>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   10d0 <getppid@plt>
lea    0xda2(%rip),%rdi
mov    %rbp,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1030 <exec_ps_cmd@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
call   1080 <WIFSIGNALED@plt>
test   %rax,%rax
je     12c9 <main+0xf9>
mov    0x4(%rsp),%edi
call   1090 <WTERMSIG@plt>
mov    %eax,%ebp
mov    0x4(%rsp),%edi
call   1070 <WCOREDUMP@plt>
test   %rax,%rax
lea    0xdbb(%rip),%rax
lea    0xdc3(%rip),%rdx
cmove  %rax,%rdx
lea    0xdaa(%rip),%rdi
xor    %ebx,%ebx
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1279 <main+0xa9>
lea    0xdb5(%rip),%rdi
call   1040 <puts@plt>
xor    %ebx,%ebx
jmp    1279 <main+0xa9>
lea    0xd20(%rip),%rdi
jmp    12e9 <main+0x119>
lea    0xd65(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%ebx
jmp    1279 <main+0xa9>

LLVM IR (-O0):
@.str = [7 x i8] c"fork()\00"
@.str.1 = [39 x i8] c"PID %ld: child started, parent is %ld\0A\00"
@.str.2 = [32 x i8] c"PID %ld: started child PID %ld\0A\00"
@.str.3 = [7 x i8] c"wait()\00"
@.str.4 = [17 x i8] c"exited: $? = %d\0A\00"
@.str.5 = [14 x i8] c"signal: %d%s\0A\00"
@.str.6 = [17 x i8] c" with core file.\00"
@.str.7 = [1 x i8] zeroinitializer
@.str.8 = [9 x i8] c"stopped\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 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %66
14: ; preds = %2
%15 = load i32, i32* %6
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %14
%18 = call i64 (...) @getpid()
%19 = call i64 (...) @getppid()
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i64 %18, i64 %19)
%21 = call i32 (...) @exec_ps_cmd()
br label %64
22: ; preds = %14
%23 = call i64 (...) @getpid()
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i64 %23, i64 %25)
%27 = call i32 @wait(i32* %8)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %22
%31 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %66
32: ; preds = %22
%33 = load i32, i32* %7
%34 = load i32, i32* %6
%35 = icmp ne i32 %33, %34
br i1 %35, label %36, label %37
36: ; preds = %32
store i32 1, i32* %3
br label %66
37: ; preds = %32
%38 = load i32, i32* %8
%39 = call i64 @WIFEXITED(i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %37
%42 = load i32, i32* %8
%43 = call i32 @WEXITSTATUS(i32 %42)
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %43)
br label %61
45: ; preds = %37
%46 = load i32, i32* %8
%47 = call i64 @WIFSIGNALED(i32 %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %58
49: ; preds = %45
%50 = load i32, i32* %8
%51 = call i32 @WTERMSIG(i32 %50)
%52 = load i32, i32* %8
%53 = call i64 @WCOREDUMP(i32 %52)
%54 = icmp ne i64 %53, 0
%55 = zext i1 %54 to i64
%56 = select i1 %54, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.7, i64 0, i64 0)
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %51, i8* %56)
br label %60
58: ; preds = %45
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0))
br label %60
60: ; preds = %58, %49
br label %61
61: ; preds = %60, %41
br label %62
62: ; preds = %61
br label %63
63: ; preds = %62
br label %64
64: ; preds = %63, %17
br label %65
65: ; preds = %64
store i32 0, i32* %3
br label %66
66: ; preds = %65, %36, %30, %12
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
@.str = [7 x i8] c"fork()\00"
@.str.1 = [39 x i8] c"PID %ld: child started, parent is %ld\0A\00"
@.str.2 = [32 x i8] c"PID %ld: started child PID %ld\0A\00"
@.str.3 = [7 x i8] c"wait()\00"
@.str.4 = [17 x i8] c"exited: $? = %d\0A\00"
@.str.5 = [14 x i8] c"signal: %d%s\0A\00"
@.str.6 = [17 x i8] c" with core file.\00"
@.str.7 = [1 x i8] zeroinitializer
@str = [8 x i8] c"stopped\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 %13 [
i32 -1, label %6
i32 0, label %8
]
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %44
8: ; preds = %2
%9 = tail call i64 (...) @getpid()
%10 = tail call i64 (...) @getppid()
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i64 %9, i64 %10)
%12 = tail call i32 (...) @exec_ps_cmd()
br label %44
13: ; preds = %2
%14 = tail call i64 (...) @getpid()
%15 = sext i32 %5 to i64
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i64 %14, i64 %15)
%17 = call i32 @wait(i32* %3)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %44
21: ; preds = %13
%22 = icmp eq i32 %17, %5
br i1 %22, label %23, label %44
23: ; preds = %21
%24 = load i32, i32* %3
%25 = call i64 @WIFEXITED(i32 %24)
%26 = icmp eq i64 %25, 0
%27 = load i32, i32* %3
br i1 %26, label %31, label %28
28: ; preds = %23
%29 = call i32 @WEXITSTATUS(i32 %27)
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %29)
br label %44
31: ; preds = %23
%32 = call i64 @WIFSIGNALED(i32 %27)
%33 = icmp eq i64 %32, 0
br i1 %33, label %42, label %34
34: ; preds = %31
%35 = load i32, i32* %3
%36 = call i32 @WTERMSIG(i32 %35)
%37 = load i32, i32* %3
%38 = call i64 @WCOREDUMP(i32 %37)
%39 = icmp eq i64 %38, 0
%40 = select i1 %39, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0)
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %36, i8* %40)
br label %44
42: ; preds = %31
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
br label %44
44: ; preds = %28, %42, %34, %8, %21, %19, %6
%45 = phi i32 [ 1, %6 ], [ 1, %19 ], [ 1, %21 ], [ 0, %8 ], [ 0, %34 ], [ 0, %42 ], [ 0, %28 ]
ret i32 %45
}