#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 eiki_execv_async(char *, char **);
int /*<<< orphan*/ eiki_free(char *);
scalar_t__ eiki_is_debugged();
char *eiki_malloc(int);
int /*<<< orphan*/ eiki_pid_to_str(int, char *);
int /*<<< orphan*/ eiki_strcat(char *, char *);
int /*<<< orphan*/ eiki_strcpy(char *, char *);
int eiki_strlen(char *);
int getpid();
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int eiki_attach_debugger() {
  pid_t debugger_pid = -1;
  pid_t pid;
  char pid_str[16];
  char *argv[] = {NULL, NULL, NULL, NULL};
  if (eiki_is_debugged() > 0) {
    return -2;
  }
  argv[0] = eiki_malloc(eiki_strlen("/usr/bin/gdb") + 1);
  if (!argv[0]) {
    goto end;
  }
  eiki_strcpy(argv[0], "/usr/bin/gdb");
  argv[1] = eiki_malloc(32);
  if (!argv[1]) {
    goto end;
  }
  eiki_strcpy(argv[1], "/proc/");
  pid = getpid();
  eiki_pid_to_str(pid, pid_str);
  eiki_strcat(argv[1], pid_str);
  eiki_strcat(argv[1], "/exe");
  argv[2] = eiki_malloc(16);
  if (!argv[2]) {
    goto end;
  }
  eiki_strcpy(argv[2], pid_str);
  debugger_pid = eiki_execv_async("/usr/bin/gdb", argv);
  if (debugger_pid != -1) {
    waitpid(debugger_pid, NULL, 0);
  }
end:
  eiki_free(argv[0]);
  eiki_free(argv[1]);
  eiki_free(argv[2]);
  return (debugger_pid != -1) ? 0 : -1;
}


x86 assembly (clang, -O0):
<eiki_attach_debugger>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0xffffffff,-0x8(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1080 <memset@plt>
mov    $0x0,%al
call   1030 <eiki_is_debugged@plt>
cmp    $0x0,%rax
jle    11ec <eiki_attach_debugger+0x3c>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1310 <eiki_attach_debugger+0x160>
lea    0xe0d(%rip),%rdi
call   1040 <eiki_strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1050 <eiki_malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1216 <eiki_attach_debugger+0x66>
jmp    12e2 <eiki_attach_debugger+0x132>
mov    -0x40(%rbp),%rdi
lea    0xddf(%rip),%rsi
call   1090 <eiki_strcpy@plt>
mov    $0x20,%edi
call   1050 <eiki_malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1244 <eiki_attach_debugger+0x94>
jmp    12e2 <eiki_attach_debugger+0x132>
mov    -0x38(%rbp),%rdi
lea    0xdbe(%rip),%rsi
call   1090 <eiki_strcpy@plt>
mov    $0x0,%al
call   1070 <getpid@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1060 <eiki_pid_to_str@plt>
mov    -0x38(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   10c0 <eiki_strcat@plt>
mov    -0x38(%rbp),%rdi
lea    0xd92(%rip),%rsi
call   10c0 <eiki_strcat@plt>
mov    $0x10,%edi
call   1050 <eiki_malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12a5 <eiki_attach_debugger+0xf5>
jmp    12e2 <eiki_attach_debugger+0x132>
mov    -0x30(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1090 <eiki_strcpy@plt>
lea    -0x40(%rbp),%rsi
lea    0xd43(%rip),%rdi
call   10d0 <eiki_execv_async@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
je     12dd <eiki_attach_debugger+0x12d>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10b0 <waitpid@plt>
jmp    12e2 <eiki_attach_debugger+0x132>
mov    -0x40(%rbp),%rdi
call   10a0 <eiki_free@plt>
mov    -0x38(%rbp),%rdi
call   10a0 <eiki_free@plt>
mov    -0x30(%rbp),%rdi
call   10a0 <eiki_free@plt>
mov    -0x8(%rbp),%edx
mov    $0xffffffff,%eax
xor    %ecx,%ecx
cmp    $0xffffffff,%edx
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eiki_attach_debugger>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
xor    %eax,%eax
call   1030 <eiki_is_debugged@plt>
mov    $0xfffffffe,%r14d
test   %rax,%rax
jg     12bb <eiki_attach_debugger+0x11b>
lea    0xe2e(%rip),%rdi
call   1040 <eiki_strlen@plt>
lea    0x1(%rax),%edi
call   1050 <eiki_malloc@plt>
mov    %rax,(%rsp)
mov    $0xffffffff,%r14d
test   %rax,%rax
je     129e <eiki_attach_debugger+0xfe>
lea    0xe07(%rip),%rsi
mov    %rax,%rdi
call   1080 <eiki_strcpy@plt>
mov    $0x20,%edi
call   1050 <eiki_malloc@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     129e <eiki_attach_debugger+0xfe>
mov    %rax,%rbx
lea    0xdea(%rip),%rsi
mov    %rax,%rdi
call   1080 <eiki_strcpy@plt>
xor    %eax,%eax
call   1070 <getpid@plt>
lea    0x20(%rsp),%r15
mov    %eax,%edi
mov    %r15,%rsi
call   1060 <eiki_pid_to_str@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   10b0 <eiki_strcat@plt>
lea    0xdc1(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <eiki_strcat@plt>
mov    $0x10,%edi
call   1050 <eiki_malloc@plt>
mov    %rax,0x10(%rsp)
test   %rax,%rax
je     129e <eiki_attach_debugger+0xfe>
lea    0x20(%rsp),%rsi
mov    %rax,%rdi
call   1080 <eiki_strcpy@plt>
lea    0xd7d(%rip),%rdi
mov    %rsp,%rsi
call   10c0 <eiki_execv_async@plt>
cmp    $0xffffffff,%eax
je     129e <eiki_attach_debugger+0xfe>
xor    %r14d,%r14d
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <waitpid@plt>
mov    (%rsp),%rdi
call   1090 <eiki_free@plt>
mov    0x8(%rsp),%rdi
call   1090 <eiki_free@plt>
mov    0x10(%rsp),%rdi
call   1090 <eiki_free@plt>
mov    %r14d,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/usr/bin/gdb\00"
@.str.1 = [7 x i8] c"/proc/\00"
@.str.2 = [5 x i8] c"/exe\00"
define i32 @eiki_attach_debugger() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [16 x i8]
%5 = alloca [4 x i8*]
store i32 -1, i32* %2
%6 = bitcast [4 x i8*]* %5 to i8*
%7 = call i64 (...) @eiki_is_debugged()
%8 = icmp sgt i64 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %0
store i32 -2, i32* %1
br label %77
10: ; preds = %0
%11 = call i32 @eiki_strlen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%12 = add nsw i32 %11, 1
%13 = call i8* @eiki_malloc(i32 %12)
%14 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 0
store i8* %13, i8** %14
%15 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 0
%16 = load i8*, i8** %15
%17 = icmp ne i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %10
br label %63
19: ; preds = %10
%20 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 0
%21 = load i8*, i8** %20
%22 = call i32 @eiki_strcpy(i8* %21, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%23 = call i8* @eiki_malloc(i32 32)
%24 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 1
store i8* %23, i8** %24
%25 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 1
%26 = load i8*, i8** %25
%27 = icmp ne i8* %26, null
br i1 %27, label %29, label %28
28: ; preds = %19
br label %63
29: ; preds = %19
%30 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 1
%31 = load i8*, i8** %30
%32 = call i32 @eiki_strcpy(i8* %31, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32 (...) @getpid()
store i32 %33, i32* %3
%34 = load i32, i32* %3
%35 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%36 = call i32 @eiki_pid_to_str(i32 %34, i8* %35)
%37 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 1
%38 = load i8*, i8** %37
%39 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%40 = call i32 @eiki_strcat(i8* %38, i8* %39)
%41 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 1
%42 = load i8*, i8** %41
%43 = call i32 @eiki_strcat(i8* %42, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%44 = call i8* @eiki_malloc(i32 16)
%45 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 2
store i8* %44, i8** %45
%46 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 2
%47 = load i8*, i8** %46
%48 = icmp ne i8* %47, null
br i1 %48, label %50, label %49
49: ; preds = %29
br label %63
50: ; preds = %29
%51 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 2
%52 = load i8*, i8** %51
%53 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%54 = call i32 @eiki_strcpy(i8* %52, i8* %53)
%55 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 0
%56 = call i32 @eiki_execv_async(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %55)
store i32 %56, i32* %2
%57 = load i32, i32* %2
%58 = icmp ne i32 %57, -1
br i1 %58, label %59, label %62
59: ; preds = %50
%60 = load i32, i32* %2
%61 = call i32 @waitpid(i32 %60, i32* null, i32 0)
br label %62
62: ; preds = %59, %50
br label %63
63: ; preds = %62, %49, %28, %18
%64 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 0
%65 = load i8*, i8** %64
%66 = call i32 @eiki_free(i8* %65)
%67 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 1
%68 = load i8*, i8** %67
%69 = call i32 @eiki_free(i8* %68)
%70 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 2
%71 = load i8*, i8** %70
%72 = call i32 @eiki_free(i8* %71)
%73 = load i32, i32* %2
%74 = icmp ne i32 %73, -1
%75 = zext i1 %74 to i64
%76 = select i1 %74, i32 0, i32 -1
store i32 %76, i32* %1
br label %77
77: ; preds = %63, %9
%78 = load i32, i32* %1
ret i32 %78
}

LLVM IR (-O2):
@.str = [13 x i8] c"/usr/bin/gdb\00"
@.str.1 = [7 x i8] c"/proc/\00"
@.str.2 = [5 x i8] c"/exe\00"
define i32 @eiki_attach_debugger() {
%1 = alloca [16 x i8]
%2 = alloca [4 x i8*]
%3 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%4 = bitcast [4 x i8*]* %2 to i8*
%5 = tail call i64 (...) @eiki_is_debugged()
%6 = icmp sgt i64 %5, 0
br i1 %6, label %43, label %7
7: ; preds = %0
%8 = tail call i32 @eiki_strlen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%9 = add nsw i32 %8, 1
%10 = tail call i8* @eiki_malloc(i32 %9)
%11 = getelementptr inbounds [4 x i8*], [4 x i8*]* %2, i64 0, i64 0
store i8* %10, i8** %11
%12 = icmp eq i8* %10, null
br i1 %12, label %33, label %13
13: ; preds = %7
%14 = tail call i32 @eiki_strcpy(i8* %10, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%15 = tail call i8* @eiki_malloc(i32 32)
%16 = getelementptr inbounds [4 x i8*], [4 x i8*]* %2, i64 0, i64 1
store i8* %15, i8** %16
%17 = icmp eq i8* %15, null
br i1 %17, label %33, label %18
18: ; preds = %13
%19 = tail call i32 @eiki_strcpy(i8* %15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%20 = tail call i32 (...) @getpid()
%21 = call i32 @eiki_pid_to_str(i32 %20, i8* %3)
%22 = call i32 @eiki_strcat(i8* %15, i8* %3)
%23 = call i32 @eiki_strcat(i8* %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%24 = call i8* @eiki_malloc(i32 16)
%25 = getelementptr inbounds [4 x i8*], [4 x i8*]* %2, i64 0, i64 2
store i8* %24, i8** %25
%26 = icmp eq i8* %24, null
br i1 %26, label %33, label %27
27: ; preds = %18
%28 = call i32 @eiki_strcpy(i8* %24, i8* %3)
%29 = call i32 @eiki_execv_async(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %11)
%30 = icmp eq i32 %29, -1
br i1 %30, label %33, label %31
31: ; preds = %27
%32 = call i32 @waitpid(i32 %29, i32* null, i32 0)
br label %33
33: ; preds = %27, %31, %18, %13, %7
%34 = phi i32 [ 0, %31 ], [ -1, %27 ], [ -1, %18 ], [ -1, %13 ], [ -1, %7 ]
%35 = load i8*, i8** %11
%36 = call i32 @eiki_free(i8* %35)
%37 = getelementptr inbounds [4 x i8*], [4 x i8*]* %2, i64 0, i64 1
%38 = load i8*, i8** %37
%39 = call i32 @eiki_free(i8* %38)
%40 = getelementptr inbounds [4 x i8*], [4 x i8*]* %2, i64 0, i64 2
%41 = load i8*, i8** %40
%42 = call i32 @eiki_free(i8* %41)
br label %43
43: ; preds = %0, %33
%44 = phi i32 [ %34, %33 ], [ -2, %0 ]
ret i32 %44
}