// File: /workspace/github_c_makefile_star2_O2/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, %r13d
movl %edx, %r15d
movq %rsi, %r12
movl %edi, %ebx
leaq 0xb0(%rsp), %r14
movq %r14, %rdi
movl %ebx, %esi
callq 0x194e
testb $0x2, 0x1(%r14)
jne 0x2518
pushq $-0x1
jmp 0x273a
movq %r12, 0x90(%rsp)
movl %r15d, %r14d
shrl $0xa, %r14d
movq %r13, 0xa8(%rsp)
leal (%r15,%r13), %r12d
decl %r12d
movl %r12d, %r13d
shrl $0xa, %r13d
movl $0x3ff, %eax            # imm = 0x3FF
movl %r15d, %ebp
andl %eax, %ebp
andl %eax, %r12d
leaq 0x88(%rsp), %rdx
pushq $0x1
popq %rcx
movl %ebx, %edi
movl %r14d, %esi
callq 0x215b
cmpl %r13d, %r14d
jne 0x2648
testl %eax, %eax
js 0x272c
movl 0x88(%rsp), %edi
leaq 0x130(%rsp), %rsi
callq 0x1319
testl %eax, %eax
js 0x2734
movl %ebp, %eax
leaq (%rsp,%rax), %rdi
addq $0x130, %rdi            # imm = 0x130
subl %ebp, %r12d
incl %r12d
movslq %r12d, %rdx
movq 0x90(%rsp), %rsi
callq 0x1110
movl 0x88(%rsp), %edi
leaq 0x130(%rsp), %rsi
callq 0x12e3
movl %r12d, %ebp
testl %eax, %eax
js 0x2738
leaq 0x538(%rsp), %r14
movq %r14, %rdi
movl %ebx, %esi
callq 0x194e
pushq $0x10
popq %rcx
leaq 0xb0(%rsp), %rdi
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
testl %ebp, %ebp
je 0x260a
addl %ebp, %r15d
cmpl %r15d, 0xd4(%rsp)
jae 0x260a
movl %r15d, 0xd4(%rsp)
xorl %edi, %edi
callq 0x1120
leaq 0xb0(%rsp), %r14
movq %rax, 0x10(%r14)
xorl %edi, %edi
callq 0x1120
movq %rax, 0x18(%r14)
pushq $0x10
popq %rcx
movq %rsp, %rdi
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
movl %ebx, %edi
callq 0x18d9
testl %eax, %eax
pushq $-0x5
popq %rax
cmovsl %eax, %ebp
jmp 0x273b
testl %eax, %eax
js 0x272c
movl 0x88(%rsp), %edi
leaq 0x130(%rsp), %rsi
callq 0x1319
pushq $-0x3
popq %rcx
testl %eax, %eax
js 0x2730
movq %r12, 0xa0(%rsp)
movl %ebp, %eax
leaq (%rsp,%rax), %rdi
addq $0x130, %rdi            # imm = 0x130
movl $0x400, %r12d           # imm = 0x400
subl %ebp, %r12d
movq 0x90(%rsp), %rbp
movq %rbp, %rsi
movq %r12, %rdx
callq 0x1110
movl 0x88(%rsp), %edi
leaq 0x130(%rsp), %rsi
callq 0x12e3
pushq $-0x4
popq %rcx
testl %eax, %eax
js 0x2730
movq %r15, 0x98(%rsp)
movl %ebx, 0x8c(%rsp)
addq %r12, %rbp
movl %r13d, %ebx
incq %r14
leaq 0x130(%rsp), %r15
cmpq %rbx, %r14
jae 0x274f
movl $0x400, %edx            # imm = 0x400
movq %r15, %rdi
movq %rbp, %rsi
callq 0x1110
movl 0x8c(%rsp), %edi
movl %r14d, %esi
leaq 0x88(%rsp), %rdx
pushq $0x1
popq %rcx
callq 0x215b
testl %eax, %eax
js 0x272c
movl 0x88(%rsp), %edi
movq %r15, %rsi
callq 0x12e3
addl %eax, %r12d
addq $0x400, %rbp            # imm = 0x400
incq %r14
jmp 0x26db
pushq $-0x2
jmp 0x273a
movl %ecx, %ebp
jmp 0x273b
pushq $-0x3
jmp 0x273a
pushq $-0x4
popq %rbp
movl %ebp, %eax
addq $0x5b8, %rsp            # imm = 0x5B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x88(%rsp), %rdx
pushq $0x1
popq %rcx
movl 0x8c(%rsp), %ebx
movl %ebx, %edi
movl %r13d, %esi
callq 0x215b
testl %eax, %eax
pushq $-0x2
popq %rbp
js 0x273b
movl 0x88(%rsp), %edi
leaq 0x130(%rsp), %rsi
callq 0x1319
pushq $-0x3
popq %rbp
testl %eax, %eax
js 0x273b
movq 0xa0(%rsp), %rdx
movl %edx, %eax
notl %eax
addl 0xa8(%rsp), %eax
movq 0x90(%rsp), %rsi
addq %rax, %rsi
incl %edx
leaq 0x130(%rsp), %r14
movq %r14, %rdi
movq %rdx, %r15
callq 0x1110
movl 0x88(%rsp), %edi
movq %r14, %rsi
callq 0x12e3
pushq $-0x4
popq %rbp
testl %eax, %eax
js 0x273b
addl %r15d, %r12d
movl %r12d, %ebp
movq 0x98(%rsp), %r15
jmp 0x25ce