#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_pid;
  scalar_t__ si_code;
  int si_signo;
};
typedef TYPE_1__ siginfo_t;
typedef int pid_t;

/* Variables and functions */
scalar_t__ CLD_EXITED;
int SIGCHLD;
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int waitpid(int, int *, int /*<<< orphan*/);
void sigchild_handler(int sig, siginfo_t *sip,
                      void __attribute__((unused)) * extra) {
  pid_t child = sip->si_pid;
  int status;
  if (sig != SIGCHLD) {
    fprintf(stderr,
            "sigchild_handler() ble calt med noe som ikke var SIGCHLD (%d), "
            "men sig %d\n",
            SIGCHLD, sig);
    fprintf(stderr, "signal stacken må ha blirr korypt. Exiter.\n");
    exit(-1);
  }
  if (sip->si_code == CLD_EXITED) {
    printf("Got signal '%d', si_code %d"
           " for child %d\n",
           (sip->si_signo), sip->si_code, child);
    if (waitpid(-1, &status, 0) == -1) {
      perror("waitpid()");
    }
  } else {
    printf("Got signal '%d', si_code %d"
           " for child %d: ",
           sip->si_signo, sip->si_code, child);
    if (waitpid(-1, &status, 0) == -1) {
      perror("waitpid()");
    } else {
      if (WIFEXITED(status)) {
        printf("Exited normally\n");
      } else if (WIFSIGNALED(status)) {
        printf("Child was signaled: %d(%d)\n", WTERMSIG(status),
               WTERMSIG(status));
      } else {
        printf("Unknown exit\n");
      }
    }
  }
}


x86 assembly (clang, -O0):
<sigchild_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%eax
mov    0x2e42(%rip),%rcx
cmp    (%rcx),%eax
je     11f2 <sigchild_handler+0x72>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
mov    -0x4(%rbp),%ecx
lea    0xe36(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
lea    0xe6a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2dcf(%rip),%rcx
cmp    (%rcx),%rax
jne    1254 <sigchild_handler+0xd4>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
mov    -0x1c(%rbp),%ecx
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xffffffff,%edi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    124f <sigchild_handler+0xcf>
lea    0xe58(%rip),%rdi
call   1080 <perror@plt>
jmp    131d <sigchild_handler+0x19d>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
mov    -0x1c(%rbp),%ecx
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xffffffff,%edi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    129e <sigchild_handler+0x11e>
lea    0xe0e(%rip),%rdi
call   1080 <perror@plt>
jmp    1318 <sigchild_handler+0x198>
mov    -0x20(%rbp),%edi
call   1090 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12c3 <sigchild_handler+0x143>
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1313 <sigchild_handler+0x193>
mov    -0x20(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1300 <sigchild_handler+0x180>
mov    -0x20(%rbp),%edi
call   1060 <WTERMSIG@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%edi
call   1060 <WTERMSIG@plt>
mov    -0x24(%rbp),%esi
mov    %eax,%edx
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    130e <sigchild_handler+0x18e>
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1313 <sigchild_handler+0x193>
jmp    1318 <sigchild_handler+0x198>
jmp    131d <sigchild_handler+0x19d>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sigchild_handler>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e4c(%rip),%rax
mov    (%rax),%edx
cmp    %edi,%edx
jne    1276 <sigchild_handler+0xe6>
mov    (%rsi),%ecx
mov    0x8(%rsi),%rdx
mov    0x2e1d(%rip),%rax
mov    0x10(%rsi),%esi
cmp    (%rax),%rdx
jne    11e5 <sigchild_handler+0x55>
lea    0xeb6(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xc(%rsp),%rsi
mov    $0xffffffff,%edi
xor    %edx,%edx
call   1080 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    1270 <sigchild_handler+0xe0>
jmp    1264 <sigchild_handler+0xd4>
lea    0xec0(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xc(%rsp),%rsi
mov    $0xffffffff,%edi
xor    %edx,%edx
call   1080 <waitpid@plt>
cmp    $0xffffffff,%eax
je     1264 <sigchild_handler+0xd4>
mov    0xc(%rsp),%edi
call   10a0 <WIFEXITED@plt>
test   %rax,%rax
je     1225 <sigchild_handler+0x95>
lea    0xee2(%rip),%rdi
call   1030 <puts@plt>
jmp    1270 <sigchild_handler+0xe0>
mov    0xc(%rsp),%edi
call   1050 <WIFSIGNALED@plt>
test   %rax,%rax
je     125b <sigchild_handler+0xcb>
mov    0xc(%rsp),%edi
call   1070 <WTERMSIG@plt>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
call   1070 <WTERMSIG@plt>
lea    0xe89(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1270 <sigchild_handler+0xe0>
lea    0xe91(%rip),%rdi
jmp    121e <sigchild_handler+0x8e>
lea    0xe37(%rip),%rdi
call   1090 <perror@plt>
add    $0x10,%rsp
pop    %rbx
ret
mov    %edi,%ecx
mov    0x2d59(%rip),%rbx
mov    (%rbx),%edi
lea    0xd78(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%rbx),%edi
lea    0xdb3(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i64, i32 }
@SIGCHLD = global i32 0
@stderr = global i32 0
@.str = [75 x i8] c"sigchild_handler() ble calt med noe som ikke var SIGCHLD (%d), men sig %d\0A\00"
@.str.1 = [45 x i8] c"signal stacken m\C3\A5 ha blirr korypt. Exiter.\0A\00"
@CLD_EXITED = global i64 0
@.str.2 = [42 x i8] c"Got signal '%d', si_code %d for child %d\0A\00"
@.str.3 = [10 x i8] c"waitpid()\00"
@.str.4 = [43 x i8] c"Got signal '%d', si_code %d for child %d: \00"
@.str.5 = [17 x i8] c"Exited normally\0A\00"
@.str.6 = [28 x i8] c"Child was signaled: %d(%d)\0A\00"
@.str.7 = [14 x i8] c"Unknown exit\0A\00"
define void @sigchild_handler(i32 %0, %struct.TYPE_3__* %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.TYPE_3__*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store %struct.TYPE_3__* %1, %struct.TYPE_3__** %5
store i8* %2, i8** %6
%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
store i32 %11, i32* %7
%12 = load i32, i32* %4
%13 = load i32, i32* @SIGCHLD
%14 = icmp ne i32 %12, %13
br i1 %14, label %15, label %23
15: ; preds = %3
%16 = load i32, i32* @stderr
%17 = load i32, i32* @SIGCHLD
%18 = load i32, i32* %4
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str, i64 0, i64 0), i32 %17, i32 %18)
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %3
%24 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%25 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %24, i32 0, i32 1
%26 = load i64, i64* %25
%27 = load i64, i64* @CLD_EXITED
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %43
29: ; preds = %23
%30 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%31 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %30, i32 0, i32 2
%32 = load i32, i32* %31
%33 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%34 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %33, i32 0, i32 1
%35 = load i64, i64* %34
%36 = load i32, i32* %7
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 %32, i64 %35, i32 %36)
%38 = call i32 @waitpid(i32 -1, i32* %8, i32 0)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %29
%41 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %40, %29
br label %77
43: ; preds = %23
%44 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%45 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %44, i32 0, i32 2
%46 = load i32, i32* %45
%47 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%48 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %47, i32 0, i32 1
%49 = load i64, i64* %48
%50 = load i32, i32* %7
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i32 %46, i64 %49, i32 %50)
%52 = call i32 @waitpid(i32 -1, i32* %8, i32 0)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %56
54: ; preds = %43
%55 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %76
56: ; preds = %43
%57 = load i32, i32* %8
%58 = call i64 @WIFEXITED(i32 %57)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %56
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %75
62: ; preds = %56
%63 = load i32, i32* %8
%64 = call i64 @WIFSIGNALED(i32 %63)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %72
66: ; preds = %62
%67 = load i32, i32* %8
%68 = call i32 @WTERMSIG(i32 %67)
%69 = load i32, i32* %8
%70 = call i32 @WTERMSIG(i32 %69)
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i32 %68, i32 %70)
br label %74
72: ; preds = %62
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
br label %74
74: ; preds = %72, %66
br label %75
75: ; preds = %74, %60
br label %76
76: ; preds = %75, %54
br label %77
77: ; preds = %76, %42
ret void
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i64, i32 }
@SIGCHLD = global i32 0
@stderr = global i32 0
@.str = [75 x i8] c"sigchild_handler() ble calt med noe som ikke var SIGCHLD (%d), men sig %d\0A\00"
@.str.1 = [45 x i8] c"signal stacken m\C3\A5 ha blirr korypt. Exiter.\0A\00"
@CLD_EXITED = global i64 0
@.str.2 = [42 x i8] c"Got signal '%d', si_code %d for child %d\0A\00"
@.str.3 = [10 x i8] c"waitpid()\00"
@.str.4 = [43 x i8] c"Got signal '%d', si_code %d for child %d: \00"
@.str.6 = [28 x i8] c"Child was signaled: %d(%d)\0A\00"
@str = [13 x i8] c"Unknown exit\00"
@str.8 = [16 x i8] c"Exited normally\00"
define void @sigchild_handler(i32 %0, %struct.TYPE_3__* nocapture %1, i8* nocapture readnone %2) {
%4 = alloca i32
%5 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 0
%6 = load i32, i32* %5
%7 = bitcast i32* %4 to i8*
%8 = load i32, i32* @SIGCHLD
%9 = icmp eq i32 %8, %0
br i1 %9, label %16, label %10
10: ; preds = %3
%11 = load i32, i32* @stderr
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %0)
%13 = load i32, i32* @stderr
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @exit(i32 -1)
unreachable
16: ; preds = %3
%17 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 1
%18 = load i64, i64* %17
%19 = load i64, i64* @CLD_EXITED
%20 = icmp eq i64 %18, %19
%21 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 2
%22 = load i32, i32* %21
br i1 %20, label %23, label %29
23: ; preds = %16
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 %22, i64 %18, i32 %6)
%25 = call i32 @waitpid(i32 -1, i32* %4, i32 0)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %53
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %53
29: ; preds = %16
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i32 %22, i64 %18, i32 %6)
%31 = call i32 @waitpid(i32 -1, i32* %4, i32 0)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %53
35: ; preds = %29
%36 = load i32, i32* %4
%37 = call i64 @WIFEXITED(i32 %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %35
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.8, i64 0, i64 0))
br label %53
41: ; preds = %35
%42 = load i32, i32* %4
%43 = call i64 @WIFSIGNALED(i32 %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %51, label %45
45: ; preds = %41
%46 = load i32, i32* %4
%47 = call i32 @WTERMSIG(i32 %46)
%48 = load i32, i32* %4
%49 = call i32 @WTERMSIG(i32 %48)
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i32 %47, i32 %49)
br label %53
51: ; preds = %41
%52 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %53
53: ; preds = %33, %45, %51, %39, %23, %27
ret void
}