#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 */
scalar_t__ EINVAL;
int /*<<< orphan*/ PTRACE_ATTACH;
int /*<<< orphan*/ PTRACE_CONT;
int /*<<< orphan*/ PTRACE_DETACH;
int WEXITSTATUS(int);
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int fork();
int getppid();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int prctl(int, int);
scalar_t__ ptrace(int /*<<< orphan*/, int, int /*<<< orphan*/ *, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int is_debugger_attached(void) {
  int status;
  int rc;
  pid_t pid;
  int pipefd[2];
  if (pipe(pipefd) == -1) {
    perror("pipe");
    return 0;
  }
  pid = fork();
  if (pid == -1) {
    perror("fork");
    close(pipefd[0]);
    close(pipefd[1]);
    return 0;
  } else if (pid == 0) {
    char buf;
    pid_t ppid = getppid();
    close(pipefd[1]);
    read(pipefd[0], &buf, 1);
    close(pipefd[0]);
    if (buf == '-')
      _exit(1);
    if (ptrace(PTRACE_ATTACH, ppid, NULL, NULL) != 0)
      _exit(1);
    if (!waitpid(-1, NULL, 0))
      _exit(1);
    ptrace(PTRACE_CONT, NULL, NULL);
    ptrace(PTRACE_DETACH, ppid, NULL, NULL);
    _exit(0);
  } else {
    close(pipefd[0]);
    rc = prctl(0x59616d61, pid);
    if (rc != 0 && errno != EINVAL) {
      perror("prctl");
      write(pipefd[1], "-", 1);
    } else {
      write(pipefd[1], "+", 1);
    }
    close(pipefd[1]);
    waitpid(pid, &status, 0);
    rc = WEXITSTATUS(status);
  }
  return rc;
}


x86 assembly (clang, -O0):
<is_debugger_attached>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0x18(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11f2 <is_debugger_attached+0x32>
lea    0xe1f(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    13a3 <is_debugger_attached+0x1e3>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    122e <is_debugger_attached+0x6e>
lea    0xdf8(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    13a3 <is_debugger_attached+0x1e3>
cmpl   $0x0,-0x10(%rbp)
jne    1302 <is_debugger_attached+0x142>
mov    $0x0,%al
call   10d0 <getppid@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movsbl -0x19(%rbp),%eax
cmp    $0x2d,%eax
jne    127a <is_debugger_attached+0xba>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
mov    $0x0,%al
call   1090 <ptrace@plt>
cmp    $0x0,%rax
je     12a8 <is_debugger_attached+0xe8>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    $0xffffffff,%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10b0 <waitpid@plt>
cmp    $0x0,%eax
jne    12cb <is_debugger_attached+0x10b>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    0x2cee(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
xor    %eax,%eax
mov    %eax,%edx
mov    $0x0,%al
call   1090 <ptrace@plt>
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
mov    $0x0,%al
call   1090 <ptrace@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%esi
mov    $0x59616d61,%edi
call   1080 <prctl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1363 <is_debugger_attached+0x1a3>
mov    0x2ca5(%rip),%rax
mov    (%rax),%rax
mov    0x2c93(%rip),%rcx
cmp    (%rcx),%rax
je     1363 <is_debugger_attached+0x1a3>
lea    0xcc5(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x14(%rbp),%edi
lea    0xcbc(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
jmp    1377 <is_debugger_attached+0x1b7>
mov    -0x14(%rbp),%edi
lea    0xca5(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%edi
lea    -0x8(%rbp),%rsi
xor    %edx,%edx
call   10b0 <waitpid@plt>
mov    -0x8(%rbp),%edi
call   10a0 <WEXITSTATUS@plt>
mov    %eax,-0xc(%rbp)
jmp    139d <is_debugger_attached+0x1dd>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_debugger_attached>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
lea    0xc(%rsp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
je     1264 <is_debugger_attached+0xa4>
xor    %ebp,%ebp
xor    %eax,%eax
call   10e0 <fork@plt>
cmp    $0xffffffff,%eax
je     1274 <is_debugger_attached+0xb4>
mov    %eax,%ebx
test   %eax,%eax
je     12a9 <is_debugger_attached+0xe9>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    $0x59616d61,%edi
mov    %ebx,%esi
call   1080 <prctl@plt>
lea    0xe01(%rip),%rsi
test   %eax,%eax
je     122b <is_debugger_attached+0x6b>
mov    0x2db4(%rip),%rax
mov    (%rax),%rax
mov    0x2da2(%rip),%rcx
cmp    (%rcx),%rax
jne    1294 <is_debugger_attached+0xd4>
mov    0x10(%rsp),%edi
mov    $0x1,%edx
call   1040 <write@plt>
mov    0x10(%rsp),%edi
call   1050 <close@plt>
lea    0x14(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   10b0 <waitpid@plt>
mov    0x14(%rsp),%edi
call   10a0 <WEXITSTATUS@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd95(%rip),%rdi
call   10c0 <perror@plt>
xor    %ebp,%ebp
jmp    125b <is_debugger_attached+0x9b>
lea    0xd8a(%rip),%rdi
call   10c0 <perror@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x10(%rsp),%edi
call   1050 <close@plt>
jmp    125b <is_debugger_attached+0x9b>
lea    0xd6f(%rip),%rdi
call   10c0 <perror@plt>
lea    0xd69(%rip),%rsi
jmp    122b <is_debugger_attached+0x6b>
xor    %eax,%eax
call   10d0 <getppid@plt>
mov    %eax,%ebx
mov    0x10(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
lea    0xb(%rsp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
cmpb   $0x2d,0xb(%rsp)
je     130b <is_debugger_attached+0x14b>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %eax,%eax
call   1090 <ptrace@plt>
test   %rax,%rax
jne    130b <is_debugger_attached+0x14b>
mov    $0xffffffff,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <waitpid@plt>
test   %eax,%eax
jne    1315 <is_debugger_attached+0x155>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    0x2ca4(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %eax,%eax
call   1090 <ptrace@plt>
mov    0x2cb8(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %eax,%eax
call   1090 <ptrace@plt>
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [5 x i8] c"fork\00"
@PTRACE_ATTACH = global i32 0
@PTRACE_CONT = global i32 0
@PTRACE_DETACH = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
@.str.2 = [6 x i8] c"prctl\00"
@.str.3 = [2 x i8] c"-\00"
@.str.4 = [2 x i8] c"+\00"
define i32 @is_debugger_attached() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [2 x i32]
%6 = alloca i8
%7 = alloca i32
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%9 = call i32 @pipe(i32* %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %95
13: ; preds = %0
%14 = call i32 (...) @fork()
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %25
17: ; preds = %13
%18 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%20 = load i32, i32* %19
%21 = call i32 @close(i32 %20)
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
store i32 0, i32* %1
br label %95
25: ; preds = %13
%26 = load i32, i32* %4
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %63
28: ; preds = %25
%29 = call i32 (...) @getppid()
store i32 %29, i32* %7
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i32 @close(i32 %31)
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%34 = load i32, i32* %33
%35 = call i32 @read(i32 %34, i8* %6, i32 1)
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%37 = load i32, i32* %36
%38 = call i32 @close(i32 %37)
%39 = load i8, i8* %6
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 45
br i1 %41, label %42, label %44
42: ; preds = %28
%43 = call i32 @_exit(i32 1)
unreachable
44: ; preds = %28
%45 = load i32, i32* @PTRACE_ATTACH
%46 = load i32, i32* %7
%47 = call i64 (i32, i32, i32*, ...) @ptrace(i32 %45, i32 %46, i32* null, i8* null)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %44
%50 = call i32 @_exit(i32 1)
unreachable
51: ; preds = %44
%52 = call i32 @waitpid(i32 -1, i32* null, i32 0)
%53 = icmp ne i32 %52, 0
br i1 %53, label %56, label %54
54: ; preds = %51
%55 = call i32 @_exit(i32 1)
unreachable
56: ; preds = %51
%57 = load i32, i32* @PTRACE_CONT
%58 = call i64 (i32, i32, i32*, ...) @ptrace(i32 %57, i32 0, i32* null)
%59 = load i32, i32* @PTRACE_DETACH
%60 = load i32, i32* %7
%61 = call i64 (i32, i32, i32*, ...) @ptrace(i32 %59, i32 %60, i32* null, i8* null)
%62 = call i32 @_exit(i32 0)
unreachable
63: ; preds = %25
%64 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%65 = load i32, i32* %64
%66 = call i32 @close(i32 %65)
%67 = load i32, i32* %4
%68 = call i32 @prctl(i32 1499557217, i32 %67)
store i32 %68, i32* %3
%69 = load i32, i32* %3
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %80
71: ; preds = %63
%72 = load i64, i64* @errno
%73 = load i64, i64* @EINVAL
%74 = icmp ne i64 %72, %73
br i1 %74, label %75, label %80
75: ; preds = %71
%76 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%77 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%78 = load i32, i32* %77
%79 = call i32 @write(i32 %78, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
br label %84
80: ; preds = %71, %63
%81 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%82 = load i32, i32* %81
%83 = call i32 @write(i32 %82, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 1)
br label %84
84: ; preds = %80, %75
%85 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%86 = load i32, i32* %85
%87 = call i32 @close(i32 %86)
%88 = load i32, i32* %4
%89 = call i32 @waitpid(i32 %88, i32* %2, i32 0)
%90 = load i32, i32* %2
%91 = call i32 @WEXITSTATUS(i32 %90)
store i32 %91, i32* %3
br label %92
92: ; preds = %84
br label %93
93: ; preds = %92
%94 = load i32, i32* %3
store i32 %94, i32* %1
br label %95
95: ; preds = %93, %17, %11
%96 = load i32, i32* %1
ret i32 %96
}

LLVM IR (-O2):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [5 x i8] c"fork\00"
@PTRACE_ATTACH = global i32 0
@PTRACE_CONT = global i32 0
@PTRACE_DETACH = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
@.str.2 = [6 x i8] c"prctl\00"
@.str.3 = [2 x i8] c"-\00"
@.str.4 = [2 x i8] c"+\00"
define i32 @is_debugger_attached() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i8
%4 = bitcast i32* %1 to i8*
%5 = bitcast [2 x i32]* %2 to i8*
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%7 = call i32 @pipe(i32* %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %72
11: ; preds = %0
%12 = call i32 (...) @fork()
switch i32 %12, label %50 [
i32 -1, label %13
i32 0, label %20
]
13: ; preds = %11
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* %6
%16 = call i32 @close(i32 %15)
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%18 = load i32, i32* %17
%19 = call i32 @close(i32 %18)
br label %72
20: ; preds = %11
%21 = call i32 (...) @getppid()
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
%25 = load i32, i32* %6
%26 = call i32 @read(i32 %25, i8* %3, i32 1)
%27 = load i32, i32* %6
%28 = call i32 @close(i32 %27)
%29 = load i8, i8* %3
%30 = icmp eq i8 %29, 45
br i1 %30, label %31, label %33
31: ; preds = %20
%32 = call i32 @_exit(i32 1)
unreachable
33: ; preds = %20
%34 = load i32, i32* @PTRACE_ATTACH
%35 = call i64 (i32, i32, i32*, ...) @ptrace(i32 %34, i32 %21, i32* null, i8* null)
%36 = icmp eq i64 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %33
%38 = call i32 @_exit(i32 1)
unreachable
39: ; preds = %33
%40 = call i32 @waitpid(i32 -1, i32* null, i32 0)
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = call i32 @_exit(i32 1)
unreachable
44: ; preds = %39
%45 = load i32, i32* @PTRACE_CONT
%46 = call i64 (i32, i32, i32*, ...) @ptrace(i32 %45, i32 0, i32* null)
%47 = load i32, i32* @PTRACE_DETACH
%48 = call i64 (i32, i32, i32*, ...) @ptrace(i32 %47, i32 %21, i32* null, i8* null)
%49 = call i32 @_exit(i32 0)
unreachable
50: ; preds = %11
%51 = load i32, i32* %6
%52 = call i32 @close(i32 %51)
%53 = call i32 @prctl(i32 1499557217, i32 %12)
%54 = icmp eq i32 %53, 0
br i1 %54, label %61, label %55
55: ; preds = %50
%56 = load i64, i64* @errno
%57 = load i64, i64* @EINVAL
%58 = icmp eq i64 %56, %57
br i1 %58, label %61, label %59
59: ; preds = %55
%60 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %61
61: ; preds = %50, %55, %59
%62 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), %59 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), %55 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), %50 ]
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%64 = load i32, i32* %63
%65 = call i32 @write(i32 %64, i8* %62, i32 1)
%66 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%67 = load i32, i32* %66
%68 = call i32 @close(i32 %67)
%69 = call i32 @waitpid(i32 %12, i32* %1, i32 0)
%70 = load i32, i32* %1
%71 = call i32 @WEXITSTATUS(i32 %70)
br label %72
72: ; preds = %61, %13, %9
%73 = phi i32 [ 0, %9 ], [ 0, %13 ], [ %71, %61 ]
ret i32 %73
}