#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_7__ TYPE_1__;

/* Type definitions */
struct TYPE_7__ {
  scalar_t__ si_pid;
  struct TYPE_7__ *si_signo;
};
typedef TYPE_1__ siginfo_t;
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ PTRACE_CONT;
int /*<<< orphan*/ PTRACE_GETSIGINFO;
int /*<<< orphan*/ PTRACE_LISTEN;
TYPE_1__ *SIGCONT;
TYPE_1__ *SIGSTOP;
TYPE_1__ *SIGTRAP;
int /*<<< orphan*/ WCONTINUED;
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int /*<<< orphan*/ WUNTRACED;
int /*<<< orphan*/ fatal(char *);
int /*<<< orphan*/ output_signal(TYPE_1__ *);
scalar_t__ ptrace(int /*<<< orphan*/, scalar_t__, int /*<<< orphan*/ *,
                  TYPE_1__ *);
int waitpid(scalar_t__, int *, int /*<<< orphan*/);
int signal_trap(pid_t pid, int *status) {
  siginfo_t siginfo;
  if (ptrace(PTRACE_GETSIGINFO, pid, NULL, &siginfo))
    fatal("PTRACE_GETSIGINFO failed");
  if (!(siginfo.si_signo == SIGTRAP && siginfo.si_pid == pid))
    output_signal(&siginfo);
  if (siginfo.si_signo != SIGCONT) {
    if (ptrace(PTRACE_CONT, pid, NULL, siginfo.si_signo))
      fatal("PTRACE_CONT failed");
  }
  if (siginfo.si_signo == SIGSTOP) {
    if (waitpid(pid, status, WUNTRACED) == -1)
      fatal("waitpid failed");
    if (ptrace(PTRACE_LISTEN, pid, NULL, NULL))
      fatal("PTRACE_LISTEN failed");
    if (waitpid(pid, status, WCONTINUED) == -1)
      fatal("waitpid failed");
  }
  return (WIFEXITED(*status) || WIFSIGNALED(*status));
}


x86 assembly (clang, -O0):
<signal_trap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x20(%rbp),%rcx
call   1040 <ptrace@plt>
cmp    $0x0,%rax
je     11a0 <signal_trap+0x40>
lea    0xe65(%rip),%rdi
call   1070 <fatal@plt>
mov    -0x18(%rbp),%rax
mov    0x2e1d(%rip),%rcx
cmp    (%rcx),%rax
jne    11c2 <signal_trap+0x62>
mov    -0x20(%rbp),%rax
cmp    -0x8(%rbp),%rax
je     11cb <signal_trap+0x6b>
lea    -0x20(%rbp),%rdi
call   1080 <output_signal@plt>
mov    -0x18(%rbp),%rax
mov    0x2e0a(%rip),%rcx
cmp    (%rcx),%rax
je     1214 <signal_trap+0xb4>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%edx
call   1040 <ptrace@plt>
cmp    $0x0,%rax
je     120f <signal_trap+0xaf>
lea    0xe0f(%rip),%rdi
call   1070 <fatal@plt>
jmp    1214 <signal_trap+0xb4>
mov    -0x18(%rbp),%rax
mov    0x2d99(%rip),%rcx
cmp    (%rcx),%rax
jne    12b2 <signal_trap+0x152>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2d99(%rip),%rax
mov    (%rax),%edx
call   1050 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    1253 <signal_trap+0xf3>
lea    0xdde(%rip),%rdi
call   1070 <fatal@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1040 <ptrace@plt>
cmp    $0x0,%rax
je     1282 <signal_trap+0x122>
lea    0xdbe(%rip),%rdi
call   1070 <fatal@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2d0f(%rip),%rax
mov    (%rax),%edx
call   1050 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    12ad <signal_trap+0x14d>
lea    0xd84(%rip),%rdi
call   1070 <fatal@plt>
jmp    12b2 <signal_trap+0x152>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1060 <WIFEXITED@plt>
mov    %rax,%rcx
mov    $0x1,%al
cmp    $0x0,%rcx
mov    %al,-0x21(%rbp)
jne    12e4 <signal_trap+0x184>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <WIFSIGNALED@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<signal_trap>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rcx
mov    %rbx,%rsi
xor    %edx,%edx
call   1040 <ptrace@plt>
test   %rax,%rax
je     1196 <signal_trap+0x36>
lea    0xe6f(%rip),%rdi
call   1070 <fatal@plt>
mov    0x10(%rsp),%rcx
mov    0x2e26(%rip),%rax
cmp    (%rax),%rcx
jne    11ae <signal_trap+0x4e>
cmp    %rbx,0x8(%rsp)
je     11bd <signal_trap+0x5d>
lea    0x8(%rsp),%rdi
call   1080 <output_signal@plt>
mov    0x10(%rsp),%rcx
mov    0x2e1c(%rip),%rax
cmp    (%rax),%rcx
je     11ed <signal_trap+0x8d>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1040 <ptrace@plt>
test   %rax,%rax
je     11ed <signal_trap+0x8d>
lea    0xe31(%rip),%rdi
call   1070 <fatal@plt>
mov    0x10(%rsp),%rax
mov    0x2dbf(%rip),%rcx
cmp    (%rcx),%rax
jne    126e <signal_trap+0x10e>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    1223 <signal_trap+0xc3>
lea    0xe0e(%rip),%rdi
call   1070 <fatal@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
test   %rax,%rax
je     1249 <signal_trap+0xe9>
lea    0xdf7(%rip),%rdi
call   1070 <fatal@plt>
mov    0x2d50(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    126e <signal_trap+0x10e>
lea    0xdc3(%rip),%rdi
call   1070 <fatal@plt>
mov    (%r14),%edi
call   1060 <WIFEXITED@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jne    1296 <signal_trap+0x136>
mov    (%r14),%edi
call   1030 <WIFSIGNALED@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
setne  %al
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.TYPE_7__ = type { i64, %struct.TYPE_7__* }
@PTRACE_GETSIGINFO = global i32 0
@.str = [25 x i8] c"PTRACE_GETSIGINFO failed\00"
@SIGTRAP = global %struct.TYPE_7__* null
@SIGCONT = global %struct.TYPE_7__* null
@PTRACE_CONT = global i32 0
@.str.1 = [19 x i8] c"PTRACE_CONT failed\00"
@SIGSTOP = global %struct.TYPE_7__* null
@WUNTRACED = global i32 0
@.str.2 = [15 x i8] c"waitpid failed\00"
@PTRACE_LISTEN = global i32 0
@.str.3 = [21 x i8] c"PTRACE_LISTEN failed\00"
@WCONTINUED = global i32 0
define i32 @signal_trap(i64 %0, i32* %1) {
%3 = alloca i64
%4 = alloca i32*
%5 = alloca %struct.TYPE_7__
store i64 %0, i64* %3
store i32* %1, i32** %4
%6 = load i32, i32* @PTRACE_GETSIGINFO
%7 = load i64, i64* %3
%8 = call i64 @ptrace(i32 %6, i64 %7, i32* null, %struct.TYPE_7__* %5)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @fatal(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
%13 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %5, i32 0, i32 1
%14 = load %struct.TYPE_7__*, %struct.TYPE_7__** %13
%15 = load %struct.TYPE_7__*, %struct.TYPE_7__** @SIGTRAP
%16 = icmp eq %struct.TYPE_7__* %14, %15
br i1 %16, label %17, label %22
17: ; preds = %12
%18 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %5, i32 0, i32 0
%19 = load i64, i64* %18
%20 = load i64, i64* %3
%21 = icmp eq i64 %19, %20
br i1 %21, label %24, label %22
22: ; preds = %17, %12
%23 = call i32 @output_signal(%struct.TYPE_7__* %5)
br label %24
24: ; preds = %22, %17
%25 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %5, i32 0, i32 1
%26 = load %struct.TYPE_7__*, %struct.TYPE_7__** %25
%27 = load %struct.TYPE_7__*, %struct.TYPE_7__** @SIGCONT
%28 = icmp ne %struct.TYPE_7__* %26, %27
br i1 %28, label %29, label %39
29: ; preds = %24
%30 = load i32, i32* @PTRACE_CONT
%31 = load i64, i64* %3
%32 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %5, i32 0, i32 1
%33 = load %struct.TYPE_7__*, %struct.TYPE_7__** %32
%34 = call i64 @ptrace(i32 %30, i64 %31, i32* null, %struct.TYPE_7__* %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %29
%37 = call i32 @fatal(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %38
38: ; preds = %36, %29
br label %39
39: ; preds = %38, %24
%40 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %5, i32 0, i32 1
%41 = load %struct.TYPE_7__*, %struct.TYPE_7__** %40
%42 = load %struct.TYPE_7__*, %struct.TYPE_7__** @SIGSTOP
%43 = icmp eq %struct.TYPE_7__* %41, %42
br i1 %43, label %44, label %68
44: ; preds = %39
%45 = load i64, i64* %3
%46 = load i32*, i32** %4
%47 = load i32, i32* @WUNTRACED
%48 = call i32 @waitpid(i64 %45, i32* %46, i32 %47)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %52
50: ; preds = %44
%51 = call i32 @fatal(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %52
52: ; preds = %50, %44
%53 = load i32, i32* @PTRACE_LISTEN
%54 = load i64, i64* %3
%55 = call i64 @ptrace(i32 %53, i64 %54, i32* null, %struct.TYPE_7__* null)
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %52
%58 = call i32 @fatal(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %59
59: ; preds = %57, %52
%60 = load i64, i64* %3
%61 = load i32*, i32** %4
%62 = load i32, i32* @WCONTINUED
%63 = call i32 @waitpid(i64 %60, i32* %61, i32 %62)
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %67
65: ; preds = %59
%66 = call i32 @fatal(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %67
67: ; preds = %65, %59
br label %68
68: ; preds = %67, %39
%69 = load i32*, i32** %4
%70 = load i32, i32* %69
%71 = call i64 @WIFEXITED(i32 %70)
%72 = icmp ne i64 %71, 0
br i1 %72, label %78, label %73
73: ; preds = %68
%74 = load i32*, i32** %4
%75 = load i32, i32* %74
%76 = call i64 @WIFSIGNALED(i32 %75)
%77 = icmp ne i64 %76, 0
br label %78
78: ; preds = %73, %68
%79 = phi i1 [ true, %68 ], [ %77, %73 ]
%80 = zext i1 %79 to i32
ret i32 %80
}

LLVM IR (-O2):
%struct.TYPE_7__ = type { i64, %struct.TYPE_7__* }
@PTRACE_GETSIGINFO = global i32 0
@.str = [25 x i8] c"PTRACE_GETSIGINFO failed\00"
@SIGTRAP = global %struct.TYPE_7__* null
@SIGCONT = global %struct.TYPE_7__* null
@PTRACE_CONT = global i32 0
@.str.1 = [19 x i8] c"PTRACE_CONT failed\00"
@SIGSTOP = global %struct.TYPE_7__* null
@WUNTRACED = global i32 0
@.str.2 = [15 x i8] c"waitpid failed\00"
@PTRACE_LISTEN = global i32 0
@.str.3 = [21 x i8] c"PTRACE_LISTEN failed\00"
@WCONTINUED = global i32 0
define i32 @signal_trap(i64 %0, i32* %1) {
%3 = alloca %struct.TYPE_7__
%4 = bitcast %struct.TYPE_7__* %3 to i8*
%5 = load i32, i32* @PTRACE_GETSIGINFO
%6 = call i64 @ptrace(i32 %5, i64 %0, i32* null, %struct.TYPE_7__* %3)
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = call i32 @fatal(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %3, i64 0, i32 1
%12 = load %struct.TYPE_7__*, %struct.TYPE_7__** %11
%13 = load %struct.TYPE_7__*, %struct.TYPE_7__** @SIGTRAP
%14 = icmp eq %struct.TYPE_7__* %12, %13
%15 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %3, i64 0, i32 0
%16 = load i64, i64* %15
%17 = icmp eq i64 %16, %0
%18 = select i1 %14, i1 %17, i1 false
br i1 %18, label %22, label %19
19: ; preds = %10
%20 = call i32 @output_signal(%struct.TYPE_7__* %3)
%21 = load %struct.TYPE_7__*, %struct.TYPE_7__** %11
br label %22
22: ; preds = %10, %19
%23 = phi %struct.TYPE_7__* [ %12, %10 ], [ %21, %19 ]
%24 = load %struct.TYPE_7__*, %struct.TYPE_7__** @SIGCONT
%25 = icmp eq %struct.TYPE_7__* %23, %24
br i1 %25, label %32, label %26
26: ; preds = %22
%27 = load i32, i32* @PTRACE_CONT
%28 = call i64 @ptrace(i32 %27, i64 %0, i32* null, %struct.TYPE_7__* %23)
%29 = icmp eq i64 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %26
%31 = call i32 @fatal(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %26, %30, %22
%33 = load %struct.TYPE_7__*, %struct.TYPE_7__** %11
%34 = load %struct.TYPE_7__*, %struct.TYPE_7__** @SIGSTOP
%35 = icmp eq %struct.TYPE_7__* %33, %34
br i1 %35, label %36, label %54
36: ; preds = %32
%37 = load i32, i32* @WUNTRACED
%38 = call i32 @waitpid(i64 %0, i32* %1, i32 %37)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @fatal(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %42
42: ; preds = %40, %36
%43 = load i32, i32* @PTRACE_LISTEN
%44 = call i64 @ptrace(i32 %43, i64 %0, i32* null, %struct.TYPE_7__* null)
%45 = icmp eq i64 %44, 0
br i1 %45, label %48, label %46
46: ; preds = %42
%47 = call i32 @fatal(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %48
48: ; preds = %46, %42
%49 = load i32, i32* @WCONTINUED
%50 = call i32 @waitpid(i64 %0, i32* %1, i32 %49)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %54
52: ; preds = %48
%53 = call i32 @fatal(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %54
54: ; preds = %48, %52, %32
%55 = load i32, i32* %1
%56 = call i64 @WIFEXITED(i32 %55)
%57 = icmp eq i64 %56, 0
br i1 %57, label %58, label %63
58: ; preds = %54
%59 = load i32, i32* %1
%60 = call i64 @WIFSIGNALED(i32 %59)
%61 = icmp ne i64 %60, 0
%62 = zext i1 %61 to i32
br label %63
63: ; preds = %58, %54
%64 = phi i32 [ 1, %54 ], [ %62, %58 ]
ret i32 %64
}