// File: /workspace/github_c_makefile_star2_O2/seuscq[P]rtk_hciattach/hciattach.c
// Function: main
int main(int argc, char *argv[])
{
	struct uart_t *u = NULL;
	int detach, printpid, raw, opt, i, n, ld, err;
	int to = 10;
	int init_speed = 0;
	int send_break = 0;
	pid_t pid;
	struct sigaction sa;
	struct pollfd p;
	sigset_t sigs;
	char dev[PATH_MAX];

	detach = 1;
	printpid = 0;
	raw = 0;

	while ((opt=getopt(argc, argv, "bnpt:s:lr")) != EOF) {
		switch(opt) {
		case 'b':
			send_break = 1;
			break;

		case 'n':
			detach = 0;
			break;

		case 'p':
			printpid = 1;
			break;

		case 't':
			to = atoi(optarg);
			break;

		case 's':
			init_speed = atoi(optarg);
			break;

		case 'l':
			for (i = 0; uart[i].type; i++) {
				printf("%-10s0x%04x,0x%04x\n", uart[i].type,
							uart[i].m_id, uart[i].p_id);
			}
			exit(0);

		case 'r':
			raw = 1;
			break;

		default:
			usage();
			exit(1);
		}
	}

	n = argc - optind;
	if (n < 2) {
		usage();
		exit(1);
	}

	for (n = 0; optind < argc; n++, optind++) {
		char *opt;

		opt = argv[optind];

		switch(n) {
		case 0:
			dev[0] = 0;
			if (!strchr(opt, '/'))
				strcpy(dev, "/dev/");
			strcat(dev, opt);
			break;

		case 1:
			if (strchr(argv[optind], ',')) {
				int m_id, p_id;
				sscanf(argv[optind], "%x,%x", &m_id, &p_id);
				u = get_by_id(m_id, p_id);
			} else {
				u = get_by_type(opt);
			}

			if (!u) {
				fprintf(stderr, "Unknown device type or id\n");
				exit(1);
			}
			
			break;

		case 2:
			u->speed = atoi(argv[optind]);
			break;

		case 3:
			if (!strcmp("flow", argv[optind]))
				u->flags |=  FLOW_CTL;
			else
				u->flags &= ~FLOW_CTL;
			break;

		case 4:
			if (!strcmp("sleep", argv[optind]))
				u->pm = ENABLE_PM;
			else
				u->pm = DISABLE_PM;
			break;

		case 5:
			u->bdaddr = argv[optind];
			break;
		}
	}

	if (!u) {
		fprintf(stderr, "Unknown device type or id\n");
		exit(1);
	}

	/* If user specified a initial speed, use that instead of
	   the hardware's default */
	if (init_speed)
		u->init_speed = init_speed;

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

	/* 10 seconds should be enough for initialization */
	alarm(to);
	bcsp_max_retries = to;

	n = init_uart(dev, u, send_break, raw);
	if (n < 0) {
		perror("Can't initialize device"); 
		exit(1);
	}

	printf("Device setup complete\n");

	alarm(0);

	memset(&sa, 0, sizeof(sa));
	sa.sa_flags   = SA_NOCLDSTOP;
	sa.sa_handler = SIG_IGN;
	sigaction(SIGCHLD, &sa, NULL);
	sigaction(SIGPIPE, &sa, NULL);

	sa.sa_handler = sig_term;
	sigaction(SIGTERM, &sa, NULL);
	sigaction(SIGINT,  &sa, NULL);

	sa.sa_handler = sig_hup;
	sigaction(SIGHUP, &sa, NULL);

	if (detach) {
		if ((pid = fork())) {
			if (printpid)
				printf("%d\n", pid);
			return 0;
		}

		for (i = 0; i < 20; i++)
			if (i != n)
				close(i);
	}

	p.fd = n;
	p.events = POLLERR | POLLHUP;

	sigfillset(&sigs);
	sigdelset(&sigs, SIGCHLD);
	sigdelset(&sigs, SIGPIPE);
	sigdelset(&sigs, SIGTERM);
	sigdelset(&sigs, SIGINT);
	sigdelset(&sigs, SIGHUP);

	while (!__io_canceled) {
		p.revents = 0;
		err = ppoll(&p, 1, NULL, &sigs);
		if (err < 0 && errno == EINTR) {
			printf("Got EINTR.\n");
			continue;
		} if (err)
			break;
	}

	/* Restore TTY line discipline */
	printf("Restore TTY line discipline\n");
	ld = N_TTY;
	if (ioctl(n, TIOCSETD, &ld) < 0) {
		perror("Can't restore line discipline");
		exit(1);
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1148, %rsp           # imm = 0x1148
movq %rsi, -0x38(%rbp)
pushq $0xa
popq %rax
movl %eax, -0x4c(%rbp)
pushq $0x1
popq %r15
movl $0x0, -0x3c(%rbp)
movb $0x1, %al
movl %eax, -0x44(%rbp)
leaq 0x5693(%rip), %rbx      # 0x807f
movl %r15d, -0x40(%rbp)
movl $0x0, -0x48(%rbp)
xorl %r14d, %r14d
movl %edi, -0x2c(%rbp)
movq -0x38(%rbp), %r13
movl -0x2c(%rbp), %r12d
movl %r14d, %ecx
movl %ecx, %r14d
movl %r12d, %edi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x2240
movl %r15d, %ecx
cmpl $0x62, %eax
je 0x2a08
leal -0x6c(%rax), %ecx
cmpl $0x8, %ecx
ja 0x2a79
leaq 0x55d4(%rip), %rdx      # 0x8004
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movl $0x0, -0x40(%rbp)
jmp 0x2a05
movq 0x958f(%rip), %rax      # 0xbfd8
movq (%rax), %rdi
callq 0x2260
movl %eax, -0x48(%rbp)
jmp 0x2a05
movl $0x0, -0x44(%rbp)
jmp 0x2a05
movl %r15d, -0x3c(%rbp)
jmp 0x2a05
movq 0x956c(%rip), %rax      # 0xbfd8
movq (%rax), %rdi
callq 0x2260
movl %eax, -0x4c(%rbp)
jmp 0x2a05
cmpl $-0x1, %eax
jne 0x3045
movl %r12d, %edi
movq 0x9544(%rip), %rax      # 0xbfd0
movl (%rax), %r12d
movl %edi, %eax
subl %r12d, %eax
cmpl $0x2, %eax
jl 0x3045
xorl %r15d, %r15d
xorl %ebx, %ebx
movq -0x38(%rbp), %rcx
cmpl %edi, %r12d
jge 0x2c30
cmpl $0x5, %r15d
ja 0x2c1b
movslq %r12d, %rax
movq (%rcx,%rax,8), %r13
movl %r15d, %eax
leaq 0x555e(%rip), %rdx      # 0x8028
movslq (%rdx,%rax,4), %rax
addq %rdx, %rax
jmpq *%rax
movb $0x0, -0x1170(%rbp)
movq %r13, %rdi
pushq $0x2f
popq %rsi
callq 0x20b0
testq %rax, %rax
jne 0x2afd
movw $0x2f, -0x116c(%rbp)
movl $0x7665642f, -0x1170(%rbp) # imm = 0x7665642F
leaq -0x1170(%rbp), %rdi
movq %r13, %rsi
callq 0x2270
jmp 0x2c14
leaq 0x55b1(%rip), %rdi      # 0x80c9
movq %r13, %rsi
callq 0x2130
testl %eax, %eax
je 0x2bdf
andl $0x0, 0x20(%rbx)
jmp 0x2c14
movq %r13, %rdi
callq 0x2260
movl -0x2c(%rbp), %edi
movq -0x38(%rbp), %rcx
movl %eax, 0x18(%rbx)
jmp 0x2c1b
leaq 0x5575(%rip), %rdi      # 0x80c4
movq %r13, %rsi
callq 0x2130
movl 0x1c(%rbx), %ecx
testl %eax, %eax
je 0x2be8
andl $-0x2, %ecx
jmp 0x2beb
movq %r13, %rdi
pushq $0x2c
popq %rsi
callq 0x20b0
testq %rax, %rax
je 0x2bf0
movq %r13, %rdi
leaq 0x551f(%rip), %rsi      # 0x80a3
leaq -0x170(%rbp), %rdx
leaq -0xd8(%rbp), %rcx
xorl %eax, %eax
callq 0x21a0
movl -0x170(%rbp), %eax
movl -0xd8(%rbp), %ecx
leaq 0x95e4(%rip), %rbx      # 0xc190
movl -0x2c(%rbp), %edi
cmpq $0x0, (%rbx)
je 0x3027
cmpl %eax, 0x8(%rbx)
jne 0x2bc3
cmpl %ecx, 0xc(%rbx)
je 0x2bcf
addq $0x40, %rbx
jmp 0x2baf
movq %r13, 0x28(%rbx)
jmp 0x2c1b
movq 0x93fa(%rip), %rax      # 0xbfd0
movl (%rax), %r12d
movq -0x38(%rbp), %rcx
jmp 0x2c1b
movl $0x1, 0x20(%rbx)
jmp 0x2c14
orl $0x1, %ecx
movl %ecx, 0x1c(%rbx)
jmp 0x2c14
leaq 0x9559(%rip), %rbx      # 0xc150
movq 0x40(%rbx), %rdi
testq %rdi, %rdi
je 0x3027
movq %r13, %rsi
callq 0x2130
addq $0x40, %rbx
testl %eax, %eax
jne 0x2bf7
movq -0x38(%rbp), %rcx
movl -0x2c(%rbp), %edi
incl %r15d
incl %r12d
movq 0x93a8(%rip), %rax      # 0xbfd0
movl %r12d, (%rax)
jmp 0x2aa6
testq %rbx, %rbx
je 0x3027
movl -0x48(%rbp), %eax
testl %eax, %eax
je 0x2c43
movl %eax, 0x14(%rbx)
leaq -0x168(%rbp), %r15
movl $0x90, %edx
movq %r15, %rdi
xorl %esi, %esi
callq 0x20d0
movl $0x1, 0x80(%r15)
leaq 0x454(%rip), %rax       # 0x30bf
movq %rax, -0x8(%r15)
pushq $0xe
popq %rdi
leaq -0x170(%rbp), %rsi
xorl %edx, %edx
callq 0x2060
movl -0x4c(%rbp), %r15d
movl %r15d, %edi
callq 0x20f0
movl %r15d, 0x963d(%rip)     # 0xc2d0
leaq -0x1170(%rbp), %rdi
movl $0x102, %esi            # imm = 0x102
xorl %eax, %eax
callq 0x2200
testl %eax, %eax
js 0x2fc8
movl %eax, %r13d
pushq $0x2
popq %rsi
movl %eax, %edi
callq 0x20a0
leaq -0xd8(%rbp), %rsi
movl %r13d, %edi
callq 0x21e0
testl %eax, %eax
js 0x2fd1
leaq -0xd8(%rbp), %r15
movq %r15, %rdi
callq 0x21c0
movl 0x8(%r15), %eax
movl %eax, %ecx
andl $0x7ffff7ff, %ecx       # imm = 0x7FFFF7FF
orl $0x800, %ecx            # imm = 0x800
orl $0x80000800, %eax       # imm = 0x80000800
testb $0x1, 0x1c(%rbx)
cmovel %ecx, %eax
movl %eax, 0x8(%r15)
movl %r13d, %edi
xorl %esi, %esi
movq %r15, %rdx
callq 0x21f0
testl %eax, %eax
js 0x2fda
movl 0x14(%rbx), %edx
leaq -0xd8(%rbp), %rsi
movl %r13d, %edi
callq 0x23fc
testl %eax, %eax
js 0x2fe3
pushq $0x2
popq %rsi
movl %r13d, %edi
callq 0x20a0
testl %r14d, %r14d
je 0x2d56
movl %r13d, %edi
xorl %esi, %esi
callq 0x2140
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x22f0
movq 0x30(%rbx), %rax
testq %rax, %rax
je 0x2d76
leaq -0xd8(%rbp), %rdx
movl %r13d, %edi
movq %rbx, %rsi
callq *%rax
testl %eax, %eax
js 0x3013
pushq $0x2
popq %rsi
movl %r13d, %edi
callq 0x20a0
movl 0x18(%rbx), %edx
leaq -0xd8(%rbp), %rsi
movl %r13d, %edi
callq 0x23fc
testl %eax, %eax
js 0x2fec
leaq -0x54(%rbp), %rdx
movl $0xf, (%rdx)
movl $0x5423, %esi           # imm = 0x5423
movl %r13d, %edi
xorl %eax, %eax
callq 0x20e0
testl %eax, %eax
js 0x2ff5
testb $0x1, -0x44(%rbp)
jne 0x2ddc
pushq $0x1
popq %rdx
movl $0x400455cb, %esi       # imm = 0x400455CB
movl %r13d, %edi
xorl %eax, %eax
callq 0x20e0
testl %eax, %eax
js 0x3007
movl 0x10(%rbx), %edx
movl $0x400455c8, %esi       # imm = 0x400455C8
movl %r13d, %edi
xorl %eax, %eax
callq 0x20e0
testl %eax, %eax
js 0x2ffe
movq 0x38(%rbx), %rax
testq %rax, %rax
je 0x2e16
leaq -0xd8(%rbp), %rdx
movl %r13d, %edi
movq %rbx, %rsi
callq *%rax
testl %eax, %eax
js 0x3013
leaq 0x5479(%rip), %rdi      # 0x8296
callq 0x2050
xorl %edi, %edi
callq 0x20f0
leaq -0x168(%rbp), %rbx
movl $0x90, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x20d0
movl $0x1, 0x80(%rbx)
movq $0x1, -0x8(%rbx)
pushq $0x11
popq %rdi
leaq -0x170(%rbp), %rbx
movq %rbx, %rsi
xorl %edx, %edx
callq 0x2060
pushq $0xd
popq %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x2060
leaq 0x26e(%rip), %rax       # 0x30e7
movq %rax, (%rbx)
pushq $0xf
popq %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x2060
pushq $0x2
popq %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x2060
leaq 0x279(%rip), %rax       # 0x3116
movq %rax, (%rbx)
pushq $0x1
popq %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x2060
cmpl $0x0, -0x40(%rbp)
je 0x2ef2
callq 0x22d0
testl %eax, %eax
je 0x2edb
cmpl $0x0, -0x3c(%rbp)
je 0x2fb4
leaq 0x6a69(%rip), %rdi      # 0x9936
movl %eax, %esi
xorl %eax, %eax
callq 0x20c0
jmp 0x2fb4
xorl %ebx, %ebx
cmpl $0x14, %ebx
je 0x2ef2
cmpl %ebx, %r13d
je 0x2eee
movl %ebx, %edi
callq 0x2100
incl %ebx
jmp 0x2edd
leaq -0x54(%rbp), %rbx
movl %r13d, (%rbx)
movw $0x18, 0x4(%rbx)
leaq -0xd8(%rbp), %r15
movq %r15, %rdi
callq 0x2170
pushq $0x11
popq %rsi
movq %r15, %rdi
callq 0x2210
pushq $0xd
popq %rsi
movq %r15, %rdi
callq 0x2210
pushq $0xf
popq %rsi
movq %r15, %rdi
callq 0x2210
pushq $0x2
popq %rsi
movq %r15, %rdi
callq 0x2210
pushq $0x1
popq %rsi
movq %r15, %rdi
callq 0x2210
pushq $0x1
popq %r12
leaq 0x5378(%rip), %r14      # 0x82c8
cmpl $0x0, 0x96ed(%rip)      # 0xc644
jne 0x2f8a
andw $0x0, -0x4e(%rbp)
movq %rbx, %rdi
movq %r12, %rsi
xorl %edx, %edx
movq %r15, %rcx
callq 0x2120
testl %eax, %eax
js 0x2f76
je 0x2f50
jmp 0x2f8a
callq 0x2040
cmpl $0x4, (%rax)
jne 0x2f8a
movq %r14, %rdi
callq 0x2050
jmp 0x2f50
leaq 0x531b(%rip), %rdi      # 0x82ac
callq 0x2050
leaq -0x58(%rbp), %rdx
andl $0x0, (%rdx)
movl $0x5423, %esi           # imm = 0x5423
movl %r13d, %edi
xorl %eax, %eax
callq 0x20e0
testl %eax, %eax
js 0x3081
xorl %eax, %eax
addq $0x1148, %rsp           # imm = 0x1148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5228(%rip), %rdi      # 0x81f7
jmp 0x300e
leaq 0x5236(%rip), %rdi      # 0x820e
jmp 0x300e
leaq 0x5140(%rip), %rdi      # 0x8121
jmp 0x300e
leaq 0x523c(%rip), %rdi      # 0x8226
jmp 0x300e
leaq 0x524f(%rip), %rdi      # 0x8242
jmp 0x300e
leaq 0x525a(%rip), %rdi      # 0x8256
jmp 0x300e
leaq 0x5280(%rip), %rdi      # 0x8285
jmp 0x300e
leaq 0x5262(%rip), %rdi      # 0x8270
callq 0x2230
leaq 0x50b5(%rip), %rdi      # 0x80cf
callq 0x2230
pushq $0x1
popq %rdi
callq 0x2290
movq 0x8fca(%rip), %rax      # 0xbff8
movq (%rax), %rcx
leaq 0x5071(%rip), %rdi      # 0x80a9
pushq $0x1a
popq %rsi
pushq $0x1
popq %rdx
callq 0x22a0
jmp 0x301f
callq 0x308a
jmp 0x301f
leaq 0x9149(%rip), %r14      # 0xc19c
leaq 0x502f(%rip), %rbx      # 0x8089
movq -0xc(%r14), %rsi
testq %rsi, %rsi
je 0x307a
movl -0x4(%r14), %edx
movl (%r14), %ecx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x20c0
addq $0x40, %r14
jmp 0x305a
xorl %edi, %edi
callq 0x2290
leaq 0x505f(%rip), %rdi      # 0x80e7
jmp 0x301a