// File: /workspace/github_c_makefile_star2_O2/seuscq[P]rtk_hciattach/hciattach.c
// Function: bcsp
static int bcsp(int fd, struct uart_t *u, struct termios *ti)
{
	unsigned char byte, bcsph[4], bcspp[4],
		bcsp_sync_resp_pkt[10] = {0xc0,0x00,0x41,0x00,0xbe,0xac,0xaf,0xef,0xee,0xc0},
		bcsp_conf_resp_pkt[10] = {0xc0,0x00,0x41,0x00,0xbe,0xde,0xad,0xd0,0xd0,0xc0},
		bcspsync[4]     = {0xda, 0xdc, 0xed, 0xed},
		bcspsyncresp[4] = {0xac,0xaf,0xef,0xee},
		bcspconf[4]     = {0xad,0xef,0xac,0xed},
		bcspconfresp[4] = {0xde,0xad,0xd0,0xd0};
	struct sigaction sa;
	int len;

	if (set_speed(fd, ti, u->speed) < 0) {
		perror("Can't set default baud rate");
		return -1;
	}

	ti->c_cflag |= PARENB;
	ti->c_cflag &= ~(PARODD);

	if (tcsetattr(fd, TCSANOW, ti) < 0) {
		perror("Can't set port settings");
		return -1;
	}

	alarm(0);

	serial_fd = fd;
	memset(&sa, 0, sizeof(sa));
	sa.sa_flags = SA_NOCLDSTOP;
	sa.sa_handler = bcsp_tshy_sig_alarm;
	sigaction(SIGALRM, &sa, NULL);

	/* State = shy */

	bcsp_tshy_sig_alarm(0);
	while (1) {
		do {
			if (read_check(fd, &byte, 1) == -1){
				perror("Failed to read");
				return -1;
			}
		} while (byte != 0xC0);

		do {
			if ( read_check(fd, &bcsph[0], 1) == -1){
				perror("Failed to read");
				return -1;
			}
		} while (bcsph[0] == 0xC0);

		if ( read_check(fd, &bcsph[1], 3) == -1){
			perror("Failed to read");
			return -1;
		}

		if (((bcsph[0] + bcsph[1] + bcsph[2]) & 0xFF) != (unsigned char)~bcsph[3])
			continue;
		if (bcsph[1] != 0x41 || bcsph[2] != 0x00)
			continue;

		if (read_check(fd, &bcspp, 4) == -1){
			perror("Failed to read");
			return -1;
		}

		if (!memcmp(bcspp, bcspsync, 4)) {
			if (write(fd, &bcsp_sync_resp_pkt,10) < 0)
				return -1;
		} else if (!memcmp(bcspp, bcspsyncresp, 4))
			break;
	}

	/* State = curious */

	alarm(0);
	sa.sa_handler = bcsp_tconf_sig_alarm;
	sigaction(SIGALRM, &sa, NULL);
	alarm(1);

	while (1) {
		do {
			if (read_check(fd, &byte, 1) == -1){
				perror("Failed to read");
				return -1;
			}
		} while (byte != 0xC0);

		do {
			if (read_check(fd, &bcsph[0], 1) == -1){
				perror("Failed to read");
				return -1;
			}
		} while (bcsph[0] == 0xC0);

		if (read_check(fd, &bcsph[1], 3) == -1){
			perror("Failed to read");
			return -1;
		}

		if (((bcsph[0] + bcsph[1] + bcsph[2]) & 0xFF) != (unsigned char)~bcsph[3])
			continue;

		if (bcsph[1] != 0x41 || bcsph[2] != 0x00)
			continue;

		if (read_check(fd, &bcspp, 4) == -1){
			perror("Failed to read");
			return -1;
		}

		if (!memcmp(bcspp, bcspsync, 4))
			len = write(fd, &bcsp_sync_resp_pkt, 10);
		else if (!memcmp(bcspp, bcspconf, 4))
			len = write(fd, &bcsp_conf_resp_pkt, 10);
		else if (!memcmp(bcspp, bcspconfresp,  4))
			break;
		else
			continue;

		if (len < 0)
			return -errno;
	}

	/* State = garrulous */

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdx, %rbx
movl %edi, %r14d
movl 0x18(%rsi), %edx
movq %rbx, %rsi
callq 0x23fc
pushq $-0x1
popq %r15
testl %eax, %eax
js 0x27c2
movl $0xfffffcff, %eax       # imm = 0xFFFFFCFF
andl 0x8(%rbx), %eax
orl $0x100, %eax            # imm = 0x100
movl %eax, 0x8(%rbx)
movl %r14d, %edi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x21f0
testl %eax, %eax
js 0x27cb
xorl %edi, %edi
callq 0x20f0
movl %r14d, 0x9f94(%rip)     # 0xc648
leaq -0xc8(%rbp), %rbx
movl $0x90, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x20d0
movl $0x1, 0x80(%rbx)
leaq 0xa4c(%rip), %rax       # 0x3127
movq %rax, -0x8(%rbx)
pushq $0xe
popq %rdi
leaq -0xd0(%rbp), %rsi
xorl %edx, %edx
callq 0x2060
callq 0x3127
leaq -0x29(%rbp), %r12
pushq $0x1
popq %r13
leaq -0x2d(%rbp), %rbx
pushq $0x3
popq %r15
movl %r14d, %edi
movq %r12, %rsi
movl %r13d, %edx
callq 0x3198
cmpl $-0x1, %eax
je 0x28e1
cmpb $-0x40, -0x29(%rbp)
jne 0x2705
movl %r14d, %edi
movq %rbx, %rsi
movl %r13d, %edx
callq 0x3198
cmpl $-0x1, %eax
je 0x28e1
cmpb $-0x40, -0x2d(%rbp)
je 0x2722
movl %r14d, %edi
leaq -0x2c(%rbp), %rsi
movl %r15d, %edx
callq 0x3198
cmpl $-0x1, %eax
je 0x28e1
movb -0x2c(%rbp), %al
movb -0x2b(%rbp), %cl
movb -0x2d(%rbp), %dl
addb %al, %dl
addb %cl, %dl
xorb -0x2a(%rbp), %dl
cmpb $-0x1, %dl
jne 0x2705
xorb $0x41, %al
orb %cl, %al
jne 0x2705
movl %r14d, %edi
leaq -0x31(%rbp), %rsi
pushq $0x4
popq %rdx
callq 0x3198
cmpl $-0x1, %eax
je 0x28e1
cmpl $0xededdcda, -0x31(%rbp) # imm = 0xEDEDDCDA
je 0x27a2
cmpl $0xeeefafac, -0x31(%rbp) # imm = 0xEEEFAFAC
jne 0x2705
jmp 0x27dc
movl %r14d, %edi
leaq 0x5894(%rip), %rsi      # 0x8040
pushq $0xa
popq %rdx
callq 0x2070
testq %rax, %rax
jns 0x2705
jmp 0x28ed
leaq 0x593c(%rip), %rdi      # 0x8105
jmp 0x27d2
leaq 0x594f(%rip), %rdi      # 0x8121
callq 0x2230
jmp 0x28f1
xorl %edi, %edi
callq 0x20f0
leaq 0xa1e(%rip), %rax       # 0x3208
leaq -0xd0(%rbp), %rsi
movq %rax, (%rsi)
pushq $0xe
popq %rdi
xorl %edx, %edx
callq 0x2060
pushq $0x1
popq %r13
movl %r13d, %edi
callq 0x20f0
leaq -0x29(%rbp), %r15
leaq -0x2d(%rbp), %r12
leaq 0x5831(%rip), %rbx      # 0x804a
movl %r14d, %edi
movq %r15, %rsi
movl %r13d, %edx
callq 0x3198
cmpl $-0x1, %eax
je 0x28e1
cmpb $-0x40, -0x29(%rbp)
jne 0x2819
movl %r14d, %edi
movq %r12, %rsi
movl %r13d, %edx
callq 0x3198
cmpl $-0x1, %eax
je 0x28e1
cmpb $-0x40, -0x2d(%rbp)
je 0x2836
movl %r14d, %edi
leaq -0x2c(%rbp), %rsi
pushq $0x3
popq %rdx
callq 0x3198
cmpl $-0x1, %eax
je 0x28e1
movb -0x2c(%rbp), %al
movb -0x2b(%rbp), %cl
movb -0x2d(%rbp), %dl
addb %al, %dl
addb %cl, %dl
xorb -0x2a(%rbp), %dl
cmpb $-0x1, %dl
jne 0x2819
xorb $0x41, %al
orb %cl, %al
jne 0x2819
movl %r14d, %edi
leaq -0x31(%rbp), %rsi
pushq $0x4
popq %rdx
callq 0x3198
cmpl $-0x1, %eax
je 0x28e1
cmpl $0xededdcda, -0x31(%rbp) # imm = 0xEDEDDCDA
leaq 0x579c(%rip), %rsi      # 0x8040
je 0x28c1
cmpl $0xedacefad, -0x31(%rbp) # imm = 0xEDACEFAD
movq %rbx, %rsi
je 0x28c1
cmpl $0xd0d0adde, -0x31(%rbp) # imm = 0xD0D0ADDE
jne 0x2819
jmp 0x2906
movl %r14d, %edi
pushq $0xa
popq %rdx
callq 0x2070
testl %eax, %eax
jns 0x2819
callq 0x2040
xorl %r15d, %r15d
subl (%rax), %r15d
jmp 0x28f1
leaq 0x5851(%rip), %rdi      # 0x8139
callq 0x2230
pushq $-0x1
popq %r15
movl %r15d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r15d, %r15d
jmp 0x28f1