void test_bsx(void) {
  {
    int res, val, resz;
    val = 0;
    asm("xorl %1, %1\n"
        "movl $0x12345678, %0\n"
        "bsrw"
        " %"
        "w"
        "2, %"
        "w"
        "0 ; setz %b1"
        : "=r"(res), "=q"(resz)
        : "r"(val));
    printf("%-10s A=%08x R=%08x %d\n", "bsrw", val, res, resz);
  };
  {
    int res, val, resz;
    val = 0x12340128;
    asm("xorl %1, %1\n"
        "movl $0x12345678, %0\n"
        "bsrw"
        " %"
        "w"
        "2, %"
        "w"
        "0 ; setz %b1"
        : "=r"(res), "=q"(resz)
        : "r"(val));
    printf("%-10s A=%08x R=%08x %d\n", "bsrw", val, res, resz);
  };
  {
    int res, val, resz;
    val = 0;
    asm("xorl %1, %1\n"
        "movl $0x12345678, %0\n"
        "bsrl"
        " %"
        ""
        "2, %"
        ""
        "0 ; setz %b1"
        : "=r"(res), "=q"(resz)
        : "r"(val));
    printf("%-10s A=%08x R=%08x %d\n", "bsrl", val, res, resz);
  };
  {
    int res, val, resz;
    val = 0x00340128;
    asm("xorl %1, %1\n"
        "movl $0x12345678, %0\n"
        "bsrl"
        " %"
        ""
        "2, %"
        ""
        "0 ; setz %b1"
        : "=r"(res), "=q"(resz)
        : "r"(val));
    printf("%-10s A=%08x R=%08x %d\n", "bsrl", val, res, resz);
  };
  {
    int res, val, resz;
    val = 0;
    asm("xorl %1, %1\n"
        "movl $0x12345678, %0\n"
        "bsfw"
        " %"
        "w"
        "2, %"
        "w"
        "0 ; setz %b1"
        : "=r"(res), "=q"(resz)
        : "r"(val));
    printf("%-10s A=%08x R=%08x %d\n", "bsfw", val, res, resz);
  };
  {
    int res, val, resz;
    val = 0x12340128;
    asm("xorl %1, %1\n"
        "movl $0x12345678, %0\n"
        "bsfw"
        " %"
        "w"
        "2, %"
        "w"
        "0 ; setz %b1"
        : "=r"(res), "=q"(resz)
        : "r"(val));
    printf("%-10s A=%08x R=%08x %d\n", "bsfw", val, res, resz);
  };
  {
    int res, val, resz;
    val = 0;
    asm("xorl %1, %1\n"
        "movl $0x12345678, %0\n"
        "bsfl"
        " %"
        ""
        "2, %"
        ""
        "0 ; setz %b1"
        : "=r"(res), "=q"(resz)
        : "r"(val));
    printf("%-10s A=%08x R=%08x %d\n", "bsfl", val, res, resz);
  };
  {
    int res, val, resz;
    val = 0x00340128;
    asm("xorl %1, %1\n"
        "movl $0x12345678, %0\n"
        "bsfl"
        " %"
        ""
        "2, %"
        ""
        "0 ; setz %b1"
        : "=r"(res), "=q"(resz)
        : "r"(val));
    printf("%-10s A=%08x R=%08x %d\n", "bsfl", val, res, resz);
  };
}


x86 assembly (clang, -O0):
<test_bsx>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
xor    %eax,%eax
mov    $0x12345678,%ecx
bsr    %ax,%cx
sete   %al
mov    %ecx,-0x4(%rbp)
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edx
mov    -0x4(%rbp),%ecx
mov    -0xc(%rbp),%r8d
lea    0xeb9(%rip),%rdi
lea    0xeca(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x12340128,-0x14(%rbp)
mov    -0x14(%rbp),%eax
xor    %eax,%eax
mov    $0x12345678,%ecx
bsr    %ax,%cx
sete   %al
mov    %ecx,-0x10(%rbp)
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edx
mov    -0x10(%rbp),%ecx
mov    -0x18(%rbp),%r8d
lea    0xe7c(%rip),%rdi
lea    0xe8d(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
xor    %eax,%eax
mov    $0x12345678,%ecx
bsr    %eax,%ecx
sete   %al
mov    %ecx,-0x1c(%rbp)
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%edx
mov    -0x1c(%rbp),%ecx
mov    -0x24(%rbp),%r8d
lea    0xe40(%rip),%rdi
lea    0xe56(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x340128,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
xor    %eax,%eax
mov    $0x12345678,%ecx
bsr    %eax,%ecx
sete   %al
mov    %ecx,-0x28(%rbp)
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%edx
mov    -0x28(%rbp),%ecx
mov    -0x30(%rbp),%r8d
lea    0xe04(%rip),%rdi
lea    0xe1a(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
xor    %eax,%eax
mov    $0x12345678,%ecx
bsf    %ax,%cx
sete   %al
mov    %ecx,-0x34(%rbp)
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%edx
mov    -0x34(%rbp),%ecx
mov    -0x3c(%rbp),%r8d
lea    0xdc7(%rip),%rdi
lea    0xde2(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x12340128,-0x44(%rbp)
mov    -0x44(%rbp),%eax
xor    %eax,%eax
mov    $0x12345678,%ecx
bsf    %ax,%cx
sete   %al
mov    %ecx,-0x40(%rbp)
mov    %eax,-0x48(%rbp)
mov    -0x44(%rbp),%edx
mov    -0x40(%rbp),%ecx
mov    -0x48(%rbp),%r8d
lea    0xd8a(%rip),%rdi
lea    0xda5(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x50(%rbp)
mov    -0x50(%rbp),%eax
xor    %eax,%eax
mov    $0x12345678,%ecx
bsf    %eax,%ecx
sete   %al
mov    %ecx,-0x4c(%rbp)
mov    %eax,-0x54(%rbp)
mov    -0x50(%rbp),%edx
mov    -0x4c(%rbp),%ecx
mov    -0x54(%rbp),%r8d
lea    0xd4e(%rip),%rdi
lea    0xd6e(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x340128,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
xor    %eax,%eax
mov    $0x12345678,%ecx
bsf    %eax,%ecx
sete   %al
mov    %ecx,-0x58(%rbp)
mov    %eax,-0x60(%rbp)
mov    -0x5c(%rbp),%edx
mov    -0x58(%rbp),%ecx
mov    -0x60(%rbp),%r8d
lea    0xd12(%rip),%rdi
lea    0xd32(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_bsx>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %ebp,%ebp
xor    %r8d,%r8d
mov    $0x12345678,%ecx
bsr    %bp,%cx
sete   %r8b
lea    0xecf(%rip),%rbx
lea    0xee0(%rip),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x12340128,%r15d
xor    %r8d,%r8d
mov    $0x12345678,%ecx
bsr    %r15w,%cx
sete   %r8b
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x12340128,%edx
xor    %eax,%eax
call   1030 <printf@plt>
xor    %r8d,%r8d
mov    $0x12345678,%ecx
bsr    %ebp,%ecx
sete   %r8b
lea    0xe97(%rip),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x340128,%r12d
xor    %r8d,%r8d
mov    $0x12345678,%ecx
bsr    %r12d,%ecx
sete   %r8b
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x340128,%edx
xor    %eax,%eax
call   1030 <printf@plt>
xor    %r8d,%r8d
mov    $0x12345678,%ecx
bsf    %bp,%cx
sete   %r8b
lea    0xe4e(%rip),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
xor    %eax,%eax
call   1030 <printf@plt>
xor    %r8d,%r8d
mov    $0x12345678,%ecx
bsf    %r15w,%cx
sete   %r8b
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x12340128,%edx
xor    %eax,%eax
call   1030 <printf@plt>
xor    %r8d,%r8d
mov    $0x12345678,%ecx
bsf    %ebp,%ecx
sete   %r8b
lea    0xe0b(%rip),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
xor    %eax,%eax
call   1030 <printf@plt>
xor    %r8d,%r8d
mov    $0x12345678,%ecx
bsf    %r12d,%ecx
sete   %r8b
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x340128,%edx
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <printf@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"%-10s A=%08x R=%08x %d\0A\00"
@.str.1 = [5 x i8] c"bsrw\00"
@.str.2 = [5 x i8] c"bsrl\00"
@.str.3 = [5 x i8] c"bsfw\00"
@.str.4 = [5 x i8] c"bsfl\00"
define void @test_bsx() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca i32
%21 = alloca i32
%22 = alloca i32
%23 = alloca i32
%24 = alloca i32
store i32 0, i32* %2
%25 = load i32, i32* %2
%26 = call { i32, i32 } asm "xorl $1, $1\0Amovl $$0x12345678, $0\0Absrw ${2:w}, ${0:w} ; setz ${1:b}", "=r,=q,r,~{dirflag},~{fpsr},~{flags}"(i32 %25)
%27 = extractvalue { i32, i32 } %26, 0
%28 = extractvalue { i32, i32 } %26, 1
store i32 %27, i32* %1
store i32 %28, i32* %3
%29 = load i32, i32* %2
%30 = load i32, i32* %1
%31 = load i32, i32* %3
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %29, i32 %30, i32 %31)
store i32 305398056, i32* %5
%33 = load i32, i32* %5
%34 = call { i32, i32 } asm "xorl $1, $1\0Amovl $$0x12345678, $0\0Absrw ${2:w}, ${0:w} ; setz ${1:b}", "=r,=q,r,~{dirflag},~{fpsr},~{flags}"(i32 %33)
%35 = extractvalue { i32, i32 } %34, 0
%36 = extractvalue { i32, i32 } %34, 1
store i32 %35, i32* %4
store i32 %36, i32* %6
%37 = load i32, i32* %5
%38 = load i32, i32* %4
%39 = load i32, i32* %6
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %37, i32 %38, i32 %39)
store i32 0, i32* %8
%41 = load i32, i32* %8
%42 = call { i32, i32 } asm "xorl $1, $1\0Amovl $$0x12345678, $0\0Absrl $2, $0 ; setz ${1:b}", "=r,=q,r,~{dirflag},~{fpsr},~{flags}"(i32 %41)
%43 = extractvalue { i32, i32 } %42, 0
%44 = extractvalue { i32, i32 } %42, 1
store i32 %43, i32* %7
store i32 %44, i32* %9
%45 = load i32, i32* %8
%46 = load i32, i32* %7
%47 = load i32, i32* %9
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %45, i32 %46, i32 %47)
store i32 3408168, i32* %11
%49 = load i32, i32* %11
%50 = call { i32, i32 } asm "xorl $1, $1\0Amovl $$0x12345678, $0\0Absrl $2, $0 ; setz ${1:b}", "=r,=q,r,~{dirflag},~{fpsr},~{flags}"(i32 %49)
%51 = extractvalue { i32, i32 } %50, 0
%52 = extractvalue { i32, i32 } %50, 1
store i32 %51, i32* %10
store i32 %52, i32* %12
%53 = load i32, i32* %11
%54 = load i32, i32* %10
%55 = load i32, i32* %12
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %53, i32 %54, i32 %55)
store i32 0, i32* %14
%57 = load i32, i32* %14
%58 = call { i32, i32 } asm "xorl $1, $1\0Amovl $$0x12345678, $0\0Absfw ${2:w}, ${0:w} ; setz ${1:b}", "=r,=q,r,~{dirflag},~{fpsr},~{flags}"(i32 %57)
%59 = extractvalue { i32, i32 } %58, 0
%60 = extractvalue { i32, i32 } %58, 1
store i32 %59, i32* %13
store i32 %60, i32* %15
%61 = load i32, i32* %14
%62 = load i32, i32* %13
%63 = load i32, i32* %15
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %61, i32 %62, i32 %63)
store i32 305398056, i32* %17
%65 = load i32, i32* %17
%66 = call { i32, i32 } asm "xorl $1, $1\0Amovl $$0x12345678, $0\0Absfw ${2:w}, ${0:w} ; setz ${1:b}", "=r,=q,r,~{dirflag},~{fpsr},~{flags}"(i32 %65)
%67 = extractvalue { i32, i32 } %66, 0
%68 = extractvalue { i32, i32 } %66, 1
store i32 %67, i32* %16
store i32 %68, i32* %18
%69 = load i32, i32* %17
%70 = load i32, i32* %16
%71 = load i32, i32* %18
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %69, i32 %70, i32 %71)
store i32 0, i32* %20
%73 = load i32, i32* %20
%74 = call { i32, i32 } asm "xorl $1, $1\0Amovl $$0x12345678, $0\0Absfl $2, $0 ; setz ${1:b}", "=r,=q,r,~{dirflag},~{fpsr},~{flags}"(i32 %73)
%75 = extractvalue { i32, i32 } %74, 0
%76 = extractvalue { i32, i32 } %74, 1
store i32 %75, i32* %19
store i32 %76, i32* %21
%77 = load i32, i32* %20
%78 = load i32, i32* %19
%79 = load i32, i32* %21
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %77, i32 %78, i32 %79)
store i32 3408168, i32* %23
%81 = load i32, i32* %23
%82 = call { i32, i32 } asm "xorl $1, $1\0Amovl $$0x12345678, $0\0Absfl $2, $0 ; setz ${1:b}", "=r,=q,r,~{dirflag},~{fpsr},~{flags}"(i32 %81)
%83 = extractvalue { i32, i32 } %82, 0
%84 = extractvalue { i32, i32 } %82, 1
store i32 %83, i32* %22
store i32 %84, i32* %24
%85 = load i32, i32* %23
%86 = load i32, i32* %22
%87 = load i32, i32* %24
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %85, i32 %86, i32 %87)
ret void
}

LLVM IR (-O2):
@.str = [24 x i8] c"%-10s A=%08x R=%08x %d\0A\00"
@.str.1 = [5 x i8] c"bsrw\00"
@.str.2 = [5 x i8] c"bsrl\00"
@.str.3 = [5 x i8] c"bsfw\00"
@.str.4 = [5 x i8] c"bsfl\00"
define void @test_bsx() {
%1 = tail call { i32, i32 } asm "xorl $1, $1\0Amovl $$0x12345678, $0\0Absrw ${2:w}, ${0:w} ; setz ${1:b}", "=r,=q,r,~{dirflag},~{fpsr},~{flags}"(i32 0)
%2 = extractvalue { i32, i32 } %1, 0
%3 = extractvalue { i32, i32 } %1, 1
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 0, i32 %2, i32 %3)
%5 = tail call { i32, i32 } asm "xorl $1, $1\0Amovl $$0x12345678, $0\0Absrw ${2:w}, ${0:w} ; setz ${1:b}", "=r,=q,r,~{dirflag},~{fpsr},~{flags}"(i32 305398056)
%6 = extractvalue { i32, i32 } %5, 0
%7 = extractvalue { i32, i32 } %5, 1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 305398056, i32 %6, i32 %7)
%9 = tail call { i32, i32 } asm "xorl $1, $1\0Amovl $$0x12345678, $0\0Absrl $2, $0 ; setz ${1:b}", "=r,=q,r,~{dirflag},~{fpsr},~{flags}"(i32 0)
%10 = extractvalue { i32, i32 } %9, 0
%11 = extractvalue { i32, i32 } %9, 1
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 0, i32 %10, i32 %11)
%13 = tail call { i32, i32 } asm "xorl $1, $1\0Amovl $$0x12345678, $0\0Absrl $2, $0 ; setz ${1:b}", "=r,=q,r,~{dirflag},~{fpsr},~{flags}"(i32 3408168)
%14 = extractvalue { i32, i32 } %13, 0
%15 = extractvalue { i32, i32 } %13, 1
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 3408168, i32 %14, i32 %15)
%17 = tail call { i32, i32 } asm "xorl $1, $1\0Amovl $$0x12345678, $0\0Absfw ${2:w}, ${0:w} ; setz ${1:b}", "=r,=q,r,~{dirflag},~{fpsr},~{flags}"(i32 0)
%18 = extractvalue { i32, i32 } %17, 0
%19 = extractvalue { i32, i32 } %17, 1
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 0, i32 %18, i32 %19)
%21 = tail call { i32, i32 } asm "xorl $1, $1\0Amovl $$0x12345678, $0\0Absfw ${2:w}, ${0:w} ; setz ${1:b}", "=r,=q,r,~{dirflag},~{fpsr},~{flags}"(i32 305398056)
%22 = extractvalue { i32, i32 } %21, 0
%23 = extractvalue { i32, i32 } %21, 1
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 305398056, i32 %22, i32 %23)
%25 = tail call { i32, i32 } asm "xorl $1, $1\0Amovl $$0x12345678, $0\0Absfl $2, $0 ; setz ${1:b}", "=r,=q,r,~{dirflag},~{fpsr},~{flags}"(i32 0)
%26 = extractvalue { i32, i32 } %25, 0
%27 = extractvalue { i32, i32 } %25, 1
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 0, i32 %26, i32 %27)
%29 = tail call { i32, i32 } asm "xorl $1, $1\0Amovl $$0x12345678, $0\0Absfl $2, $0 ; setz ${1:b}", "=r,=q,r,~{dirflag},~{fpsr},~{flags}"(i32 3408168)
%30 = extractvalue { i32, i32 } %29, 0
%31 = extractvalue { i32, i32 } %29, 1
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 3408168, i32 %30, i32 %31)
ret void
}