#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 /*<<< orphan*/ SIGKILL;
int /*<<< orphan*/ SIGTSTP;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
scalar_t__ kill(scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ raise(int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
scalar_t__ wait(int *);
int main(int argc, char *argv[]) {
  int status;
  pid_t p, waitp;
  p = fork();
  if (p < 0) {
    perror("fork");
    exit(1);
  }
  if (p == 0) {
    raise(SIGTSTP);
    printf("Child: FAIL.\n");
    exit(1);
  }
  printf("Parent: sleeping for a while...\n");
  sleep(2);
  if (kill(p, SIGKILL) < 0) {
    perror("kill");
    exit(1);
  }
  waitp = wait(&status);
  assert(p == waitp);
  if (WIFEXITED(status)) {
    printf("Parent: FAIL. Child exited, status = %d\n", WEXITSTATUS(status));
    exit(1);
  }
  if (!WIFSIGNALED(status)) {
    printf("Parent: FAIL. Child not signaled?\n");
    exit(1);
  }
  printf("Parent: SUCCESS. Child killed, sig = %d\n", WTERMSIG(status));
  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   10f0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    1212 <main+0x42>
lea    0xdfd(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jne    1243 <main+0x73>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   1030 <raise@plt>
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xdc9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x2,%edi
call   10d0 <sleep@plt>
mov    -0x20(%rbp),%rdi
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
call   1070 <kill@plt>
cmp    $0x0,%rax
jge    128d <main+0xbd>
lea    0xdb6(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    -0x14(%rbp),%rdi
call   10e0 <wait@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x14(%rbp),%edi
call   10b0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12e3 <main+0x113>
mov    -0x14(%rbp),%edi
call   1090 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1050 <WIFSIGNALED@plt>
cmp    $0x0,%eax
jne    130c <main+0x13c>
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1080 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xd68(%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
xor    %eax,%eax
call   1100 <fork@plt>
test   %rax,%rax
js     127e <main+0x9e>
je     1287 <main+0xa7>
mov    %rax,%rbx
lea    0xe57(%rip),%rdi
call   1040 <puts@plt>
mov    $0x2,%edi
call   10e0 <sleep@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <kill@plt>
test   %rax,%rax
js     129e <main+0xbe>
lea    0xc(%rsp),%rdi
call   10f0 <wait@plt>
xor    %edi,%edi
cmp    %rax,%rbx
sete   %dil
call   1070 <assert@plt>
mov    0xc(%rsp),%edi
call   10c0 <WIFEXITED@plt>
mov    0xc(%rsp),%edi
test   %rax,%rax
jne    12b4 <main+0xd4>
call   1060 <WIFSIGNALED@plt>
test   %eax,%eax
je     12d3 <main+0xf3>
mov    0xc(%rsp),%edi
call   1090 <WTERMSIG@plt>
lea    0xdc6(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xd7b(%rip),%rdi
jmp    12a5 <main+0xc5>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
call   1030 <raise@plt>
lea    0xe02(%rip),%rdi
jmp    12da <main+0xfa>
lea    0xd60(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
call   10a0 <WEXITSTATUS@plt>
lea    0xd4a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xda2(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@SIGTSTP = global i32 0
@.str.1 = [14 x i8] c"Child: FAIL.\0A\00"
@.str.2 = [33 x i8] c"Parent: sleeping for a while...\0A\00"
@SIGKILL = global i32 0
@.str.3 = [5 x i8] c"kill\00"
@.str.4 = [41 x i8] c"Parent: FAIL. Child exited, status = %d\0A\00"
@.str.5 = [35 x i8] c"Parent: FAIL. Child not signaled?\0A\00"
@.str.6 = [41 x i8] c"Parent: SUCCESS. Child killed, sig = %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i64 (...) @fork()
store i64 %9, i64* %7
%10 = load i64, i64* %7
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load i64, i64* %7
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %15
%19 = load i32, i32* @SIGTSTP
%20 = call i32 @raise(i32 %19)
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %15
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @sleep(i32 2)
%26 = load i64, i64* %7
%27 = load i32, i32* @SIGKILL
%28 = call i64 @kill(i64 %26, i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %23
%31 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %23
%34 = call i64 @wait(i32* %6)
store i64 %34, i64* %8
%35 = load i64, i64* %7
%36 = load i64, i64* %8
%37 = icmp eq i64 %35, %36
%38 = zext i1 %37 to i32
%39 = call i32 @assert(i32 %38)
%40 = load i32, i32* %6
%41 = call i64 @WIFEXITED(i32 %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %33
%44 = load i32, i32* %6
%45 = call i32 @WEXITSTATUS(i32 %44)
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i32 %45)
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %33
%49 = load i32, i32* %6
%50 = call i32 @WIFSIGNALED(i32 %49)
%51 = icmp ne i32 %50, 0
br i1 %51, label %55, label %52
52: ; preds = %48
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0))
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %48
%56 = load i32, i32* %6
%57 = call i32 @WTERMSIG(i32 %56)
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.6, i64 0, i64 0), i32 %57)
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@SIGTSTP = global i32 0
@SIGKILL = global i32 0
@.str.3 = [5 x i8] c"kill\00"
@.str.4 = [41 x i8] c"Parent: FAIL. Child exited, status = %d\0A\00"
@.str.6 = [41 x i8] c"Parent: SUCCESS. Child killed, sig = %d\0A\00"
@str = [32 x i8] c"Parent: sleeping for a while...\00"
@str.7 = [34 x i8] c"Parent: FAIL. Child not signaled?\00"
@str.8 = [13 x i8] c"Child: FAIL.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i64 (...) @fork()
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = icmp eq i64 %5, 0
br i1 %11, label %12, label %17
12: ; preds = %10
%13 = load i32, i32* @SIGTSTP
%14 = tail call i32 @raise(i32 %13)
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.8, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %10
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%19 = tail call i32 @sleep(i32 2)
%20 = load i32, i32* @SIGKILL
%21 = tail call i64 @kill(i64 %5, i32 %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %17
%27 = call i64 @wait(i32* %3)
%28 = icmp eq i64 %5, %27
%29 = zext i1 %28 to i32
%30 = call i32 @assert(i32 %29)
%31 = load i32, i32* %3
%32 = call i64 @WIFEXITED(i32 %31)
%33 = icmp eq i64 %32, 0
%34 = load i32, i32* %3
br i1 %33, label %39, label %35
35: ; preds = %26
%36 = call i32 @WEXITSTATUS(i32 %34)
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i32 %36)
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %26
%40 = call i32 @WIFSIGNALED(i32 %34)
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.7, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %39
%46 = load i32, i32* %3
%47 = call i32 @WTERMSIG(i32 %46)
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.6, i64 0, i64 0), i32 %47)
ret i32 0
}