#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 */
int EAGAIN;
int /*<<< orphan*/ SIGCHLD;
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int, int, char *,
                           int);
int getpid();
int getppid();
int kill(int /*<<< orphan*/, int);
int sigaddset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int sigemptyset(int /*<<< orphan*/ *);
int sigtimedwait(int /*<<< orphan*/ *, int /*<<< orphan*/ *, struct timespec *);
int /*<<< orphan*/ stderr;
char *strerror(int);
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) {
    fprintf(stderr,
            "[error:%d] "
            "kill() failed"
            ": pid=%d, ppid=%d, error=%s(%d)\n",
            63, getpid(), getppid(), strerror(errno), errno);
    return -1;
  }
  if (sigemptyset(&sigs) == -1) {
    fprintf(stderr,
            "[error:%d] "
            "sigemptyset() failed"
            ": pid=%d, ppid=%d, error=%s(%d)\n",
            64, getpid(), getppid(), strerror(errno), errno);
    return -1;
  }
  if (sigaddset(&sigs, SIGCHLD) == -1) {
    fprintf(stderr,
            "[error:%d] "
            "sigaddset() failed"
            ": pid=%d, ppid=%d, error=%s(%d)\n",
            65, getpid(), getppid(), strerror(errno), errno);
    return -1;
  }
  if (sigtimedwait(&sigs, NULL, &wait_timeout) == -1) {
    if (errno != EAGAIN) {
      fprintf(stderr,
              "[error:%d] "
              "sigtimedwait() failed"
              ": pid=%d, ppid=%d, error=%s(%d)\n",
              67, getpid(), getppid(), strerror(errno), errno);
    }
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<kill_and_wait_sigchild>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%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   1070 <kill@plt>
cmp    $0xffffffff,%eax
jne    1216 <kill_and_wait_sigchild+0x96>
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    %eax,-0x24(%rbp)
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%ecx
mov    -0x24(%rbp),%r8d
mov    %rax,%r9
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
lea    0xe03(%rip),%rsi
mov    $0x3f,%edx
mov    %eax,(%rsp)
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a8 <kill_and_wait_sigchild+0x228>
lea    -0x14(%rbp),%rdi
call   1060 <sigemptyset@plt>
cmp    $0xffffffff,%eax
jne    128c <kill_and_wait_sigchild+0x10c>
mov    0x2da9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    %eax,-0x30(%rbp)
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    -0x34(%rbp),%ecx
mov    -0x30(%rbp),%r8d
mov    %rax,%r9
mov    0x2d66(%rip),%rax
mov    (%rax),%eax
lea    0xdc6(%rip),%rsi
mov    $0x40,%edx
mov    %eax,(%rsp)
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a8 <kill_and_wait_sigchild+0x228>
mov    0x2d55(%rip),%rax
mov    (%rax),%esi
lea    -0x14(%rbp),%rdi
call   10a0 <sigaddset@plt>
cmp    $0xffffffff,%eax
jne    130b <kill_and_wait_sigchild+0x18b>
mov    0x2d2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x40(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    %eax,-0x3c(%rbp)
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%ecx
mov    -0x3c(%rbp),%r8d
mov    %rax,%r9
mov    0x2ce7(%rip),%rax
mov    (%rax),%eax
lea    0xd87(%rip),%rsi
mov    $0x41,%edx
mov    %eax,(%rsp)
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a8 <kill_and_wait_sigchild+0x228>
lea    -0x14(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x20(%rbp),%rdx
call   1040 <sigtimedwait@plt>
cmp    $0xffffffff,%eax
jne    13a1 <kill_and_wait_sigchild+0x221>
mov    0x2ca4(%rip),%rax
mov    (%rax),%eax
mov    0x2c8b(%rip),%rcx
cmp    (%rcx),%eax
je     1395 <kill_and_wait_sigchild+0x215>
mov    0x2c94(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x4c(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    %eax,-0x48(%rbp)
mov    0x2c6c(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x50(%rbp),%edi
mov    -0x4c(%rbp),%ecx
mov    -0x48(%rbp),%r8d
mov    %rax,%r9
mov    0x2c51(%rip),%rax
mov    (%rax),%eax
lea    0xd2f(%rip),%rsi
mov    $0x43,%edx
mov    %eax,(%rsp)
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a8 <kill_and_wait_sigchild+0x228>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kill_and_wait_sigchild>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edx,0x18(%rsp)
movl   $0x0,0x1c(%rsp)
call   1070 <kill@plt>
cmp    $0xffffffff,%eax
je     125d <kill_and_wait_sigchild+0xdd>
lea    0x14(%rsp),%rdi
call   1060 <sigemptyset@plt>
cmp    $0xffffffff,%eax
je     12a3 <kill_and_wait_sigchild+0x123>
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
lea    0x14(%rsp),%rdi
call   10a0 <sigaddset@plt>
cmp    $0xffffffff,%eax
je     12e6 <kill_and_wait_sigchild+0x166>
xor    %ebx,%ebx
lea    0x14(%rsp),%rdi
lea    0x18(%rsp),%rdx
xor    %esi,%esi
call   1040 <sigtimedwait@plt>
cmp    $0xffffffff,%eax
jne    133a <kill_and_wait_sigchild+0x1ba>
mov    0x2dd8(%rip),%rbp
mov    0x0(%rbp),%eax
mov    0x2dbe(%rip),%rcx
mov    $0xffffffff,%ebx
cmp    (%rcx),%eax
je     133a <kill_and_wait_sigchild+0x1ba>
mov    0x2dc2(%rip),%rax
mov    (%rax),%r14d
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1080 <getppid@plt>
mov    %eax,%r12d
mov    0x0(%rbp),%edi
call   1090 <strerror@plt>
mov    0x0(%rbp),%ecx
mov    %ecx,(%rsp)
lea    0xe75(%rip),%rsi
mov    %r14d,%edi
mov    $0x43,%edx
mov    %r15d,%ecx
mov    %r12d,%r8d
mov    %rax,%r9
call   1050 <fprintf@plt>
jmp    133a <kill_and_wait_sigchild+0x1ba>
mov    0x2d74(%rip),%rax
mov    (%rax),%r14d
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1080 <getppid@plt>
mov    %eax,%ebx
mov    0x2d4f(%rip),%rbp
mov    0x0(%rbp),%edi
call   1090 <strerror@plt>
mov    0x0(%rbp),%ecx
mov    %ecx,(%rsp)
lea    0xd6a(%rip),%rsi
mov    %r14d,%edi
mov    $0x3f,%edx
jmp    1327 <kill_and_wait_sigchild+0x1a7>
mov    0x2d2e(%rip),%rax
mov    (%rax),%r14d
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1080 <getppid@plt>
mov    %eax,%ebx
mov    0x2d09(%rip),%rbp
mov    0x0(%rbp),%edi
call   1090 <strerror@plt>
mov    0x0(%rbp),%ecx
mov    %ecx,(%rsp)
lea    0xd5d(%rip),%rsi
mov    %r14d,%edi
mov    $0x40,%edx
jmp    1327 <kill_and_wait_sigchild+0x1a7>
mov    0x2ceb(%rip),%rax
mov    (%rax),%r14d
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1080 <getppid@plt>
mov    %eax,%ebx
mov    0x2cc6(%rip),%rbp
mov    0x0(%rbp),%edi
call   1090 <strerror@plt>
mov    0x0(%rbp),%ecx
mov    %ecx,(%rsp)
lea    0xd5a(%rip),%rsi
mov    %r14d,%edi
mov    $0x41,%edx
mov    %r15d,%ecx
mov    %ebx,%r8d
mov    %rax,%r9
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@stderr = global i32 0
@.str = [57 x i8] c"[error:%d] kill() failed: pid=%d, ppid=%d, error=%s(%d)\0A\00"
@errno = global i32 0
@.str.1 = [64 x i8] c"[error:%d] sigemptyset() failed: pid=%d, ppid=%d, error=%s(%d)\0A\00"
@SIGCHLD = global i32 0
@.str.2 = [62 x i8] c"[error:%d] sigaddset() failed: pid=%d, ppid=%d, error=%s(%d)\0A\00"
@EAGAIN = global i32 0
@.str.3 = [65 x i8] c"[error:%d] sigtimedwait() failed: pid=%d, ppid=%d, error=%s(%d)\0A\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 %25
17: ; preds = %3
%18 = load i32, i32* @stderr
%19 = call i32 (...) @getpid()
%20 = call i32 (...) @getppid()
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = load i32, i32* @errno
%24 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i32 63, i32 %19, i32 %20, i8* %22, i32 %23)
store i32 -1, i32* %4
br label %65
25: ; preds = %3
%26 = call i32 @sigemptyset(i32* %8)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %36
28: ; preds = %25
%29 = load i32, i32* @stderr
%30 = call i32 (...) @getpid()
%31 = call i32 (...) @getppid()
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = load i32, i32* @errno
%35 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.1, i64 0, i64 0), i32 64, i32 %30, i32 %31, i8* %33, i32 %34)
store i32 -1, i32* %4
br label %65
36: ; preds = %25
%37 = load i32, i32* @SIGCHLD
%38 = call i32 @sigaddset(i32* %8, i32 %37)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %48
40: ; preds = %36
%41 = load i32, i32* @stderr
%42 = call i32 (...) @getpid()
%43 = call i32 (...) @getppid()
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = load i32, i32* @errno
%47 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.2, i64 0, i64 0), i32 65, i32 %42, i32 %43, i8* %45, i32 %46)
store i32 -1, i32* %4
br label %65
48: ; preds = %36
%49 = call i32 @sigtimedwait(i32* %8, i32* null, %struct.timespec* %9)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %64
51: ; preds = %48
%52 = load i32, i32* @errno
%53 = load i32, i32* @EAGAIN
%54 = icmp ne i32 %52, %53
br i1 %54, label %55, label %63
55: ; preds = %51
%56 = load i32, i32* @stderr
%57 = call i32 (...) @getpid()
%58 = call i32 (...) @getppid()
%59 = load i32, i32* @errno
%60 = call i8* @strerror(i32 %59)
%61 = load i32, i32* @errno
%62 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.3, i64 0, i64 0), i32 67, i32 %57, i32 %58, i8* %60, i32 %61)
br label %63
63: ; preds = %55, %51
store i32 -1, i32* %4
br label %65
64: ; preds = %48
store i32 0, i32* %4
br label %65
65: ; preds = %64, %63, %40, %28, %17
%66 = load i32, i32* %4
ret i32 %66
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@stderr = global i32 0
@.str = [57 x i8] c"[error:%d] kill() failed: pid=%d, ppid=%d, error=%s(%d)\0A\00"
@errno = global i32 0
@.str.1 = [64 x i8] c"[error:%d] sigemptyset() failed: pid=%d, ppid=%d, error=%s(%d)\0A\00"
@SIGCHLD = global i32 0
@.str.2 = [62 x i8] c"[error:%d] sigaddset() failed: pid=%d, ppid=%d, error=%s(%d)\0A\00"
@EAGAIN = global i32 0
@.str.3 = [65 x i8] c"[error:%d] sigtimedwait() failed: pid=%d, ppid=%d, error=%s(%d)\0A\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 %20
12: ; preds = %3
%13 = load i32, i32* @stderr
%14 = tail call i32 (...) @getpid()
%15 = tail call i32 (...) @getppid()
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
%18 = load i32, i32* @errno
%19 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i32 63, i32 %14, i32 %15, i8* %17, i32 %18)
br label %58
20: ; preds = %3
%21 = call i32 @sigemptyset(i32* %4)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %31
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = call i32 (...) @getpid()
%26 = call i32 (...) @getppid()
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = load i32, i32* @errno
%30 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.1, i64 0, i64 0), i32 64, i32 %25, i32 %26, i8* %28, i32 %29)
br label %58
31: ; preds = %20
%32 = load i32, i32* @SIGCHLD
%33 = call i32 @sigaddset(i32* %4, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %43
35: ; preds = %31
%36 = load i32, i32* @stderr
%37 = call i32 (...) @getpid()
%38 = call i32 (...) @getppid()
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = load i32, i32* @errno
%42 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.2, i64 0, i64 0), i32 65, i32 %37, i32 %38, i8* %40, i32 %41)
br label %58
43: ; preds = %31
%44 = call i32 @sigtimedwait(i32* %4, i32* null, %struct.timespec* %5)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %58
46: ; preds = %43
%47 = load i32, i32* @errno
%48 = load i32, i32* @EAGAIN
%49 = icmp eq i32 %47, %48
br i1 %49, label %58, label %50
50: ; preds = %46
%51 = load i32, i32* @stderr
%52 = call i32 (...) @getpid()
%53 = call i32 (...) @getppid()
%54 = load i32, i32* @errno
%55 = call i8* @strerror(i32 %54)
%56 = load i32, i32* @errno
%57 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.3, i64 0, i64 0), i32 67, i32 %52, i32 %53, i8* %55, i32 %56)
br label %58
58: ; preds = %43, %46, %50, %35, %23, %12
%59 = phi i32 [ -1, %12 ], [ -1, %23 ], [ -1, %35 ], [ -1, %50 ], [ -1, %46 ], [ 0, %43 ]
ret i32 %59
}