// File: /workspace/github_c_makefile_star2_O0/j-hoppe[P]tu58fs/tu58drive.c
// Function: turead
static void turead(tu_cmdpkt *pk) {
	int32_t count;
	tu_datpkt dk;
	image_t *img;

	// check unit number for validity
	img = tu58image_get(pk->unit);
	if (!img || !img->open) {
		error("turead bad unit %d", pk->unit);
		endpacket(pk->unit, TUE_BADU, 0, 0);
		return;
	}

	// offline = no cartridges
	if (tu58_offline) {
		endpacket(pk->unit, TUE_BADF, 0, 0);
		return;
	}

	// seek to desired ending block offset
	if (image_blockseek(img, blocksize(pk->modifier), pk->block, pk->count - 1)) {
		error("turead unit %d bad block 0x%04X", pk->unit, pk->block);
		endpacket(pk->unit, TUE_BADB, 0, 0);
		return;
	}

	// seek to desired starting block offset
	if (image_blockseek(img, blocksize(pk->modifier), pk->block, 0)) {
		error("turead unit %d bad block 0x%04X", pk->unit, pk->block);
		endpacket(pk->unit, TUE_BADB, 0, 0);
		return;
	}

	// fake a seek time
	delay_ms(tudelay[opt_timing].seek);

	// send data in packets until we run out
	for (count = pk->count; count > 0; count -= dk.length) {

		// max bytes to send at once is TU_DATA_LEN
		dk.flag = TUF_DATA;
		dk.length = count < TU_DATA_LEN ? count : TU_DATA_LEN;

		if (image_read(img, dk.data, dk.length) == dk.length) {
			// successful file read, send packet
			putpacket((tu_packet *) &dk);
			// fake a read time
			delay_ms(tudelay[opt_timing].read);
		} else {
			// whoops, something bad happened
			error("turead unit %d data error block 0x%04X count 0x%04X", pk->unit, pk->block,
					pk->count);
			endpacket(pk->unit, TUE_PARO, pk->count - count, 0);
			return;
		}
	}

	// success if we get here
	endpacket(pk->unit, TUE_SUCC, pk->count, 0);

	return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movzbl 0x4(%rax), %edi
callq 0x7aa0
movq %rax, -0x98(%rbp)
cmpq $0x0, -0x98(%rbp)
je 0x88fd
movq -0x98(%rbp), %rax
cmpb $0x0, 0xf4(%rax)
jne 0x892e
movq -0x8(%rbp), %rax
movzbl 0x4(%rax), %esi
leaq 0x13185(%rip), %rdi     # 0x1ba91
movb $0x0, %al
callq 0xce70
movq -0x8(%rbp), %rax
movl $0xf8, %esi
xorl %ecx, %ecx
movzbl 0x4(%rax), %edi
movl %ecx, %edx
callq 0x8850
jmp 0x8b7e
movl 0x1c890(%rip), %eax     # 0x251c4
cmpl $0x0, %eax
je 0x8954
movq -0x8(%rbp), %rax
movl $0xf7, %esi
xorl %ecx, %ecx
movzbl 0x4(%rax), %edi
movl %ecx, %edx
callq 0x8850
jmp 0x8b7e
movq -0x98(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x8(%rbp), %rax
movzbl 0x3(%rax), %edi
callq 0x9510
movq -0xa0(%rbp), %rdi
movl %eax, %esi
movq -0x8(%rbp), %rax
movzwl 0xa(%rax), %edx
movq -0x8(%rbp), %rax
movzwl 0x8(%rax), %ecx
subl $0x1, %ecx
callq 0x9f00
cmpl $0x0, %eax
je 0x89ce
movq -0x8(%rbp), %rax
movzbl 0x4(%rax), %esi
movq -0x8(%rbp), %rax
movzwl 0xa(%rax), %edx
leaq 0x130f8(%rip), %rdi     # 0x1baa4
movb $0x0, %al
callq 0xce70
movq -0x8(%rbp), %rax
movl $0xc9, %esi
xorl %ecx, %ecx
movzbl 0x4(%rax), %edi
movl %ecx, %edx
callq 0x8850
jmp 0x8b7e
movq -0x98(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x8(%rbp), %rax
movzbl 0x3(%rax), %edi
callq 0x9510
movq -0xa8(%rbp), %rdi
movl %eax, %esi
movq -0x8(%rbp), %rax
movzwl 0xa(%rax), %edx
xorl %ecx, %ecx
callq 0x9f00
cmpl $0x0, %eax
je 0x8a3f
movq -0x8(%rbp), %rax
movzbl 0x4(%rax), %esi
movq -0x8(%rbp), %rax
movzwl 0xa(%rax), %edx
leaq 0x13087(%rip), %rdi     # 0x1baa4
movb $0x0, %al
callq 0xce70
movq -0x8(%rbp), %rax
movl $0xc9, %esi
xorl %ecx, %ecx
movzbl 0x4(%rax), %edi
movl %ecx, %edx
callq 0x8850
jmp 0x8b7e
leaq 0x1b322(%rip), %rax     # 0x23d68
movslq (%rax), %rcx
leaq 0x1ab80(%rip), %rax     # 0x235d0
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movzwl 0x6(%rax), %edi
callq 0xd2a0
movq -0x8(%rbp), %rax
movzwl 0x8(%rax), %eax
movl %eax, -0xc(%rbp)
cmpl $0x0, -0xc(%rbp)
jle 0x8b63
movb $0x1, -0x90(%rbp)
cmpl $0x80, -0xc(%rbp)
jge 0x8a90
movl -0xc(%rbp), %eax
movl %eax, -0xac(%rbp)
jmp 0x8a9d
movl $0x80, %eax
movl %eax, -0xac(%rbp)
jmp 0x8a9d
movl -0xac(%rbp), %eax
movb %al, -0x8f(%rbp)
movq -0x98(%rbp), %rdi
leaq -0x90(%rbp), %rsi
addq $0x2, %rsi
movzbl -0x8f(%rbp), %edx
callq 0xa020
movzbl -0x8f(%rbp), %ecx
cmpl %ecx, %eax
jne 0x8b01
leaq -0x90(%rbp), %rdi
callq 0x9290
leaq 0x1b283(%rip), %rax     # 0x23d68
movslq (%rax), %rcx
leaq 0x1aae1(%rip), %rax     # 0x235d0
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movzwl 0x8(%rax), %edi
callq 0xd2a0
jmp 0x8b4d
movq -0x8(%rbp), %rax
movzbl 0x4(%rax), %esi
movq -0x8(%rbp), %rax
movzwl 0xa(%rax), %edx
movq -0x8(%rbp), %rax
movzwl 0x8(%rax), %ecx
leaq 0x12fa4(%rip), %rdi     # 0x1bac4
movb $0x0, %al
callq 0xce70
movq -0x8(%rbp), %rax
movb 0x4(%rax), %dl
movq -0x8(%rbp), %rax
movzwl 0x8(%rax), %eax
subl -0xc(%rbp), %eax
movl $0xfe, %esi
xorl %ecx, %ecx
movzbl %dl, %edi
movzwl %ax, %edx
callq 0x8850
jmp 0x8b7e
jmp 0x8b4f
movzbl -0x8f(%rbp), %ecx
movl -0xc(%rbp), %eax
subl %ecx, %eax
movl %eax, -0xc(%rbp)
jmp 0x8a6b
movq -0x8(%rbp), %rax
movb 0x4(%rax), %dl
movq -0x8(%rbp), %rax
xorl %ecx, %ecx
movzbl %dl, %edi
movl %ecx, %esi
movzwl 0x8(%rax), %edx
callq 0x8850
addq $0xb0, %rsp
popq %rbp
retq
nopw (%rax,%rax)