#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 tv_sec;
  int tv_nsec;
};
typedef int /*<<< orphan*/ sigset_t;

/* Variables and functions */
scalar_t__ EAGAIN;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ WIFSIGNALED(int);
int /*<<< orphan*/ WNOHANG;
scalar_t__ errno;
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int sigtimedwait(int /*<<< orphan*/ *, int /*<<< orphan*/ *, struct timespec *);
int waitpid(int, int *, int /*<<< orphan*/);
int waitpidTimeout(int pid, int *status, int timeMs) {
  struct timespec ts;
  ts.tv_sec = timeMs / 1000;
  ts.tv_nsec = timeMs % 1000 * 1000000;
  sigset_t mask;
  sigemptyset(&mask);
  sigaddset(&mask, SIGCHLD);
  int r = sigtimedwait(&mask, NULL, &ts);
  if (r < 0 && errno != EAGAIN) {
    return r;
  }
  r = waitpid(pid, status, WNOHANG);
  if (r <= 0) {
    return r;
  }
  if (!WIFEXITED(*status) && !WIFSIGNALED(*status)) {
    return 0;
  }
  return pid;
}


x86 assembly (clang, -O0):
<waitpidTimeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0xf4240,%edx,%eax
mov    %eax,-0x1c(%rbp)
lea    -0x24(%rbp),%rdi
call   1050 <sigemptyset@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
lea    -0x24(%rbp),%rdi
call   1080 <sigaddset@plt>
lea    -0x24(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x20(%rbp),%rdx
call   1030 <sigtimedwait@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11f2 <waitpidTimeout+0x92>
mov    0x2dfc(%rip),%rax
mov    (%rax),%rax
mov    0x2de2(%rip),%rcx
cmp    (%rcx),%rax
je     11f2 <waitpidTimeout+0x92>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1259 <waitpidTimeout+0xf9>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
call   1060 <waitpid@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jg     121f <waitpidTimeout+0xbf>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1259 <waitpidTimeout+0xf9>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%eax
jne    1253 <waitpidTimeout+0xf3>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%eax
jne    1253 <waitpidTimeout+0xf3>
movl   $0x0,-0x4(%rbp)
jmp    1259 <waitpidTimeout+0xf9>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitpidTimeout>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
movslq %edx,%rax
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
mov    %ecx,(%rsp)
imul   $0x3e8,%ecx,%ecx
sub    %ecx,%eax
imul   $0xf4240,%eax,%eax
mov    %eax,0x4(%rsp)
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1050 <sigemptyset@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1080 <sigaddset@plt>
mov    %rsp,%rdx
mov    %rbp,%rdi
xor    %esi,%esi
call   1030 <sigtimedwait@plt>
test   %eax,%eax
jns    11df <waitpidTimeout+0x7f>
mov    0x2e00(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2de6(%rip),%rdx
cmp    (%rdx),%rcx
jne    1210 <waitpidTimeout+0xb0>
mov    0x2e02(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1060 <waitpid@plt>
test   %eax,%eax
jle    1210 <waitpidTimeout+0xb0>
mov    (%rbx),%edi
call   1070 <WIFEXITED@plt>
test   %eax,%eax
jne    120d <waitpidTimeout+0xad>
mov    (%rbx),%edi
call   1040 <WIFSIGNALED@plt>
test   %eax,%eax
je     1219 <waitpidTimeout+0xb9>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
jmp    1210 <waitpidTimeout+0xb0>

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@SIGCHLD = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@WNOHANG = global i32 0
define i32 @waitpidTimeout(i32 %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca %struct.timespec
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%11 = load i32, i32* %7
%12 = sdiv i32 %11, 1000
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* %7
%15 = srem i32 %14, 1000
%16 = mul nsw i32 %15, 1000000
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 1
store i32 %16, i32* %17
%18 = call i32 @sigemptyset(i32* %9)
%19 = load i32, i32* @SIGCHLD
%20 = call i32 @sigaddset(i32* %9, i32 %19)
%21 = call i32 @sigtimedwait(i32* %9, i32* null, %struct.timespec* %8)
store i32 %21, i32* %10
%22 = load i32, i32* %10
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %3
%25 = load i64, i64* @errno
%26 = load i64, i64* @EAGAIN
%27 = icmp ne i64 %25, %26
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = load i32, i32* %10
store i32 %29, i32* %4
br label %52
30: ; preds = %24, %3
%31 = load i32, i32* %5
%32 = load i32*, i32** %6
%33 = load i32, i32* @WNOHANG
%34 = call i32 @waitpid(i32 %31, i32* %32, i32 %33)
store i32 %34, i32* %10
%35 = load i32, i32* %10
%36 = icmp sle i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %30
%38 = load i32, i32* %10
store i32 %38, i32* %4
br label %52
39: ; preds = %30
%40 = load i32*, i32** %6
%41 = load i32, i32* %40
%42 = call i32 @WIFEXITED(i32 %41)
%43 = icmp ne i32 %42, 0
br i1 %43, label %50, label %44
44: ; preds = %39
%45 = load i32*, i32** %6
%46 = load i32, i32* %45
%47 = call i32 @WIFSIGNALED(i32 %46)
%48 = icmp ne i32 %47, 0
br i1 %48, label %50, label %49
49: ; preds = %44
store i32 0, i32* %4
br label %52
50: ; preds = %44, %39
%51 = load i32, i32* %5
store i32 %51, i32* %4
br label %52
52: ; preds = %50, %49, %37, %28
%53 = load i32, i32* %4
ret i32 %53
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@SIGCHLD = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@WNOHANG = global i32 0
define i32 @waitpidTimeout(i32 %0, i32* %1, i32 %2) {
%4 = alloca %struct.timespec
%5 = alloca i32
%6 = bitcast %struct.timespec* %4 to i8*
%7 = sdiv i32 %2, 1000
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
store i32 %7, i32* %8
%9 = srem i32 %2, 1000
%10 = mul nsw i32 %9, 1000000
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
store i32 %10, i32* %11
%12 = bitcast i32* %5 to i8*
%13 = call i32 @sigemptyset(i32* %5)
%14 = load i32, i32* @SIGCHLD
%15 = call i32 @sigaddset(i32* %5, i32 %14)
%16 = call i32 @sigtimedwait(i32* %5, i32* null, %struct.timespec* %4)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %3
%19 = load i64, i64* @errno
%20 = load i64, i64* @EAGAIN
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %35
22: ; preds = %18, %3
%23 = load i32, i32* @WNOHANG
%24 = call i32 @waitpid(i32 %0, i32* %1, i32 %23)
%25 = icmp slt i32 %24, 1
br i1 %25, label %35, label %26
26: ; preds = %22
%27 = load i32, i32* %1
%28 = call i32 @WIFEXITED(i32 %27)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = load i32, i32* %1
%32 = call i32 @WIFSIGNALED(i32 %31)
%33 = icmp eq i32 %32, 0
br i1 %33, label %35, label %34
34: ; preds = %30, %26
br label %35
35: ; preds = %30, %22, %18, %34
%36 = phi i32 [ %0, %34 ], [ %16, %18 ], [ %24, %22 ], [ 0, %30 ]
ret i32 %36
}