// File: /workspace/github_c_makefile_star2_O1/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 0x68eb
movl $0x28, 0x8f4b(%rip)     # 0xf710
xorl %edi, %edi
callq 0x20f0
leaq -0xd8(%rbp), %r14
movl $0x98, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x20d0
movl $0x1, 0x88(%r14)
leaq 0x121(%rip), %rax       # 0x6915
movq %rax, (%r14)
movl $0xe, %edi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2060
callq 0x6915
movl $0x0, 0x8edf(%rip)      # 0xf6f4
leaq -0x40(%rbp), %r14
movl %ebx, %edi
movq %r14, %rsi
callq 0x625a
cmpl $-0x1, %eax
je 0x68f2
movq %r14, %rdi
movl %eax, %esi
callq 0x62b5
cmpl $0x0, 0x8eb7(%rip)      # 0xf6f4
je 0x6819
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       # 0x69ac
leaq -0xd8(%rbp), %rsi
movq %rax, (%rsi)
movl $0xe, %edi
xorl %edx, %edx
callq 0x2060
callq 0x69ac
cmpl $0x1, 0x8e75(%rip)      # 0xf6f4
jne 0x68a7
leaq -0x40(%rbp), %r14
movl %ebx, %edi
movq %r14, %rsi
callq 0x625a
cmpl $-0x1, %eax
je 0x6904
movq %r14, %rdi
movl %eax, %esi
callq 0x62b5
cmpl $0x1, 0x8e4f(%rip)      # 0xf6f4
je 0x6885
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 0x6a40
cmpb $0x0, 0x8a11(%rip)      # 0xf2e0
jne 0x68fd
movq $0x0, 0x8e84(%rip) # 0xf760
movl %r14d, %eax
addq $0xd0, %rsp
popq %rbx
popq %r14
popq %rbp
retq
callq 0x30c8
jmp 0x690d
leaq -0x30(%rbp), %rdi
callq 0x3083
jmp 0x690d
callq 0x3019
jmp 0x68d1
leaq -0x30(%rbp), %rdi
callq 0x303e
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0x68dc