int set_instrumentation(unsigned long addr, int enable) {
  unsigned char nop[] = {0x67, 0x0f, 0x1f, 0x04, 0x00};
  unsigned char call[5] = {0xe8, 0x00, 0x00, 0x00, 0x00};
  unsigned long new, old;
  unsigned long *insn = (void *)addr;
  unsigned int target_addr;
  target_addr = get_target_addr(addr);
  if (target_addr == 0)
    return -1;
  memcpy(&call[1], &target_addr, sizeof(target_addr));
  do {
    new = old = *insn;
    if (!enable && !memcmp(insn, call, sizeof(call))) {
      memcpy(&new, nop, sizeof(nop));
    } else if (enable && !memcmp(insn, nop, sizeof(nop))) {
      memcpy(&new, call, sizeof(call));
    } else
      return -1;
  } while (!atomic_compare_exchange_weak(insn, &old, new));
  return 0;
}


x86 assembly (clang, -O0):
<set_instrumentation>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0xebb(%rip),%eax
mov    %eax,-0x19(%rbp)
mov    0xeb6(%rip),%al
mov    %al,-0x15(%rbp)
mov    0xeae(%rip),%eax
mov    %eax,-0x1e(%rbp)
mov    0xea9(%rip),%al
mov    %al,-0x1a(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1050 <get_target_addr@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jne    118f <set_instrumentation+0x5f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1250 <set_instrumentation+0x120>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x1d(%rbp)
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11da <set_instrumentation+0xaa>
mov    -0x38(%rbp),%rdi
lea    -0x1e(%rbp),%rsi
mov    $0x5,%edx
call   1040 <memcmp@plt>
cmp    $0x0,%eax
jne    11da <set_instrumentation+0xaa>
mov    -0x19(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x15(%rbp),%al
mov    %al,-0x24(%rbp)
jmp    1221 <set_instrumentation+0xf1>
cmpl   $0x0,-0x14(%rbp)
je     1210 <set_instrumentation+0xe0>
mov    -0x38(%rbp),%rdi
lea    -0x19(%rbp),%rsi
mov    $0x5,%edx
call   1040 <memcmp@plt>
cmp    $0x0,%eax
jne    1210 <set_instrumentation+0xe0>
mov    -0x1e(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x1a(%rbp),%al
mov    %al,-0x24(%rbp)
jmp    121c <set_instrumentation+0xec>
movl   $0xffffffff,-0x4(%rbp)
jmp    1250 <set_instrumentation+0x120>
jmp    1221 <set_instrumentation+0xf1>
jmp    1226 <set_instrumentation+0xf6>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rsi
mov    $0x0,%al
call   1030 <atomic_compare_exchange_weak@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1195 <set_instrumentation+0x65>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_instrumentation>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r15d
mov    %rdi,%rbx
movb   $0x0,0xc(%rsp)
movl   $0xe8,0x8(%rsp)
xor    %eax,%eax
call   1040 <get_target_addr@plt>
test   %eax,%eax
je     11d2 <set_instrumentation+0xb2>
mov    %eax,0x9(%rsp)
mov    0x8(%rsp),%eax
movzbl 0xc(%rsp),%r12d
shl    $0x20,%r12
or     %rax,%r12
movabs $0xffffff0000000000,%r13
ff ff ff
lea    0x10(%rsp),%r14
xor    %ebp,%ebp
jmp    11b1 <set_instrumentation+0x91>
nopl   0x0(%rax,%rax,1)
mov    (%rbx),%ecx
xor    0x8(%rsp),%ecx
movzbl 0x4(%rbx),%eax
xor    0xc(%rsp),%al
movzbl %al,%esi
mov    $0x41f0f67,%eax
or     %ecx,%esi
jne    11d2 <set_instrumentation+0xb2>
and    %r13,%rdx
or     %rax,%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <atomic_compare_exchange_weak@plt>
test   %eax,%eax
jne    11d7 <set_instrumentation+0xb7>
mov    (%rbx),%rdx
mov    %rdx,0x10(%rsp)
test   %r15d,%r15d
je     1180 <set_instrumentation+0x60>
mov    (%rbx),%eax
mov    $0x41f0f67,%ecx
xor    %ecx,%eax
movzbl 0x4(%rbx),%ecx
or     %eax,%ecx
mov    %r12,%rax
je     119a <set_instrumentation+0x7a>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.set_instrumentation.nop = [5 x i8] c"g\0F\1F\04\00"
@__const.set_instrumentation.call = [5 x i8] c"\E8\00\00\00\00"
define i32 @set_instrumentation(i64 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
%6 = alloca [5 x i8]
%7 = alloca [5 x i8]
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64*
%11 = alloca i32
store i64 %0, i64* %4
store i32 %1, i32* %5
%12 = bitcast [5 x i8]* %6 to i8*
%13 = bitcast [5 x i8]* %7 to i8*
%14 = load i64, i64* %4
%15 = inttoptr i64 %14 to i8*
%16 = bitcast i8* %15 to i64*
store i64* %16, i64** %10
%17 = load i64, i64* %4
%18 = call i32 (i64, ...) bitcast (i32 (...)* @get_target_addr to i32 (i64, ...)*)(i64 %17)
store i32 %18, i32* %11
%19 = load i32, i32* %11
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %2
store i32 -1, i32* %3
br label %61
22: ; preds = %2
%23 = getelementptr inbounds [5 x i8], [5 x i8]* %7, i64 0, i64 1
%24 = bitcast i32* %11 to i8*
br label %25
25: ; preds = %54, %22
%26 = load i64*, i64** %10
%27 = load i64, i64* %26
store i64 %27, i64* %9
store i64 %27, i64* %8
%28 = load i32, i32* %5
%29 = icmp ne i32 %28, 0
br i1 %29, label %39, label %30
30: ; preds = %25
%31 = load i64*, i64** %10
%32 = bitcast i64* %31 to i8*
%33 = getelementptr inbounds [5 x i8], [5 x i8]* %7, i64 0, i64 0
%34 = call i32 @memcmp(i8* %32, i8* %33, i64 5)
%35 = icmp ne i32 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %30
%37 = bitcast i64* %8 to i8*
%38 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
br label %53
39: ; preds = %30, %25
%40 = load i32, i32* %5
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %51
42: ; preds = %39
%43 = load i64*, i64** %10
%44 = bitcast i64* %43 to i8*
%45 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%46 = call i32 @memcmp(i8* %44, i8* %45, i64 5)
%47 = icmp ne i32 %46, 0
br i1 %47, label %51, label %48
48: ; preds = %42
%49 = bitcast i64* %8 to i8*
%50 = getelementptr inbounds [5 x i8], [5 x i8]* %7, i64 0, i64 0
br label %52
51: ; preds = %42, %39
store i32 -1, i32* %3
br label %61
52: ; preds = %48
br label %53
53: ; preds = %52, %36
br label %54
54: ; preds = %53
%55 = load i64*, i64** %10
%56 = load i64, i64* %8
%57 = call i32 (i64*, i64*, i64, ...) bitcast (i32 (...)* @atomic_compare_exchange_weak to i32 (i64*, i64*, i64, ...)*)(i64* %55, i64* %9, i64 %56)
%58 = icmp ne i32 %57, 0
%59 = xor i1 %58, true
br i1 %59, label %25, label %60
60: ; preds = %54
store i32 0, i32* %3
br label %61
61: ; preds = %60, %51, %21
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
@__const.set_instrumentation.nop = [5 x i8] c"g\0F\1F\04\00"
@__const.set_instrumentation.call = [5 x i8] c"\E8\00\00\00\00"
define i32 @set_instrumentation(i64 %0, i32 %1) {
%3 = alloca [5 x i8]
%4 = alloca i64
%5 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%6 = bitcast i64* %4 to i8*
%7 = inttoptr i64 %0 to i8*
%8 = inttoptr i64 %0 to i64*
%9 = tail call i32 (i64, ...) bitcast (i32 (...)* @get_target_addr to i32 (i64, ...)*)(i64 %0)
%10 = icmp eq i32 %9, 0
br i1 %10, label %32, label %11
11: ; preds = %2
%12 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 1
%13 = bitcast i8* %12 to i32*
store i32 %9, i32* %13
%14 = icmp eq i32 %1, 0
%15 = bitcast [5 x i8]* %3 to i40*
%16 = load i40, i40* %15
%17 = zext i40 %16 to i64
br label %18
18: ; preds = %26, %11
%19 = load i64, i64* %8
store i64 %19, i64* %4
br i1 %14, label %20, label %23
20: ; preds = %18
%21 = call i32 @bcmp(i8* dereferenceable(5) %7, i8* dereferenceable(5) %5, i64 5)
%22 = icmp eq i32 %21, 0
br i1 %22, label %26, label %32
23: ; preds = %18
%24 = call i32 @bcmp(i8* dereferenceable(5) %7, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @__const.set_instrumentation.nop, i64 0, i64 0), i64 5)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %23, %20
%27 = phi i64 [ 69144423, %20 ], [ %17, %23 ]
%28 = and i64 %19, -1099511627776
%29 = or i64 %28, %27
%30 = call i32 (i64*, i64*, i64, ...) bitcast (i32 (...)* @atomic_compare_exchange_weak to i32 (i64*, i64*, i64, ...)*)(i64* %8, i64* %4, i64 %29)
%31 = icmp eq i32 %30, 0
br i1 %31, label %18, label %32
32: ; preds = %20, %26, %23, %2
%33 = phi i32 [ -1, %2 ], [ -1, %20 ], [ 0, %26 ], [ -1, %23 ]
ret i32 %33
}