#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 */

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ LOG_NOTICE;
int SIGKILL;
int SIGTERM;
scalar_t__ is_running(int);
int /*<<< orphan*/ kill(int, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *, ...);
void stop_cmd(char *cmd, int cmdpid) {
  int s, i;
  syslog(LOG_NOTICE, "stopping %s", cmd);
  for (s = SIGTERM; s >= SIGKILL; s -= (SIGTERM - SIGKILL)) {
    if (!kill(cmdpid, s)) {
      return;
    }
    for (i = 0; i < 30; i++) {
      if (!is_running(cmdpid)) {
        break;
      }
      sleep(1);
    }
    if (!is_running(cmdpid)) {
      return;
    }
  }
  if (is_running(cmdpid)) {
    syslog(LOG_ERR, "%s (%d) did not die!", cmd, cmdpid);
  }
  return;
}


x86 assembly (clang, -O0):
<stop_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe9d(%rip),%rsi
mov    $0x0,%al
call   1050 <syslog@plt>
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    0x2e58(%rip),%rcx
cmp    (%rcx),%eax
jl     121c <stop_cmd+0xdc>
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1040 <kill@plt>
cmp    $0x0,%eax
jne    11a1 <stop_cmd+0x61>
jmp    1251 <stop_cmd+0x111>
movl   $0x0,-0x14(%rbp)
cmpl   $0x1e,-0x14(%rbp)
jge    11e1 <stop_cmd+0xa1>
mov    -0xc(%rbp),%edi
call   1030 <is_running@plt>
cmp    $0x0,%rax
jne    11c9 <stop_cmd+0x89>
jmp    11e1 <stop_cmd+0xa1>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11a8 <stop_cmd+0x68>
mov    -0xc(%rbp),%edi
call   1030 <is_running@plt>
cmp    $0x0,%rax
jne    11f8 <stop_cmd+0xb8>
jmp    1251 <stop_cmd+0x111>
jmp    11fd <stop_cmd+0xbd>
mov    0x2de4(%rip),%rax
mov    (%rax),%ecx
mov    0x2dcb(%rip),%rax
sub    (%rax),%ecx
mov    -0x10(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
jmp    1176 <stop_cmd+0x36>
mov    -0xc(%rbp),%edi
call   1030 <is_running@plt>
cmp    $0x0,%rax
je     124c <stop_cmd+0x10c>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0xc(%rbp),%ecx
lea    0xdc7(%rip),%rsi
mov    $0x0,%al
call   1050 <syslog@plt>
jmp    1251 <stop_cmd+0x111>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stop_cmd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r13d
mov    %rdi,%r14
mov    0x2e78(%rip),%rax
mov    (%rax),%edi
lea    0xe9f(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <syslog@plt>
mov    0x2e76(%rip),%r15
mov    (%r15),%ebp
mov    0x2e5c(%rip),%r12
cmp    (%r12),%ebp
jl     11e6 <stop_cmd+0xa6>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r13d,%edi
mov    %ebp,%esi
call   1040 <kill@plt>
test   %eax,%eax
je     121e <stop_cmd+0xde>
mov    $0x1e,%ebx
nopw   0x0(%rax,%rax,1)
mov    %r13d,%edi
call   1030 <is_running@plt>
test   %rax,%rax
je     11cc <stop_cmd+0x8c>
mov    $0x1,%edi
call   1060 <sleep@plt>
add    $0xffffffff,%ebx
jne    11b0 <stop_cmd+0x70>
mov    %r13d,%edi
call   1030 <is_running@plt>
test   %rax,%rax
je     121e <stop_cmd+0xde>
mov    (%r12),%eax
sub    (%r15),%ebp
add    %eax,%ebp
cmp    %eax,%ebp
jge    1190 <stop_cmd+0x50>
mov    %r13d,%edi
call   1030 <is_running@plt>
test   %rax,%rax
je     121e <stop_cmd+0xde>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xe09(%rip),%rsi
mov    %r14,%rdx
mov    %r13d,%ecx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <syslog@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@LOG_NOTICE = global i32 0
@.str = [12 x i8] c"stopping %s\00"
@SIGTERM = global i32 0
@SIGKILL = global i32 0
@LOG_ERR = global i32 0
@.str.1 = [21 x i8] c"%s (%d) did not die!\00"
define void @stop_cmd(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i32, i32* @LOG_NOTICE
%8 = load i8*, i8** %3
%9 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %7, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = load i32, i32* @SIGTERM
store i32 %10, i32* %5
br label %11
11: ; preds = %41, %2
%12 = load i32, i32* %5
%13 = load i32, i32* @SIGKILL
%14 = icmp sge i32 %12, %13
br i1 %14, label %15, label %47
15: ; preds = %11
%16 = load i32, i32* %4
%17 = load i32, i32* %5
%18 = call i32 @kill(i32 %16, i32 %17)
%19 = icmp ne i32 %18, 0
br i1 %19, label %21, label %20
20: ; preds = %15
br label %57
21: ; preds = %15
store i32 0, i32* %6
br label %22
22: ; preds = %32, %21
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, 30
br i1 %24, label %25, label %35
25: ; preds = %22
%26 = load i32, i32* %4
%27 = call i64 @is_running(i32 %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %30, label %29
29: ; preds = %25
br label %35
30: ; preds = %25
%31 = call i32 @sleep(i32 1)
br label %32
32: ; preds = %30
%33 = load i32, i32* %6
%34 = add nsw i32 %33, 1
store i32 %34, i32* %6
br label %22
35: ; preds = %29, %22
%36 = load i32, i32* %4
%37 = call i64 @is_running(i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %40, label %39
39: ; preds = %35
br label %57
40: ; preds = %35
br label %41
41: ; preds = %40
%42 = load i32, i32* @SIGTERM
%43 = load i32, i32* @SIGKILL
%44 = sub nsw i32 %42, %43
%45 = load i32, i32* %5
%46 = sub nsw i32 %45, %44
store i32 %46, i32* %5
br label %11
47: ; preds = %11
%48 = load i32, i32* %4
%49 = call i64 @is_running(i32 %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %47
%52 = load i32, i32* @LOG_ERR
%53 = load i8*, i8** %3
%54 = load i32, i32* %4
%55 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %52, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %53, i32 %54)
br label %56
56: ; preds = %51, %47
br label %57
57: ; preds = %56, %39, %20
ret void
}

LLVM IR (-O2):
@LOG_NOTICE = global i32 0
@.str = [12 x i8] c"stopping %s\00"
@SIGTERM = global i32 0
@SIGKILL = global i32 0
@LOG_ERR = global i32 0
@.str.1 = [21 x i8] c"%s (%d) did not die!\00"
define void @stop_cmd(i8* %0, i32 %1) {
%3 = load i32, i32* @LOG_NOTICE
%4 = tail call i32 (i32, i8*, i8*, ...) @syslog(i32 %3, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = load i32, i32* @SIGTERM
%6 = load i32, i32* @SIGKILL
%7 = icmp slt i32 %5, %6
br i1 %7, label %29, label %8
8: ; preds = %2, %23
%9 = phi i32 [ %27, %23 ], [ %5, %2 ]
%10 = tail call i32 @kill(i32 %1, i32 %9)
%11 = icmp eq i32 %10, 0
br i1 %11, label %35, label %12
12: ; preds = %8, %16
%13 = phi i32 [ %18, %16 ], [ 0, %8 ]
%14 = tail call i64 @is_running(i32 %1)
%15 = icmp eq i64 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %12
%17 = tail call i32 @sleep(i32 1)
%18 = add nuw nsw i32 %13, 1
%19 = icmp eq i32 %18, 30
br i1 %19, label %20, label %12
20: ; preds = %12, %16
%21 = tail call i64 @is_running(i32 %1)
%22 = icmp eq i64 %21, 0
br i1 %22, label %35, label %23
23: ; preds = %20
%24 = load i32, i32* @SIGTERM
%25 = load i32, i32* @SIGKILL
%26 = sub i32 %9, %24
%27 = add i32 %26, %25
%28 = icmp slt i32 %27, %25
br i1 %28, label %29, label %8
29: ; preds = %23, %2
%30 = tail call i64 @is_running(i32 %1)
%31 = icmp eq i64 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %29
%33 = load i32, i32* @LOG_ERR
%34 = tail call i32 (i32, i8*, i8*, ...) @syslog(i32 %33, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %1)
br label %35
35: ; preds = %20, %8, %29, %32
ret void
}