// File: /workspace/github_c_makefile_star2_O1/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, %r12d
movl %edx, %r15d
movq %rsi, %r14
movl %edi, %ebp
leaq 0xb0(%rsp), %rbx
movq %rbx, %rdi
movl %ebp, %esi
callq 0x1991
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testb $0x2, 0x1(%rbx)
je 0x2951
movq %r14, 0x90(%rsp)
movl %r15d, %r14d
shrl $0xa, %r14d
movq %r12, 0xa8(%rsp)
leal (%r15,%r12), %r13d
decl %r13d
movl %r13d, %r12d
shrl $0xa, %r12d
movl $0x3ff, %eax            # imm = 0x3FF
movl %r15d, %ebx
andl %eax, %ebx
andl %eax, %r13d
leaq 0x88(%rsp), %rdx
movl %ebp, %edi
movl %r14d, %esi
movl $0x1, %ecx
callq 0x225b
cmpl %r12d, %r14d
jne 0x26fa
testl %eax, %eax
js 0x287d
movl 0x88(%rsp), %edi
leaq 0x130(%rsp), %rsi
callq 0x131d
testl %eax, %eax
js 0x2887
movq %r15, 0x98(%rsp)
movl %ebx, %eax
leaq (%rsp,%rax), %rdi
addq $0x130, %rdi            # imm = 0x130
subl %ebx, %r13d
incl %r13d
movslq %r13d, %rdx
movq 0x90(%rsp), %rsi
callq 0x1110
movl 0x88(%rsp), %edi
leaq 0x130(%rsp), %rsi
callq 0x12e5
movl %r13d, %r15d
testl %eax, %eax
jns 0x28c8
jmp 0x2891
testl %eax, %eax
js 0x287d
movl 0x88(%rsp), %edi
leaq 0x130(%rsp), %rsi
callq 0x131d
testl %eax, %eax
js 0x2887
movq %r15, 0x98(%rsp)
movl %ebx, %eax
leaq (%rsp,%rax), %rdi
addq $0x130, %rdi            # imm = 0x130
movl $0x400, %r15d           # imm = 0x400
subl %ebx, %r15d
movq 0x90(%rsp), %rsi
movq %r15, %rdx
callq 0x1110
movl 0x88(%rsp), %edi
leaq 0x130(%rsp), %rsi
callq 0x12e5
testl %eax, %eax
js 0x2891
movl %ebp, 0x8c(%rsp)
leal 0x1(%r14), %eax
cmpl %r12d, %eax
jae 0x27fc
movq 0x90(%rsp), %rax
leaq (%rax,%r15), %rbp
leaq 0x1(%r14), %rbx
leal (%r12,%r14), %eax
subl %r14d, %eax
movq %rax, 0xa0(%rsp)
leaq 0x130(%rsp), %r14
movl $0x400, %edx            # imm = 0x400
movq %r14, %rdi
movq %rbp, %rsi
callq 0x1110
movl 0x8c(%rsp), %edi
movl %ebx, %esi
leaq 0x88(%rsp), %rdx
movl $0x1, %ecx
callq 0x225b
testl %eax, %eax
js 0x28a2
movl 0x88(%rsp), %edi
movq %r14, %rsi
callq 0x12e5
addl %eax, %r15d
incq %rbx
addq $0x400, %rbp            # imm = 0x400
cmpq %rbx, 0xa0(%rsp)
jne 0x27a3
leaq 0x88(%rsp), %rdx
movl 0x8c(%rsp), %ebp
movl %ebp, %edi
movl %r12d, %esi
movl $0x1, %ecx
callq 0x225b
testl %eax, %eax
js 0x289b
movl 0x88(%rsp), %edi
leaq 0x130(%rsp), %rsi
callq 0x131d
testl %eax, %eax
js 0x28b2
movl %r13d, %eax
notl %eax
addl 0xa8(%rsp), %eax
movq 0x90(%rsp), %rsi
addq %rax, %rsi
incl %r13d
leaq 0x130(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rdx
callq 0x1110
movl 0x88(%rsp), %edi
movq %rbx, %rsi
callq 0x12e5
testl %eax, %eax
js 0x28b9
addl %r13d, %r15d
movb $0x1, %cl
jmp 0x28c0
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
jmp 0x2951
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
jmp 0x2951
movl $0xfffffffc, %eax       # imm = 0xFFFFFFFC
jmp 0x2951
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
jmp 0x28be
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
xorl %ecx, %ecx
movl 0x8c(%rsp), %ebp
jmp 0x28c0
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
jmp 0x28be
movl $0xfffffffc, %eax       # imm = 0xFFFFFFFC
xorl %ecx, %ecx
testb %cl, %cl
je 0x2951
leaq 0x538(%rsp), %rbx
movq %rbx, %rdi
movl %ebp, %esi
callq 0x1991
leaq 0xb0(%rsp), %rdi
movl $0x10, %ecx
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
testl %r15d, %r15d
je 0x2910
movq 0x98(%rsp), %rcx
addl %r15d, %ecx
cmpl %ecx, 0xd4(%rsp)
jae 0x2910
movq %rcx, %rax
movl %eax, 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 %ebp, %edi
callq 0x1918
testl %eax, %eax
movl $0xfffffffb, %eax       # imm = 0xFFFFFFFB
cmovsl %eax, %r15d
movl %r15d, %eax
addq $0x5b8, %rsp            # imm = 0x5B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq