// File: /workspace/github_c_makefile_star2_O0/j-hoppe[P]tu58fs/tu58drive.c
// Function: command
static void command(int8_t flag) {
	tu_cmdpkt pk;
	struct timespec time_start;
	struct timespec time_end;
	char *name = "none";
	uint8_t mode = 0;

	// avoid uninitialized variable warnings
	time_start.tv_sec = 0;
	time_start.tv_nsec = 0;
	time_end.tv_sec = 0;
	time_end.tv_nsec = 0;

	pk.flag = flag;
	pk.length = serial_devrxget(&tu58_serial);

	// check control packet length ... if too long flush it
	if (pk.length > sizeof(tu_cmdpkt)) {
		error("bad length 0x%02X in cmd packet", pk.length);
		reinit();
		return;
	}

	// check packet checksum ... if bad error it
	if (getpacket((tu_packet *) &pk)) {
		error("cmd checksum error");
		endpacket(pk.unit, TUE_DERR, 0, 0);
		return;
	}

	if (opt_debug)
		info("opcode=0x%02X length=0x%02X", pk.opcode, pk.length);

	// dump command if requested
	if (opt_verbose) {

		// parse commands to classes
		switch (pk.opcode) {
		case TUO_DIAGNOSE:
			name = "diagnose";
			mode = 1;
			break;
		case TUO_GETCHAR:
			name = "getchar";
			mode = 1;
			break;
		case TUO_INIT:
			name = "init";
			mode = 1;
			break;
		case TUO_NOP:
			name = "nop";
			mode = 1;
			break;
		case TUO_GETSTATUS:
			name = "getstat";
			mode = 1;
			break;
		case TUO_SETSTATUS:
			name = "setstat";
			mode = 1;
			break;
		case TUO_SEEK:
			name = "seek";
			mode = 2;
			break;
		case TUO_READ:
			name = "read";
			mode = 3;
			break;
		case TUO_WRITE:
			name = "write";
			mode = 3;
			break;
		default:
			name = "unknown";
			mode = 3;
			break;
		}

		// dump data
		switch (mode) {
		case 0:
			info("%-8s", name);
			break;
		case 1:
			info("%-8s unit=%d", name, pk.unit);
			break;
		case 2:
			info("%-8s unit=%d sw=0x%02X mod=0x%02X blk=0x%04X", name, pk.unit, pk.switches,
					pk.modifier, pk.block);
			break;
		case 3:
			info("%-8s unit=%d sw=0x%02X mod=0x%02X blk=0x%04X cnt=0x%04X", name, pk.unit,
					pk.switches, pk.modifier, pk.block, pk.count);
			break;
		}

		// get start time of processing
		clock_gettime(CLOCK_REALTIME, &time_start);

	}

	// if we are MRSP capable, look at the switches
	if (opt_mrspen)
		mrsp = (pk.switches & TUS_MRSP) ? 1 : 0;

	// decode packet
	switch (pk.opcode) {

	case TUO_READ: // read data from tu58
		turead(&pk);
		break;

	case TUO_WRITE: // write data to tu58
		tuwrite(&pk);
		break;

	case TUO_SEEK: // reposition tu58
		tuseek(&pk);
		break;

	case TUO_DIAGNOSE: // diagnose packet
		delay_ms(tudelay[opt_timing].test);
		endpacket(pk.unit, TUE_SUCC, 0, 0);
		break;

	case TUO_GETCHAR: // get characteristics packet
		delay_ms(tudelay[opt_timing].nop);
		if (opt_mrspen) {
			// MRSP capable just sends the end packet
			endpacket(pk.unit, TUE_SUCC, 0, 0);
		} else {
			// MRSP detect mode not enabled
			// indicate we are not MRSP capable
			tu_datpkt dk;
			dk.flag = TUF_DATA;
			dk.length = TU_CHAR_LEN;
			bzero(dk.data, dk.length);
			putpacket((tu_packet *) &dk);
		}
		break;

	case TUO_INIT: // init packet
		delay_ms(tudelay[opt_timing].init);
		serial_devtxinit(&tu58_serial);
		serial_devrxinit(&tu58_serial);
		endpacket(pk.unit, TUE_SUCC, 0, 0);
		break;

	case TUO_NOP: // nop packet
	case TUO_GETSTATUS: // get status packet
	case TUO_SETSTATUS: // set status packet
		delay_ms(tudelay[opt_timing].nop);
		endpacket(pk.unit, TUE_SUCC, 0, 0);
		break;

	default: // unknown packet
		delay_ms(tudelay[opt_timing].nop);
		endpacket(pk.unit, TUE_BADO, 0, 0);
		break;

	}

	if (opt_verbose) {

		uint32_t delta;

		// get end time of processing
		clock_gettime(CLOCK_REALTIME, &time_end);

		// compute elapsed time in milliseconds
		delta = 1000L * (time_end.tv_sec - time_start.tv_sec)
				+ (time_end.tv_nsec - time_start.tv_nsec) / 1000000L;
		if (delta == 0)
			delta = 1;

		// print elapsed time in milliseconds
		if (opt_debug)
			info("%-8s time=%dms", name, delta);

	}

	return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movb %dil, %al
movb %al, -0x1(%rbp)
leaq 0x138bf(%rip), %rax     # 0x1b927
movq %rax, -0x38(%rbp)
movb $0x0, -0x39(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x28(%rbp)
movb -0x1(%rbp), %al
movb %al, -0x10(%rbp)
leaq 0x1d12b(%rip), %rdi     # 0x251c8
callq 0xa940
movb %al, -0xf(%rbp)
movzbl -0xf(%rbp), %eax
cmpq $0xe, %rax
jbe 0x80cb
movzbl -0xf(%rbp), %esi
leaq 0x13872(%rip), %rdi     # 0x1b92c
movb $0x0, %al
callq 0xce70
callq 0x7fe0
jmp 0x8520
leaq -0x10(%rbp), %rdi
callq 0x8770
cmpl $0x0, %eax
je 0x80fe
leaq 0x1386c(%rip), %rdi     # 0x1b94c
movb $0x0, %al
callq 0xce70
movl $0xef, %esi
xorl %ecx, %ecx
movzbl -0xc(%rbp), %edi
movl %ecx, %edx
callq 0x8850
jmp 0x8520
leaq 0x1bc6f(%rip), %rax     # 0x23d74
cmpl $0x0, (%rax)
je 0x8120
movzbl -0xe(%rbp), %esi
movzbl -0xf(%rbp), %edx
leaq 0x13846(%rip), %rdi     # 0x1b95f
movb $0x0, %al
callq 0xcf60
leaq 0x1bc3d(%rip), %rax     # 0x23d64
cmpl $0x0, (%rax)
je 0x82be
movzbl -0xe(%rbp), %eax
movq %rax, -0xd0(%rbp)
subq $0xa, %rax
ja 0x81fb
movq -0xd0(%rbp), %rax
leaq 0x135f1(%rip), %rcx     # 0x1b744
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x13818(%rip), %rax     # 0x1b97b
movq %rax, -0x38(%rbp)
movb $0x1, -0x39(%rbp)
jmp 0x820a
leaq 0x1380d(%rip), %rax     # 0x1b984
movq %rax, -0x38(%rbp)
movb $0x1, -0x39(%rbp)
jmp 0x820a
leaq 0x13801(%rip), %rax     # 0x1b98c
movq %rax, -0x38(%rbp)
movb $0x1, -0x39(%rbp)
jmp 0x820a
leaq 0x137f5(%rip), %rax     # 0x1b991
movq %rax, -0x38(%rbp)
movb $0x1, -0x39(%rbp)
jmp 0x820a
leaq 0x137e8(%rip), %rax     # 0x1b995
movq %rax, -0x38(%rbp)
movb $0x1, -0x39(%rbp)
jmp 0x820a
leaq 0x137df(%rip), %rax     # 0x1b99d
movq %rax, -0x38(%rbp)
movb $0x1, -0x39(%rbp)
jmp 0x820a
leaq 0x137d6(%rip), %rax     # 0x1b9a5
movq %rax, -0x38(%rbp)
movb $0x2, -0x39(%rbp)
jmp 0x820a
leaq 0x14049(%rip), %rax     # 0x1c229
movq %rax, -0x38(%rbp)
movb $0x3, -0x39(%rbp)
jmp 0x820a
leaq 0x13997(%rip), %rax     # 0x1bb88
movq %rax, -0x38(%rbp)
movb $0x3, -0x39(%rbp)
jmp 0x820a
leaq 0x137a8(%rip), %rax     # 0x1b9aa
movq %rax, -0x38(%rbp)
movb $0x3, -0x39(%rbp)
movzbl -0x39(%rbp), %eax
movq %rax, -0xd8(%rbp)
subq $0x3, %rax
ja 0x82b3
movq -0xd8(%rbp), %rax
leaq 0x13543(%rip), %rcx     # 0x1b770
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x38(%rbp), %rsi
leaq 0x13771(%rip), %rdi     # 0x1b9b2
movb $0x0, %al
callq 0xcf60
jmp 0x82b3
movq -0x38(%rbp), %rsi
movzbl -0xc(%rbp), %edx
leaq 0x1375e(%rip), %rdi     # 0x1b9b7
movb $0x0, %al
callq 0xcf60
jmp 0x82b3
movq -0x38(%rbp), %rsi
movzbl -0xc(%rbp), %edx
movzbl -0xb(%rbp), %ecx
movzbl -0xd(%rbp), %r8d
movzwl -0x6(%rbp), %r9d
leaq 0x13745(%rip), %rdi     # 0x1b9c4
movb $0x0, %al
callq 0xcf60
jmp 0x82b3
movq -0x38(%rbp), %rsi
movzbl -0xc(%rbp), %edx
movzbl -0xb(%rbp), %ecx
movzbl -0xd(%rbp), %r8d
movzwl -0x6(%rbp), %r9d
movzwl -0x8(%rbp), %eax
leaq 0x13748(%rip), %rdi     # 0x1b9f1
movl %eax, (%rsp)
movb $0x0, %al
callq 0xcf60
xorl %edi, %edi
leaq -0x20(%rbp), %rsi
callq 0x3130
leaq 0x1baa7(%rip), %rax     # 0x23d6c
cmpl $0x0, (%rax)
je 0x82e4
movzbl -0xb(%rbp), %edx
andl $0x8, %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movb %al, 0x1ce8c(%rip)      # 0x25170
movzbl -0xe(%rbp), %eax
movq %rax, -0xe0(%rbp)
subq $0xa, %rax
ja 0x8461
movq -0xe0(%rbp), %rax
leaq 0x13479(%rip), %rcx     # 0x1b780
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq -0x10(%rbp), %rdi
callq 0x88c0
jmp 0x8493
leaq -0x10(%rbp), %rdi
callq 0x8b90
jmp 0x8493
leaq -0x10(%rbp), %rdi
callq 0x9160
jmp 0x8493
leaq 0x1ba27(%rip), %rax     # 0x23d68
movslq (%rax), %rcx
leaq 0x1b285(%rip), %rax     # 0x235d0
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movzwl 0x4(%rax), %edi
callq 0xd2a0
xorl %ecx, %ecx
movzbl -0xc(%rbp), %edi
movl %ecx, %esi
movl %ecx, %edx
callq 0x8850
jmp 0x8493
leaq 0x1b9f2(%rip), %rax     # 0x23d68
movslq (%rax), %rcx
leaq 0x1b250(%rip), %rax     # 0x235d0
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movzwl (%rax), %edi
callq 0xd2a0
leaq 0x1b9d6(%rip), %rax     # 0x23d6c
cmpl $0x0, (%rax)
je 0x83ac
xorl %ecx, %ecx
movzbl -0xc(%rbp), %edi
movl %ecx, %esi
movl %ecx, %edx
callq 0x8850
jmp 0x83e1
movb $0x1, -0xbe(%rbp)
movb $0x18, -0xbd(%rbp)
leaq -0xbe(%rbp), %rdi
addq $0x2, %rdi
movzbl -0xbd(%rbp), %eax
movl %eax, %edx
xorl %esi, %esi
callq 0x3220
leaq -0xbe(%rbp), %rdi
callq 0x9290
jmp 0x8493
leaq 0x1b97b(%rip), %rax     # 0x23d68
movslq (%rax), %rcx
leaq 0x1b1d9(%rip), %rax     # 0x235d0
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movzwl 0x2(%rax), %edi
callq 0xd2a0
leaq 0x1cdba(%rip), %rdi     # 0x251c8
callq 0xa6f0
leaq 0x1cdae(%rip), %rdi     # 0x251c8
callq 0xa740
xorl %ecx, %ecx
movzbl -0xc(%rbp), %edi
movl %ecx, %esi
movl %ecx, %edx
callq 0x8850
jmp 0x8493
leaq 0x1b931(%rip), %rax     # 0x23d68
movslq (%rax), %rcx
leaq 0x1b18f(%rip), %rax     # 0x235d0
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movzwl (%rax), %edi
callq 0xd2a0
xorl %ecx, %ecx
movzbl -0xc(%rbp), %edi
movl %ecx, %esi
movl %ecx, %edx
callq 0x8850
jmp 0x8493
leaq 0x1b900(%rip), %rax     # 0x23d68
movslq (%rax), %rcx
leaq 0x1b15e(%rip), %rax     # 0x235d0
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movzwl (%rax), %edi
callq 0xd2a0
movl $0xd0, %esi
xorl %ecx, %ecx
movzbl -0xc(%rbp), %edi
movl %ecx, %edx
callq 0x8850
leaq 0x1b8ca(%rip), %rax     # 0x23d64
cmpl $0x0, (%rax)
je 0x851e
xorl %edi, %edi
leaq -0x30(%rbp), %rsi
callq 0x3130
movq -0x30(%rbp), %rax
subq -0x20(%rbp), %rax
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
movq %rax, -0xe8(%rbp)
movq -0x28(%rbp), %rax
subq -0x18(%rbp), %rax
movl $0xf4240, %ecx          # imm = 0xF4240
cqto
idivq %rcx
movq %rax, %rcx
movq -0xe8(%rbp), %rax
addq %rcx, %rax
movl %eax, -0xc4(%rbp)
cmpl $0x0, -0xc4(%rbp)
jne 0x84f8
movl $0x1, -0xc4(%rbp)
leaq 0x1b875(%rip), %rax     # 0x23d74
cmpl $0x0, (%rax)
je 0x851c
movq -0x38(%rbp), %rsi
movl -0xc4(%rbp), %edx
leaq 0x13514(%rip), %rdi     # 0x1ba29
movb $0x0, %al
callq 0xcf60
jmp 0x851e
jmp 0x8520
addq $0xf0, %rsp
popq %rbp
retq
nopl (%rax)