// File: /workspace/github_c_makefile_star2_O0/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
subq $0x30, %rsp
movb %dl, %al
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movb %al, -0x11(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x20(%rbp)
movl $0x0, -0x28(%rbp)
movb $-0x1, -0x29(%rbp)
jmp 0x3f99
movl -0x8(%rbp), %edi
movq -0x20(%rbp), %rsi
movl $0x1, %edx
callq 0x3cf0
movl %eax, -0x24(%rbp)
cmpl $0x1, -0x24(%rbp)
jne 0x3fc1
movq -0x20(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x4, %eax
jne 0x3fc1
jmp 0x3ffc
jmp 0x3fc3
cmpb $0x0, 0xc306(%rip)      # 0x102d0
je 0x3feb
movq 0xc025(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x764c(%rip), %rsi      # 0xb629
leaq 0x75a5(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
jmp 0x3fed
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
cmpl $0x6, -0x28(%rbp)
jl 0x3f99
cmpl $0x6, -0x28(%rbp)
jl 0x400e
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41be
movl -0x8(%rbp), %edi
movq -0x20(%rbp), %rsi
addq $0x1, %rsi
movl $0x1, %edx
callq 0x3cf0
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x4060
jmp 0x402e
movq 0xbfc3(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x7606(%rip), %rsi      # 0xb645
leaq 0x7543(%rip), %rdx      # 0xb589
leaq 0x7622(%rip), %rcx      # 0xb66f
movb $0x0, %al
callq 0x2150
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41be
movl -0x8(%rbp), %edi
movq -0x20(%rbp), %rsi
addq $0x2, %rsi
movl $0x1, %edx
callq 0x3cf0
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x40b2
jmp 0x4080
movq 0xbf71(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x75eb(%rip), %rsi      # 0xb67c
leaq 0x74f1(%rip), %rdx      # 0xb589
leaq 0x75d0(%rip), %rcx      # 0xb66f
movb $0x0, %al
callq 0x2150
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41be
movl -0x8(%rbp), %edi
movq -0x20(%rbp), %rsi
addq $0x3, %rsi
movq -0x20(%rbp), %rax
movzbl 0x2(%rax), %edx
callq 0x3cf0
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x4107
jmp 0x40d5
movq 0xbf1c(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x75ca(%rip), %rsi      # 0xb6b0
leaq 0x749c(%rip), %rdx      # 0xb589
leaq 0x757b(%rip), %rcx      # 0xb66f
movb $0x0, %al
callq 0x2150
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41be
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %eax
addl $0x3, %eax
movzwl %ax, %esi
callq 0x3e20
movq -0x20(%rbp), %rax
movzbl 0x1(%rax), %eax
movzbl -0x29(%rbp), %ecx
cmpl %ecx, %eax
jne 0x4166
movl $0x0, -0x28(%rbp)
cmpb $0x0, 0xc199(%rip)      # 0x102d0
je 0x415f
movq 0xbeb8(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x7597(%rip), %rsi      # 0xb6e1
leaq 0x7438(%rip), %rdx      # 0xb589
leaq 0x7517(%rip), %rcx      # 0xb66f
movb $0x0, %al
callq 0x2150
jmp 0x4161
jmp 0x3f97
movq -0x20(%rbp), %rax
movzbl 0x1(%rax), %eax
movzbl -0x11(%rbp), %ecx
cmpl %ecx, %eax
je 0x41b5
jmp 0x4178
movq 0xbe79(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movq -0x20(%rbp), %rax
movzbl 0x1(%rax), %r8d
movzbl -0x11(%rbp), %r9d
leaq 0x7578(%rip), %rsi      # 0xb70f
leaq 0x73eb(%rip), %rdx      # 0xb589
leaq 0x74ca(%rip), %rcx      # 0xb66f
movb $0x0, %al
callq 0x2150
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41be
movl -0x24(%rbp), %eax
addl $0x3, %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopw (%rax,%rax)