unsigned long ibs_event_to_counter(unsigned long x) {
  unsigned long ret = ~0UL;
  if ((0xf000 <= x && x <= 0xf00e))
    ret = (x - 0xf000);
  else if ((0xf100 <= x && x <= 0xf109))
    ret = (x - 0xf100 + (0xf00e - 0xf000 + 1));
  else if ((0xf200 <= x && x <= 0xf219))
    ret = (x - 0xf200 + (0xf109 - 0xf100 + 1) + (0xf00e - 0xf000 + 1));
  else if ((0xf240 <= x && x <= 0xf24c))
    ret = (x - 0xf240 + (0xf219 - 0xf200 + 1) + (0xf109 - 0xf100 + 1) +
           (0xf00e - 0xf000 + 1));
  return (ret != ~0UL) ? ret + 8 : ret;
}


x86 assembly (clang, -O0):
<ibs_event_to_counter>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
movq   $0xffffffffffffffff,-0x10(%rbp)
ff
mov    $0xf000,%eax
cmp    -0x8(%rbp),%rax
ja     1140 <ibs_event_to_counter+0x40>
cmpq   $0xf00e,-0x8(%rbp)
ja     1140 <ibs_event_to_counter+0x40>
mov    -0x8(%rbp),%rax
sub    $0xf000,%rax
mov    %rax,-0x10(%rbp)
jmp    11f2 <ibs_event_to_counter+0xf2>
mov    $0xf100,%eax
cmp    -0x8(%rbp),%rax
ja     1174 <ibs_event_to_counter+0x74>
cmpq   $0xf109,-0x8(%rbp)
ja     1174 <ibs_event_to_counter+0x74>
mov    -0x8(%rbp),%rax
sub    $0xf100,%rax
add    $0xf,%rax
mov    %rax,-0x10(%rbp)
jmp    11ed <ibs_event_to_counter+0xed>
mov    $0xf200,%eax
cmp    -0x8(%rbp),%rax
ja     11ac <ibs_event_to_counter+0xac>
cmpq   $0xf219,-0x8(%rbp)
ja     11ac <ibs_event_to_counter+0xac>
mov    -0x8(%rbp),%rax
sub    $0xf200,%rax
add    $0xa,%rax
add    $0xf,%rax
mov    %rax,-0x10(%rbp)
jmp    11e8 <ibs_event_to_counter+0xe8>
mov    $0xf240,%eax
cmp    -0x8(%rbp),%rax
ja     11e3 <ibs_event_to_counter+0xe3>
cmpq   $0xf24c,-0x8(%rbp)
ja     11e3 <ibs_event_to_counter+0xe3>
mov    -0x8(%rbp),%rax
sub    $0xf240,%rax
add    $0x1a,%rax
add    $0xa,%rax
add    $0xf,%rax
mov    %rax,-0x10(%rbp)
jmp    11e8 <ibs_event_to_counter+0xe8>
jmp    11ed <ibs_event_to_counter+0xed>
jmp    11f2 <ibs_event_to_counter+0xf2>
cmpq   $0xffffffffffffffff,-0x10(%rbp)
je     120e <ibs_event_to_counter+0x10e>
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x18(%rbp)
jmp    1216 <ibs_event_to_counter+0x116>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<ibs_event_to_counter>:
lea    -0xf000(%rdi),%rcx
cmp    $0xf,%rcx
jb     115c <ibs_event_to_counter+0x5c>
lea    -0xf100(%rdi),%rax
cmp    $0x9,%rax
ja     1126 <ibs_event_to_counter+0x26>
add    $0xffffffffffff0f0f,%rdi
mov    %rdi,%rcx
jmp    115c <ibs_event_to_counter+0x5c>
lea    -0xf200(%rdi),%rax
cmp    $0x19,%rax
ja     113f <ibs_event_to_counter+0x3f>
add    $0xffffffffffff0e19,%rdi
mov    %rdi,%rcx
jmp    115c <ibs_event_to_counter+0x5c>
lea    -0xf240(%rdi),%rax
add    $0xffffffffffff0df3,%rdi
cmp    $0xd,%rax
mov    $0xffffffffffffffff,%rcx
cmovb  %rdi,%rcx
lea    0x8(%rcx),%rax
cmp    $0xffffffffffffffff,%rcx
cmove  %rcx,%rax
ret

LLVM IR (-O0):
define i64 @ibs_event_to_counter(i64 %0) {
%2 = alloca i64
%3 = alloca i64
store i64 %0, i64* %2
store i64 -1, i64* %3
%4 = load i64, i64* %2
%5 = icmp ule i64 61440, %4
br i1 %5, label %6, label %12
6: ; preds = %1
%7 = load i64, i64* %2
%8 = icmp ule i64 %7, 61454
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = load i64, i64* %2
%11 = sub i64 %10, 61440
store i64 %11, i64* %3
br label %48
12: ; preds = %6, %1
%13 = load i64, i64* %2
%14 = icmp ule i64 61696, %13
br i1 %14, label %15, label %22
15: ; preds = %12
%16 = load i64, i64* %2
%17 = icmp ule i64 %16, 61705
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i64, i64* %2
%20 = sub i64 %19, 61696
%21 = add i64 %20, 15
store i64 %21, i64* %3
br label %47
22: ; preds = %15, %12
%23 = load i64, i64* %2
%24 = icmp ule i64 61952, %23
br i1 %24, label %25, label %33
25: ; preds = %22
%26 = load i64, i64* %2
%27 = icmp ule i64 %26, 61977
br i1 %27, label %28, label %33
28: ; preds = %25
%29 = load i64, i64* %2
%30 = sub i64 %29, 61952
%31 = add i64 %30, 10
%32 = add i64 %31, 15
store i64 %32, i64* %3
br label %46
33: ; preds = %25, %22
%34 = load i64, i64* %2
%35 = icmp ule i64 62016, %34
br i1 %35, label %36, label %45
36: ; preds = %33
%37 = load i64, i64* %2
%38 = icmp ule i64 %37, 62028
br i1 %38, label %39, label %45
39: ; preds = %36
%40 = load i64, i64* %2
%41 = sub i64 %40, 62016
%42 = add i64 %41, 26
%43 = add i64 %42, 10
%44 = add i64 %43, 15
store i64 %44, i64* %3
br label %45
45: ; preds = %39, %36, %33
br label %46
46: ; preds = %45, %28
br label %47
47: ; preds = %46, %18
br label %48
48: ; preds = %47, %9
%49 = load i64, i64* %3
%50 = icmp ne i64 %49, -1
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i64, i64* %3
%53 = add i64 %52, 8
br label %56
54: ; preds = %48
%55 = load i64, i64* %3
br label %56
56: ; preds = %54, %51
%57 = phi i64 [ %53, %51 ], [ %55, %54 ]
ret i64 %57
}

LLVM IR (-O2):
define i64 @ibs_event_to_counter(i64 %0) {
%2 = add i64 %0, -61440
%3 = icmp ult i64 %2, 15
br i1 %3, label %19, label %4
4: ; preds = %1
%5 = add i64 %0, -61696
%6 = icmp ult i64 %5, 10
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = add nsw i64 %0, -61681
br label %19
9: ; preds = %4
%10 = add i64 %0, -61952
%11 = icmp ult i64 %10, 26
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = add nsw i64 %0, -61927
br label %19
14: ; preds = %9
%15 = add i64 %0, -62016
%16 = icmp ult i64 %15, 13
%17 = add i64 %0, -61965
%18 = select i1 %16, i64 %17, i64 -1
br label %19
19: ; preds = %1, %14, %7, %12
%20 = phi i64 [ %8, %7 ], [ %13, %12 ], [ %18, %14 ], [ %2, %1 ]
%21 = icmp eq i64 %20, -1
%22 = add i64 %20, 8
%23 = select i1 %21, i64 -1, i64 %22
ret i64 %23
}