#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;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ PTRACE_ATTACH;
int /*<<< orphan*/ PTRACE_CONT;
int /*<<< orphan*/ PTRACE_DETACH;
int WEXITSTATUS(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fatal_errno(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fork();
int getppid();
scalar_t__ ptrace(int /*<<< orphan*/, int, int /*<<< orphan*/ *, ...);
scalar_t__ strstr(char *, char *);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
bool is_debugger_running(void) {
  static int cached = -1;
  if (cached != -1)
    return cached;
  FILE *f = fopen("/proc/self/maps", "r");
  if (f != NULL) {
    char buf[1024];
    bool valgrind = false;
    while (!valgrind && fgets(buf, sizeof(buf), f)) {
      if (strstr(buf, "vgpreload"))
        valgrind = true;
    }
    fclose(f);
    if (valgrind)
      return (cached = true);
  }
  pid_t pid = fork();
  if (pid == -1)
    fatal_errno("fork");
  else if (pid == 0) {
    int ppid = getppid();
    if (ptrace(PTRACE_ATTACH, ppid, NULL, NULL) == 0) {
      waitpid(ppid, NULL, 0);
      ptrace(PTRACE_CONT, NULL, NULL);
      ptrace(PTRACE_DETACH, ppid, NULL, NULL);
      exit(0);
    } else {
      exit(1);
    }
  } else {
    int status;
    waitpid(pid, &status, 0);
    return (cached = WEXITSTATUS(status));
  }
}


x86 assembly (clang, -O0):
<is_debugger_running>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
cmpl   $0xffffffff,0x2eb6(%rip)
je     11d6 <is_debugger_running+0x26>
mov    0x2eaa(%rip),%eax
mov    %eax,-0x4(%rbp)
jmp    1392 <is_debugger_running+0x1e2>
lea    0xe23(%rip),%rdi
lea    0xe2c(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12a9 <is_debugger_running+0xf9>
movl   $0x0,-0x414(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x414(%rbp)
mov    %al,-0x421(%rbp)
jne    1239 <is_debugger_running+0x89>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x400,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x421(%rbp)
mov    -0x421(%rbp),%al
test   $0x1,%al
jne    124c <is_debugger_running+0x9c>
jmp    1278 <is_debugger_running+0xc8>
lea    -0x410(%rbp),%rdi
lea    0xdb8(%rip),%rsi
call   10d0 <strstr@plt>
cmp    $0x0,%rax
je     1273 <is_debugger_running+0xc3>
movl   $0x1,-0x414(%rbp)
jmp    1202 <is_debugger_running+0x52>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x0,-0x414(%rbp)
je     12a4 <is_debugger_running+0xf4>
movl   $0x1,0x2de0(%rip)
movl   $0x1,-0x4(%rbp)
jmp    1392 <is_debugger_running+0x1e2>
jmp    12a9 <is_debugger_running+0xf9>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x418(%rbp)
cmpl   $0xffffffff,-0x418(%rbp)
jne    12d4 <is_debugger_running+0x124>
lea    0xd52(%rip),%rdi
call   1050 <fatal_errno@plt>
jmp    1392 <is_debugger_running+0x1e2>
cmpl   $0x0,-0x418(%rbp)
jne    136a <is_debugger_running+0x1ba>
mov    $0x0,%al
call   10a0 <getppid@plt>
mov    %eax,-0x41c(%rbp)
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
mov    -0x41c(%rbp),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
mov    $0x0,%al
call   1060 <ptrace@plt>
cmp    $0x0,%rax
jne    1360 <is_debugger_running+0x1b0>
mov    -0x41c(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    0x2ca3(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
xor    %eax,%eax
mov    %eax,%edx
mov    $0x0,%al
call   1060 <ptrace@plt>
mov    0x2ca5(%rip),%rax
mov    (%rax),%edi
mov    -0x41c(%rbp),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
mov    $0x0,%al
call   1060 <ptrace@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x418(%rbp),%edi
lea    -0x420(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    -0x420(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,0x2ce9(%rip)
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_debugger_running>:
push   %r15
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    0x2eb6(%rip),%eax
cmp    $0xffffffff,%eax
je     11d4 <is_debugger_running+0x24>
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe25(%rip),%rdi
lea    0xe2e(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1246 <is_debugger_running+0x96>
mov    %rax,%rbx
mov    %rsp,%r14
lea    0xe19(%rip),%r15
nopl   0x0(%rax)
mov    %r14,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     123e <is_debugger_running+0x8e>
mov    %r14,%rdi
mov    %r15,%rsi
call   10d0 <strstr@plt>
test   %rax,%rax
je     1200 <is_debugger_running+0x50>
mov    %rbx,%rdi
call   1030 <fclose@plt>
movl   $0x1,0x2e41(%rip)
mov    $0x1,%eax
jmp    11c7 <is_debugger_running+0x17>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
cmp    $0xffffffff,%eax
je     1275 <is_debugger_running+0xc5>
test   %eax,%eax
je     128b <is_debugger_running+0xdb>
mov    %rsp,%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    (%rsp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,0x2e08(%rip)
jmp    11c7 <is_debugger_running+0x17>
lea    0xda0(%rip),%rdi
call   1050 <fatal_errno@plt>
mov    $0x1,%eax
jmp    11c7 <is_debugger_running+0x17>
xor    %eax,%eax
call   10a0 <getppid@plt>
mov    %eax,%ebx
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %eax,%eax
call   1060 <ptrace@plt>
test   %rax,%rax
jne    12eb <is_debugger_running+0x13b>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %eax,%eax
call   1060 <ptrace@plt>
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %eax,%eax
call   1060 <ptrace@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@is_debugger_running.cached = internal global i32 -1
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [10 x i8] c"vgpreload\00"
@.str.3 = [5 x i8] c"fork\00"
@PTRACE_ATTACH = global i32 0
@PTRACE_CONT = global i32 0
@PTRACE_DETACH = global i32 0
define i32 @is_debugger_running() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = load i32, i32* @is_debugger_running.cached
%9 = icmp ne i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = load i32, i32* @is_debugger_running.cached
store i32 %11, i32* %1
br label %71
12: ; preds = %0
%13 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %2
%14 = load i32*, i32** %2
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %40
16: ; preds = %12
store i32 0, i32* %4
br label %17
17: ; preds = %32, %16
%18 = load i32, i32* %4
%19 = icmp ne i32 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %17
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%22 = load i32*, i32** %2
%23 = call i64 @fgets(i8* %21, i32 1024, i32* %22)
%24 = icmp ne i64 %23, 0
br label %25
25: ; preds = %20, %17
%26 = phi i1 [ false, %17 ], [ %24, %20 ]
br i1 %26, label %27, label %33
27: ; preds = %25
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%29 = call i64 @strstr(i8* %28, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %27
store i32 1, i32* %4
br label %32
32: ; preds = %31, %27
br label %17
33: ; preds = %25
%34 = load i32*, i32** %2
%35 = call i32 @fclose(i32* %34)
%36 = load i32, i32* %4
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %33
store i32 1, i32* @is_debugger_running.cached
store i32 1, i32* %1
br label %71
39: ; preds = %33
br label %40
40: ; preds = %39, %12
%41 = call i32 (...) @fork()
store i32 %41, i32* %5
%42 = load i32, i32* %5
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = call i32 @fatal_errno(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %71
46: ; preds = %40
%47 = load i32, i32* %5
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %66
49: ; preds = %46
%50 = call i32 (...) @getppid()
store i32 %50, i32* %6
%51 = load i32, i32* @PTRACE_ATTACH
%52 = load i32, i32* %6
%53 = call i64 (i32, i32, i32*, ...) @ptrace(i32 %51, i32 %52, i32* null, i8* null)
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %64
55: ; preds = %49
%56 = load i32, i32* %6
%57 = call i32 @waitpid(i32 %56, i32* null, i32 0)
%58 = load i32, i32* @PTRACE_CONT
%59 = call i64 (i32, i32, i32*, ...) @ptrace(i32 %58, i32 0, i32* null)
%60 = load i32, i32* @PTRACE_DETACH
%61 = load i32, i32* %6
%62 = call i64 (i32, i32, i32*, ...) @ptrace(i32 %60, i32 %61, i32* null, i8* null)
%63 = call i32 @exit(i32 0)
unreachable
64: ; preds = %49
%65 = call i32 @exit(i32 1)
unreachable
66: ; preds = %46
%67 = load i32, i32* %5
%68 = call i32 @waitpid(i32 %67, i32* %7, i32 0)
%69 = load i32, i32* %7
%70 = call i32 @WEXITSTATUS(i32 %69)
store i32 %70, i32* @is_debugger_running.cached
store i32 %70, i32* %1
br label %71
71: ; preds = %10, %38, %66, %44
%72 = load i32, i32* %1
ret i32 %72
}

LLVM IR (-O2):
@is_debugger_running.cached = internal unnamed_addr global i32 -1
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [10 x i8] c"vgpreload\00"
@.str.3 = [5 x i8] c"fork\00"
@PTRACE_ATTACH = global i32 0
@PTRACE_CONT = global i32 0
@PTRACE_DETACH = global i32 0
define i32 @is_debugger_running() {
%1 = alloca [1024 x i8]
%2 = alloca i32
%3 = load i32, i32* @is_debugger_running.cached
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %43
5: ; preds = %0
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %20, label %8
8: ; preds = %5
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
br label %10
10: ; preds = %8, %13
%11 = call i64 @fgets(i8* %9, i32 1024, i32* %6)
%12 = icmp eq i64 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %10
%14 = call i64 @strstr(i8* %9, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %10, label %16
16: ; preds = %13
%17 = call i32 @fclose(i32* %6)
store i32 1, i32* @is_debugger_running.cached
br label %43
18: ; preds = %10
%19 = call i32 @fclose(i32* %6)
br label %20
20: ; preds = %18, %5
%21 = call i32 (...) @fork()
switch i32 %21, label %38 [
i32 -1, label %22
i32 0, label %24
]
22: ; preds = %20
%23 = call i32 @fatal_errno(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %43
24: ; preds = %20
%25 = call i32 (...) @getppid()
%26 = load i32, i32* @PTRACE_ATTACH
%27 = call i64 (i32, i32, i32*, ...) @ptrace(i32 %26, i32 %25, i32* null, i8* null)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %36
29: ; preds = %24
%30 = call i32 @waitpid(i32 %25, i32* null, i32 0)
%31 = load i32, i32* @PTRACE_CONT
%32 = call i64 (i32, i32, i32*, ...) @ptrace(i32 %31, i32 0, i32* null)
%33 = load i32, i32* @PTRACE_DETACH
%34 = call i64 (i32, i32, i32*, ...) @ptrace(i32 %33, i32 %25, i32* null, i8* null)
%35 = call i32 @exit(i32 0)
unreachable
36: ; preds = %24
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %20
%39 = bitcast i32* %2 to i8*
%40 = call i32 @waitpid(i32 %21, i32* %2, i32 0)
%41 = load i32, i32* %2
%42 = call i32 @WEXITSTATUS(i32 %41)
store i32 %42, i32* @is_debugger_running.cached
br label %43
43: ; preds = %16, %22, %38, %0
%44 = phi i32 [ %3, %0 ], [ 1, %16 ], [ 1, %22 ], [ %42, %38 ]
ret i32 %44
}