// File: /workspace/github_c_makefile_star2_O0/seuscq[P]rtk_hciattach/hciattach_rtk.c
// Function: h5_recv
static int h5_recv(rtk_hw_cfg_t * h5, void *data, int count)
{
	unsigned char *ptr;
	//RS_DBG("count %d rx_state %d rx_count %ld", count, h5->rx_state, h5->rx_count);
	ptr = (unsigned char *)data;

	while (count) {
		if (h5->rx_count) {
			if (*ptr == 0xc0) {
				RS_ERR("short h5 packet");
				skb_free(h5->rx_skb);
				h5->rx_state = H5_W4_PKT_START;
				h5->rx_count = 0;
			} else
				h5_unslip_one_byte(h5, *ptr);

			ptr++;
			count--;
			continue;
		}

		switch (h5->rx_state) {
		case H5_W4_HDR:
			/* check header checksum. see Core Spec V4 "3-wire uart" page 67 */
			if ((0xff & (RT_U8) ~
			     (h5->rx_skb->data[0] + h5->rx_skb->data[1] +
			      h5->rx_skb->data[2])) != h5->rx_skb->data[3]) {
				RS_ERR("h5 hdr checksum error!!!");
				skb_free(h5->rx_skb);
				h5->rx_state = H5_W4_PKT_DELIMITER;
				h5->rx_count = 0;
				continue;
			}

			/* reliable pkt  & h5->hdr->SeqNumber != h5->Rxseq_txack */
			if (h5->rx_skb->data[0] & 0x80
			    && (h5->rx_skb->data[0] & 0x07) !=
			    h5->rxseq_txack) {
				RS_ERR
				    ("Out-of-order packet arrived, got(%u)expected(%u)",
				     h5->rx_skb->data[0] & 0x07,
				     h5->rxseq_txack);
				h5->is_txack_req = 1;

				skb_free(h5->rx_skb);
				h5->rx_state = H5_W4_PKT_DELIMITER;
				h5->rx_count = 0;

				/* depend on weather remote will reset ack numb or not!!!!!!special */
				if (rtk_hw_cfg.tx_index == rtk_hw_cfg.total_num) {
					rtk_hw_cfg.rxseq_txack =
					    h5->rx_skb->data[0] & 0x07;
				}
				continue;
			}
			h5->rx_state = H5_W4_DATA;
			h5->rx_count =
			    (h5->rx_skb->data[1] >> 4) +
			    (h5->rx_skb->data[2] << 4);
			continue;

		case H5_W4_DATA:
			/* pkt with crc */
			if (h5->rx_skb->data[0] & 0x40) {
				h5->rx_state = H5_W4_CRC;
				h5->rx_count = 2;
			} else {
				h5_complete_rx_pkt(h5);
				//RS_DBG(DF_SLIP,("--------> H5_W4_DATA ACK\n"));
			}
			continue;

		case H5_W4_CRC:
			if (bit_rev16(h5->message_crc) != h5_get_crc(h5)) {
				RS_ERR
				    ("Checksum failed, computed(%04x)received(%04x)",
				     bit_rev16(h5->message_crc),
				     h5_get_crc(h5));
				skb_free(h5->rx_skb);
				h5->rx_state = H5_W4_PKT_DELIMITER;
				h5->rx_count = 0;
				continue;
			}
			skb_trim(h5->rx_skb, h5->rx_skb->data_len - 2);
			h5_complete_rx_pkt(h5);
			continue;

		case H5_W4_PKT_DELIMITER:
			switch (*ptr) {
			case 0xc0:
				h5->rx_state = H5_W4_PKT_START;
				break;

			default:
				break;
			}
			ptr++;
			count--;
			break;

		case H5_W4_PKT_START:
			switch (*ptr) {
			case 0xc0:
				ptr++;
				count--;
				break;

			default:
				h5->rx_state = H5_W4_HDR;
				h5->rx_count = 4;
				h5->rx_esc_state = H5_ESCSTATE_NOESC;
				H5_CRC_INIT(h5->message_crc);

				// Do not increment ptr or decrement count
				// Allocate packet. Max len of a H5 pkt=
				// 0xFFF (payload) +4 (header) +2 (crc)
				h5->rx_skb = skb_alloc(0x1005);
				if (!h5->rx_skb) {
					h5->rx_state = H5_W4_PKT_DELIMITER;
					h5->rx_count = 0;
					return 0;
				}
				break;
			}
			break;

		default:
			break;
		}
	}
	return count;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x28(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x5d19
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x599d
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
cmpl $0xc0, %eax
jne 0x5973
jmp 0x592f
movq 0xa6c2(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x6b61(%rip), %rsi      # 0xc4a1
leaq 0x5c42(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x66c0
movq -0x10(%rbp), %rax
movl $0x1, 0xc(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x8(%rax)
jmp 0x5983
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rax
movzbl (%rax), %esi
callq 0x87f0
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movl -0x1c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x590b
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
movq %rax, -0x30(%rbp)
subq $0x4, %rax
ja 0x5d12
movq -0x30(%rbp), %rax
leaq 0x5a33(%rip), %rcx      # 0xb3f0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movzbl 0x8(%rax), %eax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
movzbl 0x9(%rcx), %ecx
addl %ecx, %eax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
movzbl 0xa(%rcx), %ecx
addl %ecx, %eax
xorl $-0x1, %eax
movzbl %al, %eax
andl $0xff, %eax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
movzbl 0xb(%rcx), %ecx
cmpl %ecx, %eax
je 0x5a52
jmp 0x5a0b
movq 0xa5e6(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x6aa0(%rip), %rsi      # 0xc4bc
leaq 0x5b66(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x66c0
movq -0x10(%rbp), %rax
movl $0x0, 0xc(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x8(%rax)
jmp 0x590b
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movzbl 0x8(%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x5b15
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movzbl 0x8(%rax), %eax
andl $0x7, %eax
movq -0x10(%rbp), %rcx
movzbl (%rcx), %ecx
cmpl %ecx, %eax
je 0x5b15
jmp 0x5a8c
movq 0xa565(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movzbl 0x8(%rax), %ecx
andl $0x7, %ecx
movq -0x10(%rbp), %rax
movzbl (%rax), %r8d
leaq 0x6a2c(%rip), %rsi      # 0xc4e0
leaq 0x5ace(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
movq -0x10(%rbp), %rax
movb $0x1, 0x3(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x66c0
movq -0x10(%rbp), %rax
movl $0x0, 0xc(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x8(%rax)
movl 0xac31(%rip), %eax      # 0x10724
cmpl 0xac27(%rip), %eax      # 0x10720
jne 0x5b10
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movzbl 0x8(%rax), %eax
andl $0x7, %eax
movb %al, 0xabc0(%rip)       # 0x106d0
jmp 0x590b
movq -0x10(%rbp), %rax
movl $0x3, 0xc(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movzbl 0x9(%rax), %ecx
sarl $0x4, %ecx
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movzbl 0xa(%rax), %eax
shll $0x4, %eax
addl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x8(%rax)
jmp 0x590b
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movzbl 0x8(%rax), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0x5b78
movq -0x10(%rbp), %rax
movl $0x4, 0xc(%rax)
movq -0x10(%rbp), %rax
movl $0x2, 0x8(%rax)
jmp 0x5b81
movq -0x10(%rbp), %rdi
callq 0x8ac0
jmp 0x590b
movq -0x10(%rbp), %rax
movzwl 0x6(%rax), %edi
callq 0x8700
movzwl %ax, %eax
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rdi
callq 0x8c10
movw %ax, %cx
movl -0x34(%rbp), %eax
movzwl %cx, %ecx
cmpl %ecx, %eax
je 0x5c23
jmp 0x5bb1
movq 0xa440(%rip), %rax      # 0xfff8
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movzwl 0x6(%rax), %edi
callq 0x8700
movzwl %ax, %eax
movl %eax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
callq 0x8c10
movq -0x40(%rbp), %rdi
movl -0x38(%rbp), %ecx
movzwl %ax, %r8d
leaq 0x692f(%rip), %rsi      # 0xc51c
leaq 0x5995(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x66c0
movq -0x10(%rbp), %rax
movl $0x0, 0xc(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x8(%rax)
jmp 0x590b
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x4(%rax), %esi
subl $0x2, %esi
callq 0x8c60
movq -0x10(%rbp), %rdi
callq 0x8ac0
jmp 0x590b
movq -0x28(%rbp), %rax
movb (%rax), %al
subb $-0x40, %al
jne 0x5c65
jmp 0x5c58
movq -0x10(%rbp), %rax
movl $0x1, 0xc(%rax)
jmp 0x5c67
jmp 0x5c67
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movl -0x1c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x5d14
movq -0x28(%rbp), %rax
movb (%rax), %al
subb $-0x40, %al
jne 0x5ca4
jmp 0x5c8d
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movl -0x1c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x5d10
movq -0x10(%rbp), %rax
movl $0x2, 0xc(%rax)
movq -0x10(%rbp), %rax
movl $0x4, 0x8(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x10(%rax)
movq -0x10(%rbp), %rax
movw $0xffff, 0x6(%rax)      # imm = 0xFFFF
movl $0x1005, %edi           # imm = 0x1005
callq 0x84f0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x5d0e
movq -0x10(%rbp), %rax
movl $0x0, 0xc(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x8(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x5d1f
jmp 0x5d10
jmp 0x5d14
jmp 0x5d14
jmp 0x590b
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)