#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*/ SIGSTOP;
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int getpid();
scalar_t__ kill(scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ raise(int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
scalar_t__ waitpid(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main() {
  pid_t pid;
  int ret;
  if ((pid = fork()) < 0) {
    printf("Fork error\n");
    exit(1);
  }
  if (0 == pid) {
    printf("Child process is %d\n", getpid());
    raise(SIGSTOP);
    exit(0);
  } else {
    sleep(1);
    if ((waitpid(pid, NULL, WNOHANG)) == 0) {
      if (kill(pid, SIGKILL) == 0) {
        printf("Parent kill %d\n", pid);
      }
    }
    waitpid(pid, NULL, 0);
    exit(0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    11bc <main+0x3c>
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    11f4 <main+0x74>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
call   1030 <raise@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x10(%rbp),%rdi
mov    0x2ddf(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <waitpid@plt>
cmp    $0x0,%rax
jne    1251 <main+0xd1>
mov    -0x10(%rbp),%rdi
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
cmp    $0x0,%rax
jne    124c <main+0xcc>
mov    -0x10(%rbp),%rsi
lea    0xddc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1251 <main+0xd1>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <waitpid@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
jns    11b3 <main+0x23>
lea    0xe81(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jne    11e1 <main+0x51>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe3d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
call   1030 <raise@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    %rax,%rbx
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <waitpid@plt>
test   %rax,%rax
jne    122d <main+0x9d>
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <kill@plt>
test   %rax,%rax
jne    122d <main+0x9d>
lea    0xdf2(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <waitpid@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"Fork error\0A\00"
@.str.1 = [21 x i8] c"Child process is %d\0A\00"
@SIGSTOP = global i32 0
@WNOHANG = global i32 0
@SIGKILL = global i32 0
@.str.2 = [16 x i8] c"Parent kill %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
store i32 0, i32* %1
%4 = call i64 (...) @fork()
store i64 %4, i64* %2
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = load i64, i64* %2
%11 = icmp eq i64 0, %10
br i1 %11, label %12, label %18
12: ; preds = %9
%13 = call i32 (...) @getpid()
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = load i32, i32* @SIGSTOP
%16 = call i32 @raise(i32 %15)
%17 = call i32 @exit(i32 0)
unreachable
18: ; preds = %9
%19 = call i32 @sleep(i32 1)
%20 = load i64, i64* %2
%21 = load i32, i32* @WNOHANG
%22 = call i64 @waitpid(i64 %20, i32* null, i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %33
24: ; preds = %18
%25 = load i64, i64* %2
%26 = load i32, i32* @SIGKILL
%27 = call i64 @kill(i64 %25, i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = load i64, i64* %2
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i64 %30)
br label %32
32: ; preds = %29, %24
br label %33
33: ; preds = %32, %18
%34 = load i64, i64* %2
%35 = call i64 @waitpid(i64 %34, i32* null, i32 0)
%36 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.1 = [21 x i8] c"Child process is %d\0A\00"
@SIGSTOP = global i32 0
@WNOHANG = global i32 0
@SIGKILL = global i32 0
@.str.2 = [16 x i8] c"Parent kill %d\0A\00"
@str = [11 x i8] c"Fork error\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = icmp eq i64 %1, 0
br i1 %7, label %8, label %14
8: ; preds = %6
%9 = tail call i32 (...) @getpid()
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = load i32, i32* @SIGSTOP
%12 = tail call i32 @raise(i32 %11)
%13 = tail call i32 @exit(i32 0)
unreachable
14: ; preds = %6
%15 = tail call i32 @sleep(i32 1)
%16 = load i32, i32* @WNOHANG
%17 = tail call i64 @waitpid(i64 %1, i32* null, i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %14
%20 = load i32, i32* @SIGKILL
%21 = tail call i64 @kill(i64 %1, i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i64 %1)
br label %25
25: ; preds = %19, %23, %14
%26 = tail call i64 @waitpid(i64 %1, i32* null, i32 0)
%27 = tail call i32 @exit(i32 0)
unreachable
}