// File: /workspace/github_c_makefile_star2_O2/baskerville[P]bspwm/src/bspwm.c
// Function: main
int main(int argc, char *argv[])
{
	fd_set descriptors;
	char socket_path[MAXLEN];
	char state_path[MAXLEN] = {0};
	int run_level = 0;
	config_path[0] = '\0';
	int sock_fd = -1, cli_fd, dpy_fd, max_fd, n;
	struct sockaddr_un sock_address;
	char msg[BUFSIZ] = {0};
	xcb_generic_event_t *event;
	char *end;
	int opt;

	while ((opt = getopt(argc, argv, "hvc:s:o:")) != -1) {
		switch (opt) {
			case 'h':
				printf(WM_NAME " [-h|-v|-c CONFIG_PATH]\n");
				exit(EXIT_SUCCESS);
				break;
			case 'v':
				printf("%s\n", VERSION);
				exit(EXIT_SUCCESS);
				break;
			case 'c':
				snprintf(config_path, sizeof(config_path), "%s", optarg);
				break;
			case 's':
				run_level |= 1;
				snprintf(state_path, sizeof(state_path), "%s", optarg);
				break;
			case 'o':
				run_level |= 2;
				sock_fd = strtol(optarg, &end, 0);
				if (*end != '\0') {
					sock_fd = -1;
				}
				break;
		}
	}

	if (config_path[0] == '\0') {
		char *config_home = getenv(CONFIG_HOME_ENV);
		if (config_home != NULL) {
			snprintf(config_path, sizeof(config_path), "%s/%s/%s", config_home, WM_NAME, CONFIG_NAME);
		} else {
			snprintf(config_path, sizeof(config_path), "%s/%s/%s/%s", getenv("HOME"), ".config", WM_NAME, CONFIG_NAME);
		}
	}

	dpy = xcb_connect(NULL, &default_screen);

	if (!check_connection(dpy)) {
		exit(EXIT_FAILURE);
	}

	load_settings();
	setup();

	if (state_path[0] != '\0') {
		restore_state(state_path);
		unlink(state_path);
	}

	dpy_fd = xcb_get_file_descriptor(dpy);

	if (sock_fd == -1) {
		char *sp = getenv(SOCKET_ENV_VAR);
		if (sp != NULL) {
			snprintf(socket_path, sizeof(socket_path), "%s", sp);
		} else {
			char *host = NULL;
			int dn = 0, sn = 0;
			if (xcb_parse_display(NULL, &host, &dn, &sn) != 0) {
				snprintf(socket_path, sizeof(socket_path), SOCKET_PATH_TPL, host, dn, sn);
			}
			free(host);
		}

		sock_address.sun_family = AF_UNIX;
		if (snprintf(sock_address.sun_path, sizeof(sock_address.sun_path), "%s", socket_path) < 0) {
			err("Couldn't write the socket path.\n");
		}

		sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);

		if (sock_fd == -1) {
			err("Couldn't create the socket.\n");
		}

		unlink(socket_path);

		if (bind(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1) {
			err("Couldn't bind a name to the socket.\n");
		}

		if (listen(sock_fd, SOMAXCONN) == -1) {
			err("Couldn't listen to the socket.\n");
		}
	}

	fcntl(sock_fd, F_SETFD, FD_CLOEXEC | fcntl(sock_fd, F_GETFD));

	signal(SIGINT, sig_handler);
	signal(SIGHUP, sig_handler);
	signal(SIGTERM, sig_handler);
	signal(SIGCHLD, sig_handler);
	signal(SIGPIPE, SIG_IGN);
	run_config(run_level);
	running = true;

	while (running) {

		xcb_flush(dpy);

		FD_ZERO(&descriptors);
		FD_SET(sock_fd, &descriptors);
		FD_SET(dpy_fd, &descriptors);
		max_fd = MAX(sock_fd, dpy_fd);

		for (pending_rule_t *pr = pending_rule_head; pr != NULL; pr = pr->next) {
			FD_SET(pr->fd, &descriptors);
			if (pr->fd > max_fd) {
				max_fd = pr->fd;
			}
		}

		if (select(max_fd + 1, &descriptors, NULL, NULL, NULL) > 0) {

			pending_rule_t *pr = pending_rule_head;
			while (pr != NULL) {
				pending_rule_t *next = pr->next;
				if (FD_ISSET(pr->fd, &descriptors)) {
					if (manage_window(pr->win, pr->csq, pr->fd)) {
						for (event_queue_t *eq = pr->event_head; eq != NULL; eq = eq->next) {
							handle_event(&eq->event);
						}
					}
					remove_pending_rule(pr);
				}
				pr = next;
			}

			if (FD_ISSET(sock_fd, &descriptors)) {
				cli_fd = accept(sock_fd, NULL, 0);
				if (cli_fd > 0 && (n = recv(cli_fd, msg, sizeof(msg)-1, 0)) > 0) {
					msg[n] = '\0';
					FILE *rsp = fdopen(cli_fd, "w");
					if (rsp != NULL) {
						handle_message(msg, n, rsp);
					} else {
						warn("Can't open the client socket as file.\n");
						close(cli_fd);
					}
				}
			}

			if (FD_ISSET(dpy_fd, &descriptors)) {
				xcb_aux_sync(dpy);
				while ((event = xcb_poll_for_event(dpy)) != NULL) {
					handle_event(event);
					free(event);
				}
			}

		}

		if (!check_connection(dpy)) {
			running = false;
		}

		prune_dead_subscribers();
	}

	if (restart) {
		char *host = NULL;
		int dn = 0, sn = 0;
		if (xcb_parse_display(NULL, &host, &dn, &sn) != 0) {
			snprintf(state_path, sizeof(state_path), STATE_PATH_TPL, host, dn, sn);
		}
		free(host);
		FILE *f = fopen(state_path, "w");
		query_state(f);
		fclose(f);
	}

	cleanup();
	ungrab_buttons();
	xcb_ewmh_connection_wipe(ewmh);
	xcb_destroy_window(dpy, meta_window);
	xcb_destroy_window(dpy, motion_recorder.id);
	free(ewmh);
	xcb_flush(dpy);
	xcb_disconnect(dpy);

	if (restart) {
		fcntl(sock_fd, F_SETFD, ~FD_CLOEXEC & fcntl(sock_fd, F_GETFD));

		int rargc;
		for (rargc = 0; rargc < argc; rargc++) {
			if (streq("-s", argv[rargc])) {
				break;
			}
		}

		int len = rargc + 5;
		char **rargv = malloc(len * sizeof(char *));

		for (int i = 0; i < rargc; i++) {
			rargv[i] = argv[i];
		}

		char sock_fd_arg[SMALEN];
		snprintf(sock_fd_arg, sizeof(sock_fd_arg), "%i", sock_fd);

		rargv[rargc] = "-s";
		rargv[rargc + 1] = state_path;
		rargv[rargc + 2] = "-o";
		rargv[rargc + 3] = sock_fd_arg;
		rargv[rargc + 4] = 0;

		execvp(*rargv, rargv);

		exit_status = 1;
		free(rargv);
	}

	close(sock_fd);
	unlink(socket_path);

	return exit_status;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2368, %rsp           # imm = 0x2368
movq %rsi, 0x18(%rsp)
movq %rdi, 0x30(%rsp)
leaq 0x160(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
xorl %esi, %esi
callq 0x40f0
movb $0x0, 0x2395e(%rip)     # 0x285c0
leaq 0x360(%rsp), %rdi
movl $0x2000, %edx           # imm = 0x2000
xorl %esi, %esi
callq 0x40f0
pushq $-0x1
popq %rbp
leaq 0x19384(%rip), %rbx     # 0x1e004
movq 0x23359(%rip), %r14     # 0x27fe0
leaq 0x23932(%rip), %r12     # 0x285c0
leaq 0x193b2(%rip), %r13     # 0x1e047
xorl %r15d, %r15d
movl %r15d, 0x8(%rsp)
orl $0x1, %r15d
movq 0x30(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %rbx, %rdx
callq 0x4570
cmpl $0x63, %eax
je 0x4cde
cmpl $-0x1, %eax
je 0x4d45
cmpl $0x6f, %eax
je 0x4d16
cmpl $0x73, %eax
je 0x4cf5
cmpl $0x76, %eax
je 0x5439
cmpl $0x68, %eax
jne 0x4ca1
jmp 0x5442
movq (%r14), %rcx
movl $0x100, %esi            # imm = 0x100
movq %r12, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x4110
jmp 0x4ca1
movq (%r14), %rcx
movl $0x100, %esi            # imm = 0x100
leaq 0x160(%rsp), %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x4110
movl %r15d, 0x8(%rsp)
jmp 0x4ca1
movl 0x8(%rsp), %r15d
orl $0x2, %r15d
movq (%r14), %rdi
leaq 0x48(%rsp), %rsi
xorl %edx, %edx
callq 0x4680
movq 0x48(%rsp), %rcx
xorl %edx, %edx
cmpb (%rcx), %dl
movl $0x0, %ebp
sbbl %ebp, %ebp
orl %eax, %ebp
jmp 0x4c98
cmpb $0x0, 0x23874(%rip)     # 0x285c0
movl %ebp, %r14d
jne 0x4dc2
leaq 0x192c8(%rip), %rdi     # 0x1e020
callq 0x4790
testq %rax, %rax
je 0x4d80
leaq 0x23857(%rip), %rdi     # 0x285c0
leaq 0x192d1(%rip), %rdx     # 0x1e041
leaq 0x192b9(%rip), %r8      # 0x1e030
leaq 0x192b8(%rip), %r9      # 0x1e036
jmp 0x4db3
leaq 0x192a4(%rip), %rdi     # 0x1e02b
callq 0x4790
leaq 0x192a3(%rip), %rcx     # 0x1e036
movq %rcx, (%rsp)
leaq 0x23822(%rip), %rdi     # 0x285c0
leaq 0x19299(%rip), %rdx     # 0x1e03e
leaq 0x1929e(%rip), %r8      # 0x1e04a
leaq 0x1927d(%rip), %r9      # 0x1e030
movl $0x100, %esi            # imm = 0x100
movq %rax, %rcx
xorl %eax, %eax
callq 0x4110
leaq 0x238f7(%rip), %rsi     # 0x286c0
xorl %edi, %edi
callq 0x43b0
movq %rax, 0x238f1(%rip)     # 0x286c8
movq %rax, %rdi
callq 0x545d
testb %al, %al
je 0x5455
callq 0x683a
callq 0x54a5
cmpb $0x0, 0x160(%rsp)
je 0x4e13
leaq 0x160(%rsp), %rbx
movq %rbx, %rdi
callq 0x19a44
movq %rbx, %rdi
callq 0x42e0
movq 0x238ae(%rip), %rdi     # 0x286c8
callq 0x4380
movl %eax, %ebp
cmpl $-0x1, %r14d
jne 0x4f68
leaq 0x19220(%rip), %rdi     # 0x1e052
callq 0x4790
testq %rax, %rax
je 0x4e5c
leaq 0x19204(%rip), %rdx     # 0x1e047
leaq 0x260(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
movq %rax, %rcx
xorl %eax, %eax
callq 0x4110
jmp 0x4eba
leaq 0x70(%rsp), %rsi
andq $0x0, (%rsi)
leaq 0xf0(%rsp), %rdx
andl $0x0, (%rdx)
leaq 0x50(%rsp), %rcx
andl $0x0, (%rcx)
xorl %edi, %edi
callq 0x4910
testl %eax, %eax
je 0x4eb0
movq 0x70(%rsp), %rcx
movl 0xf0(%rsp), %r8d
movl 0x50(%rsp), %r9d
leaq 0x191c3(%rip), %rdx     # 0x1e05f
leaq 0x260(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
xorl %eax, %eax
callq 0x4110
movq 0x70(%rsp), %rdi
callq 0x4460
leaq 0xf2(%rsp), %rdi
movw $0x1, -0x2(%rdi)
leaq 0x19178(%rip), %rdx     # 0x1e047
pushq $0x6c
popq %rsi
leaq 0x260(%rsp), %rcx
xorl %eax, %eax
callq 0x4110
testl %eax, %eax
jns 0x4ef3
leaq 0x1918d(%rip), %rdi     # 0x1e079
xorl %eax, %eax
callq 0x5be8
pushq $0x1
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x4760
movl %eax, %r14d
cmpl $-0x1, %eax
jne 0x4f15
leaq 0x1918c(%rip), %rdi     # 0x1e09a
xorl %eax, %eax
callq 0x5be8
leaq 0x260(%rsp), %rdi
callq 0x42e0
leaq 0xf0(%rsp), %rsi
pushq $0x6e
popq %rdx
movl %r14d, %edi
callq 0x49c0
cmpl $-0x1, %eax
jne 0x4f48
leaq 0x19176(%rip), %rdi     # 0x1e0b7
xorl %eax, %eax
callq 0x5be8
movl %r14d, %edi
movl $0x1000, %esi           # imm = 0x1000
callq 0x44d0
cmpl $-0x1, %eax
jne 0x4f68
leaq 0x1917b(%rip), %rdi     # 0x1e0dc
xorl %eax, %eax
callq 0x5be8
pushq $0x1
popq %rbx
movl %r14d, %edi
movl %ebx, %esi
xorl %eax, %eax
callq 0x4aa0
orl $0x1, %eax
pushq $0x2
popq %r15
movl %r14d, %edi
movl %r15d, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x4aa0
leaq 0x985(%rip), %r13       # 0x5919
movl %r15d, %edi
movq %r13, %rsi
callq 0x4070
movl %ebx, %edi
movq %r13, %rsi
callq 0x4070
pushq $0xf
popq %rdi
movq %r13, %rsi
callq 0x4070
pushq $0x11
popq %rdi
movq %r13, %rsi
callq 0x4070
pushq $0xd
popq %rdi
pushq $0x1
popq %rbx
movq %rbx, %rsi
callq 0x4070
movl 0x8(%rsp), %edi
callq 0x67c4
movb $0x1, 0x236f3(%rip)     # 0x286d0
pushq $0x40
popq %r12
movl %r14d, %eax
cltd
idivl %r12d
movslq %eax, %rcx
movq %rcx, 0x8(%rsp)
shll $0x6, %eax
movl %r14d, %ecx
subl %eax, %ecx
movq %rbx, %rax
shlq %cl, %rax
movq %rax, 0x28(%rsp)
movl %ebp, %eax
cltd
idivl %r12d
movslq %eax, %rdx
shll $0x6, %eax
movl %ebp, %ecx
subl %eax, %ecx
movq %rbx, %rsi
shlq %cl, %rsi
cmpl %ebp, %r14d
movl %r14d, 0x14(%rsp)
cmovgl %r14d, %ebp
movq %rdx, %r14
movb $0x1, %al
movl %ebp, %r15d
movq %rsi, %rbp
movq %rdx, 0x40(%rsp)
movq %rsi, 0x38(%rsp)
testb $0x1, %al
je 0x5224
movq 0x2367f(%rip), %rdi     # 0x286c8
callq 0x4660
xorl %eax, %eax
cmpq $0x10, %rax
je 0x5061
andq $0x0, 0x70(%rsp,%rax,8)
incq %rax
jmp 0x5050
movq 0x8(%rsp), %rax
movq 0x28(%rsp), %rcx
orq %rcx, 0x70(%rsp,%rax,8)
orq %rbp, 0x70(%rsp,%r14,8)
leaq 0x2365c(%rip), %r8      # 0x286d8
movl %r15d, %edi
movq (%r8), %r8
testq %r8, %r8
je 0x50b0
movl (%r8), %esi
movl %esi, %eax
cltd
idivl %r12d
movslq %eax, %rdx
shll $0x6, %eax
movl %esi, %ecx
subl %eax, %ecx
movq %rbx, %rax
shlq %cl, %rax
orq %rax, 0x70(%rsp,%rdx,8)
cmpl %edi, %esi
cmovgl %esi, %edi
addq $0x28, %r8
jmp 0x507f
incl %edi
leaq 0x70(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x41d0
testl %eax, %eax
jle 0x51fd
movq 0x23606(%rip), %r14     # 0x286d8
testq %r14, %r14
je 0x512b
movq %r14, %rbp
movq 0x28(%r14), %r14
movl (%rbp), %ecx
movl %ecx, %eax
cltd
idivl %r12d
cltq
movq 0x70(%rsp,%rax,8), %rax
btq %rdx, %rax
jae 0x50d2
movl 0x4(%rbp), %edi
movq 0x8(%rbp), %rsi
movl %ecx, %edx
callq 0xebb8
testb %al, %al
je 0x5121
leaq 0x10(%rbp), %r13
movq (%r13), %r13
testq %r13, %r13
je 0x5121
movq %r13, %rdi
callq 0xd0d4
addq $0x30, %r13
jmp 0x510a
movq %rbp, %rdi
callq 0x1b8a1
jmp 0x50d2
movq 0x8(%rsp), %rax
movq 0x28(%rsp), %rcx
testq %rcx, 0x70(%rsp,%rax,8)
movq 0x40(%rsp), %r14
je 0x51bf
movl 0x14(%rsp), %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4a80
testl %eax, %eax
jle 0x51bf
movl %eax, %ebp
movl $0x1fff, %edx           # imm = 0x1FFF
movl %eax, %edi
leaq 0x360(%rsp), %rsi
xorl %ecx, %ecx
callq 0x42c0
movq %rax, %r13
testl %r13d, %r13d
jle 0x51bf
movl %r13d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movb $0x0, 0x360(%rsp,%rax)
movl %ebp, %edi
leaq 0x1ab0a(%rip), %rsi     # 0x1fc95
callq 0x4550
testq %rax, %rax
je 0x51aa
leaq 0x360(%rsp), %rdi
movl %r13d, %esi
movq %rax, %rdx
callq 0x10784
jmp 0x51bf
leaq 0x18f4b(%rip), %rdi     # 0x1e0fc
xorl %eax, %eax
callq 0x5b50
movl %ebp, %edi
callq 0x4150
movq 0x38(%rsp), %rbp
testq %rbp, 0x70(%rsp,%r14,8)
je 0x51fd
movq 0x234f6(%rip), %rdi     # 0x286c8
callq 0x40e0
movq 0x234ea(%rip), %rdi     # 0x286c8
callq 0x46c0
testq %rax, %rax
je 0x51fd
movq %rax, %r13
movq %rax, %rdi
callq 0xd0d4
movq %r13, %rdi
callq 0x4460
jmp 0x51d7
movq 0x234c4(%rip), %rdi     # 0x286c8
callq 0x545d
testb %al, %al
jne 0x5214
movb $0x0, 0x234bc(%rip)     # 0x286d0
callq 0x1d1b8
movb 0x234b1(%rip), %al      # 0x286d0
jmp 0x503a
cmpb $0x1, 0x234b5(%rip)     # 0x286e0
movl 0x14(%rsp), %ebp
jne 0x52b0
leaq 0x50(%rsp), %rsi
andq $0x0, (%rsi)
leaq 0x24(%rsp), %rdx
andl $0x0, (%rdx)
leaq 0x20(%rsp), %rcx
andl $0x0, (%rcx)
xorl %edi, %edi
callq 0x4910
testl %eax, %eax
je 0x527f
movq 0x50(%rsp), %rcx
movl 0x24(%rsp), %r8d
movl 0x20(%rsp), %r9d
leaq 0x18eb8(%rip), %rdx     # 0x1e123
leaq 0x160(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
xorl %eax, %eax
callq 0x4110
movq 0x50(%rsp), %rdi
callq 0x4460
leaq 0x1aa05(%rip), %rsi     # 0x1fc95
leaq 0x160(%rsp), %rdi
callq 0x42a0
movq %rax, %rbx
movq %rax, %rdi
callq 0x16fa4
movq %rbx, %rdi
callq 0x48c0
callq 0x5961
callq 0xe4ee
leaq 0x23b6f(%rip), %rbx     # 0x28e30
movq (%rbx), %rdi
callq 0x59ba
movq 0x233f8(%rip), %rdi     # 0x286c8
movl 0x2340e(%rip), %esi     # 0x286e4
callq 0x4130
movq 0x233e6(%rip), %rdi     # 0x286c8
movl 0x23400(%rip), %esi     # 0x286e8
callq 0x4130
movq (%rbx), %rdi
callq 0x4460
movq 0x233cc(%rip), %rdi     # 0x286c8
callq 0x4660
movq 0x233c0(%rip), %rdi     # 0x286c8
callq 0x4980
cmpb $0x1, 0x233cc(%rip)     # 0x286e0
jne 0x540d
xorl %r15d, %r15d
pushq $0x1
popq %rsi
movl %ebp, %edi
xorl %eax, %eax
callq 0x4aa0
andl $-0x2, %eax
pushq $0x2
popq %rsi
movl %ebp, %edi
movl %eax, %edx
xorl %eax, %eax
callq 0x4aa0
movq 0x30(%rsp), %rdx
testl %edx, %edx
cmovlel %r15d, %edx
cmpq %r15, %rdx
je 0x5373
movq 0x18(%rsp), %rax
movq (%rax,%r15,8), %rcx
movzbl (%rcx), %eax
subl $0x2d, %eax
jne 0x5368
movzbl 0x1(%rcx), %eax
subl $0x73, %eax
jne 0x5368
movzbl 0x2(%rcx), %eax
negl %eax
testl %eax, %eax
je 0x5378
incq %r15
jmp 0x5345
movq %rdx, %r15
jmp 0x537b
movl %r15d, %edx
movl %edx, %eax
leaq 0x28(,%rax,8), %rdi
callq 0x4290
movq %rax, %rbx
xorl %eax, %eax
cmpq %rax, %r15
je 0x53a6
movq 0x18(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
movq %rcx, (%rbx,%rax,8)
incq %rax
jmp 0x538f
leaq 0x1ad72(%rip), %rdx     # 0x2011f
leaq 0x50(%rsp), %r14
pushq $0x20
popq %rsi
movq %r14, %rdi
movl %ebp, %ecx
xorl %eax, %eax
callq 0x4110
leaq 0x1998f(%rip), %rax     # 0x1ed57
movq %rax, (%rbx,%r15,8)
leaq 0x160(%rsp), %rax
movq %rax, 0x8(%rbx,%r15,8)
leaq 0x18d5c(%rip), %rax     # 0x1e13c
movq %rax, 0x10(%rbx,%r15,8)
movq %r14, 0x18(%rbx,%r15,8)
andq $0x0, 0x20(%rbx,%r15,8)
movq (%rbx), %rdi
movq %rbx, %rsi
callq 0x41e0
movl $0x1, 0x232eb(%rip)     # 0x286f0
movq %rbx, %rdi
callq 0x4460
movl %ebp, %edi
callq 0x4150
leaq 0x260(%rsp), %rdi
callq 0x42e0
movl 0x232c9(%rip), %eax     # 0x286f0
addq $0x2368, %rsp           # imm = 0x2368
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x18bcd(%rip), %rdi     # 0x1e00d
jmp 0x5449
leaq 0x18e92(%rip), %rdi     # 0x1e2db
callq 0x4190
xorl %edi, %edi
callq 0x41f0
pushq $0x1
popq %rdi
callq 0x41f0