#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
struct rlite {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ free(unsigned char *);
int rl_getrange(struct rlite *, unsigned char const *, long, long, long,
                unsigned char **, long *);
int rl_getbit(struct rlite *db, const unsigned char *key, long keylen,
              long bitoffset, int *value) {
  int retval;
  unsigned char *rangevalue = NULL;
  long rangevaluelen, start;
  start = bitoffset >> 3;
  long bit = 7 - (bitoffset & 0x7);
  retval =
      rl_getrange(db, key, keylen, start, start, &rangevalue, &rangevaluelen);
  if (0 != retval && 3 != retval)
    goto cleanup;
  ;
  if (retval == 3 || rangevaluelen == 0) {
    *value = 0;
    retval = 0;
    goto cleanup;
  }
  *value = (rangevalue[0] & (1 << bit)) ? 1 : 0;
  retval = 0;
cleanup:
  free(rangevalue);
  return retval;
}


x86 assembly (clang, -O0):
<rl_getbit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x20(%rbp),%rax
sar    $0x3,%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rcx
and    $0x7,%rcx
mov    $0x7,%eax
sub    %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x48(%rbp),%rcx
mov    -0x48(%rbp),%r8
lea    -0x38(%rbp),%r9
lea    -0x40(%rbp),%rax
mov    %rax,(%rsp)
call   1040 <rl_getrange@plt>
mov    %eax,-0x2c(%rbp)
xor    %eax,%eax
cmp    -0x2c(%rbp),%eax
je     11aa <rl_getbit+0x8a>
mov    $0x3,%eax
cmp    -0x2c(%rbp),%eax
je     11aa <rl_getbit+0x8a>
jmp    1205 <rl_getbit+0xe5>
cmpl   $0x3,-0x2c(%rbp)
je     11bf <rl_getbit+0x9f>
cmpq   $0x0,-0x40(%rbp)
jne    11d5 <rl_getbit+0xb5>
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x2c(%rbp)
jmp    1205 <rl_getbit+0xe5>
mov    -0x38(%rbp),%rax
movzbl (%rax),%edx
mov    -0x50(%rbp),%rax
mov    %eax,%ecx
mov    $0x1,%eax
shl    %cl,%eax
and    %eax,%edx
xor    %ecx,%ecx
mov    $0x1,%eax
cmp    $0x0,%edx
cmovne %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x2c(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rl_getbit>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %r8,%r14
mov    %rcx,%rbx
movq   $0x0,0x10(%rsp)
sar    $0x3,%rcx
lea    0x18(%rsp),%rax
mov    %rax,(%rsp)
lea    0x10(%rsp),%r9
mov    %rcx,%r8
call   1040 <rl_getrange@plt>
cmp    $0x3,%eax
je     115c <rl_getbit+0x3c>
mov    %eax,%ebp
test   %eax,%eax
jne    118c <rl_getbit+0x6c>
xor    %ebp,%ebp
mov    $0x0,%edx
cmp    $0x3,%eax
je     1189 <rl_getbit+0x69>
cmpq   $0x0,0x18(%rsp)
mov    $0x0,%edx
je     1189 <rl_getbit+0x69>
mov    0x10(%rsp),%rax
movzbl (%rax),%edx
not    %bl
and    $0x7,%bl
mov    %ebx,%ecx
shr    %cl,%edx
and    $0x1,%edx
mov    %edx,(%r14)
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.rlite = type { i32 }
define i32 @rl_getbit(%struct.rlite* %0, i8* %1, i64 %2, i64 %3, i32* %4) {
%6 = alloca %struct.rlite*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
store %struct.rlite* %0, %struct.rlite** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i64 %3, i64* %9
store i32* %4, i32** %10
store i8* null, i8** %12
%16 = load i64, i64* %9
%17 = ashr i64 %16, 3
store i64 %17, i64* %14
%18 = load i64, i64* %9
%19 = and i64 %18, 7
%20 = sub nsw i64 7, %19
store i64 %20, i64* %15
%21 = load %struct.rlite*, %struct.rlite** %6
%22 = load i8*, i8** %7
%23 = load i64, i64* %8
%24 = load i64, i64* %14
%25 = load i64, i64* %14
%26 = call i32 @rl_getrange(%struct.rlite* %21, i8* %22, i64 %23, i64 %24, i64 %25, i8** %12, i64* %13)
store i32 %26, i32* %11
%27 = load i32, i32* %11
%28 = icmp ne i32 0, %27
br i1 %28, label %29, label %33
29: ; preds = %5
%30 = load i32, i32* %11
%31 = icmp ne i32 3, %30
br i1 %31, label %32, label %33
32: ; preds = %29
br label %54
33: ; preds = %29, %5
%34 = load i32, i32* %11
%35 = icmp eq i32 %34, 3
br i1 %35, label %39, label %36
36: ; preds = %33
%37 = load i64, i64* %13
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36, %33
%40 = load i32*, i32** %10
store i32 0, i32* %40
store i32 0, i32* %11
br label %54
41: ; preds = %36
%42 = load i8*, i8** %12
%43 = getelementptr inbounds i8, i8* %42, i64 0
%44 = load i8, i8* %43
%45 = zext i8 %44 to i32
%46 = load i64, i64* %15
%47 = trunc i64 %46 to i32
%48 = shl i32 1, %47
%49 = and i32 %45, %48
%50 = icmp ne i32 %49, 0
%51 = zext i1 %50 to i64
%52 = select i1 %50, i32 1, i32 0
%53 = load i32*, i32** %10
store i32 %52, i32* %53
store i32 0, i32* %11
br label %54
54: ; preds = %41, %39, %32
%55 = load i8*, i8** %12
%56 = call i32 @free(i8* %55)
%57 = load i32, i32* %11
ret i32 %57
}

LLVM IR (-O2):
%struct.rlite = type { i32 }
define i32 @rl_getbit(%struct.rlite* %0, i8* %1, i64 %2, i64 %3, i32* nocapture writeonly %4) {
%6 = alloca i8*
%7 = alloca i64
%8 = bitcast i8** %6 to i8*
store i8* null, i8** %6
%9 = bitcast i64* %7 to i8*
%10 = ashr i64 %3, 3
%11 = call i32 @rl_getrange(%struct.rlite* %0, i8* %1, i64 %2, i64 %10, i64 %10, i8** %6, i64* %7)
switch i32 %11, label %28 [
i32 3, label %12
i32 0, label %12
]
12: ; preds = %5, %5
%13 = icmp eq i32 %11, 3
%14 = load i64, i64* %7
%15 = icmp eq i64 %14, 0
%16 = select i1 %13, i1 true, i1 %15
br i1 %16, label %26, label %17
17: ; preds = %12
%18 = load i8*, i8** %6
%19 = load i8, i8* %18
%20 = zext i8 %19 to i32
%21 = trunc i64 %3 to i32
%22 = and i32 %21, 7
%23 = xor i32 %22, 7
%24 = lshr i32 %20, %23
%25 = and i32 %24, 1
br label %26
26: ; preds = %12, %17
%27 = phi i32 [ %25, %17 ], [ 0, %12 ]
store i32 %27, i32* %4
br label %28
28: ; preds = %26, %5
%29 = phi i32 [ %11, %5 ], [ 0, %26 ]
%30 = load i8*, i8** %6
%31 = call i32 @free(i8* %30)
ret i32 %29
}