// File: /workspace/github_c_makefile_star2_O0/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
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movw $0xffff, -0x2e(%rbp)    # imm = 0xFFFF
movl -0x20(%rbp), %eax
movl %eax, -0x3c(%rbp)
testl %eax, %eax
je 0x5654
jmp 0x5628
movl -0x3c(%rbp), %eax
decl %eax
subl $0x2, %eax
jb 0x564b
jmp 0x5634
movl -0x3c(%rbp), %eax
subl $0x4, %eax
je 0x564b
jmp 0x563e
movl -0x3c(%rbp), %eax
addl $-0xe, %eax
subl $0x2, %eax
jb 0x5654
jmp 0x565d
movl $0x1, -0x34(%rbp)
jmp 0x568b
movl $0x0, -0x34(%rbp)
jmp 0x568b
jmp 0x565f
movq 0xa992(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x6dd5(%rip), %rsi      # 0xc445
leaq 0x5f12(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
movq $0x0, -0x8(%rbp)
jmp 0x5840
movl -0x1c(%rbp), %edi
addl $0x6, %edi
shll %edi
addl $0x2, %edi
callq 0x84f0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x56b3
movq $0x0, -0x8(%rbp)
jmp 0x5840
movq -0x28(%rbp), %rdi
callq 0x8580
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
shll $0x3, %eax
movb %al, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movb $0x0, 0x3(%rax)
cmpl $0x0, -0x34(%rbp)
je 0x570f
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %ecx
addl $0x80, %ecx
movzbl -0x2c(%rbp), %eax
orl %ecx, %eax
movb %al, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movb 0x4(%rax), %cl
addb $0x1, %cl
movb %cl, 0x4(%rax)
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %eax
andl $0x7, %eax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x4(%rax)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x2(%rax)
je 0x5723
movzbl -0x2c(%rbp), %eax
orl $0x40, %eax
movb %al, -0x2c(%rbp)
movl -0x1c(%rbp), %eax
shll $0x4, %eax
andl $0xff, %eax
orl -0x20(%rbp), %eax
movb %al, -0x2b(%rbp)
movl -0x1c(%rbp), %eax
sarl $0x4, %eax
movb %al, -0x2a(%rbp)
movzbl -0x2c(%rbp), %eax
movzbl -0x2b(%rbp), %ecx
addl %ecx, %eax
movzbl -0x2a(%rbp), %ecx
addl %ecx, %eax
xorl $-0x1, %eax
movb %al, -0x29(%rbp)
movl $0x0, -0x38(%rbp)
cmpl $0x4, -0x38(%rbp)
jge 0x579b
movq -0x28(%rbp), %rdi
movslq -0x38(%rbp), %rax
movzbl -0x2c(%rbp,%rax), %esi
callq 0x85b0
movq -0x10(%rbp), %rax
cmpb $0x0, 0x2(%rax)
je 0x578e
movslq -0x38(%rbp), %rax
leaq -0x2e(%rbp), %rdi
movzbl -0x2c(%rbp,%rax), %esi
callq 0x8680
jmp 0x5790
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x575a
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x57eb
movq -0x28(%rbp), %rdi
movq -0x18(%rbp), %rax
movslq -0x38(%rbp), %rcx
movzbl (%rax,%rcx), %esi
callq 0x85b0
movq -0x10(%rbp), %rax
cmpb $0x0, 0x2(%rax)
je 0x57de
movq -0x18(%rbp), %rax
movslq -0x38(%rbp), %rcx
leaq -0x2e(%rbp), %rdi
movzbl (%rax,%rcx), %esi
callq 0x8680
jmp 0x57e0
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x57a2
movq -0x10(%rbp), %rax
cmpb $0x0, 0x2(%rax)
je 0x582f
movzwl -0x2e(%rbp), %edi
callq 0x8700
movw %ax, -0x2e(%rbp)
movq -0x28(%rbp), %rdi
movzwl -0x2e(%rbp), %eax
sarl $0x8, %eax
andl $0xff, %eax
movzbl %al, %esi
callq 0x85b0
movq -0x28(%rbp), %rdi
movzwl -0x2e(%rbp), %eax
andl $0xff, %eax
movzbl %al, %esi
callq 0x85b0
movq -0x28(%rbp), %rdi
callq 0x8580
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq
nopw (%rax,%rax)