#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 /*<<< orphan*/ PTRACE_CONT;
scalar_t__ SIGBUS;
scalar_t__ SIGSEGV;
scalar_t__ WIFSTOPPED(int);
scalar_t__ WSTOPSIG(int);
int /*<<< orphan*/ __WALL;
int /*<<< orphan*/ perror(char *);
scalar_t__ ptrace(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *);
scalar_t__ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int resume_and_wait(pid_t pid) {
  int status, r = -1;
  if (ptrace(PTRACE_CONT, pid, NULL, NULL) != 0) {
    perror("resume_and_wait: ptrace");
    goto ret;
  }
  do {
    if (waitpid(pid, &status, __WALL) < 0) {
      perror("resume_and_wait: waitpid");
      goto ret;
    }
  } while (WIFSTOPPED(status) == 0 ||
           (WSTOPSIG(status) != SIGSEGV && WSTOPSIG(status) != SIGBUS));
  r = 0;
ret:
  return r;
}


x86 assembly (clang, -O0):
<resume_and_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0xffffffff,-0xc(%rbp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1040 <ptrace@plt>
cmp    $0x0,%rax
je     1195 <resume_and_wait+0x45>
lea    0xe75(%rip),%rdi
call   1060 <perror@plt>
jmp    1239 <resume_and_wait+0xe9>
jmp    119a <resume_and_wait+0x4a>
mov    -0x4(%rbp),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
lea    -0x8(%rbp),%rsi
call   1050 <waitpid@plt>
cmp    $0x0,%rax
jge    11ca <resume_and_wait+0x7a>
lea    0xe58(%rip),%rdi
call   1060 <perror@plt>
jmp    1239 <resume_and_wait+0xe9>
jmp    11cf <resume_and_wait+0x7f>
mov    -0x8(%rbp),%edi
call   1030 <WIFSTOPPED@plt>
mov    %rax,%rcx
mov    $0x1,%al
cmp    $0x0,%rcx
mov    %al,-0xd(%rbp)
je     1227 <resume_and_wait+0xd7>
mov    -0x8(%rbp),%edi
call   1070 <WSTOPSIG@plt>
mov    %rax,%rcx
xor    %eax,%eax
mov    0x2de3(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0xe(%rbp)
je     1221 <resume_and_wait+0xd1>
mov    -0x8(%rbp),%edi
call   1070 <WSTOPSIG@plt>
mov    0x2dd8(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
mov    %al,-0xe(%rbp)
mov    -0xe(%rbp),%al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    119a <resume_and_wait+0x4a>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resume_and_wait>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
test   %rax,%rax
jne    11f9 <resume_and_wait+0xa9>
mov    0x2e4f(%rip),%r15
mov    (%r15),%edx
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
call   1050 <waitpid@plt>
test   %rax,%rax
js     11f0 <resume_and_wait+0xa0>
lea    0x4(%rsp),%r14
mov    0x2e3f(%rip),%r12
mov    0x2e48(%rip),%r13
xor    %ebp,%ebp
jmp    11c2 <resume_and_wait+0x72>
nopl   0x0(%rax)
mov    (%r15),%edx
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <waitpid@plt>
test   %rax,%rax
js     11f0 <resume_and_wait+0xa0>
mov    0x4(%rsp),%edi
call   1030 <WIFSTOPPED@plt>
test   %rax,%rax
je     11b0 <resume_and_wait+0x60>
mov    0x4(%rsp),%edi
call   1070 <WSTOPSIG@plt>
cmp    (%r12),%rax
je     120a <resume_and_wait+0xba>
mov    0x4(%rsp),%edi
call   1070 <WSTOPSIG@plt>
cmp    0x0(%r13),%rax
jne    11b0 <resume_and_wait+0x60>
jmp    120a <resume_and_wait+0xba>
lea    0xe21(%rip),%rdi
jmp    1200 <resume_and_wait+0xb0>
lea    0xe00(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%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 = [24 x i8] c"resume_and_wait: ptrace\00"
@__WALL = global i32 0
@.str.1 = [25 x i8] c"resume_and_wait: waitpid\00"
@SIGSEGV = global i64 0
@SIGBUS = global i64 0
define i32 @resume_and_wait(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
store i32 -1, i32* %4
%5 = load i32, i32* @PTRACE_CONT
%6 = load i32, i32* %2
%7 = call i64 @ptrace(i32 %5, i32 %6, i32* null, i32* null)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %39
11: ; preds = %1
br label %12
12: ; preds = %36, %11
%13 = load i32, i32* %2
%14 = load i32, i32* @__WALL
%15 = call i64 @waitpid(i32 %13, i32* %3, i32 %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %39
19: ; preds = %12
br label %20
20: ; preds = %19
%21 = load i32, i32* %3
%22 = call i64 @WIFSTOPPED(i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %36, label %24
24: ; preds = %20
%25 = load i32, i32* %3
%26 = call i64 @WSTOPSIG(i32 %25)
%27 = load i64, i64* @SIGSEGV
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %34
29: ; preds = %24
%30 = load i32, i32* %3
%31 = call i64 @WSTOPSIG(i32 %30)
%32 = load i64, i64* @SIGBUS
%33 = icmp ne i64 %31, %32
br label %34
34: ; preds = %29, %24
%35 = phi i1 [ false, %24 ], [ %33, %29 ]
br label %36
36: ; preds = %34, %20
%37 = phi i1 [ true, %20 ], [ %35, %34 ]
br i1 %37, label %12, label %38
38: ; preds = %36
store i32 0, i32* %4
br label %39
39: ; preds = %38, %17, %9
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
@PTRACE_CONT = global i32 0
@.str = [24 x i8] c"resume_and_wait: ptrace\00"
@__WALL = global i32 0
@.str.1 = [25 x i8] c"resume_and_wait: waitpid\00"
@SIGSEGV = global i64 0
@SIGBUS = global i64 0
define i32 @resume_and_wait(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32, i32* @PTRACE_CONT
%5 = tail call i64 @ptrace(i32 %4, i32 %0, i32* null, i32* null)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = load i32, i32* @__WALL
%9 = call i64 @waitpid(i32 %0, i32* %2, i32 %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %13, label %15
11: ; preds = %1
%12 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %33
13: ; preds = %29, %7
%14 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %33
15: ; preds = %7, %29
%16 = load i32, i32* %2
%17 = call i64 @WIFSTOPPED(i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %29, label %19
19: ; preds = %15
%20 = load i32, i32* %2
%21 = call i64 @WSTOPSIG(i32 %20)
%22 = load i64, i64* @SIGSEGV
%23 = icmp eq i64 %21, %22
br i1 %23, label %33, label %24
24: ; preds = %19
%25 = load i32, i32* %2
%26 = call i64 @WSTOPSIG(i32 %25)
%27 = load i64, i64* @SIGBUS
%28 = icmp eq i64 %26, %27
br i1 %28, label %33, label %29
29: ; preds = %24, %15
%30 = load i32, i32* @__WALL
%31 = call i64 @waitpid(i32 %0, i32* %2, i32 %30)
%32 = icmp slt i64 %31, 0
br i1 %32, label %13, label %15
33: ; preds = %24, %19, %13, %11
%34 = phi i32 [ -1, %11 ], [ -1, %13 ], [ 0, %19 ], [ 0, %24 ]
ret i32 %34
}