#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 */

/* Variables and functions */
int /*<<< orphan*/ WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ log_err(char *);
int /*<<< orphan*/ log_msg(char *, int, int /*<<< orphan*/);
int waitpid(int, int *, int /*<<< orphan*/);
void sigchld_handler(int sig) {
  int status;
  int pid;
  sig = 0;
  if ((pid = waitpid(-1, &status, WNOHANG)) < 0) {
    log_err("[s] waitpid failed\n");
    return;
  }
  if (WIFEXITED(status)) {
    log_msg("[s] process %i exited, status %i\n", pid, WEXITSTATUS(status));
  }
}


x86 assembly (clang, -O0):
<sigchld_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x4(%rbp)
mov    0x2e7f(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
lea    -0x8(%rbp),%rsi
call   1050 <waitpid@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1196 <sigchld_handler+0x46>
lea    0xe74(%rip),%rdi
call   1070 <log_err@plt>
jmp    11c7 <sigchld_handler+0x77>
mov    -0x8(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11c7 <sigchld_handler+0x77>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    -0x10(%rbp),%esi
mov    %eax,%edx
lea    0xe52(%rip),%rdi
call   1030 <log_msg@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sigchld_handler>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e8c(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rsi
mov    $0xffffffff,%edi
call   1050 <waitpid@plt>
test   %eax,%eax
js     119c <sigchld_handler+0x4c>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %rax,%rax
je     11a8 <sigchld_handler+0x58>
mov    0xc(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
lea    0xe83(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1030 <log_msg@plt>
jmp    11a8 <sigchld_handler+0x58>
lea    0xe5d(%rip),%rdi
call   1070 <log_err@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@WNOHANG = global i32 0
@.str = [20 x i8] c"[s] waitpid failed\0A\00"
@.str.1 = [34 x i8] c"[s] process %i exited, status %i\0A\00"
define void @sigchld_handler(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %2
%5 = load i32, i32* @WNOHANG
%6 = call i32 @waitpid(i32 -1, i32* %3, i32 %5)
store i32 %6, i32* %4
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @log_err(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %19
10: ; preds = %1
%11 = load i32, i32* %3
%12 = call i64 @WIFEXITED(i32 %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %10
%15 = load i32, i32* %4
%16 = load i32, i32* %3
%17 = call i32 @WEXITSTATUS(i32 %16)
%18 = call i32 @log_msg(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %17)
br label %19
19: ; preds = %8, %14, %10
ret void
}

LLVM IR (-O2):
@WNOHANG = global i32 0
@.str = [20 x i8] c"[s] waitpid failed\0A\00"
@.str.1 = [34 x i8] c"[s] process %i exited, status %i\0A\00"
define void @sigchld_handler(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32, i32* @WNOHANG
%5 = call i32 @waitpid(i32 -1, i32* %2, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 @log_err(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %17
9: ; preds = %1
%10 = load i32, i32* %2
%11 = call i64 @WIFEXITED(i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %9
%14 = load i32, i32* %2
%15 = call i32 @WEXITSTATUS(i32 %14)
%16 = call i32 @log_msg(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %5, i32 %15)
br label %17
17: ; preds = %9, %13, %7
ret void
}