// File: /workspace/github_c_makefile_star2_O0/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
subq $0x1190, %rsp           # imm = 0x1190
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movl $0xa, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x1, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x82fe(%rip), %rdx      # 0xb0a7
callq 0x2230
movl %eax, -0x28(%rbp)
cmpl $-0x1, %eax
je 0x2ed6
movl -0x28(%rbp), %eax
addl $-0x62, %eax
movl %eax, %ecx
movq %rcx, -0x1188(%rbp)
subl $0x12, %eax
ja 0x2ec2
movq -0x1188(%rbp), %rax
leaq 0x823c(%rip), %rcx      # 0xb01c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x1, -0x44(%rbp)
jmp 0x2ed1
movl $0x0, -0x1c(%rbp)
jmp 0x2ed1
movl $0x1, -0x20(%rbp)
jmp 0x2ed1
movq 0xd1c4(%rip), %rax      # 0xffd8
movq (%rax), %rdi
callq 0x2250
movl %eax, -0x3c(%rbp)
jmp 0x2ed1
movq 0xd1ad(%rip), %rax      # 0xffd8
movq (%rax), %rdi
callq 0x2250
movl %eax, -0x40(%rbp)
jmp 0x2ed1
movl $0x0, -0x2c(%rbp)
movslq -0x2c(%rbp), %rcx
leaq 0xd333(%rip), %rax      # 0x10180
shlq $0x6, %rcx
addq %rcx, %rax
cmpq $0x0, (%rax)
je 0x2eb2
movslq -0x2c(%rbp), %rcx
leaq 0xd31b(%rip), %rax      # 0x10180
shlq $0x6, %rcx
addq %rcx, %rax
movq (%rax), %rsi
movslq -0x2c(%rbp), %rcx
leaq 0xd306(%rip), %rax      # 0x10180
shlq $0x6, %rcx
addq %rcx, %rax
movl 0x8(%rax), %edx
movslq -0x2c(%rbp), %rcx
leaq 0xd2f1(%rip), %rax      # 0x10180
shlq $0x6, %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
leaq 0x8211(%rip), %rdi      # 0xb0b1
movb $0x0, %al
callq 0x20c0
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x2e42
xorl %edi, %edi
callq 0x2280
movl $0x1, -0x24(%rbp)
jmp 0x2ed1
callq 0x3440
movl $0x1, %edi
callq 0x2280
jmp 0x2d9b
movl -0x8(%rbp), %eax
movq 0xd0f0(%rip), %rcx      # 0xffd0
subl (%rcx), %eax
movl %eax, -0x30(%rbp)
cmpl $0x2, -0x30(%rbp)
jge 0x2efa
callq 0x3440
movl $0x1, %edi
callq 0x2280
movl $0x0, -0x30(%rbp)
movq 0xd0c8(%rip), %rax      # 0xffd0
movl (%rax), %eax
cmpl -0x8(%rbp), %eax
jge 0x3127
movq -0x10(%rbp), %rax
movq 0xd0b2(%rip), %rcx      # 0xffd0
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x1178(%rbp)
movl -0x30(%rbp), %eax
movq %rax, -0x1190(%rbp)
subq $0x5, %rax
ja 0x3102
movq -0x1190(%rbp), %rax
leaq 0x80b6(%rip), %rcx      # 0xb004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb $0x0, -0x1170(%rbp)
movq -0x1178(%rbp), %rdi
movl $0x2f, %esi
callq 0x20b0
cmpq $0x0, %rax
jne 0x2f88
leaq -0x1170(%rbp), %rdi
leaq 0x8142(%rip), %rsi      # 0xb0c5
callq 0x2050
leaq -0x1170(%rbp), %rdi
movq -0x1178(%rbp), %rsi
callq 0x2260
jmp 0x3102
movq -0x10(%rbp), %rax
movq 0xd025(%rip), %rcx      # 0xffd0
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rdi
movl $0x2c, %esi
callq 0x20b0
cmpq $0x0, %rax
je 0x3007
movq -0x10(%rbp), %rax
movq 0xd003(%rip), %rcx      # 0xffd0
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x80f0(%rip), %rsi      # 0xb0cb
leaq -0x117c(%rbp), %rdx
leaq -0x1180(%rbp), %rcx
movb $0x0, %al
callq 0x21a0
movl -0x117c(%rbp), %edi
movl -0x1180(%rbp), %esi
callq 0x3480
movq %rax, -0x18(%rbp)
jmp 0x3017
movq -0x1178(%rbp), %rdi
callq 0x3510
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x3040
movq 0xcfd3(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x80a2(%rip), %rsi      # 0xb0d1
movb $0x0, %al
callq 0x2150
movl $0x1, %edi
callq 0x2280
jmp 0x3102
movq -0x10(%rbp), %rax
movq 0xcf80(%rip), %rcx      # 0xffd0
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x2250
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x18(%rax)
jmp 0x3102
movq -0x10(%rbp), %rax
movq 0xcf5b(%rip), %rcx      # 0xffd0
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rsi
leaq 0x8069(%rip), %rdi      # 0xb0ec
callq 0x2130
cmpl $0x0, %eax
jne 0x309c
movq -0x18(%rbp), %rax
movl 0x1c(%rax), %ecx
orl $0x1, %ecx
movl %ecx, 0x1c(%rax)
jmp 0x30a9
movq -0x18(%rbp), %rax
movl 0x1c(%rax), %ecx
andl $-0x2, %ecx
movl %ecx, 0x1c(%rax)
jmp 0x3102
movq -0x10(%rbp), %rax
movq 0xcf1a(%rip), %rcx      # 0xffd0
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rsi
leaq 0x802d(%rip), %rdi      # 0xb0f1
callq 0x2130
cmpl $0x0, %eax
jne 0x30db
movq -0x18(%rbp), %rax
movl $0x1, 0x20(%rax)
jmp 0x30e6
movq -0x18(%rbp), %rax
movl $0x0, 0x20(%rax)
jmp 0x3102
movq -0x10(%rbp), %rax
movq 0xcedd(%rip), %rcx      # 0xffd0
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
jmp 0x3104
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
movq 0xcebc(%rip), %rax      # 0xffd0
movl (%rax), %ecx
addl $0x1, %ecx
movq 0xceb0(%rip), %rax      # 0xffd0
movl %ecx, (%rax)
jmp 0x2f01
cmpq $0x0, -0x18(%rbp)
jne 0x3150
movq 0xcec3(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x7f92(%rip), %rsi      # 0xb0d1
movb $0x0, %al
callq 0x2150
movl $0x1, %edi
callq 0x2280
cmpl $0x0, -0x40(%rbp)
je 0x3160
movl -0x40(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x14(%rax)
leaq -0xe0(%rbp), %rdi
xorl %esi, %esi
movl $0x98, %edx
callq 0x20d0
movl $0x1, -0x58(%rbp)
leaq 0x41f(%rip), %rax       # 0x35a0
movq %rax, -0xe0(%rbp)
movl $0xe, %edi
leaq -0xe0(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x2060
movl -0x3c(%rbp), %edi
callq 0x20f0
movl -0x3c(%rbp), %eax
movl %eax, 0xd112(%rip)      # 0x102c0
leaq -0x1170(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x44(%rbp), %edx
movl -0x24(%rbp), %ecx
callq 0x35d0
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jge 0x31e3
leaq 0x7f23(%rip), %rdi      # 0xb0f7
callq 0x2220
movl $0x1, %edi
callq 0x2280
leaq 0x7f25(%rip), %rdi      # 0xb10f
movb $0x0, %al
callq 0x20c0
xorl %edi, %edi
callq 0x20f0
leaq -0xe0(%rbp), %rdi
xorl %esi, %esi
movl $0x98, %edx
callq 0x20d0
movl $0x1, -0x58(%rbp)
movl $0x1, %eax
movq %rax, -0xe0(%rbp)
movl $0x11, %edi
leaq -0xe0(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x2060
movl $0xd, %edi
leaq -0xe0(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x2060
leaq 0x621(%rip), %rax       # 0x3870
movq %rax, -0xe0(%rbp)
movl $0xf, %edi
leaq -0xe0(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x2060
movl $0x2, %edi
leaq -0xe0(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x2060
leaq 0x639(%rip), %rax       # 0x38c0
movq %rax, -0xe0(%rbp)
movl $0x1, %edi
leaq -0xe0(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x2060
cmpl $0x0, -0x1c(%rbp)
je 0x3305
callq 0x22b0
movl %eax, -0x48(%rbp)
cmpl $0x0, %eax
je 0x32d9
cmpl $0x0, -0x20(%rbp)
je 0x32cd
movl -0x48(%rbp), %esi
leaq 0x9715(%rip), %rdi      # 0xc9db
movb $0x0, %al
callq 0x20c0
movl $0x0, -0x4(%rbp)
jmp 0x3428
movl $0x0, -0x2c(%rbp)
cmpl $0x14, -0x2c(%rbp)
jge 0x3303
movl -0x2c(%rbp), %eax
cmpl -0x30(%rbp), %eax
je 0x32f6
movl -0x2c(%rbp), %edi
callq 0x2100
jmp 0x32f8
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x32e0
jmp 0x3305
movl -0x30(%rbp), %eax
movl %eax, -0xe8(%rbp)
movw $0x18, -0xe4(%rbp)
leaq -0x168(%rbp), %rdi
callq 0x2170
leaq -0x168(%rbp), %rdi
movl $0x11, %esi
callq 0x2200
leaq -0x168(%rbp), %rdi
movl $0xd, %esi
callq 0x2200
leaq -0x168(%rbp), %rdi
movl $0xf, %esi
callq 0x2200
leaq -0x168(%rbp), %rdi
movl $0x2, %esi
callq 0x2200
leaq -0x168(%rbp), %rdi
movl $0x1, %esi
callq 0x2200
movl 0xd2b6(%rip), %eax      # 0x10634
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x338c
jmp 0x33de
movw $0x0, -0xe2(%rbp)
leaq -0xe8(%rbp), %rdi
movl $0x1, %esi
xorl %eax, %eax
movl %eax, %edx
leaq -0x168(%rbp), %rcx
callq 0x2120
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
jge 0x33d4
callq 0x2040
cmpl $0x4, (%rax)
jne 0x33d4
leaq 0x7d5b(%rip), %rdi      # 0xb126
movb $0x0, %al
callq 0x20c0
jmp 0x3378
cmpl $0x0, -0x38(%rbp)
je 0x33dc
jmp 0x33de
jmp 0x3378
leaq 0x7d4d(%rip), %rdi      # 0xb132
movb $0x0, %al
callq 0x20c0
movl $0x0, -0x34(%rbp)
movl -0x30(%rbp), %edi
movl $0x5423, %esi           # imm = 0x5423
leaq -0x34(%rbp), %rdx
movb $0x0, %al
callq 0x20e0
cmpl $0x0, %eax
jge 0x3421
leaq 0x7d3d(%rip), %rdi      # 0xb14f
callq 0x2220
movl $0x1, %edi
callq 0x2280
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x1190, %rsp           # imm = 0x1190
popq %rbp
retq
nopw %cs:(%rax,%rax)