// File: /workspace/github_c_makefile_star2_O3/gplessis[P]dotdeb-redis/src/bitops.c
// Function: redisBitpos
long redisBitpos(void *s, unsigned long count, int bit) {
    unsigned long *l;
    unsigned char *c;
    unsigned long skipval, word = 0, one;
    long pos = 0; /* Position of bit, to return to the caller. */
    unsigned long j;

    /* Process whole words first, seeking for first word that is not
     * all ones or all zeros respectively if we are lookig for zeros
     * or ones. This is much faster with large strings having contiguous
     * blocks of 1 or 0 bits compared to the vanilla bit per bit processing.
     *
     * Note that if we start from an address that is not aligned
     * to sizeof(unsigned long) we consume it byte by byte until it is
     * aligned. */

    /* Skip initial bits not aligned to sizeof(unsigned long) byte by byte. */
    skipval = bit ? 0 : UCHAR_MAX;
    c = (unsigned char*) s;
    while((unsigned long)c & (sizeof(*l)-1) && count) {
        if (*c != skipval) break;
        c++;
        count--;
        pos += 8;
    }

    /* Skip bits with full word step. */
    skipval = bit ? 0 : ULONG_MAX;
    l = (unsigned long*) c;
    while (count >= sizeof(*l)) {
        if (*l != skipval) break;
        l++;
        count -= sizeof(*l);
        pos += sizeof(*l)*8;
    }

    /* Load bytes into "word" considering the first byte as the most significant
     * (we basically consider it as written in big endian, since we consider the
     * string as a set of bits from left to right, with the first bit at position
     * zero.
     *
     * Note that the loading is designed to work even when the bytes left
     * (count) are less than a full word. We pad it with zero on the right. */
    c = (unsigned char*)l;
    for (j = 0; j < sizeof(*l); j++) {
        word <<= 8;
        if (count) {
            word |= *c;
            c++;
            count--;
        }
    }

    /* Special case:
     * If bits in the string are all zero and we are looking for one,
     * return -1 to signal that there is not a single "1" in the whole
     * string. This can't happen when we are looking for "0" as we assume
     * that the right of the string is zero padded. */
    if (bit == 1 && word == 0) return -1;

    /* Last word left, scan bit by bit. The first thing we need is to
     * have a single "1" set in the most significant position in an
     * unsigned long. We don't know the size of the long so we use a
     * simple trick. */
    one = ULONG_MAX; /* All bits set to 1.*/
    one >>= 1;       /* All bits set to 1 but the MSB. */
    one = ~one;      /* All bits set to 0 but the MSB. */

    while(one) {
        if (((one & word) != 0) == bit) return pos;
        pos++;
        one >>= 1;
    }

    /* If we reached this point, there is a bug in the algorithm, since
     * the case of no match is handled as a special case before. */
    serverPanic("End of redisBitpos() reached.");
    return 0; /* Just to avoid warnings. */
}

// Assembly:
pushq %rax
xorl %ecx, %ecx
testl %edx, %edx
movl $0xff, %r8d
cmovneq %rcx, %r8
testb $0x7, %dil
sete %al
testq %rsi, %rsi
sete %r9b
orb %al, %r9b
jne 0x73a74
leaq 0x1(%rdi), %r9
xorl %ecx, %ecx
movzbl (%rdi), %eax
cmpl %eax, %r8d
jne 0x73a74
incq %rdi
leaq -0x1(%rsi), %rax
addq $0x8, %rcx
testb $0x7, %r9b
je 0x73a77
incq %r9
cmpq $0x1, %rsi
movq %rax, %rsi
jne 0x73a4d
jmp 0x73a77
movq %rsi, %rax
xorl %esi, %esi
cmpl $0x1, %edx
sbbq %rsi, %rsi
cmpq $0x8, %rax
jb 0x73a9c
cmpq %rsi, (%rdi)
jne 0x73a9c
addq $0x8, %rdi
addq $-0x8, %rax
addq $0x40, %rcx
cmpq $0x7, %rax
ja 0x73a85
movl $0x8, %r8d
xorl %esi, %esi
shlq $0x8, %rsi
testq %rax, %rax
je 0x73abc
movzbl (%rdi), %r9d
orq %r9, %rsi
incq %rdi
decq %rax
jmp 0x73abe
xorl %eax, %eax
decq %r8
jne 0x73aa4
cmpl $0x1, %edx
jne 0x73ad4
movq $-0x1, %rax
testq %rsi, %rsi
je 0x73b22
movabsq $-0x8000000000000000, %rdi # imm = 0x8000000000000000
xorl %eax, %eax
xorl %r8d, %r8d
testq %rsi, %rdi
setne %r8b
cmpl %edx, %r8d
je 0x73b1c
shrq %rdi
incq %rax
cmpl $0x40, %eax
jne 0x73ae0
leaq 0x5842f(%rip), %rdi     # 0xcbf30
leaq 0x58448(%rip), %rsi     # 0xcbf50
movl $0xb1, %edx
callq 0x619b4
movl $0x1, %edi
callq 0x263c0
addq %rax, %rcx
movq %rcx, %rax
popq %rcx
retq