#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  int si_signo;
  int si_code;
  int si_pid;
};
typedef TYPE_1__ siginfo_t;

/* Variables and functions */
scalar_t__ SIGCHLD;
scalar_t__ SIGTERM;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, ...);
int getpid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait(int *);
void sig_handler(int arg, siginfo_t *info, void *data) {
  int err;
  fprintf(stderr, "signal, signo=%d code=%d pid=%d\n", info->si_signo,
          info->si_code, info->si_pid);
  if (info->si_signo == SIGCHLD)
    wait(&err);
  else if (info->si_signo == SIGTERM) {
    fprintf(stderr, "terminate, pid=%d\n", getpid());
    exit(1);
  }
}


x86 assembly (clang, -O0):
<sig_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%ecx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%r8d
lea    0xe88(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rax
movslq (%rax),%rax
mov    0x2e53(%rip),%rcx
cmp    (%rcx),%rax
jne    11a4 <sig_handler+0x64>
lea    -0x1c(%rbp),%rdi
call   1060 <wait@plt>
jmp    11f0 <sig_handler+0xb0>
mov    -0x10(%rbp),%rax
movslq (%rax),%rax
mov    0x2e36(%rip),%rcx
cmp    (%rcx),%rax
jne    11eb <sig_handler+0xab>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x20(%rbp),%edi
mov    %eax,%edx
lea    0xe47(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
jmp    11f0 <sig_handler+0xb0>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sig_handler>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    0x2e82(%rip),%r14
mov    (%r14),%edi
mov    (%rsi),%edx
mov    0x4(%rsi),%ecx
mov    0x8(%rsi),%r8d
lea    0xe9f(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
movslq (%rbx),%rax
mov    0x2e6e(%rip),%rcx
cmp    %rax,(%rcx)
jne    1183 <sig_handler+0x43>
lea    0x4(%rsp),%rdi
call   1060 <wait@plt>
jmp    118f <sig_handler+0x4f>
mov    0x2e5e(%rip),%rcx
cmp    %rax,(%rcx)
je     1197 <sig_handler+0x57>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    (%r14),%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe79(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i32, i32 }
@stderr = global i32 0
@.str = [33 x i8] c"signal, signo=%d code=%d pid=%d\0A\00"
@SIGCHLD = global i64 0
@SIGTERM = global i64 0
@.str.1 = [19 x i8] c"terminate, pid=%d\0A\00"
define void @sig_handler(i32 %0, %struct.TYPE_3__* %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.TYPE_3__*
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %4
store %struct.TYPE_3__* %1, %struct.TYPE_3__** %5
store i8* %2, i8** %6
%8 = load i32, i32* @stderr
%9 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%10 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %9, i32 0, i32 0
%11 = load i32, i32* %10
%12 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%13 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %12, i32 0, i32 1
%14 = load i32, i32* %13
%15 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%16 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %15, i32 0, i32 2
%17 = load i32, i32* %16
%18 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %14, i32 %17)
%19 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%20 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = sext i32 %21 to i64
%23 = load i64, i64* @SIGCHLD
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %3
%26 = call i32 @wait(i32* %7)
br label %40
27: ; preds = %3
%28 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%29 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = sext i32 %30 to i64
%32 = load i64, i64* @SIGTERM
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %39
34: ; preds = %27
%35 = load i32, i32* @stderr
%36 = call i32 (...) @getpid()
%37 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %36)
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %27
br label %40
40: ; preds = %39, %25
ret void
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32, i32 }
@stderr = global i32 0
@.str = [33 x i8] c"signal, signo=%d code=%d pid=%d\0A\00"
@SIGCHLD = global i64 0
@SIGTERM = global i64 0
@.str.1 = [19 x i8] c"terminate, pid=%d\0A\00"
define void @sig_handler(i32 %0, %struct.TYPE_3__* nocapture %1, i8* nocapture readnone %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = load i32, i32* @stderr
%7 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 0
%8 = load i32, i32* %7
%9 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 1
%10 = load i32, i32* %9
%11 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 2
%12 = load i32, i32* %11
%13 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %10, i32 %12)
%14 = load i32, i32* %7
%15 = sext i32 %14 to i64
%16 = load i64, i64* @SIGCHLD
%17 = icmp eq i64 %16, %15
br i1 %17, label %18, label %20
18: ; preds = %3
%19 = call i32 @wait(i32* %4)
br label %28
20: ; preds = %3
%21 = load i64, i64* @SIGTERM
%22 = icmp eq i64 %21, %15
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = tail call i32 (...) @getpid()
%26 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %20, %18
ret void
}