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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ SIGSTOP;
int /*<<< orphan*/ WIFSTOPPED(int);
scalar_t__ WSTOPSIG(int);
int /*<<< orphan*/ __WALL;
int /*<<< orphan*/ _print(char *, int, ...);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ ptrace(int, int, int /*<<< orphan*/, void *);
int waitpid(int, int *, int /*<<< orphan*/);
void parent_attach_new_thread(int newTid, int starterTid) {
  int result;
  int status;
  do {
    _print("Waiting for new task %d to stop"
           "\n",
           newTid);
    ;
    result = waitpid(newTid, &status, __WALL);
  } while (result == -1 && errno == EINTR);
  if (result == -1) {
    perror("Error waiting for new task to stop");
    exit(1);
  } else if (result != newTid) {
    _print("Wait returned unexpected PID %d"
           "\n",
           result);
    ;
    exit(1);
  } else if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP) {
    _print("Wait returned status %p"
           "\n",
           status);
    ;
    exit(1);
  }
  ptrace(0x4200, newTid, 0, (void *)(0x00000008 | 0x00000040));
  _print("Resuming tasks %d and %d"
         "\n",
         newTid, starterTid);
  ;
  ptrace(7, newTid, 0, 0);
  ptrace(7, starterTid, 0, 0);
}


x86 assembly (clang, -O0):
<parent_attach_new_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%esi
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1070 <_print@plt>
mov    -0x4(%rbp),%edi
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rsi
call   1050 <waitpid@plt>
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0xc(%rbp)
mov    %al,-0x11(%rbp)
jne    11d0 <parent_attach_new_thread+0x60>
mov    0x2e23(%rip),%rax
mov    (%rax),%rax
mov    0x2e09(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    117e <parent_attach_new_thread+0xe>
cmpl   $0xffffffff,-0xc(%rbp)
jne    11fb <parent_attach_new_thread+0x8b>
lea    0xe35(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%eax
cmp    -0x4(%rbp),%eax
je     1222 <parent_attach_new_thread+0xb2>
mov    -0xc(%rbp),%esi
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1070 <_print@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%edi
call   1030 <WIFSTOPPED@plt>
cmp    $0x0,%eax
je     124b <parent_attach_new_thread+0xdb>
mov    -0x10(%rbp),%edi
call   1090 <WSTOPSIG@plt>
mov    0x2d86(%rip),%rcx
cmp    (%rcx),%rax
je     1266 <parent_attach_new_thread+0xf6>
mov    -0x10(%rbp),%esi
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1070 <_print@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    126b <parent_attach_new_thread+0xfb>
jmp    1270 <parent_attach_new_thread+0x100>
mov    -0x4(%rbp),%esi
mov    $0x4200,%edi
xor    %edx,%edx
mov    $0x48,%ecx
call   1040 <ptrace@plt>
mov    -0x4(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1070 <_print@plt>
mov    -0x4(%rbp),%esi
mov    $0x7,%edi
xor    %edx,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <ptrace@plt>
mov    -0x8(%rbp),%esi
mov    $0x7,%edi
xor    %edx,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <ptrace@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parent_attach_new_thread>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,0x4(%rsp)
mov    %edi,%ebp
lea    0xe78(%rip),%r15
mov    0x2e49(%rip),%r12
mov    %rsp,%rbx
mov    0x2e47(%rip),%r13
mov    0x2e30(%rip),%r14
mov    %r15,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1070 <_print@plt>
mov    (%r12),%edx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1050 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    11c8 <parent_attach_new_thread+0x58>
mov    (%r14),%rcx
cmp    %rcx,0x0(%r13)
je     11a0 <parent_attach_new_thread+0x30>
cmp    $0xffffffff,%eax
je     125d <parent_attach_new_thread+0xed>
cmp    %ebp,%eax
jne    1273 <parent_attach_new_thread+0x103>
mov    (%rsp),%edi
call   1030 <WIFSTOPPED@plt>
test   %eax,%eax
je     1251 <parent_attach_new_thread+0xe1>
mov    (%rsp),%edi
call   1090 <WSTOPSIG@plt>
mov    0x2dd4(%rip),%rcx
cmp    (%rcx),%rax
jne    1251 <parent_attach_new_thread+0xe1>
mov    $0x48,%ecx
mov    $0x4200,%edi
mov    %ebp,%esi
xor    %edx,%edx
call   1040 <ptrace@plt>
lea    0xe6b(%rip),%rdi
mov    %ebp,%esi
mov    0x4(%rsp),%ebx
mov    %ebx,%edx
xor    %eax,%eax
call   1070 <_print@plt>
mov    $0x7,%edi
mov    %ebp,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
mov    $0x7,%edi
mov    %ebx,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%esi
lea    0xe0a(%rip),%rdi
jmp    127c <parent_attach_new_thread+0x10c>
lea    0xdbd(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xdca(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <_print@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [33 x i8] c"Waiting for new task %d to stop\0A\00"
@__WALL = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [35 x i8] c"Error waiting for new task to stop\00"
@.str.2 = [33 x i8] c"Wait returned unexpected PID %d\0A\00"
@SIGSTOP = global i64 0
@.str.3 = [25 x i8] c"Wait returned status %p\0A\00"
@.str.4 = [26 x i8] c"Resuming tasks %d and %d\0A\00"
define void @parent_attach_new_thread(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
br label %7
7: ; preds = %20, %2
%8 = load i32, i32* %3
%9 = call i32 (i8*, i32, ...) @_print(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = load i32, i32* %3
%11 = load i32, i32* @__WALL
%12 = call i32 @waitpid(i32 %10, i32* %6, i32 %11)
store i32 %12, i32* %5
br label %13
13: ; preds = %7
%14 = load i32, i32* %5
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINTR
%19 = icmp eq i64 %17, %18
br label %20
20: ; preds = %16, %13
%21 = phi i1 [ false, %13 ], [ %19, %16 ]
br i1 %21, label %7, label %22
22: ; preds = %20
%23 = load i32, i32* %5
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %22
%29 = load i32, i32* %5
%30 = load i32, i32* %3
%31 = icmp ne i32 %29, %30
br i1 %31, label %32, label %36
32: ; preds = %28
%33 = load i32, i32* %5
%34 = call i32 (i8*, i32, ...) @_print(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %33)
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %28
%37 = load i32, i32* %6
%38 = call i32 @WIFSTOPPED(i32 %37)
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %36
%41 = load i32, i32* %6
%42 = call i64 @WSTOPSIG(i32 %41)
%43 = load i64, i64* @SIGSTOP
%44 = icmp ne i64 %42, %43
br i1 %44, label %45, label %49
45: ; preds = %40, %36
%46 = load i32, i32* %6
%47 = call i32 (i8*, i32, ...) @_print(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %46)
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %40
br label %50
50: ; preds = %49
br label %51
51: ; preds = %50
%52 = load i32, i32* %3
%53 = call i32 @ptrace(i32 16896, i32 %52, i32 0, i8* inttoptr (i64 72 to i8*))
%54 = load i32, i32* %3
%55 = load i32, i32* %4
%56 = call i32 (i8*, i32, ...) @_print(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %54, i32 %55)
%57 = load i32, i32* %3
%58 = call i32 @ptrace(i32 7, i32 %57, i32 0, i8* null)
%59 = load i32, i32* %4
%60 = call i32 @ptrace(i32 7, i32 %59, i32 0, i8* null)
ret void
}

LLVM IR (-O2):
@.str = [33 x i8] c"Waiting for new task %d to stop\0A\00"
@__WALL = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [35 x i8] c"Error waiting for new task to stop\00"
@.str.2 = [33 x i8] c"Wait returned unexpected PID %d\0A\00"
@SIGSTOP = global i64 0
@.str.3 = [25 x i8] c"Wait returned status %p\0A\00"
@.str.4 = [26 x i8] c"Resuming tasks %d and %d\0A\00"
define void @parent_attach_new_thread(i32 %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
br label %5
5: ; preds = %5, %2
%6 = call i32 (i8*, i32, ...) @_print(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = load i32, i32* @__WALL
%8 = call i32 @waitpid(i32 %0, i32* %3, i32 %7)
%9 = icmp eq i32 %8, -1
%10 = load i64, i64* @errno
%11 = load i64, i64* @EINTR
%12 = icmp eq i64 %10, %11
%13 = select i1 %9, i1 %12, i1 false
br i1 %13, label %5, label %14
14: ; preds = %5
br i1 %9, label %15, label %18
15: ; preds = %14
%16 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %14
%19 = icmp eq i32 %8, %0
br i1 %19, label %23, label %20
20: ; preds = %18
%21 = call i32 (i8*, i32, ...) @_print(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %8)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %18
%24 = load i32, i32* %3
%25 = call i32 @WIFSTOPPED(i32 %24)
%26 = icmp eq i32 %25, 0
br i1 %26, label %32, label %27
27: ; preds = %23
%28 = load i32, i32* %3
%29 = call i64 @WSTOPSIG(i32 %28)
%30 = load i64, i64* @SIGSTOP
%31 = icmp eq i64 %29, %30
br i1 %31, label %36, label %32
32: ; preds = %27, %23
%33 = load i32, i32* %3
%34 = call i32 (i8*, i32, ...) @_print(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %33)
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %27
%37 = call i32 @ptrace(i32 16896, i32 %0, i32 0, i8* inttoptr (i64 72 to i8*))
%38 = call i32 (i8*, i32, ...) @_print(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %0, i32 %1)
%39 = call i32 @ptrace(i32 7, i32 %0, i32 0, i8* null)
%40 = call i32 @ptrace(i32 7, i32 %1, i32 0, i8* null)
ret void
}