#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 */
typedef int uint8_t;
typedef int int8_t;

/* Variables and functions */
int readByte(int, int, int *);
int8_t readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart,
                uint8_t length, uint8_t *data) {
  uint8_t count, b;
  if ((count = readByte(devAddr, regAddr, &b)) != 0) {
    uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1);
    b &= mask;
    b >>= (bitStart - length + 1);
    *data = b;
  }
  return count;
}


x86 assembly (clang, -O0):
<readBits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    %r8,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
lea    -0x20(%rbp),%rdx
call   1030 <readByte@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     1181 <readBits+0x71>
mov    -0x10(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
sub    $0x1,%eax
mov    -0xc(%rbp),%ecx
sub    -0x10(%rbp),%ecx
add    $0x1,%ecx
shl    %cl,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%ecx
sub    -0x10(%rbp),%ecx
add    $0x1,%ecx
mov    -0x20(%rbp),%eax
sar    %cl,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readBits>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %r8,%r14
mov    %ecx,%ebx
mov    %edx,%ebp
lea    0xc(%rsp),%rdx
call   1030 <readByte@plt>
test   %eax,%eax
je     114b <readBits+0x3b>
mov    $0xffffffff,%edx
mov    %ebx,%ecx
shl    %cl,%edx
not    %edx
sub    %ebx,%ebp
add    $0x1,%bpl
mov    %ebp,%ecx
shl    %cl,%edx
and    0xc(%rsp),%edx
sar    %cl,%edx
mov    %edx,(%r14)
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @readBits(i32 %0, i32 %1, i32 %2, i32 %3, i32* nocapture writeonly %4) {
%6 = alloca i32
%7 = bitcast i32* %6 to i8*
%8 = call i32 @readByte(i32 %0, i32 %1, i32* %6)
%9 = icmp eq i32 %8, 0
br i1 %9, label %19, label %10
10: ; preds = %5
%11 = shl nsw i32 -1, %3
%12 = xor i32 %11, -1
%13 = sub nsw i32 %2, %3
%14 = add nsw i32 %13, 1
%15 = shl i32 %12, %14
%16 = load i32, i32* %6
%17 = and i32 %16, %15
%18 = ashr i32 %17, %14
store i32 %18, i32* %4
br label %19
19: ; preds = %10, %5
ret i32 %8
}