#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 u_int8_t;
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
scalar_t__ WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ WNOHANG;
int waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
u_int8_t __nohang_waitpid(pid_t process_pid) {
  int status = 0;
  int pid = waitpid(process_pid, &status, WNOHANG);
  if (pid == 0) {
    return 0;
  }
  if (WIFEXITED(status) && (WEXITSTATUS(status) == 0)) {
    return 1;
  } else {
    return 2;
  }
}


x86 assembly (clang, -O0):
<__nohang_waitpid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e9c(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rsi
call   1040 <waitpid@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    1170 <__nohang_waitpid+0x40>
movl   $0x0,-0x4(%rbp)
jmp    11a7 <__nohang_waitpid+0x77>
mov    -0xc(%rbp),%edi
call   1050 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11a0 <__nohang_waitpid+0x70>
mov    -0xc(%rbp),%edi
call   1030 <WEXITSTATUS@plt>
cmp    $0x0,%rax
jne    11a0 <__nohang_waitpid+0x70>
movl   $0x1,-0x4(%rbp)
jmp    11a7 <__nohang_waitpid+0x77>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__nohang_waitpid>:
push   %rax
movl   $0x0,0x4(%rsp)
mov    0x2ea8(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rsi
call   1040 <waitpid@plt>
test   %eax,%eax
je     1173 <__nohang_waitpid+0x43>
mov    0x4(%rsp),%edi
call   1050 <WIFEXITED@plt>
test   %rax,%rax
je     116c <__nohang_waitpid+0x3c>
mov    0x4(%rsp),%edi
call   1030 <WEXITSTATUS@plt>
test   %rax,%rax
je     1177 <__nohang_waitpid+0x47>
mov    $0x2,%eax
pop    %rcx
ret
xor    %eax,%eax
pop    %rcx
ret
mov    $0x1,%eax
pop    %rcx
ret

LLVM IR (-O0):
@WNOHANG = global i32 0
define i32 @__nohang_waitpid(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %4
%6 = load i32, i32* %3
%7 = load i32, i32* @WNOHANG
%8 = call i32 @waitpid(i32 %6, i32* %4, i32 %7)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 0, i32* %2
br label %22
12: ; preds = %1
%13 = load i32, i32* %4
%14 = call i64 @WIFEXITED(i32 %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = load i32, i32* %4
%18 = call i64 @WEXITSTATUS(i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %16
store i32 1, i32* %2
br label %22
21: ; preds = %16, %12
store i32 2, i32* %2
br label %22
22: ; preds = %21, %20, %11
%23 = load i32, i32* %2
ret i32 %23
}

LLVM IR (-O2):
@WNOHANG = global i32 0
define i32 @__nohang_waitpid(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%4 = load i32, i32* @WNOHANG
%5 = call i32 @waitpid(i32 %0, i32* %2, i32 %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %16, label %7
7: ; preds = %1
%8 = load i32, i32* %2
%9 = call i64 @WIFEXITED(i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %7
%12 = load i32, i32* %2
%13 = call i64 @WEXITSTATUS(i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %16, label %15
15: ; preds = %11, %7
br label %16
16: ; preds = %11, %1, %15
%17 = phi i32 [ 2, %15 ], [ 0, %1 ], [ 1, %11 ]
ret i32 %17
}