#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_DETACH;
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*/ *);
int detach(pid_t pid, char all_thrs) {
  pid_t *thr_list, tid;
  int 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_DETACH, tid, NULL, NULL) != 0) {
      perror("detach: ptrace");
      goto free;
    }
  }
  r = 0;
free:
  if (all_thrs)
    free(thr_list);
ret:
  return r;
}


x86 assembly (clang, -O0):
<detach>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %edi,-0x4(%rbp)
mov    %al,-0x5(%rbp)
movl   $0xffffffff,-0x20(%rbp)
cmpb   $0x0,-0x5(%rbp)
je     1186 <detach+0x46>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1030 <get_thr_list@plt>
cmp    $0x0,%rax
je     1181 <detach+0x41>
jmp    121b <detach+0xdb>
jmp    1195 <detach+0x55>
lea    -0x4(%rbp),%rax
mov    %rax,-0x10(%rbp)
movl   $0x1,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11fc <detach+0xbc>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1050 <ptrace@plt>
cmp    $0x0,%rax
je     11e9 <detach+0xa9>
lea    0xe21(%rip),%rdi
call   1060 <perror@plt>
jmp    1203 <detach+0xc3>
jmp    11ee <detach+0xae>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    119c <detach+0x5c>
movl   $0x0,-0x20(%rbp)
cmpb   $0x0,-0x5(%rbp)
je     1216 <detach+0xd6>
mov    -0x10(%rbp),%rdi
call   1040 <free@plt>
jmp    121b <detach+0xdb>
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<detach>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r14d
mov    %edi,0x1c(%rsp)
test   %r14b,%r14b
je     1178 <detach+0x38>
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1030 <get_thr_list@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    11d7 <detach+0x97>
cmpl   $0x0,0xc(%rsp)
jg     118a <detach+0x4a>
xor    %ebp,%ebp
jmp    11cd <detach+0x8d>
lea    0x1c(%rsp),%rax
mov    %rax,0x10(%rsp)
movl   $0x1,0xc(%rsp)
xor    %ebp,%ebp
mov    0x2e55(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x10(%rsp),%rax
mov    (%rax,%rbp,4),%esi
mov    (%rbx),%edi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <ptrace@plt>
test   %rax,%rax
jne    11e2 <detach+0xa2>
add    $0x1,%rbp
movslq 0xc(%rsp),%rax
cmp    %rax,%rbp
jl     11a0 <detach+0x60>
xor    %ebp,%ebp
test   %r14b,%r14b
je     11d7 <detach+0x97>
mov    0x10(%rsp),%rdi
call   1040 <free@plt>
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe17(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebp
test   %r14b,%r14b
jne    11cd <detach+0x8d>
jmp    11d7 <detach+0x97>

LLVM IR (-O0):
@PTRACE_DETACH = global i32 0
@.str = [15 x i8] c"detach: ptrace\00"
define i32 @detach(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
store i32 %0, i32* %3
store i8 %1, i8* %4
store i32 -1, i32* %9
%10 = load i8, i8* %4
%11 = icmp ne i8 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i32, i32* %3
%14 = call i64 @get_thr_list(i32 %13, i32** %5, i32* %7)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %12
br label %48
17: ; preds = %12
br label %19
18: ; preds = %2
store i32* %3, i32** %5
store i32 1, i32* %7
br label %19
19: ; preds = %18, %17
store i32 0, i32* %8
br label %20
20: ; preds = %37, %19
%21 = load i32, i32* %8
%22 = load i32, i32* %7
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %40
24: ; preds = %20
%25 = load i32*, i32** %5
%26 = load i32, i32* %8
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32, i32* %25, i64 %27
%29 = load i32, i32* %28
store i32 %29, i32* %6
%30 = load i32, i32* @PTRACE_DETACH
%31 = load i32, i32* %6
%32 = call i64 @ptrace(i32 %30, i32 %31, i32* null, i32* null)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %24
%35 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %41
36: ; preds = %24
br label %37
37: ; preds = %36
%38 = load i32, i32* %8
%39 = add nsw i32 %38, 1
store i32 %39, i32* %8
br label %20
40: ; preds = %20
store i32 0, i32* %9
br label %41
41: ; preds = %40, %34
%42 = load i8, i8* %4
%43 = icmp ne i8 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %41
%45 = load i32*, i32** %5
%46 = call i32 @free(i32* %45)
br label %47
47: ; preds = %44, %41
br label %48
48: ; preds = %47, %16
%49 = load i32, i32* %9
ret i32 %49
}

LLVM IR (-O2):
@PTRACE_DETACH = global i32 0
@.str = [15 x i8] c"detach: ptrace\00"
define i32 @detach(i32 %0, i8 signext %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
store i32 %0, i32* %3
%6 = bitcast i32** %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = icmp eq i8 %1, 0
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = call i64 @get_thr_list(i32 %0, i32** %4, i32* %5)
%11 = icmp eq i64 %10, 0
br i1 %11, label %13, label %38
12: ; preds = %2
store i32* %3, i32** %4
store i32 1, i32* %5
br label %16
13: ; preds = %9
%14 = load i32, i32* %5
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %34
16: ; preds = %12, %13
br label %21
17: ; preds = %21
%18 = load i32, i32* %5
%19 = sext i32 %18 to i64
%20 = icmp slt i64 %29, %19
br i1 %20, label %21, label %32
21: ; preds = %16, %17
%22 = phi i64 [ %29, %17 ], [ 0, %16 ]
%23 = load i32*, i32** %4
%24 = getelementptr inbounds i32, i32* %23, i64 %22
%25 = load i32, i32* %24
%26 = load i32, i32* @PTRACE_DETACH
%27 = call i64 @ptrace(i32 %26, i32 %25, i32* null, i32* null)
%28 = icmp eq i64 %27, 0
%29 = add nuw nsw i64 %22, 1
br i1 %28, label %17, label %30
30: ; preds = %21
%31 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %32
32: ; preds = %17, %30
%33 = phi i32 [ -1, %30 ], [ 0, %17 ]
br i1 %8, label %38, label %34
34: ; preds = %13, %32
%35 = phi i32 [ %33, %32 ], [ 0, %13 ]
%36 = load i32*, i32** %4
%37 = call i32 @free(i32* %36)
br label %38
38: ; preds = %32, %34, %9
%39 = phi i32 [ -1, %9 ], [ %35, %34 ], [ %33, %32 ]
ret i32 %39
}