#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 ORIG_RAX;
int /*<<< orphan*/ PTRACE_CONT;
int PTRACE_EVENT_SECCOMP;
int /*<<< orphan*/ PTRACE_PEEKUSER;
int SIGTRAP;
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ __NR_open;
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ ptrace(int /*<<< orphan*/, int /*<<< orphan*/, int,
                          int /*<<< orphan*/);
int /*<<< orphan*/ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int wait_for_open(pid_t child) {
  int status;
  while (1) {
    ptrace(PTRACE_CONT, child, 0, 0);
    waitpid(child, &status, 0);
    printf("[waitpid status: 0x%08x]\n", status);
    if (status >> 8 == (SIGTRAP | (PTRACE_EVENT_SECCOMP << 8)) &&
        ptrace(PTRACE_PEEKUSER, child, sizeof(long) * ORIG_RAX, 0) == __NR_open)
      return 0;
    if (WIFEXITED(status))
      return 1;
  }
}


x86 assembly (clang, -O0):
<wait_for_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1040 <ptrace@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    -0xc(%rbp),%esi
lea    0xe88(%rip),%rdi
call   1030 <printf@plt>
mov    -0xc(%rbp),%eax
sar    $0x8,%eax
mov    0x2e3e(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e4d(%rip),%rdx
mov    (%rdx),%edx
shl    $0x8,%edx
or     %edx,%ecx
cmp    %ecx,%eax
jne    11e0 <wait_for_open+0xa0>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
mov    0x2e23(%rip),%rax
movslq (%rax),%rax
shl    $0x3,%rax
mov    %eax,%edx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
mov    0x2e24(%rip),%rcx
cmp    (%rcx),%eax
jne    11e0 <wait_for_open+0xa0>
movl   $0x0,-0x4(%rbp)
jmp    1203 <wait_for_open+0xc3>
mov    -0xc(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11fe <wait_for_open+0xbe>
movl   $0x1,-0x4(%rbp)
jmp    1203 <wait_for_open+0xc3>
jmp    114b <wait_for_open+0xb>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_open>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,0x4(%rsp)
mov    0x2e6a(%rip),%rbp
mov    %rsp,%r14
lea    0xea0(%rip),%r13
mov    0x2e61(%rip),%r12
mov    0x2e72(%rip),%r15
jmp    117a <wait_for_open+0x3a>
call   1060 <WIFEXITED@plt>
test   %rax,%rax
jne    11f5 <wait_for_open+0xb5>
mov    0x0(%rbp),%edi
mov    0x4(%rsp),%ebx
mov    %ebx,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    (%rsp),%esi
mov    %r13,%rdi
call   1030 <printf@plt>
mov    (%rsp),%edi
mov    %edi,%eax
sar    $0x8,%eax
mov    (%r15),%ecx
shl    $0x8,%ecx
or     (%r12),%ecx
cmp    %ecx,%eax
jne    1170 <wait_for_open+0x30>
mov    %r14,%rbx
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
shl    $0x3,%edx
xor    %r14d,%r14d
mov    0x4(%rsp),%esi
xor    %ecx,%ecx
call   1040 <ptrace@plt>
mov    0x2e0a(%rip),%rcx
cmp    (%rcx),%eax
je     11fb <wait_for_open+0xbb>
mov    (%rsp),%edi
mov    %rbx,%r14
jmp    1170 <wait_for_open+0x30>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PTRACE_CONT = global i32 0
@.str = [26 x i8] c"[waitpid status: 0x%08x]\0A\00"
@SIGTRAP = global i32 0
@PTRACE_EVENT_SECCOMP = global i32 0
@PTRACE_PEEKUSER = global i32 0
@ORIG_RAX = global i32 0
@__NR_open = global i32 0
define i32 @wait_for_open(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
br label %5
5: ; preds = %1, %36
%6 = load i32, i32* @PTRACE_CONT
%7 = load i32, i32* %3
%8 = call i32 @ptrace(i32 %6, i32 %7, i32 0, i32 0)
%9 = load i32, i32* %3
%10 = call i32 @waitpid(i32 %9, i32* %4, i32 0)
%11 = load i32, i32* %4
%12 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = load i32, i32* %4
%14 = ashr i32 %13, 8
%15 = load i32, i32* @SIGTRAP
%16 = load i32, i32* @PTRACE_EVENT_SECCOMP
%17 = shl i32 %16, 8
%18 = or i32 %15, %17
%19 = icmp eq i32 %14, %18
br i1 %19, label %20, label %31
20: ; preds = %5
%21 = load i32, i32* @PTRACE_PEEKUSER
%22 = load i32, i32* %3
%23 = load i32, i32* @ORIG_RAX
%24 = sext i32 %23 to i64
%25 = mul i64 8, %24
%26 = trunc i64 %25 to i32
%27 = call i32 @ptrace(i32 %21, i32 %22, i32 %26, i32 0)
%28 = load i32, i32* @__NR_open
%29 = icmp eq i32 %27, %28
br i1 %29, label %30, label %31
30: ; preds = %20
store i32 0, i32* %2
br label %37
31: ; preds = %20, %5
%32 = load i32, i32* %4
%33 = call i64 @WIFEXITED(i32 %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %31
store i32 1, i32* %2
br label %37
36: ; preds = %31
br label %5
37: ; preds = %35, %30
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
@PTRACE_CONT = global i32 0
@.str = [26 x i8] c"[waitpid status: 0x%08x]\0A\00"
@SIGTRAP = global i32 0
@PTRACE_EVENT_SECCOMP = global i32 0
@PTRACE_PEEKUSER = global i32 0
@ORIG_RAX = global i32 0
@__NR_open = global i32 0
define i32 @wait_for_open(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
br label %4
4: ; preds = %26, %1
%5 = load i32, i32* @PTRACE_CONT
%6 = call i32 @ptrace(i32 %5, i32 %0, i32 0, i32 0)
%7 = call i32 @waitpid(i32 %0, i32* %2, i32 0)
%8 = load i32, i32* %2
%9 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = load i32, i32* %2
%11 = ashr i32 %10, 8
%12 = load i32, i32* @SIGTRAP
%13 = load i32, i32* @PTRACE_EVENT_SECCOMP
%14 = shl i32 %13, 8
%15 = or i32 %14, %12
%16 = icmp eq i32 %11, %15
br i1 %16, label %17, label %26
17: ; preds = %4
%18 = load i32, i32* @PTRACE_PEEKUSER
%19 = load i32, i32* @ORIG_RAX
%20 = shl i32 %19, 3
%21 = call i32 @ptrace(i32 %18, i32 %0, i32 %20, i32 0)
%22 = load i32, i32* @__NR_open
%23 = icmp eq i32 %21, %22
br i1 %23, label %30, label %24
24: ; preds = %17
%25 = load i32, i32* %2
br label %26
26: ; preds = %24, %4
%27 = phi i32 [ %25, %24 ], [ %10, %4 ]
%28 = call i64 @WIFEXITED(i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %4, label %30
30: ; preds = %26, %17
%31 = phi i32 [ 0, %17 ], [ 1, %26 ]
ret i32 %31
}