#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*/ pid_t;

/* Variables and functions */
int PTRACE_EVENT_EXIT;
int SIGTRAP;
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSTOPPED(int);
int WSTOPSIG(int);
int /*<<< orphan*/ ptrace(int, int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int run(pid_t pid, int cmd) {
  int status, last_sig = 0, event;
  while (1) {
    ptrace(cmd, pid, 0, last_sig);
    waitpid(pid, &status, 0);
    if (WIFEXITED(status))
      return 0;
    if (WIFSTOPPED(status)) {
      last_sig = WSTOPSIG(status);
      if (last_sig == SIGTRAP) {
        event = (status >> 16) & 0xffff;
        return (event == PTRACE_EVENT_EXIT) ? 0 : 1;
      }
    }
  }
}


x86 assembly (clang, -O0):
<run>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x14(%rbp),%ecx
xor    %edx,%edx
call   1040 <ptrace@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    -0x10(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11a1 <run+0x51>
movl   $0x0,-0x4(%rbp)
jmp    1208 <run+0xb8>
mov    -0x10(%rbp),%edi
call   1030 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     1203 <run+0xb3>
mov    -0x10(%rbp),%edi
call   1070 <WSTOPSIG@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e10(%rip),%rcx
cmp    (%rcx),%eax
jne    11fe <run+0xae>
mov    -0x10(%rbp),%eax
sar    $0x10,%eax
and    $0xffff,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edx
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%eax
xor    %ecx,%ecx
cmp    %esi,%edx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    1208 <run+0xb8>
jmp    1203 <run+0xb3>
jmp    1165 <run+0x15>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%r12d
xor    %ebx,%ebx
mov    %esi,%edi
mov    %r12d,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
lea    0x4(%rsp),%rsi
mov    %r12d,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    0x4(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %rax,%rax
jne    11fc <run+0xac>
xor    %ebp,%ebp
lea    0x4(%rsp),%r15
mov    0x2e3c(%rip),%r13
xor    %ebx,%ebx
jmp    11ca <run+0x7a>
mov    %r14d,%edi
mov    %r12d,%esi
xor    %edx,%edx
mov    %ebp,%ecx
call   1040 <ptrace@plt>
mov    %r12d,%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    0x4(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %rax,%rax
jne    11fc <run+0xac>
mov    0x4(%rsp),%edi
call   1030 <WIFSTOPPED@plt>
test   %rax,%rax
je     11a0 <run+0x50>
mov    0x4(%rsp),%edi
call   1070 <WSTOPSIG@plt>
mov    %eax,%ebp
cmp    0x0(%r13),%eax
jne    11a0 <run+0x50>
movzwl 0x6(%rsp),%eax
mov    0x2deb(%rip),%rcx
xor    %ebx,%ebx
cmp    (%rcx),%eax
setne  %bl
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SIGTRAP = global i32 0
@PTRACE_EVENT_EXIT = global i32 0
define i32 @run(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 0, i32* %7
br label %9
9: ; preds = %2, %40
%10 = load i32, i32* %5
%11 = load i32, i32* %4
%12 = load i32, i32* %7
%13 = call i32 @ptrace(i32 %10, i32 %11, i32 0, i32 %12)
%14 = load i32, i32* %4
%15 = call i32 @waitpid(i32 %14, i32* %6, i32 0)
%16 = load i32, i32* %6
%17 = call i64 @WIFEXITED(i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %9
store i32 0, i32* %3
br label %41
20: ; preds = %9
%21 = load i32, i32* %6
%22 = call i64 @WIFSTOPPED(i32 %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %40
24: ; preds = %20
%25 = load i32, i32* %6
%26 = call i32 @WSTOPSIG(i32 %25)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = load i32, i32* @SIGTRAP
%29 = icmp eq i32 %27, %28
br i1 %29, label %30, label %39
30: ; preds = %24
%31 = load i32, i32* %6
%32 = ashr i32 %31, 16
%33 = and i32 %32, 65535
store i32 %33, i32* %8
%34 = load i32, i32* %8
%35 = load i32, i32* @PTRACE_EVENT_EXIT
%36 = icmp eq i32 %34, %35
%37 = zext i1 %36 to i64
%38 = select i1 %36, i32 0, i32 1
store i32 %38, i32* %3
br label %41
39: ; preds = %24
br label %40
40: ; preds = %39, %20
br label %9
41: ; preds = %30, %19
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
@SIGTRAP = global i32 0
@PTRACE_EVENT_EXIT = global i32 0
define i32 @run(i32 %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i32 @ptrace(i32 %1, i32 %0, i32 0, i32 0)
%6 = call i32 @waitpid(i32 %0, i32* %3, i32 0)
%7 = load i32, i32* %3
%8 = call i64 @WIFEXITED(i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %33
10: ; preds = %2, %26
%11 = phi i32 [ %27, %26 ], [ 0, %2 ]
%12 = load i32, i32* %3
%13 = call i64 @WIFSTOPPED(i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %26, label %15
15: ; preds = %10
%16 = load i32, i32* %3
%17 = call i32 @WSTOPSIG(i32 %16)
%18 = load i32, i32* @SIGTRAP
%19 = icmp eq i32 %17, %18
br i1 %19, label %20, label %26
20: ; preds = %15
%21 = load i32, i32* %3
%22 = lshr i32 %21, 16
%23 = load i32, i32* @PTRACE_EVENT_EXIT
%24 = icmp ne i32 %22, %23
%25 = zext i1 %24 to i32
br label %33
26: ; preds = %15, %10
%27 = phi i32 [ %17, %15 ], [ %11, %10 ]
%28 = call i32 @ptrace(i32 %1, i32 %0, i32 0, i32 %27)
%29 = call i32 @waitpid(i32 %0, i32* %3, i32 0)
%30 = load i32, i32* %3
%31 = call i64 @WIFEXITED(i32 %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %10, label %33
33: ; preds = %26, %2, %20
%34 = phi i32 [ %25, %20 ], [ 0, %2 ], [ 0, %26 ]
ret i32 %34
}