// File: /workspace/github_c_makefile_star2_O2/seuscq[P]rtk_hciattach/hciattach_rtk.c
// Function: h5_prepare_pkt
static struct sk_buff *h5_prepare_pkt(rtk_hw_cfg_t * h5, RT_U8 * data,
				      RT_S32 len, RT_S32 pkt_type)
{
	struct sk_buff *nskb;
	RT_U8 hdr[4];
	RT_U16 H5_CRC_INIT(h5_txmsg_crc);
	int rel, i;

	switch (pkt_type) {
	case HCI_ACLDATA_PKT:
	case HCI_COMMAND_PKT:
	case HCI_EVENT_PKT:
		rel = 1;	// reliable
		break;

	case H5_ACK_PKT:
	case H5_VDRSPEC_PKT:
	case H5_LINK_CTL_PKT:
		rel = 0;	// unreliable
		break;

	default:
		RS_ERR("Unknown packet type");
		return NULL;
	}

	// Max len of packet: (original len +4(h5 hdr) +2(crc))*2
	//   (because bytes 0xc0 and 0xdb are escaped, worst case is
	//   when the packet is all made of 0xc0 and 0xdb :) )
	//   + 2 (0xc0 delimiters at start and end).

	nskb = skb_alloc((len + 6) * 2 + 2);
	if (!nskb)
		return NULL;

	//Add SLIP start byte: 0xc0
	h5_slip_msgdelim(nskb);
	// set AckNumber in SlipHeader
	hdr[0] = h5->rxseq_txack << 3;
	h5->is_txack_req = 0;

	//RS_DBG("We request packet no(%u) to card", h5->rxseq_txack);
	//RS_DBG("Sending packet with seqno %u and wait %u", h5->msgq_txseq, h5->rxseq_txack);
	if (rel) {
		// set reliable pkt bit and SeqNumber
		hdr[0] |= 0x80 + h5->msgq_txseq;
		//RS_DBG("Sending packet with seqno(%u)", h5->msgq_txseq);
		++(h5->msgq_txseq);
		h5->msgq_txseq = (h5->msgq_txseq) & 0x07;
	}
	// set DicPresent bit
	if (h5->use_crc)
		hdr[0] |= 0x40;

	// set packet type and payload length
	hdr[1] = ((len << 4) & 0xff) | pkt_type;
	hdr[2] = (RT_U8) (len >> 4);
	// set checksum
	hdr[3] = ~(hdr[0] + hdr[1] + hdr[2]);

	// Put h5 header */
	for (i = 0; i < 4; i++) {
		h5_slip_one_byte(nskb, hdr[i]);

		if (h5->use_crc)
			h5_crc_update(&h5_txmsg_crc, hdr[i]);
	}

	// Put payload */
	for (i = 0; i < len; i++) {
		h5_slip_one_byte(nskb, data[i]);

		if (h5->use_crc)
			h5_crc_update(&h5_txmsg_crc, data[i]);
	}

	// Put CRC */
	if (h5->use_crc) {
		h5_txmsg_crc = bit_rev16(h5_txmsg_crc);
		h5_slip_one_byte(nskb, (RT_U8) ((h5_txmsg_crc >> 8) & 0x00ff));
		h5_slip_one_byte(nskb, (RT_U8) (h5_txmsg_crc & 0x00ff));
	}
	// Add SLIP end byte: 0xc0
	h5_slip_msgdelim(nskb);
	return nskb;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
orw $-0x1, -0x2a(%rbp)
cmpl $0x10, %edx
jae 0x4915
movl %edx, %r13d
movl $0xc017, %eax           # imm = 0xC017
btl %edx, %eax
jae 0x4915
movl %esi, %r14d
movq %rdi, %r15
leal 0xe(,%r14,2), %edi
callq 0x710f
testq %rax, %rax
je 0x4911
movq %rax, %rbx
movq %rax, %rdi
callq 0x716b
movb 0x7ee1(%rip), %al       # 0xc6e0
shlb $0x3, %al
movb %al, -0x2e(%rbp)
movb $0x0, 0x7ed7(%rip)      # 0xc6e3
movl $0xc001, %ecx           # imm = 0xC001
btl %r13d, %ecx
jb 0x4832
movb 0x7ec7(%rip), %cl       # 0xc6e4
leal -0x80(%rcx), %edx
orb %al, %dl
movb %dl, -0x2e(%rbp)
incb %cl
andb $0x7, %cl
movb %cl, 0x7eb4(%rip)       # 0xc6e4
movl %edx, %eax
movb 0x7ea9(%rip), %r12b     # 0xc6e2
testb %r12b, %r12b
je 0x4843
orb $0x40, %al
movb %al, -0x2e(%rbp)
movl %r14d, %ecx
shll $0x4, %ecx
orl %r13d, %ecx
movb %cl, -0x2d(%rbp)
movq %r14, -0x38(%rbp)
movl %r14d, %edx
shrl $0x4, %edx
movb %dl, -0x2c(%rbp)
addl %ecx, %edx
addb %al, %dl
notb %dl
movb %dl, -0x2b(%rbp)
xorl %r14d, %r14d
cmpq $0x4, %r14
je 0x489c
movzbl -0x2e(%rbp,%r14), %r13d
movq %rbx, %rdi
movl %r13d, %esi
callq 0x717c
movb 0x7e5c(%rip), %r12b     # 0xc6e2
testb %r12b, %r12b
je 0x4897
leaq -0x2a(%rbp), %rdi
movl %r13d, %esi
callq 0x71eb
incq %r14
jmp 0x4868
xorl %r13d, %r13d
movq -0x38(%rbp), %r14
testl %r14d, %r14d
cmovlel %r13d, %r14d
cmpq %r13, %r14
je 0x48db
movzbl (%r15,%r13), %esi
movq %rbx, %rdi
callq 0x717c
movb 0x7e1f(%rip), %r12b     # 0xc6e2
testb %r12b, %r12b
je 0x48d6
movzbl (%r15,%r13), %esi
leaq -0x2a(%rbp), %rdi
callq 0x71eb
incq %r13
jmp 0x48aa
testb %r12b, %r12b
je 0x4907
movzwl -0x2a(%rbp), %edi
callq 0x721e
movzwl %ax, %r14d
movl %r14d, %esi
shrl $0x8, %esi
movq %rbx, %rdi
callq 0x717c
movzbl %r14b, %esi
movq %rbx, %rdi
callq 0x717c
movq %rbx, %rdi
callq 0x716b
jmp 0x4936
xorl %ebx, %ebx
jmp 0x4936
movq 0x76dc(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x4acc(%rip), %rsi      # 0x93f2
leaq 0x3c0c(%rip), %rdx      # 0x8539
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2150
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq