// File: /workspace/github_c_makefile_star2_O1/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, -0x30(%rbp)
movl %edi, %r12d
movl $0xa, -0x44(%rbp)
movl $0x1, -0x3c(%rbp)
movl $0x0, -0x34(%rbp)
movb $0x1, %al
movl %eax, -0x38(%rbp)
leaq 0x49cb(%rip), %r14      # 0x907f
leaq 0x4949(%rip), %r15      # 0x9004
movl $0x0, -0x40(%rbp)
xorl %ebx, %ebx
movq -0x30(%rbp), %r13
movl %ebx, %ecx
movl %ecx, %ebx
movl %r12d, %edi
movq %r13, %rsi
movq %r14, %rdx
callq 0x2240
movl $0x1, %ecx
cmpl $0x62, %eax
je 0x46ca
leal -0x6c(%rax), %ecx
cmpl $0x8, %ecx
ja 0x4738
movslq (%r15,%rcx,4), %rax
addq %r15, %rax
jmpq *%rax
movl $0x0, -0x3c(%rbp)
jmp 0x46c8
movq 0xa8d3(%rip), %rax      # 0xefd8
movq (%rax), %rdi
callq 0x2260
movl %eax, -0x44(%rbp)
jmp 0x46c8
movl $0x0, -0x38(%rbp)
jmp 0x46c8
movl $0x1, -0x34(%rbp)
jmp 0x46c8
movq 0xa8ad(%rip), %rax      # 0xefd8
movq (%rax), %rdi
callq 0x2260
movl %eax, -0x40(%rbp)
jmp 0x46c8
cmpl $-0x1, %eax
jne 0x4d4a
movq 0xa888(%rip), %rdx      # 0xefd0
movl %r12d, %eax
subl (%rdx), %eax
cmpl $0x2, %eax
jl 0x4d4a
movl (%rdx), %eax
cmpl %r12d, %eax
movq -0x30(%rbp), %rcx
jge 0x4930
xorl %r14d, %r14d
xorl %r15d, %r15d
cmpl $0x5, %r15d
ja 0x491c
cltq
movq (%rcx,%rax,8), %r13
movl %r15d, %eax
leaq 0x48a3(%rip), %rcx      # 0x9028
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb $0x0, -0x1170(%rbp)
movq %r13, %rdi
movl $0x2f, %esi
callq 0x20b0
testq %rax, %rax
jne 0x47ba
movw $0x2f, -0x116c(%rbp)
movl $0x7665642f, -0x1170(%rbp) # imm = 0x7665642F
leaq -0x1170(%rbp), %rdi
movq %r13, %rsi
callq 0x2270
jmp 0x48b7
leaq 0x48f4(%rip), %rdi      # 0x90c9
movq %r13, %rsi
callq 0x2130
testl %eax, %eax
je 0x48a6
movl $0x0, 0x20(%r14)
jmp 0x48b7
movq %r13, %rdi
callq 0x2260
movq 0xa7cf(%rip), %rdx      # 0xefd0
movl %eax, 0x18(%r14)
jmp 0x48a0
leaq 0x48b3(%rip), %rdi      # 0x90c4
movq %r13, %rsi
callq 0x2130
movl 0x1c(%r14), %ecx
testl %eax, %eax
je 0x48b0
andl $-0x2, %ecx
jmp 0x48b3
movq %r13, %rdi
movl $0x2c, %esi
callq 0x20b0
testq %rax, %rax
je 0x48c4
movq %r13, %rdi
leaq 0x4856(%rip), %rsi      # 0x90a3
leaq -0x168(%rbp), %rdx
leaq -0xd0(%rbp), %rcx
xorl %eax, %eax
callq 0x21a0
cmpq $0x0, 0xa926(%rip)      # 0xf190
je 0x4905
movl -0x168(%rbp), %eax
movl -0xd0(%rbp), %ecx
leaq 0xa90d(%rip), %r14      # 0xf190
cmpl %eax, 0x8(%r14)
jne 0x488f
cmpl %ecx, 0xc(%r14)
je 0x4908
cmpq $0x0, 0x40(%r14)
leaq 0x40(%r14), %r14
jne 0x4883
jmp 0x4905
movq %r13, 0x28(%r14)
movq -0x30(%rbp), %rcx
jmp 0x491c
movl $0x1, 0x20(%r14)
jmp 0x48b7
orl $0x1, %ecx
movl %ecx, 0x1c(%r14)
movq -0x30(%rbp), %rcx
movq 0xa70e(%rip), %rdx      # 0xefd0
jmp 0x491c
movq 0xa8c5(%rip), %rdi      # 0xf190
testq %rdi, %rdi
je 0x4905
movq %r13, %rsi
callq 0x2130
leaq 0xa8b1(%rip), %r14      # 0xf190
testl %eax, %eax
je 0x4908
leaq 0xa8a6(%rip), %r14      # 0xf190
movq 0x40(%r14), %rdi
testq %rdi, %rdi
je 0x4905
movq %r13, %rsi
callq 0x2130
addq $0x40, %r14
testl %eax, %eax
jne 0x48ea
jmp 0x4908
xorl %r14d, %r14d
testq %r14, %r14
movq -0x30(%rbp), %rcx
movq 0xa6ba(%rip), %rdx      # 0xefd0
je 0x4d98
incl %r15d
movl (%rdx), %eax
incl %eax
movl %eax, (%rdx)
cmpl %r12d, %eax
jl 0x476b
jmp 0x4933
xorl %r14d, %r14d
testq %r14, %r14
je 0x4d9d
movl -0x40(%rbp), %eax
testl %eax, %eax
je 0x4947
movl %eax, 0x14(%r14)
leaq -0x168(%rbp), %r15
movl $0x98, %edx
movq %r15, %rdi
xorl %esi, %esi
callq 0x20d0
movl $0x1, 0x88(%r15)
leaq 0x472(%rip), %rax       # 0x4de1
movq %rax, (%r15)
movl $0xe, %edi
movq %r15, %rsi
xorl %edx, %edx
callq 0x2060
movl -0x44(%rbp), %r15d
movl %r15d, %edi
callq 0x20f0
movl %r15d, 0xa93c(%rip)     # 0xf2d0
leaq -0x1170(%rbp), %rdi
movl $0x102, %esi            # imm = 0x102
xorl %eax, %eax
callq 0x2200
testl %eax, %eax
js 0x4b1c
movl %eax, %r12d
movl %eax, %edi
movl $0x2, %esi
callq 0x20a0
leaq -0xd0(%rbp), %rsi
movl %r12d, %edi
callq 0x21e0
testl %eax, %eax
js 0x4b25
leaq -0xd0(%rbp), %r15
movq %r15, %rdi
callq 0x21c0
movl 0x8(%r15), %eax
movl %eax, %ecx
orl $0x800, %ecx            # imm = 0x800
movl %ecx, 0x8(%r15)
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x80000800, %eax       # imm = 0x80000800
testb $0x1, 0x1c(%r14)
cmovel %ecx, %eax
movl %eax, 0x8(%r15)
movl %r12d, %edi
xorl %esi, %esi
movq %r15, %rdx
callq 0x21f0
testl %eax, %eax
js 0x4b2e
movl 0x14(%r14), %edx
leaq -0xd0(%rbp), %rsi
movl %r12d, %edi
callq 0x412c
testl %eax, %eax
js 0x4b37
movl %r12d, %edi
movl $0x2, %esi
callq 0x20a0
testl %ebx, %ebx
je 0x4a60
movl %r12d, %edi
xorl %esi, %esi
callq 0x2140
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x22f0
movq 0x30(%r14), %rax
testq %rax, %rax
je 0x4a80
leaq -0xd0(%rbp), %rdx
movl %r12d, %edi
movq %r14, %rsi
callq *%rax
testl %eax, %eax
js 0x4b67
movl %r12d, %edi
movl $0x2, %esi
callq 0x20a0
movl 0x18(%r14), %edx
leaq -0xd0(%rbp), %rsi
movl %r12d, %edi
callq 0x412c
testl %eax, %eax
js 0x4b40
leaq -0x4c(%rbp), %rdx
movl $0xf, (%rdx)
movl $0x5423, %esi           # imm = 0x5423
movl %r12d, %edi
xorl %eax, %eax
callq 0x20e0
testl %eax, %eax
js 0x4b49
testb $0x1, -0x38(%rbp)
jne 0x4ae7
movl $0x400455cb, %esi       # imm = 0x400455CB
movl $0x1, %edx
movl %r12d, %edi
xorl %eax, %eax
callq 0x20e0
testl %eax, %eax
js 0x4b5b
movl 0x10(%r14), %edx
movl $0x400455c8, %esi       # imm = 0x400455C8
movl %r12d, %edi
xorl %eax, %eax
callq 0x20e0
testl %eax, %eax
js 0x4b52
movq 0x38(%r14), %rax
testq %rax, %rax
je 0x4b6d
leaq -0xd0(%rbp), %rdx
movl %r12d, %edi
movq %r14, %rsi
callq *%rax
testl %eax, %eax
jns 0x4b6d
jmp 0x4b67
leaq 0x46d4(%rip), %rdi      # 0x91f7
jmp 0x4b62
leaq 0x46e2(%rip), %rdi      # 0x920e
jmp 0x4b62
leaq 0x45ec(%rip), %rdi      # 0x9121
jmp 0x4b62
leaq 0x46e8(%rip), %rdi      # 0x9226
jmp 0x4b62
leaq 0x46fb(%rip), %rdi      # 0x9242
jmp 0x4b62
leaq 0x4706(%rip), %rdi      # 0x9256
jmp 0x4b62
leaq 0x472c(%rip), %rdi      # 0x9285
jmp 0x4b62
leaq 0x470e(%rip), %rdi      # 0x9270
callq 0x2230
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testl %r12d, %r12d
js 0x4da2
leaq 0x4719(%rip), %rdi      # 0x9296
callq 0x2050
xorl %edi, %edi
callq 0x20f0
leaq -0x168(%rbp), %r14
movl $0x98, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x20d0
movl $0x1, 0x88(%r14)
movq $0x1, (%r14)
movl $0x11, %edi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2060
movl $0xd, %edi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2060
leaq 0x239(%rip), %rax       # 0x4e0f
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 0x240(%rip), %rax       # 0x4e3e
movq %rax, (%r14)
movl $0x1, %edi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2060
cmpl $0x0, -0x3c(%rbp)
je 0x4c57
callq 0x22d0
testl %eax, %eax
je 0x4c3e
cmpl $0x0, -0x34(%rbp)
je 0x4d36
leaq 0x5d06(%rip), %rdi      # 0xa936
movl %eax, %esi
xorl %eax, %eax
callq 0x20c0
jmp 0x4d36
xorl %r14d, %r14d
cmpl %r14d, %r12d
je 0x4c4e
movl %r14d, %edi
callq 0x2100
incl %r14d
cmpl $0x14, %r14d
jne 0x4c41
movl %r12d, -0x4c(%rbp)
movw $0x18, -0x48(%rbp)
leaq -0xd0(%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, 0xa98c(%rip)      # 0xf644
jne 0x4d0d
leaq -0x4c(%rbp), %r14
leaq -0xd0(%rbp), %r15
leaq 0x45fc(%rip), %rbx      # 0x92c8
movw $0x0, -0x46(%rbp)
movl $0x1, %esi
movq %r14, %rdi
xorl %edx, %edx
movq %r15, %rcx
callq 0x2120
movl %eax, %r13d
testl %eax, %eax
jns 0x4cff
callq 0x2040
cmpl $0x4, (%rax)
jne 0x4cff
movq %rbx, %rdi
callq 0x2050
jmp 0x4d04
testl %r13d, %r13d
jne 0x4d0d
cmpl $0x0, 0xa939(%rip)      # 0xf644
je 0x4ccc
leaq 0x4598(%rip), %rdi      # 0x92ac
callq 0x2050
leaq -0x50(%rbp), %rdx
movl $0x0, (%rdx)
movl $0x5423, %esi           # imm = 0x5423
movl %r12d, %edi
xorl %eax, %eax
callq 0x20e0
testl %eax, %eax
js 0x4da7
xorl %eax, %eax
addq $0x1148, %rsp           # imm = 0x1148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x4dac
movl $0x1, %edi
callq 0x2290
movq 0xa430(%rip), %rsi      # 0xf190
testq %rsi, %rsi
je 0x4d91
leaq 0xa42c(%rip), %r14      # 0xf198
leaq 0x4316(%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 0x4d73
xorl %edi, %edi
callq 0x2290
callq 0x23bc
callq 0x2414
callq 0x23fc
callq 0x23e4