#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 /*<<< orphan*/ uint32_t;
struct timespec {
  int /*<<< orphan*/ member_1;
  int /*<<< orphan*/ member_0;
};
typedef int /*<<< orphan*/ sigset_t;
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
scalar_t__ EAGAIN;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ errmsgf(char *);
scalar_t__ errno;
int kill(int /*<<< orphan*/, int);
int sigaddset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int sigemptyset(int /*<<< orphan*/ *);
int sigtimedwait(int /*<<< orphan*/ *, int /*<<< orphan*/ *, struct timespec *);
int kill_and_wait_sigchild(pid_t child_pid, int signal,
                           uint32_t timeout_seconds) {
  sigset_t sigs;
  struct timespec wait_timeout = {timeout_seconds, 0};
  if (kill(child_pid, signal) == -1) {
    errmsgf("kill() failed");
    return -1;
  }
  if (sigemptyset(&sigs) == -1) {
    errmsgf("sigemptyset() failed");
    return -1;
  }
  if (sigaddset(&sigs, SIGCHLD) == -1) {
    errmsgf("sigaddset() failed");
    return -1;
  }
  if (sigtimedwait(&sigs, NULL, &wait_timeout) == -1) {
    if (errno != EAGAIN) {
      errmsgf("sigtimedwait() failed");
    }
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<kill_and_wait_sigchild>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1050 <kill@plt>
cmp    $0xffffffff,%eax
jne    119a <kill_and_wait_sigchild+0x4a>
lea    0xe77(%rip),%rdi
call   1060 <errmsgf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124a <kill_and_wait_sigchild+0xfa>
lea    -0x14(%rbp),%rdi
call   1040 <sigemptyset@plt>
cmp    $0xffffffff,%eax
jne    11c4 <kill_and_wait_sigchild+0x74>
lea    0xe5b(%rip),%rdi
call   1060 <errmsgf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124a <kill_and_wait_sigchild+0xfa>
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
lea    -0x14(%rbp),%rdi
call   1070 <sigaddset@plt>
cmp    $0xffffffff,%eax
jne    11f7 <kill_and_wait_sigchild+0xa7>
lea    0xe3d(%rip),%rdi
call   1060 <errmsgf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124a <kill_and_wait_sigchild+0xfa>
lea    -0x14(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x20(%rbp),%rdx
call   1030 <sigtimedwait@plt>
cmp    $0xffffffff,%eax
jne    1243 <kill_and_wait_sigchild+0xf3>
mov    0x2dc0(%rip),%rax
mov    (%rax),%rax
mov    0x2da6(%rip),%rcx
cmp    (%rcx),%rax
je     1237 <kill_and_wait_sigchild+0xe7>
lea    0xe04(%rip),%rdi
call   1060 <errmsgf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124a <kill_and_wait_sigchild+0xfa>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kill_and_wait_sigchild>:
push   %rbx
sub    $0x10,%rsp
mov    %edx,0x8(%rsp)
movl   $0x0,0xc(%rsp)
call   1050 <kill@plt>
cmp    $0xffffffff,%eax
je     11d3 <kill_and_wait_sigchild+0x83>
lea    0x4(%rsp),%rdi
call   1040 <sigemptyset@plt>
cmp    $0xffffffff,%eax
je     11dc <kill_and_wait_sigchild+0x8c>
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdi
call   1070 <sigaddset@plt>
cmp    $0xffffffff,%eax
je     11e5 <kill_and_wait_sigchild+0x95>
xor    %ebx,%ebx
lea    0x4(%rsp),%rdi
lea    0x8(%rsp),%rdx
xor    %esi,%esi
call   1030 <sigtimedwait@plt>
cmp    $0xffffffff,%eax
jne    11f6 <kill_and_wait_sigchild+0xa6>
mov    0x2e27(%rip),%rax
mov    (%rax),%rax
mov    0x2e0d(%rip),%rcx
mov    $0xffffffff,%ebx
cmp    (%rcx),%rax
je     11f6 <kill_and_wait_sigchild+0xa6>
lea    0xe6a(%rip),%rdi
call   1060 <errmsgf@plt>
jmp    11f6 <kill_and_wait_sigchild+0xa6>
lea    0xe26(%rip),%rdi
jmp    11ec <kill_and_wait_sigchild+0x9c>
lea    0xe2b(%rip),%rdi
jmp    11ec <kill_and_wait_sigchild+0x9c>
lea    0xe37(%rip),%rdi
call   1060 <errmsgf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@.str = [14 x i8] c"kill() failed\00"
@.str.1 = [21 x i8] c"sigemptyset() failed\00"
@SIGCHLD = global i32 0
@.str.2 = [19 x i8] c"sigaddset() failed\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.3 = [22 x i8] c"sigtimedwait() failed\00"
define i32 @kill_and_wait_sigchild(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.timespec
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 0
%11 = load i32, i32* %7
store i32 %11, i32* %10
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 1
store i32 0, i32* %12
%13 = load i32, i32* %5
%14 = load i32, i32* %6
%15 = call i32 @kill(i32 %13, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = call i32 @errmsgf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %41
19: ; preds = %3
%20 = call i32 @sigemptyset(i32* %8)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @errmsgf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %41
24: ; preds = %19
%25 = load i32, i32* @SIGCHLD
%26 = call i32 @sigaddset(i32* %8, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @errmsgf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %41
30: ; preds = %24
%31 = call i32 @sigtimedwait(i32* %8, i32* null, %struct.timespec* %9)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %40
33: ; preds = %30
%34 = load i64, i64* @errno
%35 = load i64, i64* @EAGAIN
%36 = icmp ne i64 %34, %35
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 @errmsgf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %39
39: ; preds = %37, %33
store i32 -1, i32* %4
br label %41
40: ; preds = %30
store i32 0, i32* %4
br label %41
41: ; preds = %40, %39, %28, %22, %17
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@.str = [14 x i8] c"kill() failed\00"
@.str.1 = [21 x i8] c"sigemptyset() failed\00"
@SIGCHLD = global i32 0
@.str.2 = [19 x i8] c"sigaddset() failed\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.3 = [22 x i8] c"sigtimedwait() failed\00"
define i32 @kill_and_wait_sigchild(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.timespec
%6 = bitcast i32* %4 to i8*
%7 = bitcast %struct.timespec* %5 to i8*
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 0
store i32 %2, i32* %8
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 1
store i32 0, i32* %9
%10 = tail call i32 @kill(i32 %0, i32 %1)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = tail call i32 @errmsgf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %34
14: ; preds = %3
%15 = call i32 @sigemptyset(i32* %4)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @errmsgf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %34
19: ; preds = %14
%20 = load i32, i32* @SIGCHLD
%21 = call i32 @sigaddset(i32* %4, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @errmsgf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %34
25: ; preds = %19
%26 = call i32 @sigtimedwait(i32* %4, i32* null, %struct.timespec* %5)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %34
28: ; preds = %25
%29 = load i64, i64* @errno
%30 = load i64, i64* @EAGAIN
%31 = icmp eq i64 %29, %30
br i1 %31, label %34, label %32
32: ; preds = %28
%33 = call i32 @errmsgf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %34
34: ; preds = %25, %28, %32, %23, %17, %12
%35 = phi i32 [ -1, %12 ], [ -1, %17 ], [ -1, %23 ], [ -1, %32 ], [ -1, %28 ], [ 0, %25 ]
ret i32 %35
}