#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ PTRACE_TRACEME;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ ptrace(int /*<<< orphan*/, int /*<<< orphan*/, int,
                  int /*<<< orphan*/);
void detect_gdb(void) {
  FILE *fd = fopen("/tmp", "r");
  if (fileno(fd) > 5) {
    printf("I'm sorry GDB! You are not allowed!\n");
    exit(1);
  }
  fclose(fd);
  if (ptrace(PTRACE_TRACEME, 0, 1, 0) < 0) {
    printf("Tracing is not allowed... Bye\n");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<detect_gdb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xe91(%rip),%rdi
lea    0xe8f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <fileno@plt>
cmp    $0x5,%eax
jle    11a7 <detect_gdb+0x47>
lea    0xe6f(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
xor    %ecx,%ecx
mov    $0x1,%edx
mov    %ecx,%esi
call   1060 <ptrace@plt>
cmp    $0x0,%rax
jge    11e7 <detect_gdb+0x87>
lea    0xe54(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<detect_gdb>:
push   %rbx
lea    0xe98(%rip),%rdi
lea    0xe96(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <fileno@plt>
cmp    $0x6,%eax
jge    11aa <detect_gdb+0x4a>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1060 <ptrace@plt>
test   %rax,%rax
js     11b3 <detect_gdb+0x53>
pop    %rbx
ret
lea    0xe74(%rip),%rdi
jmp    11ba <detect_gdb+0x5a>
lea    0xe4d(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"/tmp\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [37 x i8] c"I'm sorry GDB! You are not allowed!\0A\00"
@PTRACE_TRACEME = global i32 0
@.str.3 = [31 x i8] c"Tracing is not allowed... Bye\0A\00"
define void @detect_gdb() {
%1 = alloca i32*
%2 = call i32* @fopen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %2, i32** %1
%3 = load i32*, i32** %1
%4 = call i32 @fileno(i32* %3)
%5 = icmp sgt i32 %4, 5
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = load i32*, i32** %1
%11 = call i32 @fclose(i32* %10)
%12 = load i32, i32* @PTRACE_TRACEME
%13 = call i64 @ptrace(i32 %12, i32 0, i32 1, i32 0)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %9
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"/tmp\00"
@.str.1 = [2 x i8] c"r\00"
@PTRACE_TRACEME = global i32 0
@str = [30 x i8] c"Tracing is not allowed... Bye\00"
@str.4 = [36 x i8] c"I'm sorry GDB! You are not allowed!\00"
define void @detect_gdb() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%2 = tail call i32 @fileno(i32* %1)
%3 = icmp sgt i32 %2, 5
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.4, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %0
%8 = tail call i32 @fclose(i32* %1)
%9 = load i32, i32* @PTRACE_TRACEME
%10 = tail call i64 @ptrace(i32 %9, i32 0, i32 1, i32 0)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %7
ret void
}