// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
testl %esi, %esi
je 0x672d
movl %esi, %ebx
movq %rdi, %r14
leaq 0x3113(%rip), %r13      # 0x93d0
movabsq $0x200000004, %r12      # imm = 0x200000004
movabsq $0x400000002, %r15      # imm = 0x400000002
cmpl $0x0, 0x9410(%rip)      # 0xf6e8
je 0x6313
movzbl (%r14), %eax
cmpl $0xc0, %eax
je 0x66b3
movl 0x9401(%rip), %ecx      # 0xf6f0
cmpl $0x1, %ecx
je 0x6346
testl %ecx, %ecx
jne 0x66a4
cmpb $-0x25, %al
jne 0x63cd
movl $0x1, 0x93e2(%rip)      # 0xf6f0
jmp 0x66a4
movl 0x93d3(%rip), %eax      # 0xf6ec
cmpq $0x4, %rax
ja 0x66a9
movslq (%r13,%rax,4), %rax
addq %r13, %rax
jmpq *%rax
cmpb $-0x40, (%r14)
jne 0x66a4
movl $0x1, 0x93ab(%rip)      # 0xf6ec
jmp 0x66a4
movl %eax, %ecx
addl $0xffffff24, %ecx       # imm = 0xFFFFFF24
cmpl $0x3, %ecx
ja 0x66f5
leaq 0x3062(%rip), %rdx      # 0x93c0
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movq 0x938a(%rip), %rax      # 0xf6f8
movl 0x4(%rax), %ecx
leal 0x1(%rcx), %edx
cmpl (%rax), %edx
ja 0x6750
movl %edx, 0x4(%rax)
movb $-0x40, 0x8(%rax,%rcx)
movq 0x936d(%rip), %rax      # 0xf6f8
testb $0x40, 0x8(%rax)
je 0x6694
cmpl $0x4, 0x9350(%rip)      # 0xf6ec
je 0x6694
movzwl 0x933d(%rip), %eax      # 0xf6e6
movl %eax, %ecx
shrl $0x4, %ecx
andl $0xf, %eax
leaq 0x4de8(%rip), %rdx      # 0xb1a0
xorw (%rdx,%rax,2), %cx
movzwl %cx, %eax
shrl $0x4, %eax
andl $0xf, %ecx
xorl $0xc, %ecx
jmp 0x6689
movq 0x9324(%rip), %rcx      # 0xf6f8
movl 0x4(%rcx), %edx
leal 0x1(%rdx), %esi
cmpl (%rcx), %esi
ja 0x673c
movl %esi, 0x4(%rcx)
movb %al, 0x8(%rcx,%rdx)
movq 0x9308(%rip), %rcx      # 0xf6f8
testb $0x40, 0x8(%rcx)
je 0x669e
cmpl $0x4, 0x92eb(%rip)      # 0xf6ec
je 0x669e
movzwl 0x92d8(%rip), %ecx      # 0xf6e6
movl %ecx, %edx
shrl $0x4, %edx
xorl %eax, %ecx
andl $0xf, %ecx
leaq 0x4d81(%rip), %rsi      # 0xb1a0
xorw (%rsi,%rcx,2), %dx
movzwl %dx, %ecx
shrl $0x4, %ecx
shrl $0x4, %eax
andl $0xf, %edx
xorl %eax, %edx
xorw (%rsi,%rdx,2), %cx
movw %cx, 0x92aa(%rip)       # 0xf6e6
jmp 0x669e
movzwl 0x929e(%rip), %eax      # 0xf6e6
movzbl %al, %ecx
leaq 0x2fae(%rip), %rdx      # 0x9400
movzbl (%rcx,%rdx), %ecx
shll $0x8, %ecx
shrl $0x8, %eax
movzbl (%rax,%rdx), %edi
orl %ecx, %edi
movq 0x928f(%rip), %rax      # 0xf6f8
movl 0x4(%rax), %ecx
movzwl 0x6(%rax,%rcx), %esi
rolw $0x8, %si
cmpw %si, %di
jne 0x66c3
leal -0x2(%rcx), %r8d
cmpl $0x2, %ecx
jb 0x66d1
movl %r8d, 0x4(%rax)
jmp 0x64fa
movq 0x9260(%rip), %rax      # 0xf6f8
movb 0x8(%rax), %cl
movb 0x9(%rax), %dl
addb %cl, %dl
addb 0xa(%rax), %dl
xorb 0xb(%rax), %dl
cmpb $-0x1, %dl
jne 0x66ca
testb %cl, %cl
jns 0x64c4
andb $0x7, %cl
movb 0x9224(%rip), %dl       # 0xf6e0
cmpb %dl, %cl
jne 0x6709
movl $0x3, 0x921e(%rip)      # 0xf6ec
movzbl 0x9(%rax), %ecx
shrl $0x4, %ecx
movzbl 0xa(%rax), %eax
shll $0x4, %eax
orl %ecx, %eax
movl %eax, 0x9204(%rip)      # 0xf6e8
jmp 0x66a9
movq 0x9208(%rip), %rax      # 0xf6f8
testb $0x40, 0x8(%rax)
jne 0x66ba
callq 0x825b
jmp 0x66a9
cmpb $-0x40, (%r14)
je 0x66a4
movq %r15, %r13
movq %r12, %r15
movq %r12, 0x91cd(%rip)      # 0xf6e8
movl $0x0, 0x91cb(%rip)      # 0xf6f0
movw $0xffff, 0x91b8(%rip)   # imm = 0xFFFF
movl $0x100d, %edi           # imm = 0x100D
callq 0x2190
movq %rax, %r12
testq %rax, %rax
je 0x66ff
movq $0x1005, (%r12)         # imm = 0x1005
movq %r12, %rdi
addq $0x8, %rdi
movl $0x1005, %edx           # imm = 0x1005
xorl %esi, %esi
callq 0x20d0
movq %r12, 0x9192(%rip)      # 0xf6f8
movq %r15, %r12
movq %r13, %r15
leaq 0x2e5d(%rip), %r13      # 0x93d0
jmp 0x66a9
movq 0x9179(%rip), %rax      # 0xf6f8
movl 0x4(%rax), %ecx
leal 0x1(%rcx), %edx
cmpl (%rax), %edx
ja 0x6746
movl %edx, 0x4(%rax)
movb $0x11, 0x8(%rax,%rcx)
movq 0x915c(%rip), %rax      # 0xf6f8
testb $0x40, 0x8(%rax)
je 0x6694
cmpl $0x4, 0x913f(%rip)      # 0xf6ec
je 0x6694
movzwl 0x912c(%rip), %eax      # 0xf6e6
movl %eax, %ecx
shrl $0x4, %ecx
andl $0xf, %eax
xorl $0x1, %eax
jmp 0x6614
movq 0x912a(%rip), %rax      # 0xf6f8
movl 0x4(%rax), %ecx
leal 0x1(%rcx), %edx
cmpl (%rax), %edx
ja 0x6741
movl %edx, 0x4(%rax)
movb $0x13, 0x8(%rax,%rcx)
movq 0x910d(%rip), %rax      # 0xf6f8
testb $0x40, 0x8(%rax)
je 0x6694
cmpl $0x4, 0x90f0(%rip)      # 0xf6ec
je 0x6694
movzwl 0x90dd(%rip), %eax      # 0xf6e6
movl %eax, %ecx
shrl $0x4, %ecx
andl $0xf, %eax
xorl $0x3, %eax
leaq 0x4b85(%rip), %rdx      # 0xb1a0
xorw (%rdx,%rax,2), %cx
movzwl %cx, %eax
shrl $0x4, %eax
andl $0xf, %ecx
xorl $0x1, %ecx
jmp 0x6689
movq 0x90c4(%rip), %rax      # 0xf6f8
movl 0x4(%rax), %ecx
leal 0x1(%rcx), %edx
cmpl (%rax), %edx
ja 0x674b
movl %edx, 0x4(%rax)
movb $-0x25, 0x8(%rax,%rcx)
movq 0x90a7(%rip), %rax      # 0xf6f8
testb $0x40, 0x8(%rax)
je 0x6694
cmpl $0x4, 0x908e(%rip)      # 0xf6ec
je 0x6694
movzwl 0x907f(%rip), %eax      # 0xf6e6
movl %eax, %ecx
shrl $0x4, %ecx
andl $0xf, %eax
xorl $0xb, %eax
leaq 0x4b27(%rip), %rdx      # 0xb1a0
xorw (%rdx,%rax,2), %cx
movzwl %cx, %eax
shrl $0x4, %eax
andl $0xf, %ecx
xorl $0xd, %ecx
xorw (%rdx,%rcx,2), %ax
movw %ax, 0x9052(%rip)       # 0xf6e6
movl $0x0, 0x9052(%rip)      # 0xf6f0
decl 0x9044(%rip)            # 0xf6e8
incq %r14
decl %ebx
testl %ebx, %ebx
jne 0x62d1
jmp 0x672d
callq 0x2ec2
jmp 0x66a4
movq %r15, 0x9027(%rip)      # 0xf6e8
jmp 0x66a9
callq 0x2f28
jmp 0x66a9
callq 0x2f6b
jmp 0x66a9
movq 0x8920(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x41ac(%rip), %rsi      # 0xa88e
leaq 0x2e50(%rip), %rdx      # 0x9539
xorl %eax, %eax
callq 0x2150
jmp 0x64fa
movzbl %al, %edi
callq 0x2e5a
jmp 0x66a4
callq 0x2f04
jmp 0x654c
movzbl %dl, %edi
movzbl %cl, %esi
movl %ebx, %edx
movq %r14, %rcx
leaq -0x2c(%rbp), %r8
leaq -0x38(%rbp), %r9
callq 0x2fa4
movl -0x2c(%rbp), %ebx
movq -0x38(%rbp), %r14
jmp 0x66a9
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2e9f
callq 0x2dce
callq 0x2df1
callq 0x2e14
callq 0x2e37