// Wave-6 slice 6-assembler: bit-manipulation group (bt/bts/btr/btc, bsf/bsr,
// popcnt, shld/shrd, xadd) — encoder expansion group
// Expected exit code: 42
section .text
global main
main:
    xor eax, eax
    bts rax, 5            ; rax = 32
    bt rax, 5             ; CF = 1
    setc cl               ; cl = 1
    btr rax, 5            ; rax = 0, CF = 1
    btc rax, 3            ; rax = 8
    bsf rdx, rax          ; rdx = 3 (index of lowest set bit)
    bsr rdx, rax          ; rdx = 3 (index of highest set bit)
    mov r8, 0xF0
    popcnt r9, r8         ; r9 = 4
    ; shrd dest, src, n: dest = (dest >> n) | (src << (64-n))
    mov r10, 0
    mov r11, 1
    shrd r10, r11, 60     ; r10 = 0 | (1 << 4) = 16
    ; shld dest, src, n: dest = (dest << n) | (src >> (64-n))
    mov r14, 1
    xor r15, r15
    shld r14, r15, 4      ; r14 = 16 | 0 = 16
    cmp r14, 16
    jne fail
    mov r12, 2
    mov r13, 3
    xadd r12, r13         ; r12 = 5, r13 = 2
    add eax, edx          ; 8 + 3 = 11
    add eax, r9d          ; 15
    add eax, r10d         ; 31
    add eax, r12d         ; 36
    add eax, r13d         ; 38
    movzx ecx, cl         ; ecx = 1 (from setc cl)
    add eax, ecx          ; 39
    add eax, 3            ; 42
    ret
fail:
    mov eax, 1
    ret
