// File: /workspace/github_c_makefile_star2_O3/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)
movl %edi, -0x2c(%rbp)
movl $0xa, -0x44(%rbp)
movl $0x1, -0x3c(%rbp)
movl $0x0, -0x48(%rbp)
movb $0x1, %al
movl %eax, -0x40(%rbp)
leaq 0x49d6(%rip), %rbx      # 0x907f
leaq 0x4954(%rip), %r14      # 0x9004
xorl %eax, %eax
xorl %r13d, %r13d
movq -0x38(%rbp), %r15
movl -0x2c(%rbp), %r12d
movl %eax, -0x30(%rbp)
movl %r13d, %ecx
movl %ecx, %r13d
movl %r12d, %edi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x2240
movl $0x1, %ecx
cmpl $0x62, %eax
je 0x46c3
leal -0x6c(%rax), %ecx
cmpl $0x8, %ecx
ja 0x473b
movslq (%r14,%rcx,4), %rax
addq %r14, %rax
jmpq *%rax
movl $0x0, -0x3c(%rbp)
movl -0x30(%rbp), %eax
jmp 0x46bd
movq 0xa8d6(%rip), %rax      # 0xefd8
movq (%rax), %rdi
callq 0x2260
movl %eax, -0x44(%rbp)
movl -0x30(%rbp), %eax
jmp 0x46bd
movl $0x0, -0x40(%rbp)
movl -0x30(%rbp), %eax
jmp 0x46bd
movl $0x1, -0x48(%rbp)
movl -0x30(%rbp), %eax
jmp 0x46bd
movq 0xa8a7(%rip), %rax      # 0xefd8
movq (%rax), %rdi
callq 0x2260
jmp 0x46bd
cmpl $-0x1, %eax
jne 0x4d53
movq 0xa885(%rip), %rax      # 0xefd0
movl (%rax), %r12d
movl -0x2c(%rbp), %edx
movl %edx, %eax
subl %r12d, %eax
cmpl $0x2, %eax
jl 0x4d53
cmpl %r12d, %edx
jle 0x4d99
xorl %r14d, %r14d
xorl %r15d, %r15d
movq -0x38(%rbp), %rcx
cmpl $0x5, %r15d
ja 0x48d5
movslq %r12d, %rax
movq (%rcx,%rax,8), %rbx
movl %r15d, %eax
leaq 0x489b(%rip), %rdx      # 0x9028
movslq (%rdx,%rax,4), %rax
addq %rdx, %rax
jmpq *%rax
movb $0x0, -0x1170(%rbp)
movq %rbx, %rdi
movl $0x2f, %esi
callq 0x20b0
testq %rax, %rax
jne 0x47c2
movw $0x2f, -0x116c(%rbp)
movl $0x7665642f, -0x1170(%rbp) # imm = 0x7665642F
leaq -0x1170(%rbp), %rdi
movq %rbx, %rsi
callq 0x2270
jmp 0x48ce
leaq 0x48ec(%rip), %rdi      # 0x90c9
movq %rbx, %rsi
callq 0x2130
testl %eax, %eax
je 0x48bd
movl $0x0, 0x20(%r14)
jmp 0x48ce
movq %rbx, %rdi
callq 0x2260
movq -0x38(%rbp), %rcx
movl %eax, 0x18(%r14)
jmp 0x48d2
leaq 0x48ae(%rip), %rdi      # 0x90c4
movq %rbx, %rsi
callq 0x2130
movl 0x1c(%r14), %ecx
testl %eax, %eax
je 0x48c7
andl $-0x2, %ecx
jmp 0x48ca
movq %rbx, %rdi
movl $0x2c, %esi
callq 0x20b0
testq %rax, %rax
je 0x48f0
movq %rbx, %rdi
leaq 0x4851(%rip), %rsi      # 0x90a3
leaq -0x170(%rbp), %rdx
leaq -0xd8(%rbp), %rcx
xorl %eax, %eax
callq 0x21a0
cmpq $0x0, 0xa921(%rip)      # 0xf190
movl -0x2c(%rbp), %edx
je 0x4d4e
movl -0x170(%rbp), %eax
movl -0xd8(%rbp), %ecx
leaq 0xa905(%rip), %r14      # 0xf190
cmpl %eax, 0x8(%r14)
jne 0x4897
cmpl %ecx, 0xc(%r14)
je 0x48ad
cmpq $0x0, 0x40(%r14)
leaq 0x40(%r14), %r14
jne 0x488b
jmp 0x4d4e
movq %rbx, 0x28(%r14)
jmp 0x48d2
movq 0xa71c(%rip), %rax      # 0xefd0
movl (%rax), %r12d
movq -0x38(%rbp), %rcx
jmp 0x48d5
movl $0x1, 0x20(%r14)
jmp 0x48ce
orl $0x1, %ecx
movl %ecx, 0x1c(%r14)
movq -0x38(%rbp), %rcx
movl -0x2c(%rbp), %edx
incl %r15d
incl %r12d
movq 0xa6ee(%rip), %rax      # 0xefd0
movl %r12d, (%rax)
cmpl %edx, %r12d
jl 0x4772
jmp 0x492e
movq 0xa899(%rip), %rdi      # 0xf190
testq %rdi, %rdi
je 0x4d4e
movq %rbx, %rsi
callq 0x2130
leaq 0xa881(%rip), %r14      # 0xf190
testl %eax, %eax
je 0x48ce
movq 0x40(%r14), %rdi
testq %rdi, %rdi
je 0x4d4e
movq %rbx, %rsi
callq 0x2130
addq $0x40, %r14
jmp 0x490f
testq %r14, %r14
je 0x4d99
movl -0x30(%rbp), %eax
testl %eax, %eax
je 0x4942
movl %eax, 0x14(%r14)
leaq -0x168(%rbp), %rbx
movl $0x90, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x20d0
movl $0x1, 0x80(%rbx)
leaq 0x46f(%rip), %rax       # 0x4dd8
movq %rax, -0x8(%rbx)
leaq -0x170(%rbp), %rsi
movl $0xe, %edi
xorl %edx, %edx
callq 0x2060
movl -0x44(%rbp), %ebx
movl %ebx, %edi
callq 0x20f0
movl %ebx, 0xa940(%rip)      # 0xf2d0
leaq -0x1170(%rbp), %rdi
movl $0x102, %esi            # imm = 0x102
xorl %eax, %eax
callq 0x2200
testl %eax, %eax
movl -0x48(%rbp), %r12d
js 0x4ced
movl %eax, %ebx
movl %eax, %edi
movl $0x2, %esi
callq 0x20a0
leaq -0xd8(%rbp), %rsi
movl %ebx, %edi
callq 0x21e0
testl %eax, %eax
js 0x4cf6
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(%r14)
cmovel %ecx, %eax
movl %eax, 0x8(%r15)
movl %ebx, %edi
xorl %esi, %esi
movq %r15, %rdx
callq 0x21f0
testl %eax, %eax
js 0x4cff
movl 0x14(%r14), %edx
leaq -0xd8(%rbp), %rsi
movl %ebx, %edi
callq 0x411c
testl %eax, %eax
js 0x4d08
movl %ebx, %edi
movl $0x2, %esi
callq 0x20a0
testl %r13d, %r13d
je 0x4a57
movl %ebx, %edi
xorl %esi, %esi
callq 0x2140
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x22f0
movq 0x30(%r14), %rax
testq %rax, %rax
je 0x4a76
leaq -0xd8(%rbp), %rdx
movl %ebx, %edi
movq %r14, %rsi
callq *%rax
testl %eax, %eax
js 0x4d38
movl %ebx, %edi
movl $0x2, %esi
callq 0x20a0
movl 0x18(%r14), %edx
leaq -0xd8(%rbp), %rsi
movl %ebx, %edi
callq 0x411c
testl %eax, %eax
js 0x4d11
leaq -0x50(%rbp), %rdx
movl $0xf, (%rdx)
movl $0x5423, %esi           # imm = 0x5423
movl %ebx, %edi
xorl %eax, %eax
callq 0x20e0
testl %eax, %eax
js 0x4d1a
testb $0x1, -0x40(%rbp)
jne 0x4add
movl $0x400455cb, %esi       # imm = 0x400455CB
movl $0x1, %edx
movl %ebx, %edi
xorl %eax, %eax
callq 0x20e0
testl %eax, %eax
js 0x4d2c
movl 0x10(%r14), %edx
movl $0x400455c8, %esi       # imm = 0x400455C8
movl %ebx, %edi
xorl %eax, %eax
callq 0x20e0
testl %eax, %eax
js 0x4d23
movq 0x38(%r14), %rax
testq %rax, %rax
je 0x4b16
leaq -0xd8(%rbp), %rdx
movl %ebx, %edi
movq %r14, %rsi
callq *%rax
testl %eax, %eax
js 0x4d38
leaq 0x4779(%rip), %rdi      # 0x9296
callq 0x2050
xorl %edi, %edi
callq 0x20f0
leaq -0x168(%rbp), %r14
movl $0x90, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x20d0
movl $0x1, 0x80(%r14)
movq $0x1, -0x8(%r14)
leaq -0x170(%rbp), %r14
movl $0x11, %edi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2060
movl $0xd, %edi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2060
leaq 0x288(%rip), %rax       # 0x4e06
movq %rax, (%r14)
movl $0xf, %edi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2060
movl $0x2, %edi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2060
leaq 0x28f(%rip), %rax       # 0x4e35
movq %rax, (%r14)
movl $0x1, %edi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2060
cmpl $0x0, -0x3c(%rbp)
je 0x4bfe
callq 0x22d0
testl %eax, %eax
je 0x4be5
testl %r12d, %r12d
je 0x4cd9
leaq 0x5d5f(%rip), %rdi      # 0xa936
movl %eax, %esi
xorl %eax, %eax
callq 0x20c0
jmp 0x4cd9
xorl %r14d, %r14d
cmpl %r14d, %ebx
je 0x4bf5
movl %r14d, %edi
callq 0x2100
incl %r14d
cmpl $0x14, %r14d
jne 0x4be8
movl %ebx, -0x50(%rbp)
movw $0x18, -0x4c(%rbp)
leaq -0xd8(%rbp), %r14
movq %r14, %rdi
callq 0x2170
movq %r14, %rdi
movl $0x11, %esi
callq 0x2210
movq %r14, %rdi
movl $0xd, %esi
callq 0x2210
movq %r14, %rdi
movl $0xf, %esi
callq 0x2210
movq %r14, %rdi
movl $0x2, %esi
callq 0x2210
movq %r14, %rdi
movl $0x1, %esi
callq 0x2210
cmpl $0x0, 0xa9e6(%rip)      # 0xf644
jne 0x4cad
leaq -0x50(%rbp), %r14
leaq -0xd8(%rbp), %r15
leaq 0x4656(%rip), %r12      # 0x92c8
movw $0x0, -0x4a(%rbp)
movl $0x1, %esi
movq %r14, %rdi
xorl %edx, %edx
movq %r15, %rcx
callq 0x2120
testl %eax, %eax
js 0x4c92
je 0x4ca4
jmp 0x4cad
callq 0x2040
cmpl $0x4, (%rax)
jne 0x4cad
movq %r12, %rdi
callq 0x2050
cmpl $0x0, 0xa999(%rip)      # 0xf644
je 0x4c72
leaq 0x45f8(%rip), %rdi      # 0x92ac
callq 0x2050
leaq -0x54(%rbp), %rdx
movl $0x0, (%rdx)
movl $0x5423, %esi           # imm = 0x5423
movl %ebx, %edi
xorl %eax, %eax
callq 0x20e0
testl %eax, %eax
js 0x4d9e
xorl %eax, %eax
addq $0x1148, %rsp           # imm = 0x1148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4503(%rip), %rdi      # 0x91f7
jmp 0x4d33
leaq 0x4511(%rip), %rdi      # 0x920e
jmp 0x4d33
leaq 0x441b(%rip), %rdi      # 0x9121
jmp 0x4d33
leaq 0x4517(%rip), %rdi      # 0x9226
jmp 0x4d33
leaq 0x452a(%rip), %rdi      # 0x9242
jmp 0x4d33
leaq 0x4535(%rip), %rdi      # 0x9256
jmp 0x4d33
leaq 0x455b(%rip), %rdi      # 0x9285
jmp 0x4d33
leaq 0x453d(%rip), %rdi      # 0x9270
callq 0x2230
leaq 0x4390(%rip), %rdi      # 0x90cf
callq 0x2230
movl $0x1, %edi
callq 0x2290
callq 0x23bc
callq 0x4da3
jmp 0x4d44
movq 0xa42f(%rip), %rsi      # 0xf190
testq %rsi, %rsi
je 0x4d92
leaq 0xa42b(%rip), %r14      # 0xf198
leaq 0x4315(%rip), %rbx      # 0x9089
movl (%r14), %edx
movl 0x4(%r14), %ecx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x20c0
movq 0x38(%r14), %rsi
addq $0x40, %r14
testq %rsi, %rsi
jne 0x4d74
xorl %edi, %edi
callq 0x2290
callq 0x23fc
callq 0x23e4