#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 scalar_t__ pid_t;

/* Variables and functions */
int EXIT_SUCCESS;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getopt(int, char **, char *);
int /*<<< orphan*/ kill(scalar_t__, int);
char *optarg;
int optind;
int /*<<< orphan*/ print_usage();
int /*<<< orphan*/ stderr;
scalar_t__ strtoul(char *, int /*<<< orphan*/ *, int);
scalar_t__ wait_for_termination(scalar_t__, unsigned int);
int main(int argc, char *argv[]) {
  pid_t pid;
  unsigned waitsecs = 15, wait_endlessly;
  int waitrc, c;
  while (-1 != (c = getopt(argc, argv, "ehs:"))) {
    switch (c) {
    case 'w':
      waitsecs = (unsigned)strtoul(optarg, NULL, 10);
      break;
    case 'e':
      wait_endlessly = 1;
      break;
    case 'h':
      print_usage();
      exit(EXIT_SUCCESS);
    default:
      exit(1);
    }
  }
  if (optind == argc) {
    fprintf(stderr, "ERROR: Need a PID\n");
    exit(1);
  }
  pid = (pid_t)strtoul(argv[optind], NULL, 10);
  kill(pid, 15);
  if (0 == wait_for_termination(pid, waitsecs))
    goto success;
  kill(pid, 2);
  if (0 == wait_for_termination(pid, waitsecs))
    goto success;
  kill(pid, 1);
  if (0 == wait_for_termination(pid, waitsecs))
    goto success;
  kill(pid, 9);
  if (wait_endlessly)
    waitsecs = -1;
  if (0 == wait_for_termination(pid, waitsecs))
    goto success;
  exit(2);
success:
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xf,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe65(%rip),%rdx
call   1060 <getopt@plt>
mov    %eax,%ecx
mov    %ecx,-0x28(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
je     1238 <main+0xc8>
mov    -0x28(%rbp),%eax
mov    %eax,-0x2c(%rbp)
sub    $0x65,%eax
je     1208 <main+0x98>
jmp    11c6 <main+0x56>
mov    -0x2c(%rbp),%eax
sub    $0x68,%eax
je     1214 <main+0xa4>
jmp    11d7 <main+0x67>
mov    -0x2c(%rbp),%eax
sub    $0x77,%eax
jne    1229 <main+0xb9>
jmp    11e8 <main+0x78>
mov    0x2e09(%rip),%rax
mov    (%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1070 <strtoul@plt>
mov    %eax,-0x1c(%rbp)
jmp    1233 <main+0xc3>
movl   $0x1,-0x20(%rbp)
jmp    1233 <main+0xc3>
mov    $0x0,%al
call   1090 <print_usage@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    118d <main+0x1d>
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
cmp    -0x8(%rbp),%eax
jne    1269 <main+0xf9>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
lea    0xdab(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x2d6c(%rip),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,8),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1070 <strtoul@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0xf,%esi
call   1040 <kill@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1050 <wait_for_termination@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    12ba <main+0x14a>
jmp    135c <main+0x1ec>
mov    -0x18(%rbp),%rdi
mov    $0x2,%esi
call   1040 <kill@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1050 <wait_for_termination@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    12e7 <main+0x177>
jmp    135c <main+0x1ec>
mov    -0x18(%rbp),%rdi
mov    $0x1,%esi
call   1040 <kill@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1050 <wait_for_termination@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1314 <main+0x1a4>
jmp    135c <main+0x1ec>
mov    -0x18(%rbp),%rdi
mov    $0x9,%esi
call   1040 <kill@plt>
cmpl   $0x0,-0x20(%rbp)
je     1333 <main+0x1c3>
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1050 <wait_for_termination@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1352 <main+0x1e2>
jmp    135c <main+0x1ec>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    0x2c65(%rip),%rax
mov    (%rax),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
mov    $0xf,%r14d
lea    0xe76(%rip),%r15
mov    0x2e67(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1060 <getopt@plt>
cmp    $0x65,%eax
je     11a0 <main+0x30>
cmp    $0xffffffff,%eax
je     11d5 <main+0x65>
cmp    $0x77,%eax
jne    1282 <main+0x112>
mov    (%r12),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <strtoul@plt>
mov    %rax,%r14
jmp    11a0 <main+0x30>
mov    0x2e04(%rip),%rax
movslq (%rax),%rax
cmp    %ebp,%eax
je     12a6 <main+0x136>
mov    (%rbx,%rax,8),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <strtoul@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0xf,%esi
call   1040 <kill@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1050 <wait_for_termination@plt>
test   %rax,%rax
je     1270 <main+0x100>
mov    %rbx,%rdi
mov    $0x2,%esi
call   1040 <kill@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1050 <wait_for_termination@plt>
test   %rax,%rax
je     1270 <main+0x100>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1040 <kill@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1050 <wait_for_termination@plt>
test   %rax,%rax
je     1270 <main+0x100>
mov    %rbx,%rdi
mov    $0x9,%esi
call   1040 <kill@plt>
mov    %rbx,%rdi
mov    $0xffffffff,%esi
call   1050 <wait_for_termination@plt>
test   %rax,%rax
jne    12c5 <main+0x155>
mov    0x2d51(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
cmp    $0x68,%eax
jne    129c <main+0x12c>
xor    %eax,%eax
call   1090 <print_usage@plt>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
lea    0xd4f(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x2,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"ehs:\00"
@optarg = global i8* null
@EXIT_SUCCESS = global i32 0
@optind = global i32 0
@stderr = global i32 0
@.str.1 = [19 x i8] c"ERROR: Need a PID\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 15, i32* %7
br label %11
11: ; preds = %29, %2
%12 = load i32, i32* %4
%13 = load i8**, i8*** %5
%14 = call i32 @getopt(i32 %12, i8** %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 %14, i32* %10
%15 = icmp ne i32 -1, %14
br i1 %15, label %16, label %30
16: ; preds = %11
%17 = load i32, i32* %10
switch i32 %17, label %27 [
i32 119, label %18
i32 101, label %22
i32 104, label %23
]
18: ; preds = %16
%19 = load i8*, i8** @optarg
%20 = call i64 @strtoul(i8* %19, i32* null, i32 10)
%21 = trunc i64 %20 to i32
store i32 %21, i32* %7
br label %29
22: ; preds = %16
store i32 1, i32* %8
br label %29
23: ; preds = %16
%24 = call i32 (...) @print_usage()
%25 = load i32, i32* @EXIT_SUCCESS
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %16
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %22, %18
br label %11
30: ; preds = %11
%31 = load i32, i32* @optind
%32 = load i32, i32* %4
%33 = icmp eq i32 %31, %32
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %30
%39 = load i8**, i8*** %5
%40 = load i32, i32* @optind
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8*, i8** %39, i64 %41
%43 = load i8*, i8** %42
%44 = call i64 @strtoul(i8* %43, i32* null, i32 10)
store i64 %44, i64* %6
%45 = load i64, i64* %6
%46 = call i32 @kill(i64 %45, i32 15)
%47 = load i64, i64* %6
%48 = load i32, i32* %7
%49 = call i64 @wait_for_termination(i64 %47, i32 %48)
%50 = icmp eq i64 0, %49
br i1 %50, label %51, label %52
51: ; preds = %38
br label %82
52: ; preds = %38
%53 = load i64, i64* %6
%54 = call i32 @kill(i64 %53, i32 2)
%55 = load i64, i64* %6
%56 = load i32, i32* %7
%57 = call i64 @wait_for_termination(i64 %55, i32 %56)
%58 = icmp eq i64 0, %57
br i1 %58, label %59, label %60
59: ; preds = %52
br label %82
60: ; preds = %52
%61 = load i64, i64* %6
%62 = call i32 @kill(i64 %61, i32 1)
%63 = load i64, i64* %6
%64 = load i32, i32* %7
%65 = call i64 @wait_for_termination(i64 %63, i32 %64)
%66 = icmp eq i64 0, %65
br i1 %66, label %67, label %68
67: ; preds = %60
br label %82
68: ; preds = %60
%69 = load i64, i64* %6
%70 = call i32 @kill(i64 %69, i32 9)
%71 = load i32, i32* %8
%72 = icmp ne i32 %71, 0
br i1 %72, label %73, label %74
73: ; preds = %68
store i32 -1, i32* %7
br label %74
74: ; preds = %73, %68
%75 = load i64, i64* %6
%76 = load i32, i32* %7
%77 = call i64 @wait_for_termination(i64 %75, i32 %76)
%78 = icmp eq i64 0, %77
br i1 %78, label %79, label %80
79: ; preds = %74
br label %82
80: ; preds = %74
%81 = call i32 @exit(i32 2)
unreachable
82: ; preds = %79, %67, %59, %51
%83 = load i32, i32* @EXIT_SUCCESS
ret i32 %83
}

LLVM IR (-O2):
@.str = [5 x i8] c"ehs:\00"
@optarg = global i8* null
@EXIT_SUCCESS = global i32 0
@optind = global i32 0
@stderr = global i32 0
@.str.1 = [19 x i8] c"ERROR: Need a PID\0A\00"
define i32 @main(i32 %0, i8** %1) {
br label %3
3: ; preds = %7, %2
%4 = phi i32 [ %10, %7 ], [ 15, %2 ]
br label %5
5: ; preds = %3, %5
%6 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
switch i32 %6, label %15 [
i32 -1, label %17
i32 119, label %7
i32 101, label %5
i32 104, label %11
]
7: ; preds = %5
%8 = load i8*, i8** @optarg
%9 = tail call i64 @strtoul(i8* nocapture %8, i32* null, i32 10)
%10 = trunc i64 %9 to i32
br label %3
11: ; preds = %5
%12 = tail call i32 (...) @print_usage()
%13 = load i32, i32* @EXIT_SUCCESS
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %5
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %5
%18 = load i32, i32* @optind
%19 = icmp eq i32 %18, %0
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %17
%25 = sext i32 %18 to i64
%26 = getelementptr inbounds i8*, i8** %1, i64 %25
%27 = load i8*, i8** %26
%28 = tail call i64 @strtoul(i8* nocapture %27, i32* null, i32 10)
%29 = tail call i32 @kill(i64 %28, i32 15)
%30 = tail call i64 @wait_for_termination(i64 %28, i32 %4)
%31 = icmp eq i64 %30, 0
br i1 %31, label %46, label %32
32: ; preds = %24
%33 = tail call i32 @kill(i64 %28, i32 2)
%34 = tail call i64 @wait_for_termination(i64 %28, i32 %4)
%35 = icmp eq i64 %34, 0
br i1 %35, label %46, label %36
36: ; preds = %32
%37 = tail call i32 @kill(i64 %28, i32 1)
%38 = tail call i64 @wait_for_termination(i64 %28, i32 %4)
%39 = icmp eq i64 %38, 0
br i1 %39, label %46, label %40
40: ; preds = %36
%41 = tail call i32 @kill(i64 %28, i32 9)
%42 = tail call i64 @wait_for_termination(i64 %28, i32 -1)
%43 = icmp eq i64 %42, 0
br i1 %43, label %46, label %44
44: ; preds = %40
%45 = tail call i32 @exit(i32 2)
unreachable
46: ; preds = %40, %36, %32, %24
%47 = load i32, i32* @EXIT_SUCCESS
ret i32 %47
}