#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 */

/* Variables and functions */
scalar_t__ bread(int, unsigned char *);
int pbit(unsigned int);
int pbyte(unsigned int);
int /*<<< orphan*/ printf(char *, unsigned int, int);
int tamMB(unsigned int);
int leer_bit(unsigned int nbit) {
  int n_bloque = tamMB(nbit);
  unsigned char buffer[1024];
  if (bread(n_bloque, buffer) < 0) {
    printf("ERROR AL LEER BIT (ficheros_basico.c -> leer_bit(%d)): Error en "
           "bread(%d, buffer)\n",
           nbit, n_bloque);
    return (-1);
  }
  int pos_bit = pbit(nbit);
  int pos_byte = pbyte(nbit);
  unsigned char mascara = 128;
  if (buffer[pos_byte % 1024] == 0) {
    return (0);
  } else if (buffer[pos_byte % 1024] == 255) {
    return (1);
  } else {
    if (pos_bit > 0) {
      mascara >>= pos_bit;
    }
    if (buffer[pos_byte % 1024] & mascara) {
      return (1);
    } else {
      return (0);
    }
  }
}


x86 assembly (clang, -O0):
<leer_bit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1070 <tamMB@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
call   1050 <bread@plt>
cmp    $0x0,%rax
jge    11a0 <leer_bit+0x50>
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xe71(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1281 <leer_bit+0x131>
mov    -0x8(%rbp),%edi
call   1030 <pbit@plt>
mov    %eax,-0x414(%rbp)
mov    -0x8(%rbp),%edi
call   1060 <pbyte@plt>
mov    %eax,-0x418(%rbp)
movb   $0x80,-0x419(%rbp)
mov    -0x418(%rbp),%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
movslq %edx,%rax
movzbl -0x410(%rbp,%rax,1),%eax
ff
cmp    $0x0,%eax
jne    11f1 <leer_bit+0xa1>
movl   $0x0,-0x4(%rbp)
jmp    1281 <leer_bit+0x131>
mov    -0x418(%rbp),%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
movslq %edx,%rax
movzbl -0x410(%rbp,%rax,1),%eax
ff
cmp    $0xff,%eax
jne    1221 <leer_bit+0xd1>
movl   $0x1,-0x4(%rbp)
jmp    1281 <leer_bit+0x131>
cmpl   $0x0,-0x414(%rbp)
jle    1243 <leer_bit+0xf3>
mov    -0x414(%rbp),%ecx
movzbl -0x419(%rbp),%eax
sar    %cl,%eax
mov    %al,-0x419(%rbp)
mov    -0x418(%rbp),%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
movslq %edx,%rax
movzbl -0x410(%rbp,%rax,1),%eax
ff
movzbl -0x419(%rbp),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     127a <leer_bit+0x12a>
movl   $0x1,-0x4(%rbp)
jmp    1281 <leer_bit+0x131>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<leer_bit>:
push   %rbp
push   %rbx
sub    $0x408,%rsp
mov    %edi,%ebp
call   1070 <tamMB@plt>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   1050 <bread@plt>
test   %rax,%rax
js     11aa <leer_bit+0x5a>
mov    %ebp,%edi
call   1030 <pbit@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1060 <pbyte@plt>
lea    0x3ff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xfffffc00,%ecx
sub    %ecx,%eax
cltq
movzbl (%rsp,%rax,1),%edx
test   %edx,%edx
je     11c1 <leer_bit+0x71>
cmp    $0xff,%dl
jne    11c5 <leer_bit+0x75>
mov    $0x1,%eax
jmp    11df <leer_bit+0x8f>
lea    0xe4f(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
call   1040 <printf@plt>
mov    $0xffffffff,%eax
jmp    11df <leer_bit+0x8f>
xor    %eax,%eax
jmp    11df <leer_bit+0x8f>
mov    $0x80,%esi
mov    %ebx,%ecx
shr    %cl,%esi
mov    $0x80,%eax
test   %ebx,%ebx
cmovle %eax,%esi
xor    %eax,%eax
test   %edx,%esi
setne  %al
add    $0x408,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [83 x i8] c"ERROR AL LEER BIT (ficheros_basico.c -> leer_bit(%d)): Error en bread(%d, buffer)\0A\00"
define i32 @leer_bit(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
store i32 %0, i32* %3
%9 = load i32, i32* %3
%10 = call i32 @tamMB(i32 %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%13 = call i64 @bread(i32 %11, i8* %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %1
%16 = load i32, i32* %3
%17 = load i32, i32* %4
%18 = call i32 @printf(i8* getelementptr inbounds ([83 x i8], [83 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %17)
store i32 -1, i32* %2
br label %63
19: ; preds = %1
%20 = load i32, i32* %3
%21 = call i32 @pbit(i32 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %3
%23 = call i32 @pbyte(i32 %22)
store i32 %23, i32* %7
store i8 -128, i8* %8
%24 = load i32, i32* %7
%25 = srem i32 %24, 1024
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %26
%28 = load i8, i8* %27
%29 = zext i8 %28 to i32
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %19
store i32 0, i32* %2
br label %63
32: ; preds = %19
%33 = load i32, i32* %7
%34 = srem i32 %33, 1024
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %35
%37 = load i8, i8* %36
%38 = zext i8 %37 to i32
%39 = icmp eq i32 %38, 255
br i1 %39, label %40, label %41
40: ; preds = %32
store i32 1, i32* %2
br label %63
41: ; preds = %32
%42 = load i32, i32* %6
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %41
%45 = load i32, i32* %6
%46 = load i8, i8* %8
%47 = zext i8 %46 to i32
%48 = ashr i32 %47, %45
%49 = trunc i32 %48 to i8
store i8 %49, i8* %8
br label %50
50: ; preds = %44, %41
%51 = load i32, i32* %7
%52 = srem i32 %51, 1024
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %53
%55 = load i8, i8* %54
%56 = zext i8 %55 to i32
%57 = load i8, i8* %8
%58 = zext i8 %57 to i32
%59 = and i32 %56, %58
%60 = icmp ne i32 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %50
store i32 1, i32* %2
br label %63
62: ; preds = %50
store i32 0, i32* %2
br label %63
63: ; preds = %62, %61, %40, %31, %15
%64 = load i32, i32* %2
ret i32 %64
}

LLVM IR (-O2):
@.str = [83 x i8] c"ERROR AL LEER BIT (ficheros_basico.c -> leer_bit(%d)): Error en bread(%d, buffer)\0A\00"
define i32 @leer_bit(i32 %0) {
%2 = alloca [1024 x i8]
%3 = tail call i32 @tamMB(i32 %0)
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = call i64 @bread(i32 %3, i8* %4)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 @printf(i8* getelementptr inbounds ([83 x i8], [83 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %3)
br label %25
9: ; preds = %1
%10 = call i32 @pbit(i32 %0)
%11 = call i32 @pbyte(i32 %0)
%12 = srem i32 %11, 1024
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %13
%15 = load i8, i8* %14
switch i8 %15, label %17 [
i8 0, label %25
i8 -1, label %16
]
16: ; preds = %9
br label %25
17: ; preds = %9
%18 = icmp sgt i32 %10, 0
%19 = lshr i32 128, %10
%20 = select i1 %18, i32 %19, i32 128
%21 = zext i8 %15 to i32
%22 = and i32 %20, %21
%23 = icmp ne i32 %22, 0
%24 = zext i1 %23 to i32
br label %25
25: ; preds = %17, %16, %9, %7
%26 = phi i32 [ -1, %7 ], [ 1, %16 ], [ 0, %9 ], [ %24, %17 ]
ret i32 %26
}