#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*/ WIFEXITED(int);
int /*<<< orphan*/ WIFSIGNALED(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ kill(int, int);
int /*<<< orphan*/ printf(char *, ...);
int waitpid(int, int *, int /*<<< orphan*/);
void init(pid_t pid, int signumber) {
  pid_t childpid;
  int status;
  printf("pid: %d sig: %d\n", pid, signumber);
  switch (childpid = fork()) {
  case -1:
    printf("Error when forking\n");
    break;
  default:
    if (waitpid(childpid, &status, 0) == -1) {
      printf("Can't perform waitpid\n");
      exit(1);
    }
    if (WIFEXITED(status)) {
      printf("Child process returned normally\n");
    } else if (WIFSIGNALED(status)) {
      printf("Child process terminated by signal\n");
    }
    break;
  case 0:
    printf("Child\n");
    kill(pid, signumber);
    printf("exiting\n");
    break;
  }
}


x86 assembly (clang, -O0):
<init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    0xe75(%rip),%rdi
xor    %eax,%eax
mov    %al,-0x15(%rbp)
call   1030 <printf@plt>
mov    -0x15(%rbp),%al
call   1090 <fork@plt>
mov    %eax,-0xc(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
sub    $0xffffffff,%eax
je     11c3 <init+0x53>
jmp    11b3 <init+0x43>
mov    -0x14(%rbp),%eax
test   %eax,%eax
je     1252 <init+0xe2>
jmp    11d6 <init+0x66>
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1279 <init+0x109>
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    1205 <init+0x95>
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%eax
je     1229 <init+0xb9>
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    124d <init+0xdd>
mov    -0x10(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%eax
je     1248 <init+0xd8>
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    124d <init+0xdd>
jmp    1279 <init+0x109>
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1050 <kill@plt>
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
lea    0xe72(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11b2 <init+0x32>
cmp    $0xffffffff,%eax
jne    11d9 <init+0x59>
lea    0xe6f(%rip),%rdi
jmp    11ce <init+0x4e>
lea    0xe58(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    %ebx,%esi
call   1060 <kill@plt>
lea    0xe49(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <puts@plt>
lea    0x4(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    $0xffffffff,%eax
je     1222 <init+0xa2>
mov    0x4(%rsp),%edi
call   1080 <WIFEXITED@plt>
test   %eax,%eax
je     1202 <init+0x82>
lea    0xe55(%rip),%rdi
jmp    1216 <init+0x96>
mov    0x4(%rsp),%edi
call   1050 <WIFSIGNALED@plt>
test   %eax,%eax
je     121b <init+0x9b>
lea    0xe1c(%rip),%rdi
call   1030 <puts@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe4c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"pid: %d sig: %d\0A\00"
@.str.1 = [20 x i8] c"Error when forking\0A\00"
@.str.2 = [23 x i8] c"Can't perform waitpid\0A\00"
@.str.3 = [33 x i8] c"Child process returned normally\0A\00"
@.str.4 = [36 x i8] c"Child process terminated by signal\0A\00"
@.str.5 = [7 x i8] c"Child\0A\00"
@.str.6 = [9 x i8] c"exiting\0A\00"
define void @init(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* %3
%8 = load i32, i32* %4
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %8)
%10 = call i32 (...) @fork()
store i32 %10, i32* %5
switch i32 %10, label %13 [
i32 -1, label %11
i32 0, label %34
]
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %40
13: ; preds = %2
%14 = load i32, i32* %5
%15 = call i32 @waitpid(i32 %14, i32* %6, i32 0)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %13
%21 = load i32, i32* %6
%22 = call i32 @WIFEXITED(i32 %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %33
26: ; preds = %20
%27 = load i32, i32* %6
%28 = call i32 @WIFSIGNALED(i32 %27)
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
br label %32
32: ; preds = %30, %26
br label %33
33: ; preds = %32, %24
br label %40
34: ; preds = %2
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%36 = load i32, i32* %3
%37 = load i32, i32* %4
%38 = call i32 @kill(i32 %36, i32 %37)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %40
40: ; preds = %34, %33, %11
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"pid: %d sig: %d\0A\00"
@str = [6 x i8] c"Child\00"
@str.7 = [8 x i8] c"exiting\00"
@str.8 = [19 x i8] c"Error when forking\00"
@str.9 = [35 x i8] c"Child process terminated by signal\00"
@str.10 = [32 x i8] c"Child process returned normally\00"
@str.11 = [22 x i8] c"Can't perform waitpid\00"
define void @init(i32 %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %1)
%6 = tail call i32 (...) @fork()
switch i32 %6, label %9 [
i32 -1, label %7
i32 0, label %27
]
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.8, i64 0, i64 0))
br label %31
9: ; preds = %2
%10 = call i32 @waitpid(i32 %6, i32* %3, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.11, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %9
%16 = load i32, i32* %3
%17 = call i32 @WIFEXITED(i32 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %15
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.10, i64 0, i64 0))
br label %31
21: ; preds = %15
%22 = load i32, i32* %3
%23 = call i32 @WIFSIGNALED(i32 %22)
%24 = icmp eq i32 %23, 0
br i1 %24, label %31, label %25
25: ; preds = %21
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.9, i64 0, i64 0))
br label %31
27: ; preds = %2
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
%29 = tail call i32 @kill(i32 %0, i32 %1)
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str.7, i64 0, i64 0))
br label %31
31: ; preds = %19, %25, %21, %27, %7
ret void
}