// File: /workspace/github_c_makefile_star2_O2/seuscq[P]rtk_hciattach/hciattach_rtk.c
// Function: read_hci_evt
int read_hci_evt(int fd, uint8_t * buff, uint8_t evt_code)
{
	uint8_t *evt_buff = buff;
	int ret;
	int try_type = 0;
	uint8_t vendor_evt = 0xff;

start_read:
	do {
		ret = os_read(fd, evt_buff, 1);
		if (ret == 1 && evt_buff[0] == 0x04)
			break;
		else {
			RS_DBG("no pkt type, continue.");
			try_type++;
			continue;
		}
	} while (try_type < 6);

	if (try_type >= 6)
		return -1;

	ret = os_read(fd, evt_buff + 1, 1);
	if (ret < 0) {
		RS_ERR("%s: failed to read event code\n", __func__);
		return -1;
	}

	ret = os_read(fd, evt_buff + 2, 1);
	if (ret < 0) {
		RS_ERR("%s: failed to read parameter total len.\n", __func__);
		return -1;
	}

	ret = os_read(fd, evt_buff + 3, evt_buff[2]);
	if (ret < 0) {
		RS_ERR("%s: failed to read payload of event.\n", __func__);
		return -1;
	}
#ifdef DUMP_HCI_EVT
	hci_dump_evt(evt_buff, ret + 3);
#endif

	/* This event to wake up host. */
	if (evt_buff[1] == vendor_evt) {
		try_type = 0;
		RS_DBG("%s: found vendor evt, continue reading.\n", __func__);
		goto start_read;
	}

	if (evt_buff[1] != evt_code) {
		RS_ERR("%s: event code mismatches, %x, expect %x.\n",
		       __func__, evt_buff[1], evt_code);
		return -1;
	}

	return (ret + 3);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, -0x2c(%rbp)
movq %rsi, %r14
movl %edi, %r15d
leaq 0x1(%rsi), %rax
movq %rax, -0x40(%rbp)
leaq 0x2(%rsi), %r13
leaq 0x3(%rsi), %rax
movq %rax, -0x38(%rbp)
pushq $0x1
popq %r12
xorl %ebx, %ebx
movl %r15d, %edi
movq %r14, %rsi
movl %r12d, %edx
callq 0x33bd
cmpl $0x1, %eax
jne 0x36de
cmpb $0x4, (%r14)
jne 0x36de
movl %r15d, %edi
movq -0x40(%rbp), %rsi
movl %r12d, %edx
callq 0x33bd
testl %eax, %eax
js 0x374c
movl %r15d, %edi
movq %r13, %rsi
movl %r12d, %edx
callq 0x33bd
testl %eax, %eax
js 0x375f
movzbl (%r13), %edx
movl %r15d, %edi
movq -0x38(%rbp), %rsi
callq 0x33bd
testl %eax, %eax
js 0x3772
movl %eax, %ebx
addl $0x3, %ebx
movzwl %bx, %esi
movq %r14, %rdi
callq 0x34be
movzbl 0x1(%r14), %r8d
cmpl $0xff, %r8d
jne 0x3718
cmpb $0x0, 0x8c3a(%rip)      # 0xc2e0
movl $0x0, %ebx
je 0x361a
movq 0x8940(%rip), %rax      # 0xbff8
movq (%rax), %rdi
xorl %ebx, %ebx
leaq 0x4fcd(%rip), %rsi      # 0x8691
leaq 0x4e6e(%rip), %rdx      # 0x8539
leaq 0x4f4d(%rip), %rcx      # 0x861f
xorl %eax, %eax
callq 0x2150
jmp 0x361a
cmpb $0x0, 0x8bfb(%rip)      # 0xc2e0
jne 0x36f7
incl %ebx
cmpl $0x6, %ebx
jne 0x361a
jmp 0x3798
movq 0x88fa(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x4ed1(%rip), %rsi      # 0x85d9
leaq 0x4e2a(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
jmp 0x36e7
movl -0x2c(%rbp), %eax
cmpb %al, %r8b
je 0x379b
movzbl %al, %r9d
movq 0x88cd(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x4f8a(%rip), %rsi      # 0x86bf
leaq 0x4dfd(%rip), %rdx      # 0x8539
leaq 0x4edc(%rip), %rcx      # 0x861f
xorl %eax, %eax
callq 0x2150
jmp 0x3798
movq 0x88a5(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x4e98(%rip), %rsi      # 0x85f5
jmp 0x3783
movq 0x8892(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x4ebc(%rip), %rsi      # 0x862c
jmp 0x3783
movq 0x887f(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x4edd(%rip), %rsi      # 0x8660
leaq 0x4daf(%rip), %rdx      # 0x8539
leaq 0x4e8e(%rip), %rcx      # 0x861f
xorl %eax, %eax
callq 0x2150
pushq $-0x1
popq %rbx
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq