// File: /workspace/github_c_makefile_star2_O3/carlitosmarin[P]Sistema-Ficheros-SOII/./src/ficheros.c
// Function: mi_write_f
int mi_write_f (unsigned int inodo, const void *buf, unsigned int offset, unsigned int nbytes) {
	struct inodo in = leer_inodo(inodo);
	if((in.permisos & 2) != 2) return -1; //Error NO_PERMISOS_ESCRITURA

	int bloqueI = offset/tamBloque, bloqueF = (offset+nbytes-1)/tamBloque, bfisico, bytes = 0;
	unsigned char bufBloque[tamBloque];
	int desp1 = offset%tamBloque; //Desplazamiento en el bloque
	int desp2 = (offset+nbytes-1) % tamBloque;

	if(bloqueI == bloqueF) { //Si son el mismo el bloque inicial y el final
		if(traducir_bloque_inodo(inodo,bloqueI,&bfisico,1) < 0) return -2; //Obtenemos el bloque físico Error TBI
		if(bread(bfisico, bufBloque) < 0) return -3; //Leemos el bloque correspondiente Error BREAD
		memcpy (bufBloque+desp1, buf, desp2-desp1+1);
		if(bwrite(bfisico, bufBloque) < 0) return -4; // Escribimos el bloque en memoria Error BWRITE
		bytes += desp2-desp1+1;	// Aumentamos el contador los bytes que hemos escrito
	} else {
	//3 fases
	//Primer bloque
	if(traducir_bloque_inodo(inodo,bloqueI,&bfisico,1) < 0) return -2; //Error TBI
	if(bread(bfisico, bufBloque) < 0) return -3; //Leemos el bloque correspondiente Error BREAD
	memcpy (bufBloque+desp1, buf, tamBloque-desp1);
	if(bwrite(bfisico, bufBloque) < 0) return -4; // Escribimos el bloque en memoria Error BWRITE
	bytes += tamBloque-desp1;

	//Intermedios
	int bucle;
	for(bucle = bloqueI+1; bucle < bloqueF; bucle++) {
		memcpy(bufBloque, buf+(tamBloque-desp1)+(bucle-bloqueI-1)*tamBloque, tamBloque);
		if(traducir_bloque_inodo(inodo,bucle,&bfisico,1) < 0) return -2; //Error TBI
		bytes += bwrite(bfisico,bufBloque);
	}

	//Ultimo bloque
	if(traducir_bloque_inodo(inodo,bloqueF,&bfisico,1) < 0) return -2; //Error TBI
	if(bread(bfisico, bufBloque) < 0) return -3; //Leemos el bloque correspondiente Error BREAD
	memcpy(bufBloque,buf+(nbytes-desp2-1),desp2+1);
	if(bwrite(bfisico, bufBloque) < 0) return -4; // Escribimos el bloque en memoria Error BWRITE
	bytes += desp2+1;
	}
	//Actualizaremos la metainformacion del inodo
	in = leer_inodo(inodo);
	if(in.tamBytesLogicos<offset+bytes && bytes != 0) in.tamBytesLogicos = offset+bytes;
	in.mtime = time(NULL);
	in.ctime = time(NULL);
	if(escribir_inodo(in, inodo) < 0) return -5; //Escribimos el inodo modificado Error ESCRIBIR_INODO
	return bytes;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x5b8, %rsp            # imm = 0x5B8
movl %ecx, %r15d
movl %edx, %r12d
movq %rsi, %r14
movl %edi, %r13d
leaq 0xb0(%rsp), %rbx
movq %rbx, %rdi
movl %r13d, %esi
callq 0x1995
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testb $0x2, 0x1(%rbx)
jne 0x2573
movl %ebp, %eax
addq $0x5b8, %rsp            # imm = 0x5B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, 0x88(%rsp)
movl %r12d, %r14d
shrl $0xa, %r14d
movq %r15, 0xa8(%rsp)
leal (%r12,%r15), %ebp
decl %ebp
movl %ebp, %r15d
shrl $0xa, %r15d
movl $0x3ff, %eax            # imm = 0x3FF
movl %r12d, %ebx
andl %eax, %ebx
andl %eax, %ebp
leaq 0x80(%rsp), %rdx
movl %r13d, %edi
movl %r14d, %esi
movl $0x1, %ecx
callq 0x21b4
cmpl %r15d, %r14d
jne 0x26a7
testl %eax, %eax
js 0x286d
movl 0x80(%rsp), %edi
leaq 0x130(%rsp), %rsi
callq 0x131d
testl %eax, %eax
js 0x2863
movl %ebx, %eax
leaq (%rsp,%rax), %rdi
addq $0x130, %rdi            # imm = 0x130
subl %ebx, %ebp
incl %ebp
movslq %ebp, %rdx
movq 0x88(%rsp), %rsi
callq 0x1110
movl 0x80(%rsp), %edi
leaq 0x130(%rsp), %rsi
callq 0x12e5
testl %eax, %eax
js 0x2877
leaq 0x538(%rsp), %rbx
movq %rbx, %rdi
movl %r13d, %esi
callq 0x1995
leaq 0xb0(%rsp), %rdi
movl $0x10, %ecx
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
testl %ebp, %ebp
je 0x2664
addl %ebp, %r12d
cmpl %r12d, 0xd4(%rsp)
jae 0x2664
movl %r12d, 0xd4(%rsp)
xorl %edi, %edi
callq 0x1120
leaq 0xb0(%rsp), %rbx
movq %rax, 0x10(%rbx)
xorl %edi, %edi
callq 0x1120
movq %rax, 0x18(%rbx)
movl $0x10, %ecx
movq %rsp, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
movl %r13d, %edi
callq 0x191c
testl %eax, %eax
movl $0xfffffffb, %eax       # imm = 0xFFFFFFFB
cmovsl %eax, %ebp
jmp 0x255f
movq %rbp, 0x98(%rsp)
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
testl %eax, %eax
js 0x255f
movl 0x80(%rsp), %edi
leaq 0x130(%rsp), %rsi
callq 0x131d
testl %eax, %eax
js 0x2863
movq %r12, 0x90(%rsp)
movl %ebx, %eax
leaq (%rsp,%rax), %rdi
addq $0x130, %rdi            # imm = 0x130
movl $0x400, %r12d           # imm = 0x400
subl %ebx, %r12d
movq 0x88(%rsp), %rsi
movq %r12, %rdx
callq 0x1110
movl 0x80(%rsp), %edi
leaq 0x130(%rsp), %rsi
callq 0x12e5
testl %eax, %eax
js 0x2877
movl %r13d, 0x84(%rsp)
leal 0x1(%r14), %eax
cmpl %r15d, %eax
jae 0x27b8
movq 0x88(%rsp), %rax
leaq (%rax,%r12), %rbx
leaq 0x1(%r14), %r13
leal (%r15,%r14), %eax
subl %r14d, %eax
movq %rax, 0xa0(%rsp)
leaq 0x130(%rsp), %r14
movl $0x400, %edx            # imm = 0x400
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1110
movl 0x84(%rsp), %edi
movl %r13d, %esi
leaq 0x80(%rsp), %rdx
movl $0x1, %ecx
callq 0x21b4
testl %eax, %eax
js 0x255f
movl 0x80(%rsp), %edi
movq %r14, %rsi
callq 0x12e5
addl %eax, %r12d
incq %r13
addq $0x400, %rbx            # imm = 0x400
cmpq %r13, 0xa0(%rsp)
jne 0x275e
leaq 0x80(%rsp), %rdx
movl 0x84(%rsp), %r13d
movl %r13d, %edi
movl %r15d, %esi
movl $0x1, %ecx
callq 0x21b4
testl %eax, %eax
js 0x255f
movl 0x80(%rsp), %edi
leaq 0x130(%rsp), %rsi
callq 0x131d
movl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
testl %eax, %eax
js 0x255f
movq 0x98(%rsp), %rdx
movl %edx, %eax
notl %eax
addl 0xa8(%rsp), %eax
movq 0x88(%rsp), %rsi
addq %rax, %rsi
incl %edx
leaq 0x130(%rsp), %rbx
movq %rbx, %rdi
movq %rdx, %r14
callq 0x1110
movl 0x80(%rsp), %edi
movq %rbx, %rsi
callq 0x12e5
movl $0xfffffffc, %ebp       # imm = 0xFFFFFFFC
testl %eax, %eax
js 0x255f
addl %r14d, %r12d
movl %r12d, %ebp
movq 0x90(%rsp), %r12
jmp 0x2625
movl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
jmp 0x255f
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
jmp 0x255f
movl $0xfffffffc, %ebp       # imm = 0xFFFFFFFC
jmp 0x255f