#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__ WIFCONTINUED(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
scalar_t__ WIFSTOPPED(int);
int WSTOPSIG(int);
int WTERMSIG(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int waitpid(int, int *, int /*<<< orphan*/);
int main(void) {
  int status, ret;
  pid_t pid;
  ret = fork();
  if (ret == -1) {
    perror("fork");
    exit(1);
  } else if (ret == 0) {
    sleep(1);
    return 1;
  } else {
    pid = waitpid(ret, &status, 0);
    if (pid == -1)
      perror("wait");
    printf("pid=%d\n", pid);
    if (WIFEXITED(status))
      printf("Normal termination with exit status=%d\n", WEXITSTATUS(status));
    if (WIFSIGNALED(status))
      printf("Killed by signal=%d%s\n", WTERMSIG(status),
             WCOREDUMP(status) ? " (dumped core)" : "");
    if (WIFSTOPPED(status))
      printf("Stopped by signal=%d\n", WSTOPSIG(status));
    if (WIFCONTINUED(status))
      printf("Continued\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1219 <main+0x39>
lea    0xdf6(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
cmpl   $0x0,-0xc(%rbp)
jne    1239 <main+0x59>
mov    $0x1,%edi
call   10e0 <sleep@plt>
movl   $0x1,-0x4(%rbp)
jmp    1345 <main+0x165>
mov    -0xc(%rbp),%edi
lea    -0x8(%rbp),%rsi
xor    %edx,%edx
call   10a0 <waitpid@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1260 <main+0x80>
lea    0xdaa(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x10(%rbp),%esi
lea    0xda0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   10c0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     129b <main+0xbb>
mov    -0x8(%rbp),%edi
call   1090 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xd7e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1050 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     12ea <main+0x10a>
mov    -0x8(%rbp),%edi
call   1080 <WTERMSIG@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <WCOREDUMP@plt>
mov    -0x14(%rbp),%esi
mov    %rax,%rcx
lea    0xd44(%rip),%rdx
lea    0xd7d(%rip),%rax
cmp    $0x0,%rcx
cmovne %rax,%rdx
lea    0xd57(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     1314 <main+0x134>
mov    -0x8(%rbp),%edi
call   10f0 <WSTOPSIG@plt>
mov    %eax,%esi
lea    0xd53(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <WIFCONTINUED@plt>
cmp    $0x0,%rax
je     1334 <main+0x154>
lea    0xd49(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1339 <main+0x159>
jmp    133e <main+0x15e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1110 <fork@plt>
test   %eax,%eax
je     12e9 <main+0xf9>
cmp    $0xffffffff,%eax
je     1311 <main+0x121>
lea    0xc(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   10b0 <waitpid@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1300 <main+0x110>
lea    0xddd(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%edi
call   10d0 <WIFEXITED@plt>
test   %rax,%rax
je     125d <main+0x6d>
mov    0xc(%rsp),%edi
call   10a0 <WEXITSTATUS@plt>
lea    0xdbe(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%edi
call   1060 <WIFSIGNALED@plt>
test   %rax,%rax
je     12a4 <main+0xb4>
mov    0xc(%rsp),%edi
call   1090 <WTERMSIG@plt>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
call   1050 <WCOREDUMP@plt>
test   %rax,%rax
lea    0xd88(%rip),%rax
lea    0xdc1(%rip),%rdx
cmove  %rax,%rdx
lea    0xd9f(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%edi
call   1080 <WIFSTOPPED@plt>
test   %rax,%rax
je     12cb <main+0xdb>
mov    0xc(%rsp),%edi
call   1100 <WSTOPSIG@plt>
lea    0xd9e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%edi
call   1070 <WIFCONTINUED@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     12f8 <main+0x108>
lea    0xd94(%rip),%rdi
call   1030 <puts@plt>
jmp    12f8 <main+0x108>
mov    $0x1,%ebx
mov    $0x1,%edi
call   10f0 <sleep@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xcfe(%rip),%rdi
call   10c0 <perror@plt>
jmp    1226 <main+0x36>
lea    0xce8(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@.str.1 = [5 x i8] c"wait\00"
@.str.2 = [8 x i8] c"pid=%d\0A\00"
@.str.3 = [40 x i8] c"Normal termination with exit status=%d\0A\00"
@.str.4 = [23 x i8] c"Killed by signal=%d%s\0A\00"
@.str.5 = [15 x i8] c" (dumped core)\00"
@.str.6 = [1 x i8] zeroinitializer
@.str.7 = [22 x i8] c"Stopped by signal=%d\0A\00"
@.str.8 = [11 x i8] c"Continued\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 (...) @fork()
store i32 %5, i32* %3
%6 = load i32, i32* %3
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = load i32, i32* %3
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @sleep(i32 1)
store i32 1, i32* %1
br label %63
16: ; preds = %11
%17 = load i32, i32* %3
%18 = call i32 @waitpid(i32 %17, i32* %2, i32 0)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %16
%24 = load i32, i32* %4
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %24)
%26 = load i32, i32* %2
%27 = call i64 @WIFEXITED(i32 %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %23
%30 = load i32, i32* %2
%31 = call i32 @WEXITSTATUS(i32 %30)
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %31)
br label %33
33: ; preds = %29, %23
%34 = load i32, i32* %2
%35 = call i64 @WIFSIGNALED(i32 %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %46
37: ; preds = %33
%38 = load i32, i32* %2
%39 = call i32 @WTERMSIG(i32 %38)
%40 = load i32, i32* %2
%41 = call i64 @WCOREDUMP(i32 %40)
%42 = icmp ne i64 %41, 0
%43 = zext i1 %42 to i64
%44 = select i1 %42, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0)
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %39, i8* %44)
br label %46
46: ; preds = %37, %33
%47 = load i32, i32* %2
%48 = call i64 @WIFSTOPPED(i32 %47)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %46
%51 = load i32, i32* %2
%52 = call i32 @WSTOPSIG(i32 %51)
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0), i32 %52)
br label %54
54: ; preds = %50, %46
%55 = load i32, i32* %2
%56 = call i64 @WIFCONTINUED(i32 %55)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %54
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0))
br label %60
60: ; preds = %58, %54
br label %61
61: ; preds = %60
br label %62
62: ; preds = %61
store i32 0, i32* %1
br label %63
63: ; preds = %62, %14
%64 = load i32, i32* %1
ret i32 %64
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@.str.1 = [5 x i8] c"wait\00"
@.str.2 = [8 x i8] c"pid=%d\0A\00"
@.str.3 = [40 x i8] c"Normal termination with exit status=%d\0A\00"
@.str.4 = [23 x i8] c"Killed by signal=%d%s\0A\00"
@.str.5 = [15 x i8] c" (dumped core)\00"
@.str.6 = [1 x i8] zeroinitializer
@.str.7 = [22 x i8] c"Stopped by signal=%d\0A\00"
@str = [10 x i8] c"Continued\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (...) @fork()
switch i32 %3, label %9 [
i32 -1, label %4
i32 0, label %7
]
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %0
%8 = tail call i32 @sleep(i32 1)
br label %49
9: ; preds = %0
%10 = call i32 @waitpid(i32 %3, i32* %1, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %9
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%16 = load i32, i32* %1
%17 = call i64 @WIFEXITED(i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %14
%20 = load i32, i32* %1
%21 = call i32 @WEXITSTATUS(i32 %20)
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %19, %14
%24 = load i32, i32* %1
%25 = call i64 @WIFSIGNALED(i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %35, label %27
27: ; preds = %23
%28 = load i32, i32* %1
%29 = call i32 @WTERMSIG(i32 %28)
%30 = load i32, i32* %1
%31 = call i64 @WCOREDUMP(i32 %30)
%32 = icmp eq i64 %31, 0
%33 = select i1 %32, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0)
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %29, i8* %33)
br label %35
35: ; preds = %27, %23
%36 = load i32, i32* %1
%37 = call i64 @WIFSTOPPED(i32 %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %43, label %39
39: ; preds = %35
%40 = load i32, i32* %1
%41 = call i32 @WSTOPSIG(i32 %40)
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0), i32 %41)
br label %43
43: ; preds = %39, %35
%44 = load i32, i32* %1
%45 = call i64 @WIFCONTINUED(i32 %44)
%46 = icmp eq i64 %45, 0
br i1 %46, label %49, label %47
47: ; preds = %43
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
br label %49
49: ; preds = %43, %47, %7
%50 = phi i32 [ 1, %7 ], [ 0, %47 ], [ 0, %43 ]
ret i32 %50
}