// File: /workspace/github_c_makefile_star2_O1/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
cmpl $0x10, %edx
jae 0x6235
movl %edx, %r12d
movl $0xc017, %eax           # imm = 0xC017
btl %edx, %eax
jae 0x6235
movl %esi, %r13d
movq %rdi, -0x38(%rbp)
movl $0xc001, %r14d          # imm = 0xC001
movl %r12d, %ecx
shrl %cl, %r14d
leal 0xe(,%r13,2), %r15d
leal 0x16(,%r13,2), %edi
callq 0x2190
testq %rax, %rax
je 0x624e
movq %rax, %rbx
movl %r15d, (%rax)
movl $0x0, 0x4(%rax)
leaq 0x8(%rbx), %rdi
movl %r15d, %edx
xorl %esi, %esi
callq 0x20d0
testq %rbx, %rbx
je 0x623a
movq %rbx, %rdi
callq 0x811c
movb 0x9604(%rip), %al       # 0xf6e0
shlb $0x3, %al
movb %al, -0x2c(%rbp)
movb $0x0, 0x95fa(%rip)      # 0xf6e3
testb $0x1, %r14b
jne 0x6108
movb 0x95ef(%rip), %cl       # 0xf6e4
leal -0x80(%rcx), %edx
orb %al, %dl
movb %dl, -0x2c(%rbp)
incb %cl
andb $0x7, %cl
movb %cl, 0x95dc(%rip)       # 0xf6e4
cmpb $0x0, 0x95d3(%rip)      # 0xf6e2
je 0x6115
orb $0x40, -0x2c(%rbp)
movl %r13d, %eax
shll $0x4, %eax
orl %r12d, %eax
movb %al, -0x2b(%rbp)
movq %r13, %r15
movl %r13d, %ecx
shrl $0x4, %ecx
movb %cl, -0x2a(%rbp)
addl %eax, %ecx
addb -0x2c(%rbp), %cl
notb %cl
movb %cl, -0x29(%rbp)
movw $0xffff, %r13w          # imm = 0xFFFF
xorl %r14d, %r14d
movzbl -0x2c(%rbp,%r14), %r12d
movq %rbx, %rdi
movl %r12d, %esi
callq 0x8138
cmpb $0x0, 0x958b(%rip)      # 0xf6e2
je 0x618b
movzwl %r13w, %eax
shrl $0x4, %eax
movl %r12d, %ecx
xorl %r13d, %ecx
andl $0xf, %ecx
leaq 0x5030(%rip), %rdx      # 0xb1a0
xorw (%rdx,%rcx,2), %ax
movzwl %ax, %r13d
shrl $0x4, %r13d
shrl $0x4, %r12d
andl $0xf, %eax
xorl %r12d, %eax
xorw (%rdx,%rax,2), %r13w
incq %r14
cmpq $0x4, %r14
jne 0x613f
testl %r15d, %r15d
jle 0x61f5
movl %r15d, %r12d
xorl %r14d, %r14d
movq -0x38(%rbp), %r15
movzbl (%r15,%r14), %esi
movq %rbx, %rdi
callq 0x8138
cmpb $0x0, 0x952b(%rip)      # 0xf6e2
je 0x61ed
movzbl (%r15,%r14), %eax
movzwl %r13w, %ecx
shrl $0x4, %ecx
xorl %eax, %r13d
andl $0xf, %r13d
leaq 0x4fcd(%rip), %rdx      # 0xb1a0
xorw (%rdx,%r13,2), %cx
movzwl %cx, %r13d
shrl $0x4, %r13d
shrl $0x4, %eax
andl $0xf, %ecx
xorl %eax, %ecx
xorw (%rdx,%rcx,2), %r13w
incq %r14
cmpq %r14, %r12
jne 0x61a3
cmpb $0x0, 0x94e6(%rip)      # 0xf6e2
je 0x622b
movzwl %r13w, %eax
movzbl %al, %ecx
leaq 0x31f4(%rip), %rdx      # 0x9400
movzbl (%rcx,%rdx), %esi
shrl $0x8, %eax
movzbl (%rax,%rdx), %r14d
movq %rbx, %rdi
callq 0x8138
movq %rbx, %rdi
movl %r14d, %esi
callq 0x8138
movq %rbx, %rdi
callq 0x811c
jmp 0x623c
callq 0x2d6e
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2d92
xorl %ebx, %ebx
jmp 0x60b7