#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_ATTACH;
int /*<<< orphan*/ WUNTRACED;
int /*<<< orphan*/ perror(char *);
scalar_t__ ptrace(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/ *,
                  int /*<<< orphan*/);
int /*<<< orphan*/ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int ptrace_attach(pid_t pid) {
  if (ptrace(PTRACE_ATTACH, pid, NULL, 0) < 0) {
    perror("ptrace_attach");
    return -1;
  }
  int status = 0;
  waitpid(pid, &status, WUNTRACED);
  return 0;
}


x86 assembly (clang, -O0):
<ptrace_attach>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2ea6(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
xor    %eax,%eax
mov    %eax,%edx
xor    %ecx,%ecx
call   1030 <ptrace@plt>
cmp    $0x0,%rax
jge    1174 <ptrace_attach+0x44>
lea    0xe9d(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1197 <ptrace_attach+0x67>
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e53(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rsi
call   1040 <waitpid@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ptrace_attach>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    0x2eac(%rip),%rax
mov    (%rax),%edi
xor    %ebx,%ebx
mov    %ebp,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <ptrace@plt>
test   %rax,%rax
js     1176 <ptrace_attach+0x46>
movl   $0x0,0x4(%rsp)
mov    0x2e79(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rsi
mov    %ebp,%edi
call   1040 <waitpid@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe83(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebx
jmp    116d <ptrace_attach+0x3d>

LLVM IR (-O0):
@PTRACE_ATTACH = global i32 0
@.str = [14 x i8] c"ptrace_attach\00"
@WUNTRACED = global i32 0
define i32 @ptrace_attach(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* @PTRACE_ATTACH
%6 = load i32, i32* %3
%7 = call i64 @ptrace(i32 %5, i32 %6, i32* null, i32 0)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %15
11: ; preds = %1
store i32 0, i32* %4
%12 = load i32, i32* %3
%13 = load i32, i32* @WUNTRACED
%14 = call i32 @waitpid(i32 %12, i32* %4, i32 %13)
store i32 0, i32* %2
br label %15
15: ; preds = %11, %9
%16 = load i32, i32* %2
ret i32 %16
}

LLVM IR (-O2):
@PTRACE_ATTACH = global i32 0
@.str = [14 x i8] c"ptrace_attach\00"
@WUNTRACED = global i32 0
define i32 @ptrace_attach(i32 %0) {
%2 = alloca i32
%3 = load i32, i32* @PTRACE_ATTACH
%4 = tail call i64 @ptrace(i32 %3, i32 %0, i32* null, i32 0)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %12
8: ; preds = %1
%9 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%10 = load i32, i32* @WUNTRACED
%11 = call i32 @waitpid(i32 %0, i32* %2, i32 %10)
br label %12
12: ; preds = %8, %6
%13 = phi i32 [ -1, %6 ], [ 0, %8 ]
ret i32 %13
}