#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*/ PTRACE_ATTACH;
int /*<<< orphan*/ SIGSTOP;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ ptrace(int /*<<< orphan*/, int, int /*<<< orphan*/,
                  int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int attach_to_pid(pid_t pid) {
  if (pid <= 0 || pid > 65535) {
    fprintf(stderr, "Invalid PID \"%d\"\n", pid);
    return 1;
  }
  if (ptrace(PTRACE_ATTACH, pid, 0, 0)) {
    perror("Failed to attach to process\n");
    return 1;
  }
  kill(pid, SIGSTOP);
  printf("Stopped %d\n", (int)pid);
  return 0;
}


x86 assembly (clang, -O0):
<attach_to_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    1172 <attach_to_pid+0x22>
cmpl   $0xffff,-0x8(%rbp)
jle    1196 <attach_to_pid+0x46>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe7b(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11f4 <attach_to_pid+0xa4>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1060 <ptrace@plt>
cmp    $0x0,%rax
je     11cd <attach_to_pid+0x7d>
lea    0xe56(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11f4 <attach_to_pid+0xa4>
mov    -0x8(%rbp),%edi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
call   1050 <kill@plt>
mov    -0x8(%rbp),%esi
lea    0xe47(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<attach_to_pid>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
lea    -0x10000(%rbx),%eax
cmp    $0xffff0000,%eax
ja     1180 <attach_to_pid+0x30>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
lea    0xe8e(%rip),%rsi
mov    %ebx,%edx
call   1040 <fprintf@plt>
mov    $0x1,%ebp
jmp    11b9 <attach_to_pid+0x69>
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
xor    %ebp,%ebp
mov    %ebx,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <ptrace@plt>
test   %rax,%rax
jne    11c2 <attach_to_pid+0x72>
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <kill@plt>
lea    0xe7d(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe49(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%ebp
jmp    11b9 <attach_to_pid+0x69>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [18 x i8] c"Invalid PID \22%d\22\0A\00"
@PTRACE_ATTACH = global i32 0
@.str.1 = [29 x i8] c"Failed to attach to process\0A\00"
@SIGSTOP = global i32 0
@.str.2 = [12 x i8] c"Stopped %d\0A\00"
define i32 @attach_to_pid(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
%5 = icmp sle i32 %4, 0
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = load i32, i32* %3
%8 = icmp sgt i32 %7, 65535
br i1 %8, label %9, label %13
9: ; preds = %6, %1
%10 = load i32, i32* @stderr
%11 = load i32, i32* %3
%12 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 1, i32* %2
br label %26
13: ; preds = %6
%14 = load i32, i32* @PTRACE_ATTACH
%15 = load i32, i32* %3
%16 = call i64 @ptrace(i32 %14, i32 %15, i32 0, i32 0)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %2
br label %26
20: ; preds = %13
%21 = load i32, i32* %3
%22 = load i32, i32* @SIGSTOP
%23 = call i32 @kill(i32 %21, i32 %22)
%24 = load i32, i32* %3
%25 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %24)
store i32 0, i32* %2
br label %26
26: ; preds = %20, %18, %9
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [18 x i8] c"Invalid PID \22%d\22\0A\00"
@PTRACE_ATTACH = global i32 0
@.str.1 = [29 x i8] c"Failed to attach to process\0A\00"
@SIGSTOP = global i32 0
@.str.2 = [12 x i8] c"Stopped %d\0A\00"
define i32 @attach_to_pid(i32 %0) {
%2 = add i32 %0, -65536
%3 = icmp ult i32 %2, -65535
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %17
7: ; preds = %1
%8 = load i32, i32* @PTRACE_ATTACH
%9 = tail call i64 @ptrace(i32 %8, i32 %0, i32 0, i32 0)
%10 = icmp eq i64 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %7
%12 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %17
13: ; preds = %7
%14 = load i32, i32* @SIGSTOP
%15 = tail call i32 @kill(i32 %0, i32 %14)
%16 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %0)
br label %17
17: ; preds = %13, %11, %4
%18 = phi i32 [ 1, %4 ], [ 1, %11 ], [ 0, %13 ]
ret i32 %18
}