#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 */
typedef int /*<<< orphan*/ sigset_t;

/* Variables and functions */
int /*<<< orphan*/ SIGKILL;
int /*<<< orphan*/ SIGSTOP;
int /*<<< orphan*/ sigdelset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigfillset(int /*<<< orphan*/ *);
int sigwait(int /*<<< orphan*/ *, int *);
int signal_thread(int (*signal_handler)(int)) {
  sigset_t sig_mask;
  int err, sig;
  sigfillset(&sig_mask);
  sigdelset(&sig_mask, SIGKILL);
  sigdelset(&sig_mask, SIGSTOP);
  for (;;) {
    err = sigwait(&sig_mask, &sig);
    if (err) {
      break;
    }
    if (signal_handler(sig)) {
      break;
    }
  }
  return err;
}


x86 assembly (clang, -O0):
<signal_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0xc(%rbp),%rdi
call   1030 <sigfillset@plt>
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rdi
call   1040 <sigdelset@plt>
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rdi
call   1040 <sigdelset@plt>
lea    -0xc(%rbp),%rdi
lea    -0x14(%rbp),%rsi
call   1050 <sigwait@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1188 <signal_thread+0x58>
jmp    11a4 <signal_thread+0x74>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%edi
call   *%rax
cmp    $0x0,%eax
je     119f <signal_thread+0x6f>
jmp    11a4 <signal_thread+0x74>
jmp    1169 <signal_thread+0x39>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<signal_thread>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0x4(%rsp),%r14
mov    %r14,%rdi
call   1030 <sigfillset@plt>
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1040 <sigdelset@plt>
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1040 <sigdelset@plt>
mov    %rsp,%r15
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <sigwait@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    118a <signal_thread+0x5a>
mov    (%rsp),%edi
call   *%rbx
test   %eax,%eax
je     1170 <signal_thread+0x40>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SIGKILL = global i32 0
@SIGSTOP = global i32 0
define i32 @signal_thread(i32 (i32)* %0) {
%2 = alloca i32 (i32)*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 (i32)* %0, i32 (i32)** %2
%6 = call i32 @sigfillset(i32* %3)
%7 = load i32, i32* @SIGKILL
%8 = call i32 @sigdelset(i32* %3, i32 %7)
%9 = load i32, i32* @SIGSTOP
%10 = call i32 @sigdelset(i32* %3, i32 %9)
br label %11
11: ; preds = %22, %1
%12 = call i32 @sigwait(i32* %3, i32* %5)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %11
br label %23
16: ; preds = %11
%17 = load i32 (i32)*, i32 (i32)** %2
%18 = load i32, i32* %5
%19 = call i32 %17(i32 %18)
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %16
br label %23
22: ; preds = %16
br label %11
23: ; preds = %21, %15
%24 = load i32, i32* %4
ret i32 %24
}

LLVM IR (-O2):
@SIGKILL = global i32 0
@SIGSTOP = global i32 0
define i32 @signal_thread(i32 (i32)* nocapture %0) {
%2 = alloca i32
%3 = alloca i32
%4 = bitcast i32* %2 to i8*
%5 = bitcast i32* %3 to i8*
%6 = call i32 @sigfillset(i32* %2)
%7 = load i32, i32* @SIGKILL
%8 = call i32 @sigdelset(i32* %2, i32 %7)
%9 = load i32, i32* @SIGSTOP
%10 = call i32 @sigdelset(i32* %2, i32 %9)
br label %11
11: ; preds = %14, %1
%12 = call i32 @sigwait(i32* %2, i32* %3)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* %3
%16 = call i32 %0(i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %11, label %18
18: ; preds = %14, %11
ret i32 %12
}