#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_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;
  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    0x2e7b(%rip),%rax
mov    (%rax),%eax
mov    0x2e82(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e7e(%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    1190 <install_handler+0x60>
lea    0xe75(%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    0x2e88(%rip),%rax
mov    0x2e91(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ebx,0x4(%rsp)
mov    %ecx,(%rsp)
mov    0x2e87(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
xor    %edx,%edx
call   1030 <sigaction@plt>
cmp    $0xffffffff,%eax
je     1178 <install_handler+0x48>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe81(%rip),%rdi
call   1050 <perror@plt>
jmp    1172 <install_handler+0x42>

LLVM IR (-O0):
%struct.sigaction = type { i32, i32, i32 }
@SA_SIGINFO = global i32 0
@SA_NOCLDSTOP = 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* @SIGCHLD
%14 = call i32 @sigaction(i32 %13, %struct.sigaction* %3, i32 0)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %1
%18 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %1
ret void
}

LLVM IR (-O2):
%struct.sigaction = type { i32, i32, i32 }
@SA_SIGINFO = global i32 0
@SA_NOCLDSTOP = 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
store i32 %9, i32* %10
%11 = load i32, i32* @SIGCHLD
%12 = call i32 @sigaction(i32 %11, %struct.sigaction* %2, i32 0)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %1
ret void
}