#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 */
struct timespec {
  int member_1;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ SIGQUIT;
int /*<<< orphan*/ check_file_owner(char *);
int check_socket(int);
scalar_t__ close(int);
int creat(char *, int);
char *get_temp_directory();
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ nanosleep(struct timespec *, int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
int /*<<< orphan*/ unlink(char *);
int start_attach_mechanism(int pid, int nspid) {
  char path[1024];
  snprintf(path, 1024, "/proc/%d/cwd/.attach_pid%d", nspid, nspid);
  int fd = creat(path, 0660);
  if (fd == -1 || (close(fd) == 0 && !check_file_owner(path))) {
    snprintf(path, 1024, "%s/.attach_pid%d", get_temp_directory(), nspid);
    fd = creat(path, 0660);
    if (fd == -1) {
      return 0;
    }
    close(fd);
  }
  kill(pid, SIGQUIT);
  int result;
  struct timespec ts = {0, 100000000};
  int retry = 0;
  do {
    nanosleep(&ts, NULL);
    result = check_socket(nspid);
  } while (!result && ++retry < 10);
  unlink(path);
  return result;
}


x86 assembly (clang, -O0):
<start_attach_mechanism>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    -0xc(%rbp),%r8d
mov    $0x400,%esi
lea    0xe45(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
lea    -0x410(%rbp),%rdi
mov    $0x1b0,%esi
call   1090 <creat@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0xffffffff,-0x414(%rbp)
je     1210 <start_attach_mechanism+0x80>
mov    -0x414(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
jne    1281 <start_attach_mechanism+0xf1>
lea    -0x410(%rbp),%rdi
call   10b0 <check_file_owner@plt>
cmp    $0x0,%eax
jne    1281 <start_attach_mechanism+0xf1>
lea    -0x410(%rbp),%rax
mov    %rax,-0x430(%rbp)
mov    $0x0,%al
call   10a0 <get_temp_directory@plt>
mov    -0x430(%rbp),%rdi
mov    %rax,%rcx
mov    -0xc(%rbp),%r8d
mov    $0x400,%esi
lea    0xddc(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
lea    -0x410(%rbp),%rdi
mov    $0x1b0,%esi
call   1090 <creat@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0xffffffff,-0x414(%rbp)
jne    1276 <start_attach_mechanism+0xe6>
movl   $0x0,-0x4(%rbp)
jmp    131b <start_attach_mechanism+0x18b>
mov    -0x414(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%edi
mov    0x2d5d(%rip),%rax
mov    (%rax),%esi
call   1070 <kill@plt>
mov    0xd93(%rip),%rax
mov    %rax,-0x420(%rbp)
movl   $0x0,-0x424(%rbp)
lea    -0x420(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <nanosleep@plt>
mov    -0xc(%rbp),%edi
call   1080 <check_socket@plt>
mov    %eax,-0x418(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x418(%rbp)
mov    %al,-0x431(%rbp)
jne    12f8 <start_attach_mechanism+0x168>
mov    -0x424(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x424(%rbp)
cmp    $0xa,%eax
setl   %al
mov    %al,-0x431(%rbp)
mov    -0x431(%rbp),%al
test   $0x1,%al
jne    12aa <start_attach_mechanism+0x11a>
lea    -0x410(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x418(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_attach_mechanism>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    %esi,%r15d
mov    %edi,%r14d
lea    0xe56(%rip),%rdx
xor    %ebp,%ebp
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r15d,%ecx
mov    %r15d,%r8d
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    %rbx,%rdi
mov    $0x1b0,%esi
call   1090 <creat@plt>
cmp    $0xffffffff,%eax
je     11f2 <start_attach_mechanism+0x62>
mov    %eax,%edi
call   1060 <close@plt>
test   %rax,%rax
jne    1233 <start_attach_mechanism+0xa3>
lea    0x10(%rsp),%rdi
call   10b0 <check_file_owner@plt>
test   %eax,%eax
jne    1233 <start_attach_mechanism+0xa3>
xor    %eax,%eax
call   10a0 <get_temp_directory@plt>
lea    0xe1b(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %rax,%rcx
mov    %r15d,%r8d
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    %rbx,%rdi
mov    $0x1b0,%esi
call   1090 <creat@plt>
cmp    $0xffffffff,%eax
je     128c <start_attach_mechanism+0xfc>
mov    %eax,%edi
call   1060 <close@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1070 <kill@plt>
movabs $0x5f5e10000000000,%rax
e1 f5 05
mov    %rax,0x8(%rsp)
xor    %ebx,%ebx
lea    0x8(%rsp),%r14
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
xor    %esi,%esi
call   1050 <nanosleep@plt>
mov    %r15d,%edi
call   1080 <check_socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1282 <start_attach_mechanism+0xf2>
lea    0x1(%rbx),%eax
cmp    $0x9,%ebx
mov    %eax,%ebx
jb     1260 <start_attach_mechanism+0xd0>
lea    0x10(%rsp),%rdi
call   1030 <unlink@plt>
mov    %ebp,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@.str = [27 x i8] c"/proc/%d/cwd/.attach_pid%d\00"
@.str.1 = [17 x i8] c"%s/.attach_pid%d\00"
@SIGQUIT = global i32 0
@__const.start_attach_mechanism.ts = %struct.timespec { i32 0, i32 100000000 }
define i32 @start_attach_mechanism(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.timespec
%10 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%12 = load i32, i32* %5
%13 = load i32, i32* %5
%14 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %11, i32 1024, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %13)
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%16 = call i32 @creat(i8* %15, i32 432)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp eq i32 %17, -1
br i1 %18, label %27, label %19
19: ; preds = %2
%20 = load i32, i32* %7
%21 = call i64 @close(i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %40
23: ; preds = %19
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%25 = call i32 @check_file_owner(i8* %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %40, label %27
27: ; preds = %23, %2
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%29 = call i8* (...) @get_temp_directory()
%30 = load i32, i32* %5
%31 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %28, i32 1024, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %29, i32 %30)
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%33 = call i32 @creat(i8* %32, i32 432)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %37
36: ; preds = %27
store i32 0, i32* %3
br label %62
37: ; preds = %27
%38 = load i32, i32* %7
%39 = call i64 @close(i32 %38)
br label %40
40: ; preds = %37, %23, %19
%41 = load i32, i32* %4
%42 = load i32, i32* @SIGQUIT
%43 = call i32 @kill(i32 %41, i32 %42)
%44 = bitcast %struct.timespec* %9 to i8*
store i32 0, i32* %10
br label %45
45: ; preds = %56, %40
%46 = call i32 @nanosleep(%struct.timespec* %9, i32* null)
%47 = load i32, i32* %5
%48 = call i32 @check_socket(i32 %47)
store i32 %48, i32* %8
br label %49
49: ; preds = %45
%50 = load i32, i32* %8
%51 = icmp ne i32 %50, 0
br i1 %51, label %56, label %52
52: ; preds = %49
%53 = load i32, i32* %10
%54 = add nsw i32 %53, 1
store i32 %54, i32* %10
%55 = icmp slt i32 %54, 10
br label %56
56: ; preds = %52, %49
%57 = phi i1 [ false, %49 ], [ %55, %52 ]
br i1 %57, label %45, label %58
58: ; preds = %56
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%60 = call i32 @unlink(i8* %59)
%61 = load i32, i32* %8
store i32 %61, i32* %3
br label %62
62: ; preds = %58, %36
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@.str = [27 x i8] c"/proc/%d/cwd/.attach_pid%d\00"
@.str.1 = [17 x i8] c"%s/.attach_pid%d\00"
@SIGQUIT = global i32 0
define i32 @start_attach_mechanism(i32 %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = alloca i64
%5 = bitcast i64* %4 to %struct.timespec*
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %6, i32 1024, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %1, i32 %1)
%8 = call i32 @creat(i8* %6, i32 432)
%9 = icmp eq i32 %8, -1
br i1 %9, label %16, label %10
10: ; preds = %2
%11 = call i64 @close(i32 %8)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %23
13: ; preds = %10
%14 = call i32 @check_file_owner(i8* %6)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %13, %2
%17 = call i8* (...) @get_temp_directory()
%18 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %6, i32 1024, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %17, i32 %1)
%19 = call i32 @creat(i8* %6, i32 432)
%20 = icmp eq i32 %19, -1
br i1 %20, label %37, label %21
21: ; preds = %16
%22 = call i64 @close(i32 %19)
br label %23
23: ; preds = %21, %13, %10
%24 = load i32, i32* @SIGQUIT
%25 = call i32 @kill(i32 %0, i32 %24)
%26 = bitcast i64* %4 to i8*
store i64 429496729600000000, i64* %4
br label %27
27: ; preds = %27, %23
%28 = phi i32 [ 0, %23 ], [ %32, %27 ]
%29 = call i32 @nanosleep(%struct.timespec* %5, i32* null)
%30 = call i32 @check_socket(i32 %1)
%31 = icmp eq i32 %30, 0
%32 = add nuw nsw i32 %28, 1
%33 = icmp ult i32 %28, 9
%34 = select i1 %31, i1 %33, i1 false
br i1 %34, label %27, label %35
35: ; preds = %27
%36 = call i32 @unlink(i8* %6)
br label %37
37: ; preds = %16, %35
%38 = phi i32 [ %30, %35 ], [ 0, %16 ]
ret i32 %38
}