// File: /workspace/github_c_makefile_star2_O3/seuscq[P]rtk_hciattach/hciattach_rtk.c
// Function: rtk_init_h5
int rtk_init_h5(int fd, struct termios *ti)
{
	unsigned char bytes[READ_DATA_SIZE];
	struct sigaction sa;
	int retlen;
	struct itimerval value;

	/* set even parity */
	ti->c_cflag |= PARENB;
	ti->c_cflag &= ~(PARODD);
	if (tcsetattr(fd, TCSANOW, ti) < 0) {
		RS_ERR("Can't set port settings");
		return -1;
	}

	rtk_hw_cfg.h5_max_retries = H5_MAX_RETRY_COUNT;

	alarm(0);
	memset(&sa, 0, sizeof(sa));
	sa.sa_flags = SA_NOCLDSTOP;
	sa.sa_handler = h5_tsync_sig_alarm;
	sigaction(SIGALRM, &sa, NULL);

	/* h5 sync */
	h5_tsync_sig_alarm(0);
	rtk_hw_cfg.link_estab_state = H5_SYNC;
	while (rtk_hw_cfg.link_estab_state == H5_SYNC) {
		if ((retlen = read_check_rtk(fd, &bytes, READ_DATA_SIZE)) == -1) {
			RS_ERR("H5 Read Sync Response Failed");

			value.it_value.tv_sec = 0;
			value.it_value.tv_usec = 0;
			value.it_interval.tv_sec = 0;
			value.it_interval.tv_usec = 0;
			setitimer(ITIMER_REAL, &value, NULL);

			return -1;
		}
		h5_recv(&rtk_hw_cfg, &bytes, retlen);
	}

	value.it_value.tv_sec = 0;
	value.it_value.tv_usec = 0;
	value.it_interval.tv_sec = 0;
	value.it_interval.tv_usec = 0;
	setitimer(ITIMER_REAL, &value, NULL);

	/* h5 config */
	sa.sa_handler = h5_tconf_sig_alarm;
	sigaction(SIGALRM, &sa, NULL);
	h5_tconf_sig_alarm(0);
	while (rtk_hw_cfg.link_estab_state == H5_CONFIG) {
		if ((retlen = read_check_rtk(fd, &bytes, READ_DATA_SIZE)) == -1) {
			RS_ERR("H5 Read Config Response Failed");
			value.it_value.tv_sec = 0;
			value.it_value.tv_usec = 0;
			value.it_interval.tv_sec = 0;
			value.it_interval.tv_usec = 0;
			setitimer(ITIMER_REAL, &value, NULL);
			return -1;
		}
		h5_recv(&rtk_hw_cfg, &bytes, retlen);
	}

	value.it_value.tv_sec = 0;
	value.it_value.tv_usec = 0;
	value.it_interval.tv_sec = 0;
	value.it_interval.tv_usec = 0;
	setitimer(ITIMER_REAL, &value, NULL);

	rtk_send_pure_ack_down(fd);
	RS_DBG("H5 init finished\n");

	rtk_hw_cfg.rom_version_cmd_state = cmd_not_send;
	rtk_hw_cfg.hci_version_cmd_state = cmd_not_send;
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0xd0, %rsp
movq %rsi, %rdx
movl %edi, %ebx
movl $0xfffffcff, %eax       # imm = 0xFFFFFCFF
andl 0x8(%rsi), %eax
orl $0x100, %eax            # imm = 0x100
movl %eax, 0x8(%rsi)
xorl %esi, %esi
callq 0x21f0
testl %eax, %eax
js 0x68bc
movl $0x28, 0x8f7f(%rip)     # 0xf710
xorl %edi, %edi
callq 0x20f0
leaq -0xd0(%rbp), %r14
movl $0x90, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x20d0
movl $0x1, 0x80(%r14)
leaq 0x126(%rip), %rax       # 0x68e6
movq %rax, -0x8(%r14)
leaq -0xd8(%rbp), %rsi
movl $0xe, %edi
xorl %edx, %edx
callq 0x2060
callq 0x68e6
movl $0x0, 0x8f0e(%rip)      # 0xf6f4
leaq -0x40(%rbp), %r14
movl %ebx, %edi
movq %r14, %rsi
callq 0x6241
cmpl $-0x1, %eax
je 0x68c3
movq %r14, %rdi
movl %eax, %esi
callq 0x6298
cmpl $0x0, 0x8ee6(%rip)      # 0xf6f4
je 0x67ea
xorps %xmm0, %xmm0
leaq -0x30(%rbp), %rsi
movaps %xmm0, 0x10(%rsi)
movaps %xmm0, (%rsi)
xorl %edi, %edi
xorl %edx, %edx
callq 0x21b0
leaq 0x14f(%rip), %rax       # 0x697d
leaq -0xd8(%rbp), %rsi
movq %rax, (%rsi)
movl $0xe, %edi
xorl %edx, %edx
callq 0x2060
callq 0x697d
cmpl $0x1, 0x8ea4(%rip)      # 0xf6f4
jne 0x6878
leaq -0x40(%rbp), %r14
movl %ebx, %edi
movq %r14, %rsi
callq 0x6241
cmpl $-0x1, %eax
je 0x68d5
movq %r14, %rdi
movl %eax, %esi
callq 0x6298
cmpl $0x1, 0x8e7e(%rip)      # 0xf6f4
je 0x6856
leaq -0x30(%rbp), %rsi
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsi)
movaps %xmm0, (%rsi)
xorl %r14d, %r14d
xorl %edi, %edi
xorl %edx, %edx
callq 0x21b0
movl %ebx, %edi
callq 0x6a11
cmpb $0x0, 0x8a40(%rip)      # 0xf2e0
jne 0x68ce
movq $0x0, 0x8eb3(%rip) # 0xf760
movl %r14d, %eax
addq $0xd0, %rsp
popq %rbx
popq %r14
popq %rbp
retq
callq 0x30e0
jmp 0x68de
leaq -0x30(%rbp), %rdi
callq 0x309b
jmp 0x68de
callq 0x3031
jmp 0x68a2
leaq -0x30(%rbp), %rdi
callq 0x3056
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0x68ad