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

/* Variables and functions */
scalar_t__ ECHILD;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
scalar_t__ WIFSTOPPED(int);
int /*<<< orphan*/ WNOHANG;
int WSTOPSIG(int);
int WTERMSIG(int);
scalar_t__ errno;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdout;
scalar_t__ waitpid(int, int *, int /*<<< orphan*/);
void print_child_status() {
  pid_t pid;
  int status;
  pid = waitpid(-1, &status, WNOHANG);
  if (pid > 0) {
    if (WIFEXITED(status)) {
      printf("Child shell terminated normally with status %d\n",
             WEXITSTATUS(status));
      fflush(stdout);
      return;
    }
    if (WIFSIGNALED(status)) {
      printf("Child shell terminated by signal %d\n", WTERMSIG(status));
      fflush(stdout);
      return;
    }
    if (WIFSTOPPED(status)) {
      printf("Child shell stopped by signal %d\n", WSTOPSIG(status));
      fflush(stdout);
      return;
    }
  } else if (pid == 0) {
    printf("Child shell still running.\n");
    fflush(stdout);
    return;
  } else if (pid < 0 && errno == ECHILD) {
    printf("Child shell appears to have never run\n");
    fflush(stdout);
    return;
  } else {
    printf("Unknown child status\n");
    fflush(stdout);
    return;
  }
}


x86 assembly (clang, -O0):
<print_child_status>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e49(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
lea    -0xc(%rbp),%rsi
call   1090 <waitpid@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jle    127a <print_child_status+0xea>
mov    -0xc(%rbp),%edi
call   10a0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11fb <print_child_status+0x6b>
mov    -0xc(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
jmp    1308 <print_child_status+0x178>
mov    -0xc(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1238 <print_child_status+0xa8>
mov    -0xc(%rbp),%edi
call   1070 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
jmp    1308 <print_child_status+0x178>
mov    -0xc(%rbp),%edi
call   1050 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     1275 <print_child_status+0xe5>
mov    -0xc(%rbp),%edi
call   10b0 <WSTOPSIG@plt>
mov    %eax,%esi
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
jmp    1308 <print_child_status+0x178>
jmp    1308 <print_child_status+0x178>
cmpq   $0x0,-0x8(%rbp)
jne    12a6 <print_child_status+0x116>
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
jmp    1308 <print_child_status+0x178>
cmpq   $0x0,-0x8(%rbp)
jge    12ec <print_child_status+0x15c>
mov    0x2d20(%rip),%rax
mov    (%rax),%rax
mov    0x2d06(%rip),%rcx
cmp    (%rcx),%rax
jne    12ec <print_child_status+0x15c>
lea    0xdc1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2cf0(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
jmp    1308 <print_child_status+0x178>
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2ccf(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_child_status>:
push   %rax
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rsi
mov    $0xffffffff,%edi
call   10a0 <waitpid@plt>
test   %rax,%rax
jle    11de <print_child_status+0x3e>
mov    0x4(%rsp),%edi
call   10b0 <WIFEXITED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     11ff <print_child_status+0x5f>
call   1090 <WEXITSTATUS@plt>
lea    0xe24(%rip),%rdi
jmp    124c <print_child_status+0xac>
je     121b <print_child_status+0x7b>
mov    0x2df1(%rip),%rax
mov    (%rax),%rax
mov    0x2dd7(%rip),%rcx
cmp    (%rcx),%rax
jne    1224 <print_child_status+0x84>
lea    0xe8f(%rip),%rdi
jmp    122b <print_child_status+0x8b>
call   1050 <WIFSIGNALED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     1232 <print_child_status+0x92>
call   1080 <WTERMSIG@plt>
lea    0xe17(%rip),%rdi
jmp    124c <print_child_status+0xac>
lea    0xe90(%rip),%rdi
jmp    122b <print_child_status+0x8b>
lea    0xe4c(%rip),%rdi
call   1030 <puts@plt>
jmp    1255 <print_child_status+0xb5>
call   1060 <WIFSTOPPED@plt>
test   %rax,%rax
je     1263 <print_child_status+0xc3>
mov    0x4(%rsp),%edi
call   10c0 <WSTOPSIG@plt>
lea    0xe09(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
pop    %rax
ret

LLVM IR (-O0):
@WNOHANG = global i32 0
@.str = [48 x i8] c"Child shell terminated normally with status %d\0A\00"
@stdout = global i32 0
@.str.1 = [37 x i8] c"Child shell terminated by signal %d\0A\00"
@.str.2 = [34 x i8] c"Child shell stopped by signal %d\0A\00"
@.str.3 = [28 x i8] c"Child shell still running.\0A\00"
@errno = global i64 0
@ECHILD = global i64 0
@.str.4 = [39 x i8] c"Child shell appears to have never run\0A\00"
@.str.5 = [22 x i8] c"Unknown child status\0A\00"
define void @print_child_status() {
%1 = alloca i64
%2 = alloca i32
%3 = load i32, i32* @WNOHANG
%4 = call i64 @waitpid(i32 -1, i32* %2, i32 %3)
store i64 %4, i64* %1
%5 = load i64, i64* %1
%6 = icmp sgt i64 %5, 0
br i1 %6, label %7, label %38
7: ; preds = %0
%8 = load i32, i32* %2
%9 = call i64 @WIFEXITED(i32 %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %7
%12 = load i32, i32* %2
%13 = call i32 @WEXITSTATUS(i32 %12)
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = load i32, i32* @stdout
%16 = call i32 @fflush(i32 %15)
br label %60
17: ; preds = %7
%18 = load i32, i32* %2
%19 = call i64 @WIFSIGNALED(i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %17
%22 = load i32, i32* %2
%23 = call i32 @WTERMSIG(i32 %22)
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = load i32, i32* @stdout
%26 = call i32 @fflush(i32 %25)
br label %60
27: ; preds = %17
%28 = load i32, i32* %2
%29 = call i64 @WIFSTOPPED(i32 %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %27
%32 = load i32, i32* %2
%33 = call i32 @WSTOPSIG(i32 %32)
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %33)
%35 = load i32, i32* @stdout
%36 = call i32 @fflush(i32 %35)
br label %60
37: ; preds = %27
br label %60
38: ; preds = %0
%39 = load i64, i64* %1
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %38
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%43 = load i32, i32* @stdout
%44 = call i32 @fflush(i32 %43)
br label %60
45: ; preds = %38
%46 = load i64, i64* %1
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %56
48: ; preds = %45
%49 = load i64, i64* @errno
%50 = load i64, i64* @ECHILD
%51 = icmp eq i64 %49, %50
br i1 %51, label %52, label %56
52: ; preds = %48
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
%54 = load i32, i32* @stdout
%55 = call i32 @fflush(i32 %54)
br label %60
56: ; preds = %48, %45
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%58 = load i32, i32* @stdout
%59 = call i32 @fflush(i32 %58)
br label %60
60: ; preds = %11, %21, %31, %41, %52, %56, %37
ret void
}

LLVM IR (-O2):
@WNOHANG = global i32 0
@.str = [48 x i8] c"Child shell terminated normally with status %d\0A\00"
@stdout = global i32 0
@.str.1 = [37 x i8] c"Child shell terminated by signal %d\0A\00"
@.str.2 = [34 x i8] c"Child shell stopped by signal %d\0A\00"
@errno = global i64 0
@ECHILD = global i64 0
@str = [21 x i8] c"Unknown child status\00"
@str.6 = [38 x i8] c"Child shell appears to have never run\00"
@str.7 = [27 x i8] c"Child shell still running.\00"
define void @print_child_status() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = load i32, i32* @WNOHANG
%4 = call i64 @waitpid(i32 -1, i32* %1, i32 %3)
%5 = icmp sgt i64 %4, 0
br i1 %5, label %6, label %28
6: ; preds = %0
%7 = load i32, i32* %1
%8 = call i64 @WIFEXITED(i32 %7)
%9 = icmp eq i64 %8, 0
%10 = load i32, i32* %1
br i1 %9, label %14, label %11
11: ; preds = %6
%12 = call i32 @WEXITSTATUS(i32 %10)
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i32 %12)
br label %40
14: ; preds = %6
%15 = call i64 @WIFSIGNALED(i32 %10)
%16 = icmp eq i64 %15, 0
%17 = load i32, i32* %1
br i1 %16, label %21, label %18
18: ; preds = %14
%19 = call i32 @WTERMSIG(i32 %17)
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %19)
br label %40
21: ; preds = %14
%22 = call i64 @WIFSTOPPED(i32 %17)
%23 = icmp eq i64 %22, 0
br i1 %23, label %43, label %24
24: ; preds = %21
%25 = load i32, i32* %1
%26 = call i32 @WSTOPSIG(i32 %25)
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %26)
br label %40
28: ; preds = %0
%29 = icmp eq i64 %4, 0
br i1 %29, label %30, label %32
30: ; preds = %28
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.7, i64 0, i64 0))
br label %40
32: ; preds = %28
%33 = load i64, i64* @errno
%34 = load i64, i64* @ECHILD
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.6, i64 0, i64 0))
br label %40
38: ; preds = %32
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %40
40: ; preds = %11, %18, %24, %30, %36, %38
%41 = load i32, i32* @stdout
%42 = call i32 @fflush(i32 %41)
br label %43
43: ; preds = %40, %21
ret void
}