#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int readBit(FILE *fptr, unsigned char *bit, unsigned char *whichbit,
            unsigned char *curbyte) {
  int ret = 1;
  if ((*whichbit) == 0)
    ret = fread(curbyte, sizeof(unsigned char), 1, fptr);
  if (ret != 1)
    return -1;
  unsigned char temp = (*curbyte) >> (7 - (*whichbit));
  temp = temp & 0X01;
  *whichbit = ((*whichbit) + 1) % 8;
  *bit = temp;
  return 0;
}


x86 assembly (clang, -O0):
<readBit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x0,%eax
jne    1156 <readBit+0x46>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x1,-0x2c(%rbp)
je     116c <readBit+0x5c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ba <readBit+0xaa>
mov    -0x28(%rbp),%rax
movzbl (%rax),%eax
mov    -0x20(%rbp),%rcx
movzbl (%rcx),%edx
mov    $0x7,%ecx
sub    %edx,%ecx
sar    %cl,%eax
mov    %al,-0x2d(%rbp)
movzbl -0x2d(%rbp),%eax
and    $0x1,%eax
mov    %al,-0x2d(%rbp)
mov    -0x20(%rbp),%rax
movzbl (%rax),%eax
add    $0x1,%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %dl,%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x2d(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readBit>:
push   %r15
push   %r14
push   %rbx
mov    %rcx,%r15
mov    %rdx,%rbx
mov    %rsi,%r14
mov    (%rdx),%al
test   %al,%al
jne    1147 <readBit+0x37>
mov    %rdi,%rcx
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0x1,%ecx
jne    115f <readBit+0x4f>
mov    (%rbx),%al
movzbl (%r15),%edx
mov    $0x7,%cl
sub    %al,%cl
shr    %cl,%edx
and    $0x1,%dl
add    $0x1,%al
and    $0x7,%al
mov    %al,(%rbx)
mov    %dl,(%r14)
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @readBit(i32* %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8
store i32* %0, i32** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i32 1, i32* %10
%12 = load i8*, i8** %8
%13 = load i8, i8* %12
%14 = zext i8 %13 to i32
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %4
%17 = load i8*, i8** %9
%18 = load i32*, i32** %6
%19 = call i32 @fread(i8* %17, i32 1, i32 1, i32* %18)
store i32 %19, i32* %10
br label %20
20: ; preds = %16, %4
%21 = load i32, i32* %10
%22 = icmp ne i32 %21, 1
br i1 %22, label %23, label %24
23: ; preds = %20
store i32 -1, i32* %5
br label %47
24: ; preds = %20
%25 = load i8*, i8** %9
%26 = load i8, i8* %25
%27 = zext i8 %26 to i32
%28 = load i8*, i8** %8
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = sub nsw i32 7, %30
%32 = ashr i32 %27, %31
%33 = trunc i32 %32 to i8
store i8 %33, i8* %11
%34 = load i8, i8* %11
%35 = zext i8 %34 to i32
%36 = and i32 %35, 1
%37 = trunc i32 %36 to i8
store i8 %37, i8* %11
%38 = load i8*, i8** %8
%39 = load i8, i8* %38
%40 = zext i8 %39 to i32
%41 = add nsw i32 %40, 1
%42 = srem i32 %41, 8
%43 = trunc i32 %42 to i8
%44 = load i8*, i8** %8
store i8 %43, i8* %44
%45 = load i8, i8* %11
%46 = load i8*, i8** %7
store i8 %45, i8* %46
store i32 0, i32* %5
br label %47
47: ; preds = %24, %23
%48 = load i32, i32* %5
ret i32 %48
}

LLVM IR (-O2):
define i32 @readBit(i32* nocapture %0, i8* nocapture writeonly %1, i8* nocapture %2, i8* nocapture %3) {
%5 = load i8, i8* %2
%6 = icmp eq i8 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %4
%8 = tail call i32 @fread(i8* %3, i32 1, i32 1, i32* %0)
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %23
10: ; preds = %7
%11 = load i8, i8* %2
br label %12
12: ; preds = %10, %4
%13 = phi i8 [ %11, %10 ], [ %5, %4 ]
%14 = load i8, i8* %3
%15 = zext i8 %14 to i32
%16 = zext i8 %13 to i32
%17 = sub nsw i32 7, %16
%18 = lshr i32 %15, %17
%19 = trunc i32 %18 to i8
%20 = and i8 %19, 1
%21 = add i8 %13, 1
%22 = and i8 %21, 7
store i8 %22, i8* %2
store i8 %20, i8* %1
br label %23
23: ; preds = %7, %12
%24 = phi i32 [ 0, %12 ], [ -1, %7 ]
ret i32 %24
}