// File: /workspace/github_c_makefile_star2_O1/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, %r14
movl %edi, %ebx
movl 0x18(%rsi), %edx
movq %r14, %rsi
callq 0x412c
testl %eax, %eax
js 0x452f
movl $0xfffffcff, %eax       # imm = 0xFFFFFCFF
andl 0x8(%r14), %eax
orl $0x100, %eax            # imm = 0x100
movl %eax, 0x8(%r14)
movl %ebx, %edi
xorl %esi, %esi
movq %r14, %rdx
callq 0x21f0
testl %eax, %eax
js 0x453b
xorl %edi, %edi
callq 0x20f0
movl %ebx, 0xb228(%rip)      # 0xf648
leaq -0xd0(%rbp), %r14
movl $0x98, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x20d0
movl $0x1, 0x88(%r14)
leaq 0xa07(%rip), %rax       # 0x4e4f
movq %rax, (%r14)
movl $0xe, %edi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2060
callq 0x4e4f
leaq -0x2c(%rbp), %r14
leaq -0x29(%rbp), %r12
leaq -0x2d(%rbp), %r13
leaq -0x31(%rbp), %r15
movl %ebx, %edi
movq %r12, %rsi
movl $0x1, %edx
callq 0x4e97
cmpl $-0x1, %eax
je 0x4651
cmpb $-0x40, -0x29(%rbp)
jne 0x446f
movl %ebx, %edi
movq %r13, %rsi
movl $0x1, %edx
callq 0x4e97
cmpl $-0x1, %eax
je 0x4651
cmpb $-0x40, -0x2d(%rbp)
je 0x448d
movl %ebx, %edi
movq %r14, %rsi
movl $0x3, %edx
callq 0x4e97
cmpl $-0x1, %eax
je 0x4651
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 0x446f
xorb $0x41, %al
orb %cl, %al
jne 0x446f
movl %ebx, %edi
movq %r15, %rsi
movl $0x4, %edx
callq 0x4e97
cmpl $-0x1, %eax
je 0x4651
cmpl $0xededdcda, -0x31(%rbp) # imm = 0xEDEDDCDA
je 0x450e
cmpl $0xeeefafac, -0x31(%rbp) # imm = 0xEEEFAFAC
jne 0x446f
jmp 0x4547
movl $0xa, %edx
movl %ebx, %edi
leaq 0x4b24(%rip), %rsi      # 0x9040
callq 0x2070
testq %rax, %rax
jns 0x446f
jmp 0x465d
leaq 0x4bcf(%rip), %rdi      # 0x9105
jmp 0x4658
leaq 0x4bdf(%rip), %rdi      # 0x9121
jmp 0x4658
xorl %edi, %edi
callq 0x20f0
leaq 0x9ea(%rip), %rax       # 0x4f3f
leaq -0xd0(%rbp), %rsi
movq %rax, (%rsi)
movl $0xe, %edi
xorl %edx, %edx
callq 0x2060
movl $0x1, %edi
callq 0x20f0
leaq -0x29(%rbp), %r12
leaq -0x2d(%rbp), %r13
leaq 0x4ac6(%rip), %r15      # 0x904a
movl %ebx, %edi
movq %r12, %rsi
movl $0x1, %edx
callq 0x4e97
cmpl $-0x1, %eax
je 0x4651
cmpb $-0x40, -0x29(%rbp)
jne 0x4584
movl %ebx, %edi
movq %r13, %rsi
movl $0x1, %edx
callq 0x4e97
cmpl $-0x1, %eax
je 0x4651
cmpb $-0x40, -0x2d(%rbp)
je 0x45a2
movl %ebx, %edi
movq %r14, %rsi
movl $0x3, %edx
callq 0x4e97
cmpl $-0x1, %eax
je 0x4651
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 0x4584
xorb $0x41, %al
orb %cl, %al
jne 0x4584
movl %ebx, %edi
leaq -0x31(%rbp), %rsi
movl $0x4, %edx
callq 0x4e97
cmpl $-0x1, %eax
je 0x4651
cmpl $0xededdcda, -0x31(%rbp) # imm = 0xEDEDDCDA
leaq 0x4a2e(%rip), %rsi      # 0x9040
je 0x462f
cmpl $0xedacefad, -0x31(%rbp) # imm = 0xEDACEFAD
movq %r15, %rsi
je 0x462f
cmpl $0xd0d0adde, -0x31(%rbp) # imm = 0xD0D0ADDE
jne 0x4584
jmp 0x4674
movl $0xa, %edx
movl %ebx, %edi
callq 0x2070
testl %eax, %eax
jns 0x4584
callq 0x2040
movq %rax, %rcx
xorl %eax, %eax
subl (%rcx), %eax
jmp 0x4662
leaq 0x4ae1(%rip), %rdi      # 0x9139
callq 0x2230
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
jmp 0x4662