#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;
scalar_t__ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
int waitpid(int, int *, int /*<<< orphan*/);
int killprog(pid_t pid) {
  if (pid <= 1)
    return 1;
  if (kill(pid, SIGTERM)) {
    perror("kill");
    return 0;
  }
  int status = 0;
  pid = waitpid(pid, &status, 0);
  if (pid < 0)
    perror("waitpid");
  if (status != 0)
    printf("=> Process [%d] exited with %d\n", pid, status);
  return 1;
}


x86 assembly (clang, -O0):
<killprog>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x1,-0x8(%rbp)
jg     1161 <killprog+0x21>
movl   $0x1,-0x4(%rbp)
jmp    11e5 <killprog+0xa5>
mov    -0x8(%rbp),%edi
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
call   1040 <kill@plt>
cmp    $0x0,%rax
je     1194 <killprog+0x54>
lea    0xe7d(%rip),%rdi
call   1060 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11e5 <killprog+0xa5>
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11c2 <killprog+0x82>
lea    0xe48(%rip),%rdi
call   1060 <perror@plt>
cmpl   $0x0,-0xc(%rbp)
je     11de <killprog+0x9e>
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xe34(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<killprog>:
push   %rbp
push   %rbx
push   %rax
mov    $0x1,%ebx
cmp    $0x2,%edi
jl     1194 <killprog+0x54>
mov    %edi,%ebp
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
call   1040 <kill@plt>
test   %rax,%rax
jne    119d <killprog+0x5d>
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11ad <killprog+0x6d>
mov    0x4(%rsp),%edx
test   %edx,%edx
je     1194 <killprog+0x54>
lea    0xe80(%rip),%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe5c(%rip),%rdi
call   1060 <perror@plt>
xor    %ebx,%ebx
jmp    1194 <killprog+0x54>
lea    0xe51(%rip),%rdi
call   1060 <perror@plt>
jmp    117e <killprog+0x3e>

LLVM IR (-O0):
@SIGTERM = global i32 0
@.str = [5 x i8] c"kill\00"
@.str.1 = [8 x i8] c"waitpid\00"
@.str.2 = [32 x i8] c"=> Process [%d] exited with %d\0A\00"
define i32 @killprog(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = icmp sle i32 %5, 1
br i1 %6, label %7, label %8
7: ; preds = %1
store i32 1, i32* %2
br label %30
8: ; preds = %1
%9 = load i32, i32* %3
%10 = load i32, i32* @SIGTERM
%11 = call i64 @kill(i32 %9, i32 %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %30
15: ; preds = %8
store i32 0, i32* %4
%16 = load i32, i32* %3
%17 = call i32 @waitpid(i32 %16, i32* %4, i32 0)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %15
%23 = load i32, i32* %4
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i32, i32* %3
%27 = load i32, i32* %4
%28 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %26, i32 %27)
br label %29
29: ; preds = %25, %22
store i32 1, i32* %2
br label %30
30: ; preds = %29, %13, %7
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@SIGTERM = global i32 0
@.str = [5 x i8] c"kill\00"
@.str.1 = [8 x i8] c"waitpid\00"
@.str.2 = [32 x i8] c"=> Process [%d] exited with %d\0A\00"
define i32 @killprog(i32 %0) {
%2 = alloca i32
%3 = icmp slt i32 %0, 2
br i1 %3, label %22, label %4
4: ; preds = %1
%5 = load i32, i32* @SIGTERM
%6 = tail call i64 @kill(i32 %0, i32 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %4
%9 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %22
10: ; preds = %4
%11 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%12 = call i32 @waitpid(i32 %0, i32* %2, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %10
%17 = load i32, i32* %2
%18 = icmp eq i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %12, i32 %17)
br label %21
21: ; preds = %19, %16
br label %22
22: ; preds = %1, %21, %8
%23 = phi i32 [ 0, %8 ], [ 1, %21 ], [ 1, %1 ]
ret i32 %23
}