#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 */
struct sigaction {
  int sa_flags;
  int /*<<< orphan*/ sa_sigaction;
  int /*<<< orphan*/ sa_mask;
};
typedef int /*<<< orphan*/ __sighandler_t;

/* Variables and functions */
int SA_NOCLDSTOP;
int SA_NOCLDWAIT;
int SA_SIGINFO;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ perror(char *);
int sigaction(int /*<<< orphan*/, struct sigaction *, int /*<<< orphan*/);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
void install_handler(__sighandler_t h) {
  struct sigaction act;
  sigemptyset(&act.sa_mask);
  act.sa_sigaction = h;
  act.sa_flags = SA_SIGINFO | SA_NOCLDSTOP;
  act.sa_flags |= SA_NOCLDWAIT;
  int ret = sigaction(SIGCHLD, &act, 0);
  if (ret == -1)
    perror("sigaction error");
}


x86 assembly (clang, -O0):
<install_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x10(%rbp),%rdi
add    $0x8,%rdi
call   1040 <sigemptyset@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
mov    0x2e7a(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1030 <sigaction@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    119f <install_handler+0x6f>
lea    0xe66(%rip),%rdi
call   1050 <perror@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<install_handler>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0x8(%rsp),%rdi
call   1040 <sigemptyset@plt>
mov    0x2e80(%rip),%rax
mov    0x2e89(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e8e(%rip),%rax
or     (%rax),%ecx
mov    %ebx,0x4(%rsp)
mov    %ecx,(%rsp)
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
xor    %edx,%edx
call   1030 <sigaction@plt>
cmp    $0xffffffff,%eax
je     1181 <install_handler+0x51>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe78(%rip),%rdi
call   1050 <perror@plt>
jmp    117b <install_handler+0x4b>

LLVM IR (-O0):
%struct.sigaction = type { i32, i32, i32 }
@SA_SIGINFO = global i32 0
@SA_NOCLDSTOP = global i32 0
@SA_NOCLDWAIT = global i32 0
@SIGCHLD = global i32 0
@.str = [16 x i8] c"sigaction error\00"
define void @install_handler(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sigaction
%4 = alloca i32
store i32 %0, i32* %2
%5 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i32 0, i32 2
%6 = call i32 @sigemptyset(i32* %5)
%7 = load i32, i32* %2
%8 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i32 0, i32 1
store i32 %7, i32* %8
%9 = load i32, i32* @SA_SIGINFO
%10 = load i32, i32* @SA_NOCLDSTOP
%11 = or i32 %9, %10
%12 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i32 0, i32 0
store i32 %11, i32* %12
%13 = load i32, i32* @SA_NOCLDWAIT
%14 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i32 0, i32 0
%15 = load i32, i32* %14
%16 = or i32 %15, %13
store i32 %16, i32* %14
%17 = load i32, i32* @SIGCHLD
%18 = call i32 @sigaction(i32 %17, %struct.sigaction* %3, i32 0)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %1
%22 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %1
ret void
}

LLVM IR (-O2):
%struct.sigaction = type { i32, i32, i32 }
@SA_SIGINFO = global i32 0
@SA_NOCLDSTOP = global i32 0
@SA_NOCLDWAIT = global i32 0
@SIGCHLD = global i32 0
@.str = [16 x i8] c"sigaction error\00"
define void @install_handler(i32 %0) {
%2 = alloca %struct.sigaction
%3 = bitcast %struct.sigaction* %2 to i8*
%4 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %2, i64 0, i32 2
%5 = call i32 @sigemptyset(i32* %4)
%6 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %2, i64 0, i32 1
store i32 %0, i32* %6
%7 = load i32, i32* @SA_SIGINFO
%8 = load i32, i32* @SA_NOCLDSTOP
%9 = or i32 %8, %7
%10 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %2, i64 0, i32 0
%11 = load i32, i32* @SA_NOCLDWAIT
%12 = or i32 %11, %9
store i32 %12, i32* %10
%13 = load i32, i32* @SIGCHLD
%14 = call i32 @sigaction(i32 %13, %struct.sigaction* %2, i32 0)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %1
%17 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %1
ret void
}