#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);
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*/ printf(char *, ...);
int wait(int *);
int main(int argc, char *argv[]) {
  int pid = fork();
  if (pid < 0)
    exit(1);
  else if (pid == 0) {
    exit(12);
  } else {
    int p, stat;
    while ((p = wait(&stat)) > 0) {
      if (WIFEXITED(stat)) {
        printf("exited, status=%d\n", WEXITSTATUS(stat));
      } else if (WIFSIGNALED(stat)) {
        printf("killed by signal %d\n", WTERMSIG(stat));
      } else if (WIFSTOPPED(stat)) {
        printf("stopped by signal %d\n", WSTOPSIG(stat));
      } else if (WIFCONTINUED(stat)) {
        printf("continued\n");
      }
    }
    printf("wait return: %d\n", p);
  }
  return 0;
}


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   10d0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e4 <main+0x34>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    11f8 <main+0x48>
mov    $0xc,%edi
call   10a0 <exit@plt>
jmp    11fd <main+0x4d>
lea    -0x1c(%rbp),%rdi
call   10c0 <wait@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    12d3 <main+0x123>
mov    -0x1c(%rbp),%edi
call   1090 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1241 <main+0x91>
mov    -0x1c(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12ce <main+0x11e>
mov    -0x1c(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1270 <main+0xc0>
mov    -0x1c(%rbp),%edi
call   1070 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12c9 <main+0x119>
mov    -0x1c(%rbp),%edi
call   1060 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     129f <main+0xef>
mov    -0x1c(%rbp),%edi
call   10b0 <WSTOPSIG@plt>
mov    %eax,%esi
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12c4 <main+0x114>
mov    -0x1c(%rbp),%edi
call   1050 <WIFCONTINUED@plt>
cmp    $0x0,%rax
je     12bf <main+0x10f>
lea    0xd86(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12c4 <main+0x114>
jmp    12c9 <main+0x119>
jmp    12ce <main+0x11e>
jmp    11fd <main+0x4d>
mov    -0x18(%rbp),%esi
lea    0xd6c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12e9 <main+0x139>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
js     12bb <main+0xfb>
je     12c5 <main+0x105>
lea    0xc(%rsp),%rdi
call   10d0 <wait@plt>
test   %eax,%eax
jle    129b <main+0xdb>
lea    0xe54(%rip),%r14
lea    0xe26(%rip),%r15
lea    0xe0a(%rip),%r12
lea    0xc(%rsp),%rbx
lea    0xdeb(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    0xc(%rsp),%edi
call   10a0 <WIFEXITED@plt>
mov    0xc(%rsp),%edi
test   %rax,%rax
je     1240 <main+0x80>
call   1090 <WEXITSTATUS@plt>
mov    %r13,%rdi
jmp    1270 <main+0xb0>
nopl   0x0(%rax)
call   1050 <WIFSIGNALED@plt>
mov    0xc(%rsp),%edi
test   %rax,%rax
je     1258 <main+0x98>
call   1080 <WTERMSIG@plt>
mov    %r12,%rdi
jmp    1270 <main+0xb0>
call   1070 <WIFSTOPPED@plt>
mov    0xc(%rsp),%edi
test   %rax,%rax
je     1287 <main+0xc7>
call   10c0 <WSTOPSIG@plt>
mov    %r15,%rdi
xchg   %ax,%ax
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
call   10d0 <wait@plt>
test   %eax,%eax
jg     1220 <main+0x60>
jmp    129b <main+0xdb>
call   1060 <WIFCONTINUED@plt>
test   %rax,%rax
je     1279 <main+0xb9>
mov    %r14,%rdi
call   1030 <puts@plt>
jmp    1279 <main+0xb9>
lea    0xd9c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    $0xc,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"exited, status=%d\0A\00"
@.str.1 = [21 x i8] c"killed by signal %d\0A\00"
@.str.2 = [22 x i8] c"stopped by signal %d\0A\00"
@.str.3 = [11 x i8] c"continued\0A\00"
@.str.4 = [17 x i8] c"wait return: %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
%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 slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i32, i32* %6
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @exit(i32 12)
unreachable
19: ; preds = %14
br label %20
20: ; preds = %56, %19
%21 = call i32 @wait(i32* %8)
store i32 %21, i32* %7
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %57
23: ; preds = %20
%24 = load i32, i32* %8
%25 = call i64 @WIFEXITED(i32 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = load i32, i32* %8
%29 = call i32 @WEXITSTATUS(i32 %28)
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %29)
br label %56
31: ; preds = %23
%32 = load i32, i32* %8
%33 = call i64 @WIFSIGNALED(i32 %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %31
%36 = load i32, i32* %8
%37 = call i32 @WTERMSIG(i32 %36)
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %37)
br label %55
39: ; preds = %31
%40 = load i32, i32* %8
%41 = call i64 @WIFSTOPPED(i32 %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %39
%44 = load i32, i32* %8
%45 = call i32 @WSTOPSIG(i32 %44)
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %45)
br label %54
47: ; preds = %39
%48 = load i32, i32* %8
%49 = call i64 @WIFCONTINUED(i32 %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %53
53: ; preds = %51, %47
br label %54
54: ; preds = %53, %43
br label %55
55: ; preds = %54, %35
br label %56
56: ; preds = %55, %27
br label %20
57: ; preds = %20
%58 = load i32, i32* %7
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %58)
br label %60
60: ; preds = %57
br label %61
61: ; preds = %60
ret i32 0
}

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