// File: /workspace/github_c_makefile_star2_O0/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
subq $0xf0, %rsp
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq 0x888b(%rip), %rax      # 0xb068
movq %rax, -0x2b(%rbp)
movw 0x8888(%rip), %ax       # 0xb070
movw %ax, -0x23(%rbp)
movq 0x887f(%rip), %rax      # 0xb072
movq %rax, -0x35(%rbp)
movw 0x887c(%rip), %ax       # 0xb07a
movw %ax, -0x2d(%rbp)
movl 0x8bd1(%rip), %eax      # 0xb3d9
movl %eax, -0x39(%rbp)
movl 0x8bcc(%rip), %eax      # 0xb3dd
movl %eax, -0x3d(%rbp)
movl 0x8bc7(%rip), %eax      # 0xb3e1
movl %eax, -0x41(%rbp)
movl 0x8bc2(%rip), %eax      # 0xb3e5
movl %eax, -0x45(%rbp)
movl -0x8(%rbp), %edi
movq -0x18(%rbp), %rsi
movq -0x10(%rbp), %rax
movl 0x18(%rax), %edx
callq 0x23e0
cmpl $0x0, %eax
jge 0x2856
leaq 0x8928(%rip), %rdi      # 0xb16d
callq 0x2220
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2c67
movq -0x18(%rbp), %rax
movl 0x8(%rax), %ecx
orl $0x100, %ecx            # imm = 0x100
movl %ecx, 0x8(%rax)
movq -0x18(%rbp), %rax
movl 0x8(%rax), %ecx
andl $0xfffffdff, %ecx       # imm = 0xFFFFFDFF
movl %ecx, 0x8(%rax)
movl -0x8(%rbp), %edi
movq -0x18(%rbp), %rdx
xorl %esi, %esi
callq 0x21e0
cmpl $0x0, %eax
jge 0x28a1
leaq 0x88f9(%rip), %rdi      # 0xb189
callq 0x2220
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2c67
xorl %edi, %edi
callq 0x20f0
movl -0x8(%rbp), %eax
movl %eax, 0xdd87(%rip)      # 0x10638
leaq -0xe0(%rbp), %rdi
xorl %esi, %esi
movl $0x98, %edx
callq 0x20d0
movl $0x1, -0x58(%rbp)
leaq 0x100e(%rip), %rax      # 0x38e0
movq %rax, -0xe0(%rbp)
movl $0xe, %edi
leaq -0xe0(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x2060
xorl %edi, %edi
callq 0x38e0
jmp 0x28f7
movl -0x8(%rbp), %edi
leaq -0x19(%rbp), %rsi
movl $0x1, %edx
callq 0x3980
cmpl $-0x1, %eax
jne 0x2925
leaq 0x888d(%rip), %rdi      # 0xb1a1
callq 0x2220
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2c67
jmp 0x2927
movzbl -0x19(%rbp), %eax
cmpl $0xc0, %eax
jne 0x28f7
jmp 0x2934
movl -0x8(%rbp), %edi
leaq -0x1d(%rbp), %rsi
movl $0x1, %edx
callq 0x3980
cmpl $-0x1, %eax
jne 0x2962
leaq 0x8850(%rip), %rdi      # 0xb1a1
callq 0x2220
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2c67
jmp 0x2964
movzbl -0x1d(%rbp), %eax
cmpl $0xc0, %eax
je 0x2934
movl -0x8(%rbp), %edi
leaq -0x1d(%rbp), %rsi
addq $0x1, %rsi
movl $0x3, %edx
callq 0x3980
cmpl $-0x1, %eax
jne 0x29a1
leaq 0x8811(%rip), %rdi      # 0xb1a1
callq 0x2220
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2c67
movzbl -0x1d(%rbp), %eax
movzbl -0x1c(%rbp), %ecx
addl %ecx, %eax
movzbl -0x1b(%rbp), %ecx
addl %ecx, %eax
andl $0xff, %eax
movzbl -0x1a(%rbp), %ecx
xorl $-0x1, %ecx
movzbl %cl, %ecx
cmpl %ecx, %eax
je 0x29c9
jmp 0x28f5
movzbl -0x1c(%rbp), %eax
cmpl $0x41, %eax
jne 0x29db
movzbl -0x1b(%rbp), %eax
cmpl $0x0, %eax
je 0x29e0
jmp 0x28f5
movl -0x8(%rbp), %edi
leaq -0x21(%rbp), %rsi
movl $0x4, %edx
callq 0x3980
cmpl $-0x1, %eax
jne 0x2a0e
leaq 0x87a4(%rip), %rdi      # 0xb1a1
callq 0x2220
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2c67
leaq -0x21(%rbp), %rax
leaq -0x39(%rbp), %rcx
movl (%rcx), %ecx
movl (%rax), %eax
subl %ecx, %eax
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x2a4c
movl -0x8(%rbp), %edi
leaq -0x2b(%rbp), %rsi
movl $0xa, %edx
callq 0x2070
cmpq $0x0, %rax
jge 0x2a4a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2c67
jmp 0x2a69
leaq -0x21(%rbp), %rax
leaq -0x3d(%rbp), %rcx
movl (%rcx), %ecx
movl (%rax), %eax
subl %ecx, %eax
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x2a67
jmp 0x2a6e
jmp 0x2a69
jmp 0x28f5
xorl %edi, %edi
callq 0x20f0
leaq 0xfa4(%rip), %rax       # 0x3a20
movq %rax, -0xe0(%rbp)
movl $0xe, %edi
leaq -0xe0(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x2060
movl $0x1, %edi
callq 0x20f0
jmp 0x2aa4
movl -0x8(%rbp), %edi
leaq -0x19(%rbp), %rsi
movl $0x1, %edx
callq 0x3980
cmpl $-0x1, %eax
jne 0x2ad2
leaq 0x86e0(%rip), %rdi      # 0xb1a1
callq 0x2220
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2c67
jmp 0x2ad4
movzbl -0x19(%rbp), %eax
cmpl $0xc0, %eax
jne 0x2aa4
jmp 0x2ae1
movl -0x8(%rbp), %edi
leaq -0x1d(%rbp), %rsi
movl $0x1, %edx
callq 0x3980
cmpl $-0x1, %eax
jne 0x2b0f
leaq 0x86a3(%rip), %rdi      # 0xb1a1
callq 0x2220
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2c67
jmp 0x2b11
movzbl -0x1d(%rbp), %eax
cmpl $0xc0, %eax
je 0x2ae1
movl -0x8(%rbp), %edi
leaq -0x1d(%rbp), %rsi
addq $0x1, %rsi
movl $0x3, %edx
callq 0x3980
cmpl $-0x1, %eax
jne 0x2b4e
leaq 0x8664(%rip), %rdi      # 0xb1a1
callq 0x2220
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2c67
movzbl -0x1d(%rbp), %eax
movzbl -0x1c(%rbp), %ecx
addl %ecx, %eax
movzbl -0x1b(%rbp), %ecx
addl %ecx, %eax
andl $0xff, %eax
movzbl -0x1a(%rbp), %ecx
xorl $-0x1, %ecx
movzbl %cl, %ecx
cmpl %ecx, %eax
je 0x2b76
jmp 0x2aa2
movzbl -0x1c(%rbp), %eax
cmpl $0x41, %eax
jne 0x2b88
movzbl -0x1b(%rbp), %eax
cmpl $0x0, %eax
je 0x2b8d
jmp 0x2aa2
movl -0x8(%rbp), %edi
leaq -0x21(%rbp), %rsi
movl $0x4, %edx
callq 0x3980
cmpl $-0x1, %eax
jne 0x2bbb
leaq 0x85f7(%rip), %rdi      # 0xb1a1
callq 0x2220
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2c67
leaq -0x21(%rbp), %rax
leaq -0x39(%rbp), %rcx
movl (%rcx), %ecx
movl (%rax), %eax
subl %ecx, %eax
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x2bed
movl -0x8(%rbp), %edi
leaq -0x2b(%rbp), %rsi
movl $0xa, %edx
callq 0x2070
movl %eax, -0xe4(%rbp)
jmp 0x2c41
leaq -0x21(%rbp), %rax
leaq -0x41(%rbp), %rcx
movl (%rcx), %ecx
movl (%rax), %eax
subl %ecx, %eax
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x2c1f
movl -0x8(%rbp), %edi
leaq -0x35(%rbp), %rsi
movl $0xa, %edx
callq 0x2070
movl %eax, -0xe4(%rbp)
jmp 0x2c3f
leaq -0x21(%rbp), %rax
leaq -0x45(%rbp), %rcx
movl (%rcx), %ecx
movl (%rax), %eax
subl %ecx, %eax
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x2c3a
jmp 0x2c60
jmp 0x2aa2
jmp 0x2c41
cmpl $0x0, -0xe4(%rbp)
jge 0x2c5b
callq 0x2040
movq %rax, %rcx
xorl %eax, %eax
subl (%rcx), %eax
movl %eax, -0x4(%rbp)
jmp 0x2c67
jmp 0x2aa2
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xf0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)