#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 */
int errno;
int /*<<< orphan*/ printf(char *, int, ...);
int waitpid(int, int *, int /*<<< orphan*/);
void my_handler(int nsig) {
  int status;
  pid_t pid;
  if ((pid = waitpid(-1, &status, 0)) < 0) {
    printf("Some error on waitpid errno = %d\n", errno);
  } else {
    if ((status & 0xff) == 0) {
      printf("Process %d was exited with status %d\n", pid, status >> 8);
    } else if ((status & 0xff00) == 0) {
      printf("Process %d killed by signal %d %s\n", pid, status & 0x7f,
             (status & 0x80) ? "with core file" : "without core file");
    }
  }
}


x86 assembly (clang, -O0):
<my_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    $0xffffffff,%edi
lea    -0x8(%rbp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1163 <my_handler+0x43>
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
lea    0xea9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11e0 <my_handler+0xc0>
mov    -0x8(%rbp),%eax
and    $0xff,%eax
cmp    $0x0,%eax
jne    1190 <my_handler+0x70>
mov    -0xc(%rbp),%esi
mov    -0x8(%rbp),%edx
sar    $0x8,%edx
lea    0xe9e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11db <my_handler+0xbb>
mov    -0x8(%rbp),%eax
and    $0xff00,%eax
cmp    $0x0,%eax
jne    11d6 <my_handler+0xb6>
mov    -0xc(%rbp),%esi
mov    -0x8(%rbp),%edx
and    $0x7f,%edx
mov    -0x8(%rbp),%edi
and    $0x80,%edi
lea    0xec0(%rip),%rcx
lea    0xeaa(%rip),%rax
cmp    $0x0,%edi
cmovne %rax,%rcx
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11db <my_handler+0xbb>
jmp    11e0 <my_handler+0xc0>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_handler>:
push   %rax
lea    0x4(%rsp),%rsi
mov    $0xffffffff,%edi
xor    %edx,%edx
call   1040 <waitpid@plt>
test   %eax,%eax
js     1148 <my_handler+0x28>
mov    0x4(%rsp),%edx
test   %dl,%dl
je     1161 <my_handler+0x41>
test   $0xff00,%edx
je     1176 <my_handler+0x56>
pop    %rax
ret
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
lea    0xea8(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
pop    %rax
ret
sar    $0x8,%edx
lea    0xeb7(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
pop    %rax
ret
mov    %edx,%r8d
and    $0x7f,%r8d
test   $0x80,%dl
lea    0xef3(%rip),%rdx
lea    0xedd(%rip),%rcx
cmove  %rdx,%rcx
lea    0xeaf(%rip),%rdi
mov    %eax,%esi
mov    %r8d,%edx
xor    %eax,%eax
call   1030 <printf@plt>
pop    %rax
ret

LLVM IR (-O0):
@.str = [34 x i8] c"Some error on waitpid errno = %d\0A\00"
@errno = global i32 0
@.str.1 = [38 x i8] c"Process %d was exited with status %d\0A\00"
@.str.2 = [35 x i8] c"Process %d killed by signal %d %s\0A\00"
@.str.3 = [15 x i8] c"with core file\00"
@.str.4 = [18 x i8] c"without core file\00"
define void @my_handler(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = call i32 @waitpid(i32 -1, i32* %3, i32 0)
store i32 %5, i32* %4
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @errno
%9 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %35
10: ; preds = %1
%11 = load i32, i32* %3
%12 = and i32 %11, 255
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %10
%15 = load i32, i32* %4
%16 = load i32, i32* %3
%17 = ashr i32 %16, 8
%18 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %17)
br label %34
19: ; preds = %10
%20 = load i32, i32* %3
%21 = and i32 %20, 65280
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %33
23: ; preds = %19
%24 = load i32, i32* %4
%25 = load i32, i32* %3
%26 = and i32 %25, 127
%27 = load i32, i32* %3
%28 = and i32 %27, 128
%29 = icmp ne i32 %28, 0
%30 = zext i1 %29 to i64
%31 = select i1 %29, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0)
%32 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %24, i32 %26, i8* %31)
br label %33
33: ; preds = %23, %19
br label %34
34: ; preds = %33, %14
br label %35
35: ; preds = %34, %7
ret void
}

LLVM IR (-O2):
@.str = [34 x i8] c"Some error on waitpid errno = %d\0A\00"
@errno = global i32 0
@.str.1 = [38 x i8] c"Process %d was exited with status %d\0A\00"
@.str.2 = [35 x i8] c"Process %d killed by signal %d %s\0A\00"
@.str.3 = [15 x i8] c"with core file\00"
@.str.4 = [18 x i8] c"without core file\00"
define void @my_handler(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i32 @waitpid(i32 -1, i32* %2, i32 0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = load i32, i32* @errno
%8 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %7)
br label %25
9: ; preds = %1
%10 = load i32, i32* %2
%11 = and i32 %10, 255
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = ashr i32 %10, 8
%15 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %4, i32 %14)
br label %25
16: ; preds = %9
%17 = and i32 %10, 65280
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %16
%20 = and i32 %10, 127
%21 = and i32 %10, 128
%22 = icmp eq i32 %21, 0
%23 = select i1 %22, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0)
%24 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %4, i32 %20, i8* %23)
br label %25
25: ; preds = %13, %19, %16, %6
ret void
}