#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*/ __WALL;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ ptrace(int /*<<< orphan*/, int, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ waitpid(int, int *, int /*<<< orphan*/);
int ptrace_attach(pid_t pid) {
  int status;
  if (ptrace(PTRACE_ATTACH, pid, NULL, NULL)) {
    fprintf(stderr, "Failed to ptrace attach: %s\n", strerror(errno));
    return 1;
  }
  if (waitpid(pid, &status, __WALL) < 0) {
    fprintf(stderr, "Failed to wait for PID %d, %s\n", pid, strerror(errno));
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<ptrace_attach>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e96(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1040 <ptrace@plt>
cmp    $0x0,%rax
je     11a7 <ptrace_attach+0x67>
mov    0x2e64(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x10(%rbp),%edi
mov    %rax,%rdx
lea    0xe6c(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1210 <ptrace_attach+0xd0>
mov    -0x8(%rbp),%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rsi
call   1050 <waitpid@plt>
cmp    $0x0,%rax
jge    1209 <ptrace_attach+0xc9>
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%edx
mov    %rax,%rcx
lea    0xe27(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1210 <ptrace_attach+0xd0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ptrace_attach>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    0x2e9c(%rip),%rax
mov    (%rax),%edi
xor    %ebx,%ebx
mov    %ebp,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
test   %rax,%rax
je     1191 <ptrace_attach+0x51>
mov    0x2e71(%rip),%rax
mov    (%rax),%ebx
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe82(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%ebx
jmp    11ab <ptrace_attach+0x6b>
mov    0x2e30(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rsi
mov    %ebp,%edi
call   1050 <waitpid@plt>
test   %rax,%rax
js     11b4 <ptrace_attach+0x74>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e1d(%rip),%rax
mov    (%rax),%ebx
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe4b(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    118a <ptrace_attach+0x4a>

LLVM IR (-O0):
@PTRACE_ATTACH = global i32 0
@stderr = global i32 0
@.str = [29 x i8] c"Failed to ptrace attach: %s\0A\00"
@errno = global i32 0
@__WALL = global i32 0
@.str.1 = [31 x i8] c"Failed to wait for PID %d, %s\0A\00"
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* null)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = call i8* @strerror(i32 %11)
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %12)
store i32 1, i32* %2
br label %26
14: ; preds = %1
%15 = load i32, i32* %3
%16 = load i32, i32* @__WALL
%17 = call i64 @waitpid(i32 %15, i32* %4, i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = load i32, i32* %3
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %21, i8* %23)
store i32 1, i32* %2
br label %26
25: ; preds = %14
store i32 0, i32* %2
br label %26
26: ; preds = %25, %19, %9
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
@PTRACE_ATTACH = global i32 0
@stderr = global i32 0
@.str = [29 x i8] c"Failed to ptrace attach: %s\0A\00"
@errno = global i32 0
@__WALL = global i32 0
@.str.1 = [31 x i8] c"Failed to wait for PID %d, %s\0A\00"
define i32 @ptrace_attach(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32, i32* @PTRACE_ATTACH
%5 = tail call i64 @ptrace(i32 %4, i32 %0, i32* null, i32* null)
%6 = icmp eq i64 %5, 0
br i1 %6, label %12, label %7
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %21
12: ; preds = %1
%13 = load i32, i32* @__WALL
%14 = call i64 @waitpid(i32 %0, i32* %2, i32 %13)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %0, i8* %19)
br label %21
21: ; preds = %12, %16, %7
%22 = phi i32 [ 1, %7 ], [ 1, %16 ], [ 0, %12 ]
ret i32 %22
}