#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 pid_t;

/* Variables and functions */
scalar_t__ ECHILD;
int /*<<< orphan*/ WNOHANG;
scalar_t__ errno;
int /*<<< orphan*/ log_err(char *, int);
int waitpid(int, int *, int /*<<< orphan*/);
int child_has_quit(pid_t pid, int *quit, int *status) {
  pid_t res = waitpid(pid, status, WNOHANG);
  if (res == -1) {
    if (errno == ECHILD)
      *quit = 2;
    else {
      log_err("error checking status of child process %d: %m", (int)pid);
      return 202;
    }
  } else if (res == 0)
    *quit = 0;
  else
    *quit = 1;
  return 0;
}


x86 assembly (clang, -O0):
<child_has_quit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    0x2ea7(%rip),%rax
mov    (%rax),%edx
call   1030 <waitpid@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    119e <child_has_quit+0x7e>
mov    0x2e7c(%rip),%rax
mov    (%rax),%rax
mov    0x2e6a(%rip),%rcx
cmp    (%rcx),%rax
jne    117e <child_has_quit+0x5e>
mov    -0x10(%rbp),%rax
movl   $0x2,(%rax)
jmp    1199 <child_has_quit+0x79>
mov    -0x8(%rbp),%esi
lea    0xe78(%rip),%rdi
call   1040 <log_err@plt>
movl   $0xca,-0x4(%rbp)
jmp    11cd <child_has_quit+0xad>
jmp    11c6 <child_has_quit+0xa6>
cmpl   $0x0,-0x1c(%rbp)
jne    11b7 <child_has_quit+0x97>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
jmp    11c1 <child_has_quit+0xa1>
mov    -0x10(%rbp),%rax
movl   $0x1,(%rax)
jmp    11c6 <child_has_quit+0xa6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<child_has_quit>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2eb9(%rip),%rax
mov    (%rax),%eax
mov    %rdx,%rsi
mov    %eax,%edx
call   1030 <waitpid@plt>
test   %eax,%eax
je     1162 <child_has_quit+0x42>
cmp    $0xffffffff,%eax
jne    116a <child_has_quit+0x4a>
mov    0x2e8d(%rip),%rax
mov    (%rax),%rax
mov    0x2e7b(%rip),%rcx
cmp    (%rcx),%rax
jne    1179 <child_has_quit+0x59>
movl   $0x2,(%rbx)
jmp    1170 <child_has_quit+0x50>
movl   $0x0,(%rbx)
jmp    1170 <child_has_quit+0x50>
movl   $0x1,(%rbx)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe80(%rip),%rdi
mov    %ebp,%esi
call   1040 <log_err@plt>
mov    $0xca,%eax
jmp    1172 <child_has_quit+0x52>

LLVM IR (-O0):
@WNOHANG = global i32 0
@errno = global i64 0
@ECHILD = global i64 0
@.str = [46 x i8] c"error checking status of child process %d: %m\00"
define i32 @child_has_quit(i32 %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32* %2, i32** %7
%9 = load i32, i32* %5
%10 = load i32*, i32** %7
%11 = load i32, i32* @WNOHANG
%12 = call i32 @waitpid(i32 %9, i32* %10, i32 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %25
15: ; preds = %3
%16 = load i64, i64* @errno
%17 = load i64, i64* @ECHILD
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = load i32*, i32** %6
store i32 2, i32* %20
br label %24
21: ; preds = %15
%22 = load i32, i32* %5
%23 = call i32 @log_err(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 %22)
store i32 202, i32* %4
br label %34
24: ; preds = %19
br label %33
25: ; preds = %3
%26 = load i32, i32* %8
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = load i32*, i32** %6
store i32 0, i32* %29
br label %32
30: ; preds = %25
%31 = load i32*, i32** %6
store i32 1, i32* %31
br label %32
32: ; preds = %30, %28
br label %33
33: ; preds = %32, %24
store i32 0, i32* %4
br label %34
34: ; preds = %33, %21
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
@WNOHANG = global i32 0
@errno = global i64 0
@ECHILD = global i64 0
@.str = [46 x i8] c"error checking status of child process %d: %m\00"
define i32 @child_has_quit(i32 %0, i32* nocapture writeonly %1, i32* %2) {
%4 = load i32, i32* @WNOHANG
%5 = tail call i32 @waitpid(i32 %0, i32* %2, i32 %4)
switch i32 %5, label %14 [
i32 -1, label %6
i32 0, label %13
]
6: ; preds = %3
%7 = load i64, i64* @errno
%8 = load i64, i64* @ECHILD
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %11
10: ; preds = %6
store i32 2, i32* %1
br label %15
11: ; preds = %6
%12 = tail call i32 @log_err(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %15
13: ; preds = %3
store i32 0, i32* %1
br label %15
14: ; preds = %3
store i32 1, i32* %1
br label %15
15: ; preds = %10, %14, %13, %11
%16 = phi i32 [ 202, %11 ], [ 0, %13 ], [ 0, %14 ], [ 0, %10 ]
ret i32 %16
}