// File: /workspace/github_c_makefile_star2_O3/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 0x622d
movl %edx, %r12d
movl $0xc017, %eax           # imm = 0xC017
btl %edx, %eax
jae 0x622d
movl %esi, %r14d
movq %rdi, -0x38(%rbp)
leal 0xe(,%r14,2), %r13d
leal 0x16(,%r14,2), %edi
callq 0x2190
testq %rax, %rax
je 0x6234
movq %rax, %rbx
movl %r13d, (%rax)
movl $0x0, 0x4(%rax)
movq %rax, %rdi
addq $0x8, %rdi
movl %r13d, %edx
xorl %esi, %esi
callq 0x20d0
movq %rbx, %rdi
callq 0x819a
movb 0x9628(%rip), %al       # 0xf6e0
shlb $0x3, %al
movb %al, -0x2c(%rbp)
movb $0x0, 0x961e(%rip)      # 0xf6e3
movl $0xc001, %ecx           # imm = 0xC001
btl %r12d, %ecx
jb 0x60eb
movb 0x960e(%rip), %cl       # 0xf6e4
leal -0x80(%rcx), %edx
orb %al, %dl
movb %dl, -0x2c(%rbp)
incb %cl
andb $0x7, %cl
movb %cl, 0x95fb(%rip)       # 0xf6e4
movl %edx, %eax
cmpb $0x0, 0x95f0(%rip)      # 0xf6e2
je 0x60f9
orb $0x40, %al
movb %al, -0x2c(%rbp)
movl %r14d, %ecx
shll $0x4, %ecx
orl %r12d, %ecx
movb %cl, -0x2b(%rbp)
movq %r14, -0x40(%rbp)
movl %r14d, %edx
shrl $0x4, %edx
movb %dl, -0x2a(%rbp)
addl %ecx, %edx
addb %al, %dl
notb %dl
movb %dl, -0x29(%rbp)
movw $0xffff, %r13w          # imm = 0xFFFF
xorl %r14d, %r14d
leaq 0x5076(%rip), %r15      # 0xb1a0
movzbl -0x2c(%rbp,%r14), %r12d
movq %rbx, %rdi
movl %r12d, %esi
callq 0x81b6
movb 0x95a1(%rip), %al       # 0xf6e2
testb %al, %al
je 0x6171
movzwl %r13w, %ecx
shrl $0x4, %ecx
movl %r12d, %edx
xorl %r13d, %edx
andl $0xf, %edx
xorw (%r15,%rdx,2), %cx
movzwl %cx, %r13d
shrl $0x4, %r13d
shrl $0x4, %r12d
andl $0xf, %ecx
xorl %r12d, %ecx
xorw (%r15,%rcx,2), %r13w
incq %r14
cmpq $0x4, %r14
jne 0x612a
movq %rbx, %r15
movq -0x40(%rbp), %rcx
testl %ecx, %ecx
jle 0x61e2
movl %ecx, %r12d
xorl %r14d, %r14d
movq -0x38(%rbp), %rbx
movzbl (%rbx,%r14), %esi
movq %r15, %rdi
callq 0x81b6
movb 0x9540(%rip), %al       # 0xf6e2
testb %al, %al
je 0x61da
movzbl (%rbx,%r14), %ecx
movzwl %r13w, %edx
shrl $0x4, %edx
xorl %ecx, %r13d
andl $0xf, %r13d
leaq 0x4fe0(%rip), %rsi      # 0xb1a0
xorw (%rsi,%r13,2), %dx
movzwl %dx, %r13d
shrl $0x4, %r13d
shrl $0x4, %ecx
andl $0xf, %edx
xorl %ecx, %edx
xorw (%rsi,%rdx,2), %r13w
incq %r14
cmpq %r14, %r12
jne 0x618f
testb %al, %al
je 0x6213
movzwl %r13w, %eax
movzbl %al, %ecx
leaq 0x320c(%rip), %rdx      # 0x9400
movzbl (%rcx,%rdx), %esi
shrl $0x8, %eax
movzbl (%rax,%rdx), %r14d
movq %r15, %rdi
callq 0x81b6
movq %r15, %rdi
movl %r14d, %esi
callq 0x81b6
movq %r15, %rdi
callq 0x819a
movq %r15, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2d71
jmp 0x623c
movl %r13d, %edi
callq 0x2d95
xorl %r15d, %r15d
jmp 0x621b