int killall(char *cmd, int brutal) {
  int ret = 0;
  int pid = 0;
  int our_pid = getpid();
  hu_log(3, "hu_uti", __func__, "killall cmd: %s  brutal: %d  our_pid: %d", cmd,
         brutal, our_pid);
  int idx = 0;
  int num_kill_attempts = 0;
  int max_kill_attempts = 16;
  for (idx = 0; idx < max_kill_attempts; idx++) {
    pid = pid_get(cmd, pid + 1);
    if (pid == our_pid) {
      hu_log(3, "hu_uti", __func__, "pid == our_pid");
    } else if (pid > 0) {
      ret = pid_kill(pid, brutal, cmd);
      num_kill_attempts++;
    } else {
      break;
    }
  }
  hu_log(3, "hu_uti", __func__, "killall num_kill_attempts: %d",
         num_kill_attempts);
  return (num_kill_attempts);
}


x86 assembly (clang, -O0):
<killall>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%r8
mov    -0xc(%rbp),%r9d
mov    -0x18(%rbp),%eax
mov    $0x3,%edi
lea    0xe82(%rip),%rsi
lea    0xe82(%rip),%rdx
lea    0xe83(%rip),%rcx
mov    %eax,(%rsp)
mov    $0x0,%al
call   1040 <hu_log@plt>
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x10,-0x24(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    124d <killall+0x10d>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    $0x1,%esi
mov    $0x0,%al
call   1060 <pid_get@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jne    1204 <killall+0xc4>
mov    $0x3,%edi
lea    0xe16(%rip),%rsi
lea    0xe16(%rip),%rdx
lea    0xe40(%rip),%rcx
mov    $0x0,%al
call   1040 <hu_log@plt>
jmp    123a <killall+0xfa>
cmpl   $0x0,-0x14(%rbp)
jle    1230 <killall+0xf0>
mov    -0x14(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x8(%rbp),%rdx
mov    $0x0,%al
call   1050 <pid_kill@plt>
mov    %eax,-0x10(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1235 <killall+0xf5>
jmp    124d <killall+0x10d>
jmp    123a <killall+0xfa>
jmp    123f <killall+0xff>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11b2 <killall+0x72>
mov    -0x20(%rbp),%r8d
mov    $0x3,%edi
lea    0xda3(%rip),%rsi
lea    0xda3(%rip),%rdx
lea    0xddc(%rip),%rcx
mov    $0x0,%al
call   1040 <hu_log@plt>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<killall>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%r15d
mov    %eax,(%rsp)
lea    0xe9b(%rip),%rsi
lea    0xe9b(%rip),%rdx
lea    0xe9c(%rip),%rcx
mov    $0x3,%edi
mov    %rbx,%r8
mov    %r14d,%r9d
xor    %eax,%eax
call   1040 <hu_log@plt>
mov    $0x10,%r12d
xor    %ebp,%ebp
xor    %r13d,%r13d
jmp    11c7 <killall+0x87>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0x3,%edi
lea    0xe54(%rip),%rsi
lea    0xe54(%rip),%rdx
lea    0xe7e(%rip),%rcx
xor    %eax,%eax
call   1040 <hu_log@plt>
add    $0xffffffff,%r12d
je     11fe <killall+0xbe>
add    $0x1,%r13d
mov    %rbx,%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1060 <pid_get@plt>
mov    %eax,%r13d
cmp    %r15d,%eax
je     11a0 <killall+0x60>
test   %r13d,%r13d
jle    11fe <killall+0xbe>
mov    %r13d,%edi
mov    %r14d,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <pid_kill@plt>
add    $0x1,%ebp
add    $0xffffffff,%r12d
jne    11c7 <killall+0x87>
lea    0xdfb(%rip),%rsi
lea    0xdfb(%rip),%rdx
lea    0xe34(%rip),%rcx
mov    $0x3,%edi
mov    %ebp,%r8d
xor    %eax,%eax
call   1040 <hu_log@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"hu_uti\00"
@__func__.killall = [8 x i8] c"killall\00"
@.str.1 = [41 x i8] c"killall cmd: %s brutal: %d our_pid: %d\00"
@.str.2 = [15 x i8] c"pid == our_pid\00"
@.str.3 = [30 x i8] c"killall num_kill_attempts: %d\00"
define i32 @killall(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
%11 = call i32 (...) @getpid()
store i32 %11, i32* %7
%12 = load i8*, i8** %3
%13 = load i32, i32* %4
%14 = load i32, i32* %7
%15 = call i32 (i32, i8*, i8*, i8*, i8*, i32, i32, ...) bitcast (i32 (...)* @hu_log to i32 (i32, i8*, i8*, i8*, i8*, i32, i32, ...)*)(i32 3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @__func__.killall, i64 0, i64 0), i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %12, i32 %13, i32 %14)
store i32 0, i32* %8
store i32 0, i32* %9
store i32 16, i32* %10
store i32 0, i32* %8
br label %16
16: ; preds = %43, %2
%17 = load i32, i32* %8
%18 = load i32, i32* %10
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %46
20: ; preds = %16
%21 = load i8*, i8** %3
%22 = load i32, i32* %6
%23 = add nsw i32 %22, 1
%24 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @pid_get to i32 (i8*, i32, ...)*)(i8* %21, i32 %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = load i32, i32* %7
%27 = icmp eq i32 %25, %26
br i1 %27, label %28, label %30
28: ; preds = %20
%29 = call i32 (i32, i8*, i8*, i8*, ...) bitcast (i32 (...)* @hu_log to i32 (i32, i8*, i8*, i8*, ...)*)(i32 3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @__func__.killall, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %42
30: ; preds = %20
%31 = load i32, i32* %6
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %40
33: ; preds = %30
%34 = load i32, i32* %6
%35 = load i32, i32* %4
%36 = load i8*, i8** %3
%37 = call i32 (i32, i32, i8*, ...) bitcast (i32 (...)* @pid_kill to i32 (i32, i32, i8*, ...)*)(i32 %34, i32 %35, i8* %36)
store i32 %37, i32* %5
%38 = load i32, i32* %9
%39 = add nsw i32 %38, 1
store i32 %39, i32* %9
br label %41
40: ; preds = %30
br label %46
41: ; preds = %33
br label %42
42: ; preds = %41, %28
br label %43
43: ; preds = %42
%44 = load i32, i32* %8
%45 = add nsw i32 %44, 1
store i32 %45, i32* %8
br label %16
46: ; preds = %40, %16
%47 = load i32, i32* %9
%48 = call i32 (i32, i8*, i8*, i8*, i32, ...) bitcast (i32 (...)* @hu_log to i32 (i32, i8*, i8*, i8*, i32, ...)*)(i32 3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @__func__.killall, i64 0, i64 0), i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %47)
%49 = load i32, i32* %9
ret i32 %49
}

LLVM IR (-O2):
@.str = [7 x i8] c"hu_uti\00"
@__func__.killall = [8 x i8] c"killall\00"
@.str.1 = [41 x i8] c"killall cmd: %s brutal: %d our_pid: %d\00"
@.str.2 = [15 x i8] c"pid == our_pid\00"
@.str.3 = [30 x i8] c"killall num_kill_attempts: %d\00"
define i32 @killall(i8* %0, i32 %1) {
%3 = tail call i32 (...) @getpid()
%4 = tail call i32 (i32, i8*, i8*, i8*, i8*, i32, i32, ...) bitcast (i32 (...)* @hu_log to i32 (i32, i8*, i8*, i8*, i8*, i32, i32, ...)*)(i32 3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @__func__.killall, i64 0, i64 0), i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %1, i32 %3)
br label %5
5: ; preds = %2, %19
%6 = phi i32 [ 0, %2 ], [ %20, %19 ]
%7 = phi i32 [ 0, %2 ], [ %21, %19 ]
%8 = phi i32 [ 0, %2 ], [ %10, %19 ]
%9 = add nsw i32 %8, 1
%10 = tail call i32 (i8*, i32, ...) bitcast (i32 (...)* @pid_get to i32 (i8*, i32, ...)*)(i8* %0, i32 %9)
%11 = icmp eq i32 %10, %3
br i1 %11, label %12, label %14
12: ; preds = %5
%13 = tail call i32 (i32, i8*, i8*, i8*, ...) bitcast (i32 (...)* @hu_log to i32 (i32, i8*, i8*, i8*, ...)*)(i32 3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @__func__.killall, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %19
14: ; preds = %5
%15 = icmp sgt i32 %10, 0
br i1 %15, label %16, label %23
16: ; preds = %14
%17 = tail call i32 (i32, i32, i8*, ...) bitcast (i32 (...)* @pid_kill to i32 (i32, i32, i8*, ...)*)(i32 %10, i32 %1, i8* %0)
%18 = add nsw i32 %6, 1
br label %19
19: ; preds = %12, %16
%20 = phi i32 [ %6, %12 ], [ %18, %16 ]
%21 = add nuw nsw i32 %7, 1
%22 = icmp eq i32 %21, 16
br i1 %22, label %23, label %5
23: ; preds = %14, %19
%24 = phi i32 [ %6, %14 ], [ %20, %19 ]
%25 = tail call i32 (i32, i8*, i8*, i8*, i32, ...) bitcast (i32 (...)* @hu_log to i32 (i32, i8*, i8*, i8*, i32, ...)*)(i32 3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @__func__.killall, i64 0, i64 0), i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %24)
ret i32 %24
}