#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*/ SIGKILL;
int errno;
int fork();
int getpid();
int kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int main(int argc, char *argv[]) {
  pid_t pid = fork();
  if (pid == -1) {
    perror("fork");
    return errno;
  }
  if (pid == 0) {
    printf("child process %d\n", getpid());
    sleep(10);
  } else {
    printf("parent process\n");
    sleep(2);
    int ret = kill(pid, SIGKILL);
    if (ret == -1) {
      perror("kill");
      return errno;
    }
    printf("kill %d\n", pid);
    printf("%d\n", ret);
  }
  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   1080 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11a7 <main+0x47>
lea    0xe6f(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1253 <main+0xf3>
cmpl   $0x0,-0x14(%rbp)
jne    11d7 <main+0x77>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xa,%edi
call   1070 <sleep@plt>
jmp    124c <main+0xec>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x2,%edi
call   1070 <sleep@plt>
mov    -0x14(%rbp),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
call   1050 <kill@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    122a <main+0xca>
lea    0xe13(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1253 <main+0xf3>
mov    -0x14(%rbp),%esi
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%esi
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
xor    %r14d,%r14d
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     11db <main+0x6b>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1204 <main+0x94>
lea    0xe95(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   1080 <sleep@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1060 <kill@plt>
cmp    $0xffffffff,%eax
je     120d <main+0x9d>
mov    %eax,%ebx
lea    0xe5f(%rip),%rdi
xor    %r14d,%r14d
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe51(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11fc <main+0x8c>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe1c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1080 <sleep@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdf5(%rip),%rdi
jmp    1214 <main+0xa4>
lea    0xe03(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%r14d
jmp    11fc <main+0x8c>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@errno = global i32 0
@.str.1 = [18 x i8] c"child process %d\0A\00"
@.str.2 = [16 x i8] c"parent process\0A\00"
@SIGKILL = global i32 0
@.str.3 = [5 x i8] c"kill\00"
@.str.4 = [9 x i8] c"kill %d\0A\00"
@.str.5 = [4 x i8] c"%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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 (...) @fork()
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @errno
store i32 %13, i32* %3
br label %38
14: ; preds = %2
%15 = load i32, i32* %6
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = call i32 (...) @getpid()
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = call i32 @sleep(i32 10)
br label %37
21: ; preds = %14
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @sleep(i32 2)
%24 = load i32, i32* %6
%25 = load i32, i32* @SIGKILL
%26 = call i32 @kill(i32 %24, i32 %25)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %21
%30 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%31 = load i32, i32* @errno
store i32 %31, i32* %3
br label %38
32: ; preds = %21
%33 = load i32, i32* %6
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %33)
%35 = load i32, i32* %7
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %32, %17
store i32 0, i32* %3
br label %38
38: ; preds = %37, %29, %11
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@errno = global i32 0
@.str.1 = [18 x i8] c"child process %d\0A\00"
@SIGKILL = global i32 0
@.str.3 = [5 x i8] c"kill\00"
@.str.4 = [9 x i8] c"kill %d\0A\00"
@.str.5 = [4 x i8] c"%d\0A\00"
@str = [15 x i8] c"parent process\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @fork()
switch i32 %3, label %11 [
i32 -1, label %4
i32 0, label %7
]
4: ; preds = %2
%5 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @errno
br label %23
7: ; preds = %2
%8 = tail call i32 (...) @getpid()
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = tail call i32 @sleep(i32 10)
br label %23
11: ; preds = %2
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%13 = tail call i32 @sleep(i32 2)
%14 = load i32, i32* @SIGKILL
%15 = tail call i32 @kill(i32 %3, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%19 = load i32, i32* @errno
br label %23
20: ; preds = %11
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %3)
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %15)
br label %23
23: ; preds = %7, %20, %17, %4
%24 = phi i32 [ %6, %4 ], [ %19, %17 ], [ 0, %20 ], [ 0, %7 ]
ret i32 %24
}