#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 *);
scalar_t__ bwrite(int, unsigned char *);
int pbit(unsigned int);
int pbyte(unsigned int);
int /*<<< orphan*/ printf(char *, unsigned int, ...);
int tamMB(unsigned int);
int escribir_bit(unsigned int nbit, char valor) {
  int num_bloq_mb = tamMB(nbit);
  unsigned char buffer[1024];
  if (bread(num_bloq_mb, buffer) < 0) {
    printf("ERROR AL ESCRIBIR BIT (ficheros_basico.c -> escribir_bit(%d, %d)): "
           "Error de lectura en bread(%d, buffer)\n",
           nbit, valor, num_bloq_mb);
    return (-1);
  } else if (0 == 1) {
    printf("DEBUG | (escribir_bit): Lectura del bloque %d completada "
           "correctamente\n",
           num_bloq_mb);
  }
  int pos_bit = pbit(nbit);
  int pos_byte = pbyte(nbit);
  unsigned char mascara = 128;
  if (pos_bit > 0) {
    mascara >>= pos_bit;
  }
  if (valor == 1) {
    buffer[pos_byte % 1024] |= mascara;
  } else if (valor == 0) {
    buffer[pos_byte % 1024] &= ~mascara;
  }
  if (bwrite(num_bloq_mb, buffer) < 0) {
    printf("ERROR AL ESCRIBIR BIT (escribir_bit): Bloque %d, valor %d\n",
           num_bloq_mb, valor);
    return (-1);
  } else if (0 == 1) {
    printf("DEBUG | (ficheros_basico.c -> escribir_bit(%d, %d)): Bit escrito "
           "correctamente.\n",
           nbit, valor);
  }
  return (1);
}


x86 assembly (clang, -O0):
<escribir_bit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %sil,%al
mov    %edi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    -0x8(%rbp),%edi
call   1080 <tamMB@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x410(%rbp),%rsi
call   1060 <bread@plt>
cmp    $0x0,%rax
jge    11bc <escribir_bit+0x5c>
mov    -0x8(%rbp),%esi
movsbl -0x9(%rbp),%edx
mov    -0x10(%rbp),%ecx
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12df <escribir_bit+0x17f>
jmp    11c1 <escribir_bit+0x61>
mov    -0x8(%rbp),%edi
call   1030 <pbit@plt>
mov    %eax,-0x414(%rbp)
mov    -0x8(%rbp),%edi
call   1070 <pbyte@plt>
mov    %eax,-0x418(%rbp)
movb   $0x80,-0x419(%rbp)
cmpl   $0x0,-0x414(%rbp)
jle    1206 <escribir_bit+0xa6>
mov    -0x414(%rbp),%ecx
movzbl -0x419(%rbp),%eax
sar    %cl,%eax
mov    %al,-0x419(%rbp)
movsbl -0x9(%rbp),%eax
cmp    $0x1,%eax
jne    124e <escribir_bit+0xee>
movzbl -0x419(%rbp),%eax
mov    %eax,-0x420(%rbp)
mov    -0x418(%rbp),%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %edx,%eax
mov    -0x420(%rbp),%edx
cltq
movzbl -0x410(%rbp,%rax,1),%ecx
ff
or     %edx,%ecx
mov    %cl,-0x410(%rbp,%rax,1)
jmp    1299 <escribir_bit+0x139>
movsbl -0x9(%rbp),%eax
cmp    $0x0,%eax
jne    1294 <escribir_bit+0x134>
movzbl -0x419(%rbp),%eax
xor    $0xffffffff,%eax
mov    %eax,-0x424(%rbp)
mov    -0x418(%rbp),%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %edx,%eax
mov    -0x424(%rbp),%edx
cltq
movzbl -0x410(%rbp,%rax,1),%ecx
ff
and    %edx,%ecx
mov    %cl,-0x410(%rbp,%rax,1)
jmp    1299 <escribir_bit+0x139>
mov    -0x10(%rbp),%edi
lea    -0x410(%rbp),%rsi
call   1040 <bwrite@plt>
cmp    $0x0,%rax
jge    12d3 <escribir_bit+0x173>
mov    -0x10(%rbp),%esi
movsbl -0x9(%rbp),%edx
lea    0xdaa(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12df <escribir_bit+0x17f>
jmp    12d8 <escribir_bit+0x178>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escribir_bit>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
call   1080 <tamMB@plt>
mov    %eax,%r15d
mov    %rsp,%rsi
mov    %eax,%edi
call   1060 <bread@plt>
test   %rax,%rax
js     11d1 <escribir_bit+0x71>
mov    %ebp,%edi
call   1030 <pbit@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1070 <pbyte@plt>
mov    $0x80,%esi
mov    $0x80,%edx
mov    %ebx,%ecx
shr    %cl,%edx
test   %ebx,%ebx
cmovle %esi,%edx
test   %r14b,%r14b
je     11ea <escribir_bit+0x8a>
cmp    $0x1,%r14b
jne    1204 <escribir_bit+0xa4>
lea    0x3ff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xfffffc00,%ecx
sub    %ecx,%eax
cltq
or     %dl,(%rsp,%rax,1)
jmp    1204 <escribir_bit+0xa4>
movsbl %r14b,%edx
lea    0xe24(%rip),%rdi
mov    %ebp,%esi
mov    %r15d,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1231 <escribir_bit+0xd1>
lea    0x3ff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xfffffc00,%ecx
sub    %ecx,%eax
cltq
not    %dl
and    %dl,(%rsp,%rax,1)
mov    %rsp,%rsi
mov    %r15d,%edi
call   1040 <bwrite@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jns    1236 <escribir_bit+0xd6>
movsbl %r14b,%edx
lea    0xe43(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [106 x i8] c"ERROR AL ESCRIBIR BIT (ficheros_basico.c -> escribir_bit(%d, %d)): Error de lectura en bread(%d, buffer)\0A\00"
@.str.1 = [59 x i8] c"ERROR AL ESCRIBIR BIT (escribir_bit): Bloque %d, valor %d\0A\00"
define i32 @escribir_bit(i32 %0, i8 signext %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
%6 = alloca i32
%7 = alloca [1024 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8
store i32 %0, i32* %4
store i8 %1, i8* %5
%11 = load i32, i32* %4
%12 = call i32 @tamMB(i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%15 = call i64 @bread(i32 %13, i8* %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %2
%18 = load i32, i32* %4
%19 = load i8, i8* %5
%20 = sext i8 %19 to i32
%21 = load i32, i32* %6
%22 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([106 x i8], [106 x i8]* @.str, i64 0, i64 0), i32 %18, i32 %20, i32 %21)
store i32 -1, i32* %3
br label %81
23: ; preds = %2
br label %24
24: ; preds = %23
%25 = load i32, i32* %4
%26 = call i32 @pbit(i32 %25)
store i32 %26, i32* %8
%27 = load i32, i32* %4
%28 = call i32 @pbyte(i32 %27)
store i32 %28, i32* %9
store i8 -128, i8* %10
%29 = load i32, i32* %8
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %24
%32 = load i32, i32* %8
%33 = load i8, i8* %10
%34 = zext i8 %33 to i32
%35 = ashr i32 %34, %32
%36 = trunc i32 %35 to i8
store i8 %36, i8* %10
br label %37
37: ; preds = %31, %24
%38 = load i8, i8* %5
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 1
br i1 %40, label %41, label %52
41: ; preds = %37
%42 = load i8, i8* %10
%43 = zext i8 %42 to i32
%44 = load i32, i32* %9
%45 = srem i32 %44, 1024
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %46
%48 = load i8, i8* %47
%49 = zext i8 %48 to i32
%50 = or i32 %49, %43
%51 = trunc i32 %50 to i8
store i8 %51, i8* %47
br label %69
52: ; preds = %37
%53 = load i8, i8* %5
%54 = sext i8 %53 to i32
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %68
56: ; preds = %52
%57 = load i8, i8* %10
%58 = zext i8 %57 to i32
%59 = xor i32 %58, -1
%60 = load i32, i32* %9
%61 = srem i32 %60, 1024
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %62
%64 = load i8, i8* %63
%65 = zext i8 %64 to i32
%66 = and i32 %65, %59
%67 = trunc i32 %66 to i8
store i8 %67, i8* %63
br label %68
68: ; preds = %56, %52
br label %69
69: ; preds = %68, %41
%70 = load i32, i32* %6
%71 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%72 = call i64 @bwrite(i32 %70, i8* %71)
%73 = icmp slt i64 %72, 0
br i1 %73, label %74, label %79
74: ; preds = %69
%75 = load i32, i32* %6
%76 = load i8, i8* %5
%77 = sext i8 %76 to i32
%78 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0), i32 %75, i32 %77)
store i32 -1, i32* %3
br label %81
79: ; preds = %69
br label %80
80: ; preds = %79
store i32 1, i32* %3
br label %81
81: ; preds = %80, %74, %17
%82 = load i32, i32* %3
ret i32 %82
}

LLVM IR (-O2):
@.str = [106 x i8] c"ERROR AL ESCRIBIR BIT (ficheros_basico.c -> escribir_bit(%d, %d)): Error de lectura en bread(%d, buffer)\0A\00"
@.str.1 = [59 x i8] c"ERROR AL ESCRIBIR BIT (escribir_bit): Bloque %d, valor %d\0A\00"
define i32 @escribir_bit(i32 %0, i8 signext %1) {
%3 = alloca [1024 x i8]
%4 = tail call i32 @tamMB(i32 %0)
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = call i64 @bread(i32 %4, i8* %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = sext i8 %1 to i32
%10 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([106 x i8], [106 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %9, i32 %4)
br label %37
11: ; preds = %2
%12 = call i32 @pbit(i32 %0)
%13 = call i32 @pbyte(i32 %0)
%14 = icmp sgt i32 %12, 0
%15 = lshr i32 128, %12
%16 = trunc i32 %15 to i8
%17 = select i1 %14, i8 %16, i8 -128
%18 = sext i8 %1 to i32
switch i8 %1, label %32 [
i8 1, label %19
i8 0, label %25
]
19: ; preds = %11
%20 = srem i32 %13, 1024
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %21
%23 = load i8, i8* %22
%24 = or i8 %23, %17
store i8 %24, i8* %22
br label %32
25: ; preds = %11
%26 = srem i32 %13, 1024
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = xor i8 %17, -1
%31 = and i8 %29, %30
store i8 %31, i8* %28
br label %32
32: ; preds = %11, %25, %19
%33 = call i64 @bwrite(i32 %4, i8* %5)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0), i32 %4, i32 %18)
br label %37
37: ; preds = %35, %32, %8
%38 = phi i32 [ -1, %8 ], [ -1, %35 ], [ 1, %32 ]
ret i32 %38
}