int find_cs(int bitness) {
  unsigned short my_cs;
  __asm__("mov %%cs,%0" : "=r"(my_cs));
  if (cs_bitness(my_cs) == bitness)
    return my_cs;
  if (cs_bitness(my_cs + (2 << 3)) == bitness)
    return my_cs + (2 << 3);
  if (my_cs > (2 << 3) && cs_bitness(my_cs - (2 << 3)) == bitness)
    return my_cs - (2 << 3);
  if (cs_bitness(0x7) == bitness)
    return 0x7;
  printf("[WARN]\tCould not find %d-bit CS\n", bitness);
  return -1;
}


x86 assembly (clang, -O0):
<find_cs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %cs,%ax
mov    %ax,-0xa(%rbp)
movzwl -0xa(%rbp),%edi
mov    $0x0,%al
call   1030 <cs_bitness@plt>
cmp    -0x8(%rbp),%eax
jne    1152 <find_cs+0x32>
movzwl -0xa(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e4 <find_cs+0xc4>
movzwl -0xa(%rbp),%edi
add    $0x10,%edi
mov    $0x0,%al
call   1030 <cs_bitness@plt>
cmp    -0x8(%rbp),%eax
jne    1178 <find_cs+0x58>
movzwl -0xa(%rbp),%eax
add    $0x10,%eax
mov    %eax,-0x4(%rbp)
jmp    11e4 <find_cs+0xc4>
movzwl -0xa(%rbp),%eax
cmp    $0x10,%eax
jle    11ab <find_cs+0x8b>
movzwl -0xa(%rbp),%edi
sub    $0x10,%edi
mov    $0x0,%al
call   1030 <cs_bitness@plt>
cmp    -0x8(%rbp),%eax
jne    11ab <find_cs+0x8b>
movzwl -0xa(%rbp),%eax
sub    $0x10,%eax
mov    %eax,-0x4(%rbp)
jmp    11e4 <find_cs+0xc4>
mov    $0x7,%edi
mov    $0x0,%al
call   1030 <cs_bitness@plt>
cmp    -0x8(%rbp),%eax
jne    11cc <find_cs+0xac>
movl   $0x7,-0x4(%rbp)
jmp    11e4 <find_cs+0xc4>
mov    -0x8(%rbp),%esi
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_cs>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
mov    %cs,%r15w
movzwl %r15w,%ebx
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <cs_bitness@plt>
cmp    %r14d,%eax
jne    1144 <find_cs+0x24>
mov    %ebx,%ebp
jmp    119b <find_cs+0x7b>
lea    0x10(%rbx),%ebp
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <cs_bitness@plt>
cmp    %r14d,%eax
je     119b <find_cs+0x7b>
cmp    $0x11,%r15w
jb     116f <find_cs+0x4f>
add    $0xfffffff0,%ebx
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <cs_bitness@plt>
mov    %ebx,%ebp
cmp    %r14d,%eax
je     119b <find_cs+0x7b>
mov    $0x7,%ebp
mov    $0x7,%edi
xor    %eax,%eax
call   1030 <cs_bitness@plt>
cmp    %r14d,%eax
je     119b <find_cs+0x7b>
lea    0xe74(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [33 x i8] c"[WARN]\09Could not find %d-bit CS\0A\00"
define i32 @find_cs(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i16
store i32 %0, i32* %3
%5 = call i16 asm "mov %cs,$0", "=r,~{dirflag},~{fpsr},~{flags}"()
store i16 %5, i16* %4
%6 = load i16, i16* %4
%7 = zext i16 %6 to i32
%8 = call i32 (i32, ...) bitcast (i32 (...)* @cs_bitness to i32 (i32, ...)*)(i32 %7)
%9 = load i32, i32* %3
%10 = icmp eq i32 %8, %9
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i16, i16* %4
%13 = zext i16 %12 to i32
store i32 %13, i32* %2
br label %48
14: ; preds = %1
%15 = load i16, i16* %4
%16 = zext i16 %15 to i32
%17 = add nsw i32 %16, 16
%18 = call i32 (i32, ...) bitcast (i32 (...)* @cs_bitness to i32 (i32, ...)*)(i32 %17)
%19 = load i32, i32* %3
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %25
21: ; preds = %14
%22 = load i16, i16* %4
%23 = zext i16 %22 to i32
%24 = add nsw i32 %23, 16
store i32 %24, i32* %2
br label %48
25: ; preds = %14
%26 = load i16, i16* %4
%27 = zext i16 %26 to i32
%28 = icmp sgt i32 %27, 16
br i1 %28, label %29, label %40
29: ; preds = %25
%30 = load i16, i16* %4
%31 = zext i16 %30 to i32
%32 = sub nsw i32 %31, 16
%33 = call i32 (i32, ...) bitcast (i32 (...)* @cs_bitness to i32 (i32, ...)*)(i32 %32)
%34 = load i32, i32* %3
%35 = icmp eq i32 %33, %34
br i1 %35, label %36, label %40
36: ; preds = %29
%37 = load i16, i16* %4
%38 = zext i16 %37 to i32
%39 = sub nsw i32 %38, 16
store i32 %39, i32* %2
br label %48
40: ; preds = %29, %25
%41 = call i32 (i32, ...) bitcast (i32 (...)* @cs_bitness to i32 (i32, ...)*)(i32 7)
%42 = load i32, i32* %3
%43 = icmp eq i32 %41, %42
br i1 %43, label %44, label %45
44: ; preds = %40
store i32 7, i32* %2
br label %48
45: ; preds = %40
%46 = load i32, i32* %3
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %46)
store i32 -1, i32* %2
br label %48
48: ; preds = %45, %44, %36, %21, %11
%49 = load i32, i32* %2
ret i32 %49
}

LLVM IR (-O2):
@.str = [33 x i8] c"[WARN]\09Could not find %d-bit CS\0A\00"
define i32 @find_cs(i32 %0) {
%2 = tail call i16 asm "mov %cs,$0", "=r,~{dirflag},~{fpsr},~{flags}"()
%3 = zext i16 %2 to i32
%4 = tail call i32 (i32, ...) bitcast (i32 (...)* @cs_bitness to i32 (i32, ...)*)(i32 %3)
%5 = icmp eq i32 %4, %0
br i1 %5, label %21, label %6
6: ; preds = %1
%7 = add nuw nsw i32 %3, 16
%8 = tail call i32 (i32, ...) bitcast (i32 (...)* @cs_bitness to i32 (i32, ...)*)(i32 %7)
%9 = icmp eq i32 %8, %0
br i1 %9, label %21, label %10
10: ; preds = %6
%11 = icmp ugt i16 %2, 16
br i1 %11, label %12, label %16
12: ; preds = %10
%13 = add nsw i32 %3, -16
%14 = tail call i32 (i32, ...) bitcast (i32 (...)* @cs_bitness to i32 (i32, ...)*)(i32 %13)
%15 = icmp eq i32 %14, %0
br i1 %15, label %21, label %16
16: ; preds = %12, %10
%17 = tail call i32 (i32, ...) bitcast (i32 (...)* @cs_bitness to i32 (i32, ...)*)(i32 7)
%18 = icmp eq i32 %17, %0
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %21
21: ; preds = %16, %12, %6, %1, %19
%22 = phi i32 [ -1, %19 ], [ %3, %1 ], [ %7, %6 ], [ %13, %12 ], [ 7, %16 ]
ret i32 %22
}