#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*/ SIGTERM;
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int getpid();
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
void mp_example4(void) {
  pid_t pid;
  pid_t tid;
  int ret;
  tid = fork();
  if (tid == 0) {
    printf("i'm child %d\n", getpid());
    int i = 0;
    while (1) {
      printf("running child %d\n", i);
      i++;
      sleep(1);
    }
    exit(0);
  } else if (tid == -1) {
    perror("fork fail ");
  } else {
    pid = getpid();
    printf("i'm father %d\n", pid);
    sleep(10);
    ret = waitpid(tid, NULL, WNOHANG);
    if (!ret)
      kill(tid, SIGTERM);
    printf("all done\n");
  }
}


x86 assembly (clang, -O0):
<mp_example4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    11d6 <mp_example4+0x66>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%esi
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    %eax,-0x10(%rbp)
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    11aa <mp_example4+0x3a>
cmpl   $0xffffffff,-0x8(%rbp)
jne    11f1 <mp_example4+0x81>
lea    0xe39(%rip),%rdi
call   1070 <perror@plt>
jmp    1257 <mp_example4+0xe7>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%esi
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xa,%edi
call   1080 <sleep@plt>
mov    -0x8(%rbp),%edi
mov    0x2dc0(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1060 <waitpid@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1249 <mp_example4+0xd9>
mov    -0x8(%rbp),%edi
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
call   1050 <kill@plt>
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    125c <mp_example4+0xec>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mp_example4>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11f2 <mp_example4+0x72>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     123b <mp_example4+0xbb>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe84(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1090 <sleep@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1070 <waitpid@plt>
test   %eax,%eax
jne    11e0 <mp_example4+0x60>
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1060 <kill@plt>
lea    0xe53(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <puts@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe00(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdfe(%rip),%rbx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%ebp
mov    $0x1,%edi
call   1090 <sleep@plt>
jmp    1220 <mp_example4+0xa0>
lea    0xdde(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1080 <perror@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"i'm child %d\0A\00"
@.str.1 = [18 x i8] c"running child %d\0A\00"
@.str.2 = [11 x i8] c"fork fail \00"
@.str.3 = [15 x i8] c"i'm father %d\0A\00"
@WNOHANG = global i32 0
@SIGTERM = global i32 0
@.str.4 = [10 x i8] c"all done\0A\00"
define void @mp_example4() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = call i32 (...) @fork()
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %17
8: ; preds = %0
%9 = call i32 (...) @getpid()
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 0, i32* %4
br label %11
11: ; preds = %8, %11
%12 = load i32, i32* %4
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = load i32, i32* %4
%15 = add nsw i32 %14, 1
store i32 %15, i32* %4
%16 = call i32 @sleep(i32 1)
br label %11
17: ; preds = %0
%18 = load i32, i32* %2
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %38
22: ; preds = %17
%23 = call i32 (...) @getpid()
store i32 %23, i32* %1
%24 = load i32, i32* %1
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%26 = call i32 @sleep(i32 10)
%27 = load i32, i32* %2
%28 = load i32, i32* @WNOHANG
%29 = call i32 @waitpid(i32 %27, i32* null, i32 %28)
store i32 %29, i32* %3
%30 = load i32, i32* %3
%31 = icmp ne i32 %30, 0
br i1 %31, label %36, label %32
32: ; preds = %22
%33 = load i32, i32* %2
%34 = load i32, i32* @SIGTERM
%35 = call i32 @kill(i32 %33, i32 %34)
br label %36
36: ; preds = %32, %22
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
br label %38
38: ; preds = %36, %20
br label %39
39: ; preds = %38
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"i'm child %d\0A\00"
@.str.1 = [18 x i8] c"running child %d\0A\00"
@.str.2 = [11 x i8] c"fork fail \00"
@.str.3 = [15 x i8] c"i'm father %d\0A\00"
@WNOHANG = global i32 0
@SIGTERM = global i32 0
@str = [9 x i8] c"all done\00"
define void @mp_example4() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %12 [
i32 0, label %2
i32 -1, label %10
]
2: ; preds = %0
%3 = tail call i32 (...) @getpid()
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %3)
br label %5
5: ; preds = %5, %2
%6 = phi i32 [ 0, %2 ], [ %8, %5 ]
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%8 = add nuw nsw i32 %6, 1
%9 = tail call i32 @sleep(i32 1)
br label %5
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %24
12: ; preds = %0
%13 = tail call i32 (...) @getpid()
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%15 = tail call i32 @sleep(i32 10)
%16 = load i32, i32* @WNOHANG
%17 = tail call i32 @waitpid(i32 %1, i32* null, i32 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = load i32, i32* @SIGTERM
%21 = tail call i32 @kill(i32 %1, i32 %20)
br label %22
22: ; preds = %19, %12
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
br label %24
24: ; preds = %10, %22
ret void
}