#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 {
  scalar_t__ sa_flags;
  int /*<<< orphan*/ sa_mask;
  int /*<<< orphan*/ sa_handler;
};
typedef int /*<<< orphan*/ sigset_t;

/* Variables and functions */
int /*<<< orphan*/ SIGTRAP;
int /*<<< orphan*/ SIG_BLOCK;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ SIG_UNBLOCK;
int /*<<< orphan*/ execl(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ pause();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sigaction(int /*<<< orphan*/, struct sigaction *,
                             int /*<<< orphan*/ *);
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  sigset_t ss;
  struct sigaction sa;
  fprintf(stderr, "starting\n");
  sleep(5);
  fprintf(stderr, "blocking SIGTRAP\n");
  sigemptyset(&ss);
  sigaddset(&ss, SIGTRAP);
  if (-1 == sigprocmask(SIG_BLOCK, &ss, 0))
    perror("block failed");
  sleep(5);
  fprintf(stderr, "unblocking SIGTRAP\n");
  if (-1 == sigprocmask(SIG_UNBLOCK, &ss, 0))
    perror("unblock failed");
  sleep(5);
  fprintf(stderr, "ignoring SIGTRAP\n");
  sa.sa_handler = SIG_IGN;
  sigemptyset(&sa.sa_mask);
  sa.sa_flags = 0;
  sigaction(SIGTRAP, &sa, NULL);
  pause();
  fprintf(stderr, "doing exec\n");
  fprintf(stderr, "before exec\n");
  execl("test/trapkid", "trapkid", 0);
  perror("badexec");
  fprintf(stderr, "after exec\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
lea    0xe4a(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x5,%edi
call   10a0 <sleep@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    0xe37(%rip),%rsi
call   1050 <fprintf@plt>
lea    -0x14(%rbp),%rdi
call   1060 <sigemptyset@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
lea    -0x14(%rbp),%rdi
call   10b0 <sigaddset@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1224 <main+0x94>
lea    0xe01(%rip),%rdi
call   1080 <perror@plt>
mov    $0x5,%edi
call   10a0 <sleep@plt>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
lea    0xdcc(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1272 <main+0xe2>
lea    0xdb1(%rip),%rdi
call   1080 <perror@plt>
mov    $0x5,%edi
call   10a0 <sleep@plt>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
lea    0xda1(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d48(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x28(%rbp),%rdi
add    $0x8,%rdi
call   1060 <sigemptyset@plt>
movq   $0x0,-0x28(%rbp)
mov    0x2d07(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sigaction@plt>
mov    $0x0,%al
call   1070 <pause@plt>
mov    0x2cfa(%rip),%rax
mov    (%rax),%edi
lea    0xd60(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2ce5(%rip),%rax
mov    (%rax),%edi
lea    0xd57(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xd58(%rip),%rdi
lea    0xd56(%rip),%rsi
xor    %edx,%edx
call   1090 <execl@plt>
lea    0xd50(%rip),%rdi
call   1080 <perror@plt>
mov    0x2caf(%rip),%rax
mov    (%rax),%edi
lea    0xd43(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2e30(%rip),%r14
mov    (%r14),%edi
lea    0xe56(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x5,%edi
call   10a0 <sleep@plt>
mov    (%r14),%edi
lea    0xe49(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <sigemptyset@plt>
mov    0x2de4(%rip),%r15
mov    (%r15),%esi
mov    %rbx,%rdi
call   10b0 <sigaddset@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
cmp    $0xffffffff,%eax
jne    120b <main+0x7b>
lea    0xe1a(%rip),%rdi
call   1080 <perror@plt>
mov    $0x5,%edi
call   10a0 <sleep@plt>
mov    (%r14),%edi
lea    0xdeb(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
cmp    $0xffffffff,%eax
jne    124a <main+0xba>
lea    0xdd9(%rip),%rdi
call   1080 <perror@plt>
mov    $0x5,%edi
call   10a0 <sleep@plt>
mov    (%r14),%edi
lea    0xdcf(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0x18(%rsp),%rdi
call   1060 <sigemptyset@plt>
movq   $0x0,0x10(%rsp)
mov    (%r15),%edi
lea    0x10(%rsp),%rsi
xor    %edx,%edx
call   1040 <sigaction@plt>
xor    %eax,%eax
call   1070 <pause@plt>
mov    (%r14),%edi
lea    0xd9c(%rip),%rsi
call   1050 <fprintf@plt>
mov    (%r14),%edi
lea    0xd99(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xd9a(%rip),%rdi
lea    0xd98(%rip),%rsi
xor    %edx,%edx
call   1090 <execl@plt>
lea    0xd92(%rip),%rdi
call   1080 <perror@plt>
mov    (%r14),%edi
lea    0xd8b(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sigaction = type { i64, i32, i32 }
@stderr = global i32 0
@.str = [10 x i8] c"starting\0A\00"
@.str.1 = [18 x i8] c"blocking SIGTRAP\0A\00"
@SIGTRAP = global i32 0
@SIG_BLOCK = global i32 0
@.str.2 = [13 x i8] c"block failed\00"
@.str.3 = [20 x i8] c"unblocking SIGTRAP\0A\00"
@SIG_UNBLOCK = global i32 0
@.str.4 = [15 x i8] c"unblock failed\00"
@.str.5 = [18 x i8] c"ignoring SIGTRAP\0A\00"
@SIG_IGN = global i32 0
@.str.6 = [12 x i8] c"doing exec\0A\00"
@.str.7 = [13 x i8] c"before exec\0A\00"
@.str.8 = [13 x i8] c"test/trapkid\00"
@.str.9 = [8 x i8] c"trapkid\00"
@.str.10 = [8 x i8] c"badexec\00"
@.str.11 = [12 x i8] c"after exec\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sigaction
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* @stderr
%9 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @sleep(i32 5)
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @sigemptyset(i32* %6)
%14 = load i32, i32* @SIGTRAP
%15 = call i32 @sigaddset(i32* %6, i32 %14)
%16 = load i32, i32* @SIG_BLOCK
%17 = call i32 @sigprocmask(i32 %16, i32* %6, i32 0)
%18 = icmp eq i32 -1, %17
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %2
%22 = call i32 @sleep(i32 5)
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%25 = load i32, i32* @SIG_UNBLOCK
%26 = call i32 @sigprocmask(i32 %25, i32* %6, i32 0)
%27 = icmp eq i32 -1, %26
br i1 %27, label %28, label %30
28: ; preds = %21
%29 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %30
30: ; preds = %28, %21
%31 = call i32 @sleep(i32 5)
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%34 = load i32, i32* @SIG_IGN
%35 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 2
store i32 %34, i32* %35
%36 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 1
%37 = call i32 @sigemptyset(i32* %36)
%38 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 0
store i64 0, i64* %38
%39 = load i32, i32* @SIGTRAP
%40 = call i32 @sigaction(i32 %39, %struct.sigaction* %7, i32* null)
%41 = call i32 (...) @pause()
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%44 = load i32, i32* @stderr
%45 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%46 = call i32 @execl(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0), i32 0)
%47 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.10, i64 0, i64 0))
%48 = load i32, i32* @stderr
%49 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.11, i64 0, i64 0))
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
%struct.sigaction = type { i64, i32, i32 }
@stderr = global i32 0
@.str = [10 x i8] c"starting\0A\00"
@.str.1 = [18 x i8] c"blocking SIGTRAP\0A\00"
@SIGTRAP = global i32 0
@SIG_BLOCK = global i32 0
@.str.2 = [13 x i8] c"block failed\00"
@.str.3 = [20 x i8] c"unblocking SIGTRAP\0A\00"
@SIG_UNBLOCK = global i32 0
@.str.4 = [15 x i8] c"unblock failed\00"
@.str.5 = [18 x i8] c"ignoring SIGTRAP\0A\00"
@SIG_IGN = global i32 0
@.str.6 = [12 x i8] c"doing exec\0A\00"
@.str.7 = [13 x i8] c"before exec\0A\00"
@.str.8 = [13 x i8] c"test/trapkid\00"
@.str.9 = [8 x i8] c"trapkid\00"
@.str.10 = [8 x i8] c"badexec\00"
@.str.11 = [12 x i8] c"after exec\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca %struct.sigaction
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.sigaction* %4 to i8*
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @sleep(i32 5)
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @sigemptyset(i32* %3)
%13 = load i32, i32* @SIGTRAP
%14 = call i32 @sigaddset(i32* %3, i32 %13)
%15 = load i32, i32* @SIG_BLOCK
%16 = call i32 @sigprocmask(i32 %15, i32* %3, i32 0)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %18, %2
%21 = call i32 @sleep(i32 5)
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%24 = load i32, i32* @SIG_UNBLOCK
%25 = call i32 @sigprocmask(i32 %24, i32* %3, i32 0)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %20
%28 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %29
29: ; preds = %27, %20
%30 = call i32 @sleep(i32 5)
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%33 = load i32, i32* @SIG_IGN
%34 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 2
store i32 %33, i32* %34
%35 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 1
%36 = call i32 @sigemptyset(i32* %35)
%37 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 0
store i64 0, i64* %37
%38 = load i32, i32* @SIGTRAP
%39 = call i32 @sigaction(i32 %38, %struct.sigaction* %4, i32* null)
%40 = call i32 (...) @pause()
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%43 = load i32, i32* @stderr
%44 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%45 = call i32 @execl(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0), i32 0)
%46 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.10, i64 0, i64 0))
%47 = load i32, i32* @stderr
%48 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.11, i64 0, i64 0))
ret i32 0
}