#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 */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, long);
scalar_t__ getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ wait(int *);
int main(int argc, char *argv[]) {
  pid_t proc_id = fork();
  if (proc_id == -1) {
    perror("fork");
    exit(EXIT_FAILURE);
  } else if (proc_id == 0) {
    fprintf(stdout, "CHILD : PID=%ld\n", (long)getpid());
    sleep(30);
    exit(EXIT_SUCCESS);
  } else {
    int status;
    wait(&status);
    if (WIFEXITED(status)) {
      printf("EXITED: %d\n", WEXITSTATUS(status));
    } else if (WIFSIGNALED(status)) {
      printf("SIGNALED: %d\n", WTERMSIG(status));
    }
    exit(EXIT_SUCCESS);
  }
}


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   10e0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1204 <main+0x44>
lea    0xe0f(%rip),%rdi
call   1090 <perror@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    124b <main+0x8b>
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xdd7(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1e,%edi
call   10c0 <sleep@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    -0x18(%rbp),%rdi
call   10d0 <wait@plt>
mov    -0x18(%rbp),%edi
call   10a0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1281 <main+0xc1>
mov    -0x18(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xd9f(%rip),%rdi
call   1040 <printf@plt>
jmp    12ae <main+0xee>
mov    -0x18(%rbp),%edi
call   1050 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     12a9 <main+0xe9>
mov    -0x18(%rbp),%edi
call   1070 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xd7e(%rip),%rdi
call   1040 <printf@plt>
jmp    12ae <main+0xee>
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     11ef <main+0x2f>
cmp    $0xffffffff,%eax
jne    121c <main+0x5c>
lea    0xe24(%rip),%rdi
call   1090 <perror@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xdff(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0x1e,%edi
call   10c0 <sleep@plt>
jmp    1267 <main+0xa7>
lea    0xc(%rsp),%rdi
call   10d0 <wait@plt>
mov    0xc(%rsp),%edi
call   10a0 <WIFEXITED@plt>
mov    0xc(%rsp),%edi
test   %rax,%rax
jne    1254 <main+0x94>
call   1050 <WIFSIGNALED@plt>
test   %rax,%rax
je     1267 <main+0xa7>
mov    0xc(%rsp),%edi
call   1070 <WTERMSIG@plt>
lea    0xdd0(%rip),%rdi
jmp    1260 <main+0xa0>
call   1080 <WEXITSTATUS@plt>
lea    0xdb6(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@stdout = global i32 0
@.str.1 = [17 x i8] c"CHILD : PID=%ld\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [12 x i8] c"EXITED: %d\0A\00"
@.str.3 = [14 x i8] c"SIGNALED: %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 (...) @fork()
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = load i32, i32* %6
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %15
%19 = load i32, i32* @stdout
%20 = call i64 (...) @getpid()
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i64 %20)
%22 = call i32 @sleep(i32 30)
%23 = load i32, i32* @EXIT_SUCCESS
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %15
%26 = call i32 @wait(i32* %7)
%27 = load i32, i32* %7
%28 = call i64 @WIFEXITED(i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %25
%31 = load i32, i32* %7
%32 = call i32 @WEXITSTATUS(i32 %31)
%33 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %32)
br label %43
34: ; preds = %25
%35 = load i32, i32* %7
%36 = call i64 @WIFSIGNALED(i32 %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = load i32, i32* %7
%40 = call i32 @WTERMSIG(i32 %39)
%41 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %38, %34
br label %43
43: ; preds = %42, %30
%44 = load i32, i32* @EXIT_SUCCESS
%45 = call i32 @exit(i32 %44)
unreachable
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@stdout = global i32 0
@.str.1 = [17 x i8] c"CHILD : PID=%ld\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [12 x i8] c"EXITED: %d\0A\00"
@.str.3 = [14 x i8] c"SIGNALED: %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = tail call i32 (...) @fork()
switch i32 %4, label %16 [
i32 -1, label %5
i32 0, label %9
]
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %2
%10 = load i32, i32* @stdout
%11 = tail call i64 (...) @getpid()
%12 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i64 %11)
%13 = tail call i32 @sleep(i32 30)
%14 = load i32, i32* @EXIT_SUCCESS
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %2
%17 = bitcast i32* %3 to i8*
%18 = call i32 @wait(i32* %3)
%19 = load i32, i32* %3
%20 = call i64 @WIFEXITED(i32 %19)
%21 = icmp eq i64 %20, 0
%22 = load i32, i32* %3
br i1 %21, label %25, label %23
23: ; preds = %16
%24 = call i32 @WEXITSTATUS(i32 %22)
br label %31
25: ; preds = %16
%26 = call i64 @WIFSIGNALED(i32 %22)
%27 = icmp eq i64 %26, 0
br i1 %27, label %35, label %28
28: ; preds = %25
%29 = load i32, i32* %3
%30 = call i32 @WTERMSIG(i32 %29)
br label %31
31: ; preds = %23, %28
%32 = phi i32 [ %30, %28 ], [ %24, %23 ]
%33 = phi i8* [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), %28 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), %23 ]
%34 = call i32 @printf(i8* %33, i32 %32)
br label %35
35: ; preds = %31, %25
%36 = load i32, i32* @EXIT_SUCCESS
%37 = call i32 @exit(i32 %36)
unreachable
}