unsigned nul_byte_index_loongson(unsigned r) {
  if (r & 0x01)
    return 0;
  if (r & 0x02)
    return 1;
  if (r & 0x04)
    return 2;
  if (r & 0x08)
    return 3;
  if (r & 0x10)
    return 4;
  if (r & 0x20)
    return 5;
  if (r & 0x40)
    return 6;
  return 7;
}


x86 assembly (clang, -O0):
<nul_byte_index_loongson>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1122 <nul_byte_index_loongson+0x22>
movl   $0x0,-0x4(%rbp)
jmp    11cb <nul_byte_index_loongson+0xcb>
mov    -0x8(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     113d <nul_byte_index_loongson+0x3d>
movl   $0x1,-0x4(%rbp)
jmp    11cb <nul_byte_index_loongson+0xcb>
mov    -0x8(%rbp),%eax
and    $0x4,%eax
cmp    $0x0,%eax
je     1158 <nul_byte_index_loongson+0x58>
movl   $0x2,-0x4(%rbp)
jmp    11cb <nul_byte_index_loongson+0xcb>
mov    -0x8(%rbp),%eax
and    $0x8,%eax
cmp    $0x0,%eax
je     1173 <nul_byte_index_loongson+0x73>
movl   $0x3,-0x4(%rbp)
jmp    11cb <nul_byte_index_loongson+0xcb>
mov    -0x8(%rbp),%eax
and    $0x10,%eax
cmp    $0x0,%eax
je     118e <nul_byte_index_loongson+0x8e>
movl   $0x4,-0x4(%rbp)
jmp    11cb <nul_byte_index_loongson+0xcb>
mov    -0x8(%rbp),%eax
and    $0x20,%eax
cmp    $0x0,%eax
je     11a9 <nul_byte_index_loongson+0xa9>
movl   $0x5,-0x4(%rbp)
jmp    11cb <nul_byte_index_loongson+0xcb>
mov    -0x8(%rbp),%eax
and    $0x40,%eax
cmp    $0x0,%eax
je     11c4 <nul_byte_index_loongson+0xc4>
movl   $0x6,-0x4(%rbp)
jmp    11cb <nul_byte_index_loongson+0xcb>
movl   $0x7,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<nul_byte_index_loongson>:
xor    %eax,%eax
test   $0x1,%dil
jne    114b <nul_byte_index_loongson+0x4b>
mov    $0x1,%eax
test   $0x2,%dil
jne    114b <nul_byte_index_loongson+0x4b>
mov    $0x2,%eax
test   $0x4,%dil
jne    114b <nul_byte_index_loongson+0x4b>
mov    $0x3,%eax
test   $0x8,%dil
jne    114b <nul_byte_index_loongson+0x4b>
mov    $0x4,%eax
test   $0x10,%dil
jne    114b <nul_byte_index_loongson+0x4b>
mov    $0x5,%eax
test   $0x20,%dil
jne    114b <nul_byte_index_loongson+0x4b>
xor    %eax,%eax
test   $0x40,%dil
sete   %al
or     $0x6,%eax
ret

LLVM IR (-O0):
define i32 @nul_byte_index_loongson(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
%5 = and i32 %4, 1
%6 = icmp ne i32 %5, 0
br i1 %6, label %7, label %8
7: ; preds = %1
store i32 0, i32* %2
br label %39
8: ; preds = %1
%9 = load i32, i32* %3
%10 = and i32 %9, 2
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %8
store i32 1, i32* %2
br label %39
13: ; preds = %8
%14 = load i32, i32* %3
%15 = and i32 %14, 4
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %13
store i32 2, i32* %2
br label %39
18: ; preds = %13
%19 = load i32, i32* %3
%20 = and i32 %19, 8
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %18
store i32 3, i32* %2
br label %39
23: ; preds = %18
%24 = load i32, i32* %3
%25 = and i32 %24, 16
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %23
store i32 4, i32* %2
br label %39
28: ; preds = %23
%29 = load i32, i32* %3
%30 = and i32 %29, 32
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %28
store i32 5, i32* %2
br label %39
33: ; preds = %28
%34 = load i32, i32* %3
%35 = and i32 %34, 64
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %33
store i32 6, i32* %2
br label %39
38: ; preds = %33
store i32 7, i32* %2
br label %39
39: ; preds = %38, %37, %32, %27, %22, %17, %12, %7
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
define i32 @nul_byte_index_loongson(i32 %0) {
%2 = and i32 %0, 1
%3 = icmp eq i32 %2, 0
br i1 %3, label %4, label %23
4: ; preds = %1
%5 = and i32 %0, 2
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %23
7: ; preds = %4
%8 = and i32 %0, 4
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %23
10: ; preds = %7
%11 = and i32 %0, 8
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %23
13: ; preds = %10
%14 = and i32 %0, 16
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %13
%17 = and i32 %0, 32
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = and i32 %0, 64
%21 = icmp eq i32 %20, 0
%22 = select i1 %21, i32 7, i32 6
br label %23
23: ; preds = %19, %16, %13, %10, %7, %4, %1
%24 = phi i32 [ 0, %1 ], [ 1, %4 ], [ 2, %7 ], [ 3, %10 ], [ 4, %13 ], [ 5, %16 ], [ %22, %19 ]
ret i32 %24
}