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

/* Variables and functions */
int O_RDWR;
int O_SYNC;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int open(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int, unsigned long *, int);
void drp_set(unsigned long addr, int which) {
  char buffer[32];
  int fd = -1;
  snprintf(buffer, sizeof buffer, "/proc/dr/dr%d", which);
  fd = open(buffer, O_RDWR | O_SYNC);
  if (fd == -1) {
    fprintf(stderr, "[DRPROBE] Couldn't open debug register %d\n", which);
    return;
  }
  write(fd, &addr, sizeof(addr));
  close(fd);
}


x86 assembly (clang, -O0):
<drp_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffffffff,-0x34(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x20,%esi
lea    0xe87(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
mov    0x2e4e(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    11c3 <drp_set+0x73>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xe55(%rip),%rsi
call   1060 <fprintf@plt>
jmp    11dc <drp_set+0x8c>
mov    -0x34(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x8,%edx
call   1030 <write@plt>
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<drp_set>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
mov    %esi,%ebx
mov    %rdi,0x8(%rsp)
lea    0xe9c(%rip),%rdx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x20,%esi
mov    %ebx,%ecx
call   1040 <snprintf@plt>
mov    0x2e69(%rip),%rax
mov    0x2e5a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbp,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11b3 <drp_set+0x63>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11ca <drp_set+0x7a>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
lea    0xe4b(%rip),%rsi
mov    %ebx,%edx
call   1060 <fprintf@plt>
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/dr/dr%d\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@stderr = global i32 0
@.str.1 = [43 x i8] c"[DRPROBE] Couldn't open debug register %d\0A\00"
define void @drp_set(i64 %0, i32 %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca [32 x i8]
%6 = alloca i32
store i64 %0, i64* %3
store i32 %1, i32* %4
store i32 -1, i32* %6
%7 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%8 = load i32, i32* %4
%9 = call i32 @snprintf(i8* %7, i32 32, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_SYNC
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %10, i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = load i32, i32* %4
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i32 %19)
br label %26
21: ; preds = %2
%22 = load i32, i32* %6
%23 = call i32 @write(i32 %22, i64* %3, i32 8)
%24 = load i32, i32* %6
%25 = call i32 @close(i32 %24)
br label %26
26: ; preds = %21, %17
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/dr/dr%d\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@stderr = global i32 0
@.str.1 = [43 x i8] c"[DRPROBE] Couldn't open debug register %d\0A\00"
define void @drp_set(i64 %0, i32 %1) {
%3 = alloca i64
%4 = alloca [32 x i8]
store i64 %0, i64* %3
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 32, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %1)
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_SYNC
%9 = or i32 %8, %7
%10 = call i32 @open(i8* %5, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i32 %1)
br label %18
15: ; preds = %2
%16 = call i32 @write(i32 %10, i64* %3, i32 8)
%17 = call i32 @close(i32 %10)
br label %18
18: ; preds = %15, %12
ret void
}