#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ PTRACE_ATTACH;
int /*<<< orphan*/ WUNTRACED;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int ptrace(int /*<<< orphan*/, scalar_t__, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
void ptrace_attach(pid_t target) {
  int waitpidstatus;
  if (ptrace(PTRACE_ATTACH, target, NULL, NULL) == -1) {
    fprintf(stderr, "ptrace(PTRACE_ATTACH) failed\n");
    exit(1);
  }
  if (waitpid(target, &waitpidstatus, WUNTRACED) != target) {
    fprintf(stderr, "waitpid(%d) failed\n", target);
    exit(1);
  }
}


x86 assembly (clang, -O0):
<ptrace_attach>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e95(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1040 <ptrace@plt>
cmp    $0xffffffff,%eax
jne    118f <ptrace_attach+0x4f>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
lea    0xe82(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e3e(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rsi
call   1050 <waitpid@plt>
cmp    -0x8(%rbp),%rax
je     11d4 <ptrace_attach+0x94>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe5b(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ptrace_attach>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2e99(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
cmp    $0xffffffff,%eax
je     1183 <ptrace_attach+0x43>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <waitpid@plt>
cmp    %rbx,%rax
jne    11a4 <ptrace_attach+0x64>
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
lea    0xe6a(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@PTRACE_ATTACH = global i32 0
@stderr = global i32 0
@.str = [30 x i8] c"ptrace(PTRACE_ATTACH) failed\0A\00"
@WUNTRACED = global i32 0
@.str.1 = [20 x i8] c"waitpid(%d) failed\0A\00"
define void @ptrace_attach(i64 %0) {
%2 = alloca i64
%3 = alloca i32
store i64 %0, i64* %2
%4 = load i32, i32* @PTRACE_ATTACH
%5 = load i64, i64* %2
%6 = call i32 @ptrace(i32 %4, i64 %5, i32* null, i32* null)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %1
%13 = load i64, i64* %2
%14 = load i32, i32* @WUNTRACED
%15 = call i64 @waitpid(i64 %13, i32* %3, i32 %14)
%16 = load i64, i64* %2
%17 = icmp ne i64 %15, %16
br i1 %17, label %18, label %23
18: ; preds = %12
%19 = load i32, i32* @stderr
%20 = load i64, i64* %2
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i64 %20)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %12
ret void
}

LLVM IR (-O2):
@PTRACE_ATTACH = global i32 0
@stderr = global i32 0
@.str = [30 x i8] c"ptrace(PTRACE_ATTACH) failed\0A\00"
@WUNTRACED = global i32 0
@.str.1 = [20 x i8] c"waitpid(%d) failed\0A\00"
define void @ptrace_attach(i64 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32, i32* @PTRACE_ATTACH
%5 = tail call i32 @ptrace(i32 %4, i64 %0, i32* null, i32* null)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %1
%12 = load i32, i32* @WUNTRACED
%13 = call i64 @waitpid(i64 %0, i32* %2, i32 %12)
%14 = icmp eq i64 %13, %0
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i64 %0)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %11
ret void
}