#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 */
int /*<<< orphan*/ PTRACE_ATTACH;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ add_thread(unsigned long, int /*<<< orphan*/ *,
                              unsigned long);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned long,
                           int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ ptrace(int /*<<< orphan*/, unsigned long, int /*<<< orphan*/,
                  int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int spu_attach(unsigned long pid) {
  if (ptrace(PTRACE_ATTACH, pid, 0, 0) != 0) {
    fprintf(stderr, "Cannot attach to process %ld: %s (%d)\n", pid,
            strerror(errno), errno);
    fflush(stderr);
    _exit(0177);
  }
  add_thread(pid, NULL, pid);
  return 0;
}


x86 assembly (clang, -O0):
<spu_attach>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1070 <ptrace@plt>
cmp    $0x0,%rax
je     11e5 <spu_attach+0x85>
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rdx
mov    %eax,%ecx
mov    0x2e12(%rip),%rax
mov    (%rax),%r8d
lea    0xe38(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    $0x7f,%edi
call   1030 <_exit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%esi
call   1040 <add_thread@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spu_attach>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e77(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <ptrace@plt>
test   %rax,%rax
jne    119e <spu_attach+0x3e>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1040 <add_thread@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e33(%rip),%r14
mov    (%r14),%ebp
mov    0x2e21(%rip),%r15
mov    (%r15),%edi
call   1080 <strerror@plt>
mov    (%r15),%r8d
lea    0xe3f(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %eax,%ecx
call   1050 <fprintf@plt>
mov    (%r14),%edi
call   1060 <fflush@plt>
mov    $0x7f,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@PTRACE_ATTACH = global i32 0
@stderr = global i32 0
@.str = [39 x i8] c"Cannot attach to process %ld: %s (%d)\0A\00"
@errno = global i32 0
define i32 @spu_attach(i64 %0) {
%2 = alloca i64
store i64 %0, i64* %2
%3 = load i32, i32* @PTRACE_ATTACH
%4 = load i64, i64* %2
%5 = call i64 @ptrace(i32 %3, i64 %4, i32 0, i32 0)
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %17
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = load i64, i64* %2
%10 = load i32, i32* @errno
%11 = call i32 @strerror(i32 %10)
%12 = load i32, i32* @errno
%13 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i64 %9, i32 %11, i32 %12)
%14 = load i32, i32* @stderr
%15 = call i32 @fflush(i32 %14)
%16 = call i32 @_exit(i32 127)
unreachable
17: ; preds = %1
%18 = load i64, i64* %2
%19 = load i64, i64* %2
%20 = call i32 @add_thread(i64 %18, i32* null, i64 %19)
ret i32 0
}

LLVM IR (-O2):
@PTRACE_ATTACH = global i32 0
@stderr = global i32 0
@.str = [39 x i8] c"Cannot attach to process %ld: %s (%d)\0A\00"
@errno = global i32 0
define i32 @spu_attach(i64 %0) {
%2 = load i32, i32* @PTRACE_ATTACH
%3 = tail call i64 @ptrace(i32 %2, i64 %0, i32 0, i32 0)
%4 = icmp eq i64 %3, 0
br i1 %4, label %14, label %5
5: ; preds = %1
%6 = load i32, i32* @stderr
%7 = load i32, i32* @errno
%8 = tail call i32 @strerror(i32 %7)
%9 = load i32, i32* @errno
%10 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i64 %0, i32 %8, i32 %9)
%11 = load i32, i32* @stderr
%12 = tail call i32 @fflush(i32 %11)
%13 = tail call i32 @_exit(i32 127)
unreachable
14: ; preds = %1
%15 = tail call i32 @add_thread(i64 %0, i32* null, i64 %0)
ret i32 0
}