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

/* Variables and functions */
int O_RDWR;
int O_SYNC;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int);
int /*<<< orphan*/ read(int, unsigned long *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int, unsigned long *, int);
void drp_disable(int which) {
  unsigned long dr7 = 0;
  unsigned long olddr7 = 0;
  if (which > 3 || which < 0) {
    fprintf(stderr, "[DRPROBE] Invalid Debug Register %d\n", which);
  }
  if (which == 0)
    dr7 |= 0x000F0003;
  else if (which == 1)
    dr7 |= 0x00F0000C;
  else if (which == 2)
    dr7 |= 0x0F000030;
  else if (which == 3)
    dr7 |= 0xF00000C0;
  dr7 = ~dr7;
  int fd = open("/proc/dr/dr7", O_RDWR | O_SYNC);
  read(fd, &olddr7, sizeof olddr7);
  dr7 = olddr7 & dr7;
  if (fd == -1) {
    fprintf(stderr, "[DRPROBE] Couldn't open debug register enable register "
                    "(dr7)\nMake sure debug_mod.ko has been loaded\n");
    return;
  }
  write(fd, &dr7, sizeof dr7);
  close(fd);
}


x86 assembly (clang, -O0):
<drp_disable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpl   $0x3,-0x4(%rbp)
jg     117f <drp_disable+0x2f>
cmpl   $0x0,-0x4(%rbp)
jge    1199 <drp_disable+0x49>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe6e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
cmpl   $0x0,-0x4(%rbp)
jne    11b6 <drp_disable+0x66>
mov    -0x10(%rbp),%rax
or     $0xf0003,%rax
mov    %rax,-0x10(%rbp)
jmp    121b <drp_disable+0xcb>
cmpl   $0x1,-0x4(%rbp)
jne    11d3 <drp_disable+0x83>
mov    -0x10(%rbp),%rax
or     $0xf0000c,%rax
mov    %rax,-0x10(%rbp)
jmp    1216 <drp_disable+0xc6>
cmpl   $0x2,-0x4(%rbp)
jne    11f0 <drp_disable+0xa0>
mov    -0x10(%rbp),%rax
or     $0xf000030,%rax
mov    %rax,-0x10(%rbp)
jmp    1211 <drp_disable+0xc1>
cmpl   $0x3,-0x4(%rbp)
jne    120c <drp_disable+0xbc>
movabs $0xf00000c0,%rax
or     -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1211 <drp_disable+0xc1>
jmp    1216 <drp_disable+0xc6>
jmp    121b <drp_disable+0xcb>
mov    -0x10(%rbp),%rax
xor    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
mov    0x2da9(%rip),%rax
or     (%rax),%esi
lea    0xde5(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1050 <read@plt>
mov    -0x18(%rbp),%rax
and    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    128b <drp_disable+0x13b>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
lea    0xdb3(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    12a4 <drp_disable+0x154>
mov    -0x1c(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1030 <write@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<drp_disable>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
movq   $0x0,0x8(%rsp)
cmp    $0x4,%edi
jb     1180 <drp_disable+0x30>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
lea    0xe89(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffffffffffff,%r14
cmp    $0x3,%ebx
ja     119a <drp_disable+0x4a>
movslq %ebx,%rax
lea    0xf02(%rip),%rcx
mov    (%rcx,%rax,8),%r14
mov    0x2e47(%rip),%rax
mov    0x2e38(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe72(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1050 <read@plt>
and    0x8(%rsp),%r14
mov    %r14,0x10(%rsp)
cmp    $0xffffffff,%ebx
je     11f9 <drp_disable+0xa9>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1030 <write@plt>
mov    %ebx,%edi
add    $0x18,%rsp
pop    %rbx
pop    %r14
jmp    1040 <close@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
lea    0xe29(%rip),%rsi
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
jmp    1060 <fprintf@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [37 x i8] c"[DRPROBE] Invalid Debug Register %d\0A\00"
@.str.1 = [13 x i8] c"/proc/dr/dr7\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.2 = [101 x i8] c"[DRPROBE] Couldn't open debug register enable register (dr7)\0AMake sure debug_mod.ko has been loaded\0A\00"
define void @drp_disable(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32
store i32 %0, i32* %2
store i64 0, i64* %3
store i64 0, i64* %4
%6 = load i32, i32* %2
%7 = icmp sgt i32 %6, 3
br i1 %7, label %11, label %8
8: ; preds = %1
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %8, %1
%12 = load i32, i32* @stderr
%13 = load i32, i32* %2
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %11, %8
%16 = load i32, i32* %2
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i64, i64* %3
%20 = or i64 %19, 983043
store i64 %20, i64* %3
br label %42
21: ; preds = %15
%22 = load i32, i32* %2
%23 = icmp eq i32 %22, 1
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i64, i64* %3
%26 = or i64 %25, 15728652
store i64 %26, i64* %3
br label %41
27: ; preds = %21
%28 = load i32, i32* %2
%29 = icmp eq i32 %28, 2
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load i64, i64* %3
%32 = or i64 %31, 251658288
store i64 %32, i64* %3
br label %40
33: ; preds = %27
%34 = load i32, i32* %2
%35 = icmp eq i32 %34, 3
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = load i64, i64* %3
%38 = or i64 %37, 4026532032
store i64 %38, i64* %3
br label %39
39: ; preds = %36, %33
br label %40
40: ; preds = %39, %30
br label %41
41: ; preds = %40, %24
br label %42
42: ; preds = %41, %18
%43 = load i64, i64* %3
%44 = xor i64 %43, -1
store i64 %44, i64* %3
%45 = load i32, i32* @O_RDWR
%46 = load i32, i32* @O_SYNC
%47 = or i32 %45, %46
%48 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %47)
store i32 %48, i32* %5
%49 = load i32, i32* %5
%50 = call i32 @read(i32 %49, i64* %4, i32 8)
%51 = load i64, i64* %4
%52 = load i64, i64* %3
%53 = and i64 %51, %52
store i64 %53, i64* %3
%54 = load i32, i32* %5
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %59
56: ; preds = %42
%57 = load i32, i32* @stderr
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([101 x i8], [101 x i8]* @.str.2, i64 0, i64 0))
br label %64
59: ; preds = %42
%60 = load i32, i32* %5
%61 = call i32 @write(i32 %60, i64* %3, i32 8)
%62 = load i32, i32* %5
%63 = call i32 @close(i32 %62)
br label %64
64: ; preds = %59, %56
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [37 x i8] c"[DRPROBE] Invalid Debug Register %d\0A\00"
@.str.1 = [13 x i8] c"/proc/dr/dr7\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.2 = [101 x i8] c"[DRPROBE] Couldn't open debug register enable register (dr7)\0AMake sure debug_mod.ko has been loaded\0A\00"
@switch.table.drp_disable = [4 x i64] [i64 -983044, i64 -15728653, i64 -251658289, i64 -4026532033]
define void @drp_disable(i32 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = bitcast i64* %2 to i8*
%5 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%6 = icmp ugt i32 %0, 3
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %10
10: ; preds = %1, %7
%11 = icmp ult i32 %0, 4
br i1 %11, label %12, label %16
12: ; preds = %10
%13 = sext i32 %0 to i64
%14 = getelementptr inbounds [4 x i64], [4 x i64]* @switch.table.drp_disable, i64 0, i64 %13
%15 = load i64, i64* %14
br label %16
16: ; preds = %12, %10
%17 = phi i64 [ -1, %10 ], [ %15, %12 ]
%18 = load i32, i32* @O_RDWR
%19 = load i32, i32* @O_SYNC
%20 = or i32 %19, %18
%21 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = call i32 @read(i32 %21, i64* %3, i32 8)
%23 = load i64, i64* %3
%24 = and i64 %23, %17
store i64 %24, i64* %2
%25 = icmp eq i32 %21, -1
br i1 %25, label %26, label %29
26: ; preds = %16
%27 = load i32, i32* @stderr
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([101 x i8], [101 x i8]* @.str.2, i64 0, i64 0))
br label %32
29: ; preds = %16
%30 = call i32 @write(i32 %21, i64* %2, i32 8)
%31 = tail call i32 @close(i32 %21)
br label %32
32: ; preds = %29, %26
ret void
}