#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 */
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
void wait_and_print_status(pid_t p) {
  int status = 0;
  pid_t event_on_pid = waitpid(p, &status, 0);
  if (event_on_pid > 0) {
    if (WIFEXITED(status)) {
      printf("Exit code: %d\n", WEXITSTATUS(status));
    } else if (WIFSIGNALED(status)) {
      printf("Signal code: %d\n", WTERMSIG(status));
    } else {
      printf("Unknown reason of process death.\n");
    }
  } else {
    perror("waitpid");
  }
}


x86 assembly (clang, -O0):
<wait_and_print_status>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0xc(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jle    1217 <wait_and_print_status+0xa7>
mov    -0xc(%rbp),%edi
call   1090 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11d0 <wait_and_print_status+0x60>
mov    -0xc(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1212 <wait_and_print_status+0xa2>
mov    -0xc(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     11ff <wait_and_print_status+0x8f>
mov    -0xc(%rbp),%edi
call   1050 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    120d <wait_and_print_status+0x9d>
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1212 <wait_and_print_status+0xa2>
jmp    1223 <wait_and_print_status+0xb3>
lea    0xe24(%rip),%rdi
call   1080 <perror@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_and_print_status>:
push   %rax
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
test   %rax,%rax
jle    11ed <wait_and_print_status+0x6d>
mov    0x4(%rsp),%edi
call   10a0 <WIFEXITED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     11ba <wait_and_print_status+0x3a>
call   1070 <WEXITSTATUS@plt>
lea    0xe48(%rip),%rdi
jmp    11d4 <wait_and_print_status+0x54>
call   1050 <WIFSIGNALED@plt>
test   %rax,%rax
je     11df <wait_and_print_status+0x5f>
mov    0x4(%rsp),%edi
call   1060 <WTERMSIG@plt>
lea    0xe3b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
pop    %rax
ret
lea    0xe42(%rip),%rdi
call   1030 <puts@plt>
pop    %rax
ret
lea    0xe2c(%rip),%rdi
call   1090 <perror@plt>
pop    %rax
ret

LLVM IR (-O0):
@.str = [15 x i8] c"Exit code: %d\0A\00"
@.str.1 = [17 x i8] c"Signal code: %d\0A\00"
@.str.2 = [34 x i8] c"Unknown reason of process death.\0A\00"
@.str.3 = [8 x i8] c"waitpid\00"
define void @wait_and_print_status(i64 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i64
store i64 %0, i64* %2
store i32 0, i32* %3
%5 = load i64, i64* %2
%6 = call i64 @waitpid(i64 %5, i32* %3, i32 0)
store i64 %6, i64* %4
%7 = load i64, i64* %4
%8 = icmp sgt i64 %7, 0
br i1 %8, label %9, label %29
9: ; preds = %1
%10 = load i32, i32* %3
%11 = call i64 @WIFEXITED(i32 %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = load i32, i32* %3
%15 = call i32 @WEXITSTATUS(i32 %14)
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %15)
br label %28
17: ; preds = %9
%18 = load i32, i32* %3
%19 = call i64 @WIFSIGNALED(i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = load i32, i32* %3
%23 = call i32 @WTERMSIG(i32 %22)
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %23)
br label %27
25: ; preds = %17
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25, %21
br label %28
28: ; preds = %27, %13
br label %31
29: ; preds = %1
%30 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %31
31: ; preds = %29, %28
ret void
}

LLVM IR (-O2):
@.str = [15 x i8] c"Exit code: %d\0A\00"
@.str.1 = [17 x i8] c"Signal code: %d\0A\00"
@.str.3 = [8 x i8] c"waitpid\00"
@str = [33 x i8] c"Unknown reason of process death.\00"
define void @wait_and_print_status(i64 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%4 = call i64 @waitpid(i64 %0, i32* %2, i32 0)
%5 = icmp sgt i64 %4, 0
br i1 %5, label %6, label %23
6: ; preds = %1
%7 = load i32, i32* %2
%8 = call i64 @WIFEXITED(i32 %7)
%9 = icmp eq i64 %8, 0
%10 = load i32, i32* %2
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 ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %12)
br label %25
14: ; preds = %6
%15 = call i64 @WIFSIGNALED(i32 %10)
%16 = icmp eq i64 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %14
%18 = load i32, i32* %2
%19 = call i32 @WTERMSIG(i32 %18)
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %19)
br label %25
21: ; preds = %14
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
br label %25
23: ; preds = %1
%24 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %11, %21, %17, %23
ret void
}