#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*/ PTRACE_DETACH;
scalar_t__ SIGSTOP;
scalar_t__ WIFSTOPPED(int);
scalar_t__ WSTOPSIG(int);
int /*<<< orphan*/ __WALL;
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
scalar_t__ get_thr_list(int /*<<< orphan*/, int /*<<< orphan*/ **, int *);
int /*<<< orphan*/ perror(char *);
scalar_t__ ptrace(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *);
scalar_t__ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int attach(pid_t pid, char all_thrs) {
  pid_t *thr_list, tid;
  int status, thr_cnt, i, r = -1;
  if (all_thrs) {
    if (get_thr_list(pid, &thr_list, &thr_cnt) != 0)
      goto ret;
  } else {
    thr_list = &pid;
    thr_cnt = 1;
  }
  for (i = 0; i < thr_cnt; i++) {
    tid = thr_list[i];
    if (ptrace(PTRACE_ATTACH, tid, NULL, NULL) != 0) {
      perror("attach: ptrace");
      goto detach;
    }
    do {
      if (waitpid(tid, &status, __WALL) < 0) {
        perror("attach: waitpid");
        goto detach;
      }
    } while (WIFSTOPPED(status) == 0 || WSTOPSIG(status) != SIGSTOP);
  }
  r = 0;
detach:
  if (r)
    while (--i >= 0)
      ptrace(PTRACE_DETACH, thr_list[i], NULL, NULL);
  if (all_thrs)
    free(thr_list);
ret:
  return r;
}


x86 assembly (clang, -O0):
<attach>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %edi,-0x4(%rbp)
mov    %al,-0x5(%rbp)
movl   $0xffffffff,-0x24(%rbp)
cmpb   $0x0,-0x5(%rbp)
je     11b6 <attach+0x46>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1030 <get_thr_list@plt>
cmp    $0x0,%rax
je     11b1 <attach+0x41>
jmp    130d <attach+0x19d>
jmp    11c5 <attach+0x55>
lea    -0x4(%rbp),%rax
mov    %rax,-0x10(%rbp)
movl   $0x1,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12a3 <attach+0x133>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x14(%rbp)
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1060 <ptrace@plt>
cmp    $0x0,%rax
je     1219 <attach+0xa9>
lea    0xdf1(%rip),%rdi
call   1080 <perror@plt>
jmp    12aa <attach+0x13a>
jmp    121e <attach+0xae>
mov    -0x14(%rbp),%edi
mov    0x2da8(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rsi
call   1070 <waitpid@plt>
cmp    $0x0,%rax
jge    124e <attach+0xde>
lea    0xdcb(%rip),%rdi
call   1080 <perror@plt>
jmp    12aa <attach+0x13a>
jmp    1253 <attach+0xe3>
mov    -0x18(%rbp),%edi
call   1050 <WIFSTOPPED@plt>
mov    %rax,%rcx
mov    $0x1,%al
cmp    $0x0,%rcx
mov    %al,-0x25(%rbp)
je     1285 <attach+0x115>
mov    -0x18(%rbp),%edi
call   1090 <WSTOPSIG@plt>
mov    0x2d4c(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    121e <attach+0xae>
jmp    1295 <attach+0x125>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11cc <attach+0x5c>
movl   $0x0,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     12f5 <attach+0x185>
jmp    12b9 <attach+0x149>
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jl     12f0 <attach+0x180>
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1060 <ptrace@plt>
jmp    12b9 <attach+0x149>
jmp    12f5 <attach+0x185>
cmpb   $0x0,-0x5(%rbp)
je     1308 <attach+0x198>
mov    -0x10(%rbp),%rdi
call   1040 <free@plt>
jmp    130d <attach+0x19d>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<attach>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebp
mov    %edi,0x24(%rsp)
test   %bpl,%bpl
je     11b4 <attach+0x44>
lea    0x18(%rsp),%rsi
lea    0x10(%rsp),%rdx
call   1030 <get_thr_list@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    12d2 <attach+0x162>
cmpl   $0x0,0x10(%rsp)
jg     11c6 <attach+0x56>
xor    %ebx,%ebx
jmp    12c8 <attach+0x158>
lea    0x24(%rsp),%rax
mov    %rax,0x18(%rsp)
movl   $0x1,0x10(%rsp)
mov    %ebp,0xc(%rsp)
mov    $0x1,%r12d
xor    %r13d,%r13d
mov    0x2df6(%rip),%r15
lea    0x14(%rsp),%rbx
mov    0x2de2(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
mov    0x18(%rsp),%rax
mov    (%rax,%r13,4),%ebp
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
mov    %ebp,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <ptrace@plt>
test   %rax,%rax
jne    12e3 <attach+0x173>
mov    (%r15),%edx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1070 <waitpid@plt>
test   %rax,%rax
jns    1242 <attach+0xd2>
jmp    1274 <attach+0x104>
nopw   0x0(%rax,%rax,1)
mov    (%r15),%edx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1070 <waitpid@plt>
test   %rax,%rax
js     1274 <attach+0x104>
mov    0x14(%rsp),%edi
call   1050 <WIFSTOPPED@plt>
test   %rax,%rax
je     1230 <attach+0xc0>
mov    0x14(%rsp),%edi
call   1090 <WSTOPSIG@plt>
cmp    (%r14),%rax
jne    1230 <attach+0xc0>
add    $0x1,%r13
movslq 0x10(%rsp),%rax
add    $0x1,%r12
cmp    %rax,%r13
jl     11f0 <attach+0x80>
xor    %ebx,%ebx
jmp    12c0 <attach+0x150>
lea    0xd94(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
test   %r13d,%r13d
je     12c0 <attach+0x150>
mov    0x2d57(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x0(%rbp),%edi
mov    0x18(%rsp),%rax
mov    -0x8(%rax,%r12,4),%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <ptrace@plt>
add    $0xffffffffffffffff,%r12
cmp    $0x1,%r12
ja     12a0 <attach+0x130>
mov    0xc(%rsp),%eax
test   %al,%al
je     12d2 <attach+0x162>
mov    0x18(%rsp),%rdi
call   1040 <free@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd16(%rip),%rdi
jmp    127b <attach+0x10b>

LLVM IR (-O0):
@PTRACE_ATTACH = global i32 0
@.str = [15 x i8] c"attach: ptrace\00"
@__WALL = global i32 0
@.str.1 = [16 x i8] c"attach: waitpid\00"
@SIGSTOP = global i64 0
@PTRACE_DETACH = global i32 0
define i32 @attach(i32 %0, i8 signext %1) {
%3 = alloca i32
%4 = alloca i8
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %3
store i8 %1, i8* %4
store i32 -1, i32* %10
%11 = load i8, i8* %4
%12 = icmp ne i8 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i32, i32* %3
%15 = call i64 @get_thr_list(i32 %14, i32** %5, i32* %8)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %13
br label %86
18: ; preds = %13
br label %20
19: ; preds = %2
store i32* %3, i32** %5
store i32 1, i32* %8
br label %20
20: ; preds = %19, %18
store i32 0, i32* %9
br label %21
21: ; preds = %58, %20
%22 = load i32, i32* %9
%23 = load i32, i32* %8
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %61
25: ; preds = %21
%26 = load i32*, i32** %5
%27 = load i32, i32* %9
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i32, i32* %26, i64 %28
%30 = load i32, i32* %29
store i32 %30, i32* %6
%31 = load i32, i32* @PTRACE_ATTACH
%32 = load i32, i32* %6
%33 = call i64 @ptrace(i32 %31, i32 %32, i32* null, i32* null)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %25
%36 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %62
37: ; preds = %25
br label %38
38: ; preds = %55, %37
%39 = load i32, i32* %6
%40 = load i32, i32* @__WALL
%41 = call i64 @waitpid(i32 %39, i32* %7, i32 %40)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %38
%44 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %62
45: ; preds = %38
br label %46
46: ; preds = %45
%47 = load i32, i32* %7
%48 = call i64 @WIFSTOPPED(i32 %47)
%49 = icmp eq i64 %48, 0
br i1 %49, label %55, label %50
50: ; preds = %46
%51 = load i32, i32* %7
%52 = call i64 @WSTOPSIG(i32 %51)
%53 = load i64, i64* @SIGSTOP
%54 = icmp ne i64 %52, %53
br label %55
55: ; preds = %50, %46
%56 = phi i1 [ true, %46 ], [ %54, %50 ]
br i1 %56, label %38, label %57
57: ; preds = %55
br label %58
58: ; preds = %57
%59 = load i32, i32* %9
%60 = add nsw i32 %59, 1
store i32 %60, i32* %9
br label %21
61: ; preds = %21
store i32 0, i32* %10
br label %62
62: ; preds = %61, %43, %35
%63 = load i32, i32* %10
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %79
65: ; preds = %62
br label %66
66: ; preds = %70, %65
%67 = load i32, i32* %9
%68 = add nsw i32 %67, -1
store i32 %68, i32* %9
%69 = icmp sge i32 %68, 0
br i1 %69, label %70, label %78
70: ; preds = %66
%71 = load i32, i32* @PTRACE_DETACH
%72 = load i32*, i32** %5
%73 = load i32, i32* %9
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds i32, i32* %72, i64 %74
%76 = load i32, i32* %75
%77 = call i64 @ptrace(i32 %71, i32 %76, i32* null, i32* null)
br label %66
78: ; preds = %66
br label %79
79: ; preds = %78, %62
%80 = load i8, i8* %4
%81 = icmp ne i8 %80, 0
br i1 %81, label %82, label %85
82: ; preds = %79
%83 = load i32*, i32** %5
%84 = call i32 @free(i32* %83)
br label %85
85: ; preds = %82, %79
br label %86
86: ; preds = %85, %17
%87 = load i32, i32* %10
ret i32 %87
}

LLVM IR (-O2):
@PTRACE_ATTACH = global i32 0
@.str = [15 x i8] c"attach: ptrace\00"
@__WALL = global i32 0
@.str.1 = [16 x i8] c"attach: waitpid\00"
@SIGSTOP = global i64 0
@PTRACE_DETACH = global i32 0
define i32 @attach(i32 %0, i8 signext %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
%7 = bitcast i32** %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = bitcast i32* %6 to i8*
%10 = icmp eq i8 %1, 0
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = call i64 @get_thr_list(i32 %0, i32** %4, i32* %6)
%13 = icmp eq i64 %12, 0
br i1 %13, label %15, label %71
14: ; preds = %2
store i32* %3, i32** %4
store i32 1, i32* %6
br label %18
15: ; preds = %11
%16 = load i32, i32* %6
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %67
18: ; preds = %14, %15
br label %19
19: ; preds = %18, %44
%20 = phi i64 [ %45, %44 ], [ 0, %18 ]
%21 = load i32*, i32** %4
%22 = getelementptr inbounds i32, i32* %21, i64 %20
%23 = load i32, i32* %22
%24 = load i32, i32* @PTRACE_ATTACH
%25 = call i64 @ptrace(i32 %24, i32 %23, i32* null, i32* null)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %49
27: ; preds = %19
%28 = load i32, i32* @__WALL
%29 = call i64 @waitpid(i32 %23, i32* %5, i32 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %49, label %31
31: ; preds = %27, %40
%32 = load i32, i32* %5
%33 = call i64 @WIFSTOPPED(i32 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %40, label %35
35: ; preds = %31
%36 = load i32, i32* %5
%37 = call i64 @WSTOPSIG(i32 %36)
%38 = load i64, i64* @SIGSTOP
%39 = icmp eq i64 %37, %38
br i1 %39, label %44, label %40
40: ; preds = %35, %31
%41 = load i32, i32* @__WALL
%42 = call i64 @waitpid(i32 %23, i32* %5, i32 %41)
%43 = icmp slt i64 %42, 0
br i1 %43, label %49, label %31
44: ; preds = %35
%45 = add nuw nsw i64 %20, 1
%46 = load i32, i32* %6
%47 = sext i32 %46 to i64
%48 = icmp slt i64 %45, %47
br i1 %48, label %19, label %65
49: ; preds = %27, %19, %40
%50 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), %40 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), %19 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), %27 ]
%51 = call i32 @perror(i8* %50)
%52 = trunc i64 %20 to i32
%53 = icmp eq i32 %52, 0
br i1 %53, label %65, label %54
54: ; preds = %49
%55 = and i64 %20, 4294967295
br label %56
56: ; preds = %54, %56
%57 = phi i64 [ %55, %54 ], [ %58, %56 ]
%58 = add nsw i64 %57, -1
%59 = load i32, i32* @PTRACE_DETACH
%60 = load i32*, i32** %4
%61 = getelementptr inbounds i32, i32* %60, i64 %58
%62 = load i32, i32* %61
%63 = call i64 @ptrace(i32 %59, i32 %62, i32* null, i32* null)
%64 = icmp ugt i64 %57, 1
br i1 %64, label %56, label %65
65: ; preds = %44, %56, %49
%66 = phi i32 [ -1, %49 ], [ -1, %56 ], [ 0, %44 ]
br i1 %10, label %71, label %67
67: ; preds = %15, %65
%68 = phi i32 [ %66, %65 ], [ 0, %15 ]
%69 = load i32*, i32** %4
%70 = call i32 @free(i32* %69)
br label %71
71: ; preds = %65, %67, %11
%72 = phi i32 [ -1, %11 ], [ %68, %67 ], [ %66, %65 ]
ret i32 %72
}