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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ PTRACE_ATTACH;
int /*<<< orphan*/ err(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ ptrace(int /*<<< orphan*/, int, int /*<<< orphan*/,
                  int /*<<< orphan*/);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
void attach(pid_t pid) {
  int stat;
  printf("attach\n");
  printf("Attaching to PID %u\n", pid);
  if (ptrace(PTRACE_ATTACH, pid, 0, 0) < 0)
    err(EXIT_FAILURE, "ptrace (ATTACH)");
  waitpid(pid, &stat, 0);
}


x86 assembly (clang, -O0):
<attach>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
lea    0xeae(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%esi
lea    0xea5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e77(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1050 <ptrace@plt>
cmp    $0x0,%rax
jge    119e <attach+0x5e>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
lea    0xe84(%rip),%rsi
call   1040 <err@plt>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<attach>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0xec7(%rip),%rdi
call   1030 <puts@plt>
lea    0xe96(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <ptrace@plt>
test   %rax,%rax
jns    11a1 <attach+0x51>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
lea    0xe79(%rip),%rsi
call   1050 <err@plt>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1070 <waitpid@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [8 x i8] c"attach\0A\00"
@.str.1 = [21 x i8] c"Attaching to PID %u\0A\00"
@PTRACE_ATTACH = global i32 0
@EXIT_FAILURE = global i32 0
@.str.2 = [16 x i8] c"ptrace (ATTACH)\00"
define void @attach(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* %2
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = load i32, i32* @PTRACE_ATTACH
%8 = load i32, i32* %2
%9 = call i64 @ptrace(i32 %7, i32 %8, i32 0, i32 0)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @err(i32 %12, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %14
14: ; preds = %11, %1
%15 = load i32, i32* %2
%16 = call i32 @waitpid(i32 %15, i32* %3, i32 0)
ret void
}

LLVM IR (-O2):
@.str.1 = [21 x i8] c"Attaching to PID %u\0A\00"
@PTRACE_ATTACH = global i32 0
@EXIT_FAILURE = global i32 0
@.str.2 = [16 x i8] c"ptrace (ATTACH)\00"
@str = [7 x i8] c"attach\00"
define void @attach(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%6 = load i32, i32* @PTRACE_ATTACH
%7 = tail call i64 @ptrace(i32 %6, i32 %0, i32 0, i32 0)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @err(i32 %10, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %12
12: ; preds = %9, %1
%13 = call i32 @waitpid(i32 %0, i32* %2, i32 0)
ret void
}