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

/* Variables and functions */
#define EAGAIN 129
#define EINTR 128
int /*<<< orphan*/ SIGCHLD;
int SIGKILL;
int SIGTERM;
int /*<<< orphan*/ SIG_BLOCK;
int WEXITSTATUS(int);
int WIFEXITED(int);
int /*<<< orphan*/ WNOHANG;
int errno;
int /*<<< orphan*/ kill(int /*<<< orphan*/ const, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
scalar_t__ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                       int /*<<< orphan*/ *);
scalar_t__ sigtimedwait(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                        struct timespec *);
scalar_t__ timer_update(struct timespec *, struct timespec *);
int /*<<< orphan*/ ts_set(struct timespec *, float const, float const);
scalar_t__ waitpid(int /*<<< orphan*/ const, int *, int /*<<< orphan*/);
int wait_pid_timeout(pid_t const pid, float const sec) {
  int sig = SIGTERM;
  struct timespec tcur, timeout;
  ts_set(&timeout, sec, (sec - (int)sec) * 1000000000L);
  if (0 != timer_update(NULL, &tcur))
    return -5;
  sigset_t mask;
  sigemptyset(&mask);
  sigaddset(&mask, SIGCHLD);
  if (0 != sigprocmask(SIG_BLOCK, &mask, NULL)) {
    perror("sigprocmask");
    return -5;
  }
  while (0 > sigtimedwait(&mask, NULL, &timeout)) {
    switch (errno) {
    case EINTR:
      if (0 != timer_update(&timeout, &tcur))
        return -5;
      break;
    case EAGAIN:
      if (0 == kill(pid, sig))
        return -3;
      perror("kill");
      if (SIGKILL == sig)
        return -9;
      sig = SIGKILL;
      ts_set(&timeout, 5, 0);
      break;
    default:
      perror("sigtimedwait");
      return -9;
    }
  }
  int status = -9;
  if (0 > waitpid(pid, &status, WNOHANG)) {
    perror("waitpid");
    return -5;
  }
  if (1 != WIFEXITED(status)) {
    perror("%s child exit failed, halt system");
    return -9;
  }
  return WEXITSTATUS(status);
}


x86 assembly (clang, -O0):
<wait_pid_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movss  %xmm0,-0xc(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movss  -0xc(%rbp),%xmm0
movss  -0xc(%rbp),%xmm1
cvttss2si -0xc(%rbp),%eax
cvtsi2ss %eax,%xmm2
subss  %xmm2,%xmm1
movss  0xe15(%rip),%xmm2
mulss  %xmm2,%xmm1
lea    -0x20(%rbp),%rdi
call   1060 <ts_set@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    -0x18(%rbp),%rsi
call   10b0 <timer_update@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     121f <wait_pid_timeout+0x6f>
movl   $0xfffffffb,-0x4(%rbp)
jmp    1400 <wait_pid_timeout+0x250>
lea    -0x24(%rbp),%rdi
call   1050 <sigemptyset@plt>
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
lea    -0x24(%rbp),%rdi
call   10d0 <sigaddset@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1276 <wait_pid_timeout+0xc6>
lea    0xda3(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xfffffffb,-0x4(%rbp)
jmp    1400 <wait_pid_timeout+0x250>
jmp    127b <wait_pid_timeout+0xcb>
lea    -0x24(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x20(%rbp),%rdx
call   1040 <sigtimedwait@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jle    1384 <wait_pid_timeout+0x1d4>
mov    0x2d27(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
sub    $0x80,%eax
je     12c9 <wait_pid_timeout+0x119>
jmp    12b6 <wait_pid_timeout+0x106>
mov    -0x2c(%rbp),%eax
sub    $0x81,%eax
je     12f5 <wait_pid_timeout+0x145>
jmp    1367 <wait_pid_timeout+0x1b7>
lea    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   10b0 <timer_update@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     12f0 <wait_pid_timeout+0x140>
movl   $0xfffffffb,-0x4(%rbp)
jmp    1400 <wait_pid_timeout+0x250>
jmp    137f <wait_pid_timeout+0x1cf>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1070 <kill@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    %ecx,%eax
jne    1318 <wait_pid_timeout+0x168>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1400 <wait_pid_timeout+0x250>
lea    0xcf5(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c8d(%rip),%rax
mov    (%rax),%eax
cmp    -0x10(%rbp),%eax
jne    1342 <wait_pid_timeout+0x192>
movl   $0xfffffff7,-0x4(%rbp)
jmp    1400 <wait_pid_timeout+0x250>
mov    0x2c6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
movss  0xcaa(%rip),%xmm0
xorps  %xmm1,%xmm1
call   1060 <ts_set@plt>
jmp    137f <wait_pid_timeout+0x1cf>
lea    0xcab(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xfffffff7,-0x4(%rbp)
jmp    1400 <wait_pid_timeout+0x250>
jmp    127b <wait_pid_timeout+0xcb>
movl   $0xfffffff7,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2c4b(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rsi
call   1090 <waitpid@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jle    13c6 <wait_pid_timeout+0x216>
lea    0xc71(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xfffffffb,-0x4(%rbp)
jmp    1400 <wait_pid_timeout+0x250>
mov    -0x28(%rbp),%edi
call   10c0 <WIFEXITED@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     13f5 <wait_pid_timeout+0x245>
lea    0xc4a(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xfffffff7,-0x4(%rbp)
jmp    1400 <wait_pid_timeout+0x250>
mov    -0x28(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_pid_timeout>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edi,%r15d
mov    0x2e22(%rip),%rax
mov    (%rax),%r14d
cvttps2dq %xmm0,%xmm1
cvtdq2ps %xmm1,%xmm2
movaps %xmm0,%xmm1
subss  %xmm2,%xmm1
mulss  0xe21(%rip),%xmm1
lea    0x10(%rsp),%rdi
call   1060 <ts_set@plt>
lea    0x18(%rsp),%rsi
xor    %edi,%edi
call   10b0 <timer_update@plt>
mov    %rax,%rcx
mov    $0xfffffffb,%eax
test   %rcx,%rcx
jne    1383 <wait_pid_timeout+0x1d3>
lea    0x14(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <sigemptyset@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10d0 <sigaddset@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
test   %rax,%rax
jne    134f <wait_pid_timeout+0x19f>
lea    0x14(%rsp),%rdi
lea    0x10(%rsp),%rdx
xor    %esi,%esi
call   1040 <sigtimedwait@plt>
test   %rax,%rax
js     129d <wait_pid_timeout+0xed>
movl   $0xfffffff7,0xc(%rsp)
ff
mov    0x2d7b(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rsi
mov    %r15d,%edi
call   1090 <waitpid@plt>
test   %rax,%rax
js     1358 <wait_pid_timeout+0x1a8>
mov    0xc(%rsp),%edi
call   10c0 <WIFEXITED@plt>
cmp    $0x1,%eax
jne    136b <wait_pid_timeout+0x1bb>
mov    0xc(%rsp),%edi
call   1080 <WEXITSTATUS@plt>
jmp    1383 <wait_pid_timeout+0x1d3>
mov    0x2d24(%rip),%rbp
lea    0x10(%rsp),%rbx
lea    0x14(%rsp),%r12
jmp    12c2 <wait_pid_timeout+0x112>
mov    %r12,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1040 <sigtimedwait@plt>
test   %rax,%rax
jns    1256 <wait_pid_timeout+0xa6>
mov    0x0(%rbp),%eax
cmp    $0x81,%eax
je     12f0 <wait_pid_timeout+0x140>
cmp    $0x80,%eax
jne    1390 <wait_pid_timeout+0x1e0>
mov    %rbx,%rdi
lea    0x18(%rsp),%rsi
call   10b0 <timer_update@plt>
test   %rax,%rax
je     12b0 <wait_pid_timeout+0x100>
jmp    137e <wait_pid_timeout+0x1ce>
xchg   %ax,%ax
mov    %r15d,%edi
mov    %r14d,%esi
call   1070 <kill@plt>
test   %eax,%eax
je     1348 <wait_pid_timeout+0x198>
mov    %r14d,0x1c(%rsp)
mov    %r15d,%r14d
mov    %rbp,%r15
lea    0xd03(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c9b(%rip),%rax
mov    (%rax),%ebp
cmp    0x1c(%rsp),%ebp
je     1377 <wait_pid_timeout+0x1c7>
xorps  %xmm1,%xmm1
mov    %rbx,%rdi
movss  0xcd1(%rip),%xmm0
call   1060 <ts_set@plt>
mov    %ebp,%eax
mov    %r15,%rbp
mov    %r14d,%r15d
mov    %eax,%r14d
jmp    12b0 <wait_pid_timeout+0x100>
mov    $0xfffffffd,%eax
jmp    1383 <wait_pid_timeout+0x1d3>
lea    0xcb2(%rip),%rdi
jmp    135f <wait_pid_timeout+0x1af>
lea    0xcc7(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xfffffffb,%eax
jmp    1383 <wait_pid_timeout+0x1d3>
lea    0xcbc(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xfffffff7,%eax
jmp    1383 <wait_pid_timeout+0x1d3>
mov    $0xfffffffb,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc82(%rip),%rdi
jmp    1372 <wait_pid_timeout+0x1c2>

LLVM IR (-O0):
%struct.timespec = type { i32 }
@SIGTERM = global i32 0
@SIGCHLD = global i32 0
@SIG_BLOCK = global i32 0
@.str = [12 x i8] c"sigprocmask\00"
@errno = global i32 0
@.str.1 = [5 x i8] c"kill\00"
@SIGKILL = global i32 0
@.str.2 = [13 x i8] c"sigtimedwait\00"
@WNOHANG = global i32 0
@.str.3 = [8 x i8] c"waitpid\00"
@.str.4 = [34 x i8] c"%s child exit failed, halt system\00"
define i32 @wait_pid_timeout(i32 %0, float %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca float
%6 = alloca i32
%7 = alloca %struct.timespec
%8 = alloca %struct.timespec
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store float %1, float* %5
%11 = load i32, i32* @SIGTERM
store i32 %11, i32* %6
%12 = load float, float* %5
%13 = load float, float* %5
%14 = load float, float* %5
%15 = fptosi float %14 to i32
%16 = sitofp i32 %15 to float
%17 = fsub float %13, %16
%18 = fmul float %17, 1.000000e+09
%19 = call i32 @ts_set(%struct.timespec* %8, float %12, float %18)
%20 = call i64 @timer_update(%struct.timespec* null, %struct.timespec* %7)
%21 = icmp ne i64 0, %20
br i1 %21, label %22, label %23
22: ; preds = %2
store i32 -5, i32* %3
br label %77
23: ; preds = %2
%24 = call i32 @sigemptyset(i32* %9)
%25 = load i32, i32* @SIGCHLD
%26 = call i32 @sigaddset(i32* %9, i32 %25)
%27 = load i32, i32* @SIG_BLOCK
%28 = call i64 @sigprocmask(i32 %27, i32* %9, i32* null)
%29 = icmp ne i64 0, %28
br i1 %29, label %30, label %32
30: ; preds = %23
%31 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 -5, i32* %3
br label %77
32: ; preds = %23
br label %33
33: ; preds = %60, %32
%34 = call i64 @sigtimedwait(i32* %9, i32* null, %struct.timespec* %8)
%35 = icmp sgt i64 0, %34
br i1 %35, label %36, label %61
36: ; preds = %33
%37 = load i32, i32* @errno
switch i32 %37, label %58 [
i32 128, label %38
i32 129, label %43
]
38: ; preds = %36
%39 = call i64 @timer_update(%struct.timespec* %8, %struct.timespec* %7)
%40 = icmp ne i64 0, %39
br i1 %40, label %41, label %42
41: ; preds = %38
store i32 -5, i32* %3
br label %77
42: ; preds = %38
br label %60
43: ; preds = %36
%44 = load i32, i32* %4
%45 = load i32, i32* %6
%46 = call i32 @kill(i32 %44, i32 %45)
%47 = icmp eq i32 0, %46
br i1 %47, label %48, label %49
48: ; preds = %43
store i32 -3, i32* %3
br label %77
49: ; preds = %43
%50 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%51 = load i32, i32* @SIGKILL
%52 = load i32, i32* %6
%53 = icmp eq i32 %51, %52
br i1 %53, label %54, label %55
54: ; preds = %49
store i32 -9, i32* %3
br label %77
55: ; preds = %49
%56 = load i32, i32* @SIGKILL
store i32 %56, i32* %6
%57 = call i32 @ts_set(%struct.timespec* %8, float 5.000000e+00, float 0.000000e+00)
br label %60
58: ; preds = %36
%59 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 -9, i32* %3
br label %77
60: ; preds = %55, %42
br label %33
61: ; preds = %33
store i32 -9, i32* %10
%62 = load i32, i32* %4
%63 = load i32, i32* @WNOHANG
%64 = call i64 @waitpid(i32 %62, i32* %10, i32 %63)
%65 = icmp sgt i64 0, %64
br i1 %65, label %66, label %68
66: ; preds = %61
%67 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
store i32 -5, i32* %3
br label %77
68: ; preds = %61
%69 = load i32, i32* %10
%70 = call i32 @WIFEXITED(i32 %69)
%71 = icmp ne i32 1, %70
br i1 %71, label %72, label %74
72: ; preds = %68
%73 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
store i32 -9, i32* %3
br label %77
74: ; preds = %68
%75 = load i32, i32* %10
%76 = call i32 @WEXITSTATUS(i32 %75)
store i32 %76, i32* %3
br label %77
77: ; preds = %74, %72, %66, %58, %54, %48, %41, %30, %22
%78 = load i32, i32* %3
ret i32 %78
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@SIGTERM = global i32 0
@SIGCHLD = global i32 0
@SIG_BLOCK = global i32 0
@.str = [12 x i8] c"sigprocmask\00"
@errno = global i32 0
@.str.1 = [5 x i8] c"kill\00"
@SIGKILL = global i32 0
@.str.2 = [13 x i8] c"sigtimedwait\00"
@WNOHANG = global i32 0
@.str.3 = [8 x i8] c"waitpid\00"
@.str.4 = [34 x i8] c"%s child exit failed, halt system\00"
define i32 @wait_pid_timeout(i32 %0, float %1) {
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca i32
%6 = alloca i32
%7 = load i32, i32* @SIGTERM
%8 = bitcast %struct.timespec* %3 to i8*
%9 = bitcast %struct.timespec* %4 to i8*
%10 = fptosi float %1 to i32
%11 = sitofp i32 %10 to float
%12 = fsub float %1, %11
%13 = fmul float %12, 1.000000e+09
%14 = call i32 @ts_set(%struct.timespec* %4, float %1, float %13)
%15 = call i64 @timer_update(%struct.timespec* null, %struct.timespec* %3)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %71
17: ; preds = %2
%18 = bitcast i32* %5 to i8*
%19 = call i32 @sigemptyset(i32* %5)
%20 = load i32, i32* @SIGCHLD
%21 = call i32 @sigaddset(i32* %5, i32 %20)
%22 = load i32, i32* @SIG_BLOCK
%23 = call i64 @sigprocmask(i32 %22, i32* %5, i32* null)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %17
%26 = call i64 @sigtimedwait(i32* %5, i32* null, %struct.timespec* %4)
%27 = icmp slt i64 %26, 0
br i1 %27, label %30, label %51
28: ; preds = %17
%29 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %69
30: ; preds = %25, %47
%31 = phi i32 [ %48, %47 ], [ %7, %25 ]
%32 = load i32, i32* @errno
switch i32 %32, label %45 [
i32 128, label %33
i32 129, label %36
]
33: ; preds = %30
%34 = call i64 @timer_update(%struct.timespec* %4, %struct.timespec* %3)
%35 = icmp eq i64 %34, 0
br i1 %35, label %47, label %69
36: ; preds = %30
%37 = call i32 @kill(i32 %0, i32 %31)
%38 = icmp eq i32 %37, 0
br i1 %38, label %69, label %39
39: ; preds = %36
%40 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%41 = load i32, i32* @SIGKILL
%42 = icmp eq i32 %41, %31
br i1 %42, label %69, label %43
43: ; preds = %39
%44 = call i32 @ts_set(%struct.timespec* %4, float 5.000000e+00, float 0.000000e+00)
br label %47
45: ; preds = %30
%46 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %69
47: ; preds = %33, %43
%48 = phi i32 [ %41, %43 ], [ %31, %33 ]
%49 = call i64 @sigtimedwait(i32* %5, i32* null, %struct.timespec* %4)
%50 = icmp slt i64 %49, 0
br i1 %50, label %30, label %51
51: ; preds = %47, %25
%52 = bitcast i32* %6 to i8*
store i32 -9, i32* %6
%53 = load i32, i32* @WNOHANG
%54 = call i64 @waitpid(i32 %0, i32* %6, i32 %53)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %51
%57 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %67
58: ; preds = %51
%59 = load i32, i32* %6
%60 = call i32 @WIFEXITED(i32 %59)
%61 = icmp eq i32 %60, 1
br i1 %61, label %64, label %62
62: ; preds = %58
%63 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
br label %67
64: ; preds = %58
%65 = load i32, i32* %6
%66 = call i32 @WEXITSTATUS(i32 %65)
br label %67
67: ; preds = %64, %62, %56
%68 = phi i32 [ -5, %56 ], [ -9, %62 ], [ %66, %64 ]
br label %69
69: ; preds = %39, %36, %33, %67, %45, %28
%70 = phi i32 [ -5, %28 ], [ -9, %45 ], [ %68, %67 ], [ -9, %39 ], [ -3, %36 ], [ -5, %33 ]
br label %71
71: ; preds = %2, %69
%72 = phi i32 [ %70, %69 ], [ -5, %2 ]
ret i32 %72
}