// File: /workspace/github_c_makefile_star2_O2/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 %r15
pushq %r14
pushq %rbx
subq $0xd8, %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 %r15d, %r15d
xorl %esi, %esi
callq 0x21f0
testl %eax, %eax
js 0x4f80
movl $0x28, 0x78b9(%rip)     # 0xc710
xorl %edi, %edi
callq 0x20f0
leaq -0xe0(%rbp), %r14
movl $0x90, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x20d0
movl $0x1, 0x80(%r14)
leaq 0x191(%rip), %rax       # 0x5017
movq %rax, -0x8(%r14)
pushq $0xe
popq %rdi
leaq -0xe8(%rbp), %rsi
xorl %edx, %edx
callq 0x2060
callq 0x5017
andl $0x0, 0x784d(%rip)      # 0xc6f4
leaq -0x50(%rbp), %r14
testl %r15d, %r15d
jne 0x4ed6
movl %ebx, %edi
movq %r14, %rsi
callq 0x4948
cmpl $-0x1, %eax
je 0x4fa1
movq %r14, %rdi
movl %eax, %esi
callq 0x498d
movl 0x7820(%rip), %r15d     # 0xc6f4
jmp 0x4eab
xorps %xmm0, %xmm0
leaq -0x40(%rbp), %rsi
movaps %xmm0, 0x10(%rsi)
movaps %xmm0, (%rsi)
xorl %edi, %edi
xorl %edx, %edx
callq 0x21b0
leaq 0x20a(%rip), %rax       # 0x50fe
leaq -0xe8(%rbp), %rsi
movq %rax, (%rsi)
pushq $0xe
popq %rdi
xorl %edx, %edx
callq 0x2060
callq 0x50fe
leaq -0x50(%rbp), %r14
cmpl $0x1, 0x77dc(%rip)      # 0xc6f4
jne 0x4f39
movl %ebx, %edi
movq %r14, %rsi
callq 0x4948
cmpl $-0x1, %eax
je 0x4fb4
movq %r14, %rdi
movl %eax, %esi
callq 0x498d
jmp 0x4f11
xorps %xmm0, %xmm0
leaq -0x40(%rbp), %rsi
movaps %xmm0, 0x10(%rsi)
movaps %xmm0, (%rsi)
xorl %r14d, %r14d
xorl %edi, %edi
xorl %edx, %edx
callq 0x21b0
movl %ebx, %edi
callq 0x51e2
cmpb $0x0, 0x737f(%rip)      # 0xc2e0
jne 0x4ff3
andq $0x0, 0x77f1(%rip)      # 0xc760
movl %r14d, %eax
addq $0xd8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq 0x7071(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x3bb6(%rip), %rsi      # 0x8b47
leaq 0x35a1(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
jmp 0x4fea
movq 0x7050(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x3bb8(%rip), %rsi      # 0x8b6a
jmp 0x4fc5
movq 0x703d(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x3bcd(%rip), %rsi      # 0x8b92
leaq 0x356d(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
xorps %xmm0, %xmm0
leaq -0x40(%rbp), %rsi
movaps %xmm0, (%rsi)
movaps %xmm0, 0x10(%rsi)
xorl %edi, %edi
xorl %edx, %edx
callq 0x21b0
pushq $-0x1
popq %r14
jmp 0x4f6f
movq 0x6ffe(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x3bb8(%rip), %rsi      # 0x8bbc
leaq 0x352e(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
jmp 0x4f67