#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 */
scalar_t__ KillPgrp(int, int /*<<< orphan*/);
int /*<<< orphan*/ LogErrno(char *, char const *);
int /*<<< orphan*/ SIGTERM;
int WaitProc(char const *, int *, int, int, int *);
int WaitPgrp(const char *name, pid_t *pid, int do_block, int already_killed,
             int *exit_status) {
  int pid_saved = *pid;
  int result = WaitProc(name, pid, do_block, already_killed, exit_status);
  if (result && !already_killed) {
    if (KillPgrp(pid_saved, SIGTERM) < 0) {
      LogErrno("KillPgrp %s", name);
    }
  }
  return result;
}


x86 assembly (clang, -O0):
<WaitPgrp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0x20(%rbp),%r8
call   1030 <WaitProc@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     11b1 <WaitPgrp+0x81>
cmpl   $0x0,-0x18(%rbp)
jne    11b1 <WaitPgrp+0x81>
mov    -0x24(%rbp),%edi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
call   1050 <KillPgrp@plt>
cmp    $0x0,%rax
jge    11ac <WaitPgrp+0x7c>
mov    -0x8(%rbp),%rsi
lea    0xe59(%rip),%rdi
call   1040 <LogErrno@plt>
jmp    11b1 <WaitPgrp+0x81>
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WaitPgrp>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%ebx
mov    %rdi,%r15
mov    (%rsi),%r14d
call   1030 <WaitProc@plt>
mov    %eax,%ebp
test   %ebx,%ebx
jne    1173 <WaitPgrp+0x43>
test   %ebp,%ebp
je     1173 <WaitPgrp+0x43>
mov    0x2e93(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1050 <KillPgrp@plt>
test   %rax,%rax
jns    1173 <WaitPgrp+0x43>
lea    0xe95(%rip),%rdi
mov    %r15,%rsi
call   1040 <LogErrno@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SIGTERM = global i32 0
@.str = [12 x i8] c"KillPgrp %s\00"
define i32 @WaitPgrp(i8* %0, i32* %1, i32 %2, i32 %3, i32* %4) {
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %6
store i32* %1, i32** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32* %4, i32** %10
%13 = load i32*, i32** %7
%14 = load i32, i32* %13
store i32 %14, i32* %11
%15 = load i8*, i8** %6
%16 = load i32*, i32** %7
%17 = load i32, i32* %8
%18 = load i32, i32* %9
%19 = load i32*, i32** %10
%20 = call i32 @WaitProc(i8* %15, i32* %16, i32 %17, i32 %18, i32* %19)
store i32 %20, i32* %12
%21 = load i32, i32* %12
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %35
23: ; preds = %5
%24 = load i32, i32* %9
%25 = icmp ne i32 %24, 0
br i1 %25, label %35, label %26
26: ; preds = %23
%27 = load i32, i32* %11
%28 = load i32, i32* @SIGTERM
%29 = call i64 @KillPgrp(i32 %27, i32 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = load i8*, i8** %6
%33 = call i32 @LogErrno(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %32)
br label %34
34: ; preds = %31, %26
br label %35
35: ; preds = %34, %23, %5
%36 = load i32, i32* %12
ret i32 %36
}

LLVM IR (-O2):
@SIGTERM = global i32 0
@.str = [12 x i8] c"KillPgrp %s\00"
define i32 @WaitPgrp(i8* %0, i32* %1, i32 %2, i32 %3, i32* %4) {
%6 = load i32, i32* %1
%7 = tail call i32 @WaitProc(i8* %0, i32* %1, i32 %2, i32 %3, i32* %4)
%8 = icmp eq i32 %7, 0
%9 = icmp ne i32 %3, 0
%10 = or i1 %9, %8
br i1 %10, label %17, label %11
11: ; preds = %5
%12 = load i32, i32* @SIGTERM
%13 = tail call i64 @KillPgrp(i32 %6, i32 %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call i32 @LogErrno(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %17
17: ; preds = %11, %15, %5
ret i32 %7
}