#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*/ SIGHUP;
int /*<<< orphan*/ SIGINT;
int /*<<< orphan*/ SIGTERM;
int fork();
char getchar();
int getpid();
int /*<<< orphan*/ install_handler();
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
void main() {
  pid_t pid;
  switch (pid = fork()) {
  case -1: {
    perror("fork");
    break;
  }
  case 0: {
    install_handler();
    sleep(20);
    printf("\tHello this is child process, PID = %d\n", getpid());
    break;
  }
  default: {
    printf("This is parent process, PID = %d\n", getpid());
    printf("Which signal should be sent to child(%d)", pid);
    printf("\n\t1.SIGINT\n\t2.SIGHUP\n\t3.SIGTERM\n");
    char op = getchar();
    switch (op) {
    case '1': {
      kill(pid, SIGINT);
      break;
    }
    case '2': {
      kill(pid, SIGHUP);
      break;
    }
    case '3': {
      kill(pid, SIGTERM);
      break;
    }
    }
    int t = 99999999;
    while (t--) {
      ;
    }
  }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,-0x4(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
sub    $0xffffffff,%eax
je     11b5 <main+0x35>
jmp    11a5 <main+0x25>
mov    -0x10(%rbp),%eax
test   %eax,%eax
je     11c6 <main+0x46>
jmp    11f3 <main+0x73>
lea    0xe44(%rip),%rdi
call   1070 <perror@plt>
jmp    12d3 <main+0x153>
mov    $0x0,%al
call   1080 <install_handler@plt>
mov    $0x14,%edi
call   1090 <sleep@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12d3 <main+0x153>
xor    %eax,%eax
mov    %al,-0x15(%rbp)
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x15(%rbp),%al
lea    0xe24(%rip),%rdi
call   1040 <printf@plt>
mov    -0x15(%rbp),%al
mov    -0x4(%rbp),%esi
lea    0xe34(%rip),%rdi
call   1040 <printf@plt>
mov    -0x15(%rbp),%al
lea    0xe4e(%rip),%rdi
call   1040 <printf@plt>
mov    -0x15(%rbp),%al
call   1050 <getchar@plt>
mov    %al,-0x5(%rbp)
movsbl -0x5(%rbp),%eax
mov    %eax,-0x14(%rbp)
sub    $0x31,%eax
je     1271 <main+0xf1>
jmp    124f <main+0xcf>
mov    -0x14(%rbp),%eax
sub    $0x32,%eax
je     1287 <main+0x107>
jmp    1260 <main+0xe0>
mov    -0x14(%rbp),%eax
sub    $0x33,%eax
je     129d <main+0x11d>
jmp    12ae <main+0x12e>
mov    -0x4(%rbp),%edi
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
jmp    12ae <main+0x12e>
mov    -0x4(%rbp),%edi
mov    0x2d5f(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
jmp    12ae <main+0x12e>
mov    -0x4(%rbp),%edi
mov    0x2d31(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
movl   $0x5f5e0ff,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xc(%rbp)
cmp    $0x0,%eax
je     12ce <main+0x14e>
jmp    12b5 <main+0x135>
jmp    12d3 <main+0x153>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     11fc <main+0x6c>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1241 <main+0xb1>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe78(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe8a(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xea3(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1060 <getchar@plt>
movsbl %al,%eax
cmp    $0x33,%eax
je     1225 <main+0x95>
cmp    $0x32,%eax
je     122e <main+0x9e>
cmp    $0x31,%eax
jne    123f <main+0xaf>
mov    0x2de6(%rip),%rax
jmp    1235 <main+0xa5>
xor    %eax,%eax
call   1090 <install_handler@plt>
mov    $0x14,%edi
call   10a0 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdea(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
pop    %rbx
jmp    1050 <printf@plt>
mov    0x2dac(%rip),%rax
jmp    1235 <main+0xa5>
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
pop    %rbx
jmp    1070 <kill@plt>
pop    %rbx
ret
lea    0xdb8(%rip),%rdi
pop    %rbx
jmp    1080 <perror@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@.str.1 = [40 x i8] c"\09Hello this is child process, PID = %d\0A\00"
@.str.2 = [34 x i8] c"This is parent process, PID = %d\0A\00"
@.str.3 = [41 x i8] c"Which signal should be sent to child(%d)\00"
@.str.4 = [33 x i8] c"\0A\091.SIGINT\0A\092.SIGHUP\0A\093.SIGTERM\0A\00"
@SIGINT = global i32 0
@SIGHUP = global i32 0
@SIGTERM = global i32 0
define void @main() {
%1 = alloca i32
%2 = alloca i8
%3 = alloca i32
%4 = call i32 (...) @fork()
store i32 %4, i32* %1
switch i32 %4, label %12 [
i32 -1, label %5
i32 0, label %7
]
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %40
7: ; preds = %0
%8 = call i32 (...) @install_handler()
%9 = call i32 @sleep(i32 20)
%10 = call i32 (...) @getpid()
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %10)
br label %40
12: ; preds = %0
%13 = call i32 (...) @getpid()
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = load i32, i32* %1
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
%18 = call signext i8 (...) @getchar()
store i8 %18, i8* %2
%19 = load i8, i8* %2
%20 = sext i8 %19 to i32
switch i32 %20, label %33 [
i32 49, label %21
i32 50, label %25
i32 51, label %29
]
21: ; preds = %12
%22 = load i32, i32* %1
%23 = load i32, i32* @SIGINT
%24 = call i32 @kill(i32 %22, i32 %23)
br label %33
25: ; preds = %12
%26 = load i32, i32* %1
%27 = load i32, i32* @SIGHUP
%28 = call i32 @kill(i32 %26, i32 %27)
br label %33
29: ; preds = %12
%30 = load i32, i32* %1
%31 = load i32, i32* @SIGTERM
%32 = call i32 @kill(i32 %30, i32 %31)
br label %33
33: ; preds = %12, %29, %25, %21
store i32 99999999, i32* %3
br label %34
34: ; preds = %38, %33
%35 = load i32, i32* %3
%36 = add nsw i32 %35, -1
store i32 %36, i32* %3
%37 = icmp ne i32 %35, 0
br i1 %37, label %38, label %39
38: ; preds = %34
br label %34
39: ; preds = %34
br label %40
40: ; preds = %39, %7, %5
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@.str.1 = [40 x i8] c"\09Hello this is child process, PID = %d\0A\00"
@.str.2 = [34 x i8] c"This is parent process, PID = %d\0A\00"
@.str.3 = [41 x i8] c"Which signal should be sent to child(%d)\00"
@SIGINT = global i32 0
@SIGHUP = global i32 0
@SIGTERM = global i32 0
@str = [32 x i8] c"\0A\091.SIGINT\0A\092.SIGHUP\0A\093.SIGTERM\00"
define void @main() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %9 [
i32 -1, label %2
i32 0, label %4
]
2: ; preds = %0
%3 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %25
4: ; preds = %0
%5 = tail call i32 (...) @install_handler()
%6 = tail call i32 @sleep(i32 20)
%7 = tail call i32 (...) @getpid()
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %7)
br label %25
9: ; preds = %0
%10 = tail call i32 (...) @getpid()
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i32 %1)
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%14 = tail call signext i8 (...) @getchar()
%15 = sext i8 %14 to i32
switch i32 %15, label %25 [
i32 49, label %16
i32 50, label %19
i32 51, label %22
]
16: ; preds = %9
%17 = load i32, i32* @SIGINT
%18 = tail call i32 @kill(i32 %1, i32 %17)
br label %25
19: ; preds = %9
%20 = load i32, i32* @SIGHUP
%21 = tail call i32 @kill(i32 %1, i32 %20)
br label %25
22: ; preds = %9
%23 = load i32, i32* @SIGTERM
%24 = tail call i32 @kill(i32 %1, i32 %23)
br label %25
25: ; preds = %16, %19, %22, %9, %4, %2
ret void
}