// File: /workspace/github_c_makefile_star2_O2/baskerville[P]bspwm/src/bspc.c
// Function: main
int main(int argc, char *argv[])
{
	int sock_fd;
	struct sockaddr_un sock_address;
	char msg[BUFSIZ], rsp[BUFSIZ];

	if (argc < 2) {
		err("No arguments given.\n");
	}

	sock_address.sun_family = AF_UNIX;
	char *sp;

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

	if (streq(argv[1], "--print-socket-path")) {
		printf("%s\n", sock_address.sun_path);
		return EXIT_SUCCESS;
	}

	if ((sock_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
		err("Failed to create the socket.\n");
	}

	if (connect(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1) {
		err("Failed to connect to the socket.\n");
	}

	argc--, argv++;
	int msg_len = 0;

	for (int offset = 0, rem = sizeof(msg), n = 0; argc > 0 && rem > 0; offset += n, rem -= n, argc--, argv++) {
		n = snprintf(msg + offset, rem, "%s%c", *argv, 0);
		msg_len += n;
	}

	if (send(sock_fd, msg, msg_len, 0) == -1) {
		err("Failed to send the data.\n");
	}

	int ret = EXIT_SUCCESS, nb;

	struct pollfd fds[] = {
		{sock_fd, POLLIN, 0},
		{STDOUT_FILENO, POLLHUP, 0},
	};

	while (poll(fds, 2, -1) > 0) {
		if (fds[0].revents & POLLIN) {
			if ((nb = recv(sock_fd, rsp, sizeof(rsp)-1, 0)) > 0) {
				rsp[nb] = '\0';
				if (rsp[0] == FAILURE_MESSAGE[0]) {
					ret = EXIT_FAILURE;
					fprintf(stderr, "%s", rsp + 1);
					fflush(stderr);
				} else {
					fprintf(stdout, "%s", rsp);
					fflush(stdout);
				}
			} else {
				break;
			}
		}
		if (fds[1].revents & (POLLERR | POLLHUP)) {
			break;
		}
	}

	close(sock_fd);
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4098, %rsp           # imm = 0x4098
movq %rsi, %r14
movl %edi, %r15d
cmpl $0x1, %edi
jg 0x1346
leaq 0xcc5(%rip), %rdi       # 0x2004
xorl %eax, %eax
callq 0x1640
movw $0x1, 0x22(%rsp)
leaq 0xcc5(%rip), %rdi       # 0x2019
callq 0x1160
testq %rax, %rax
je 0x1379
leaq 0x24(%rsp), %rdi
leaq 0xdbd(%rip), %rdx       # 0x2127
pushq $0x6c
popq %rsi
movq %rax, %rcx
xorl %eax, %eax
callq 0x1030
jmp 0x13db
leaq 0x2090(%rsp), %rsi
andq $0x0, (%rsi)
leaq 0x90(%rsp), %rdx
andl $0x0, (%rdx)
leaq 0x10(%rsp), %rcx
andl $0x0, (%rcx)
xorl %edi, %edi
callq 0x11c0
testl %eax, %eax
je 0x13ce
leaq 0x24(%rsp), %rdi
movq 0x2090(%rsp), %rcx
movl 0x90(%rsp), %r8d
movl 0x10(%rsp), %r9d
leaq 0xc62(%rip), %rdx       # 0x2026
pushq $0x6c
popq %rsi
xorl %eax, %eax
callq 0x1030
movq 0x2090(%rsp), %rdi
callq 0x10c0
movq 0x8(%r14), %rdi
leaq 0xc5a(%rip), %rsi       # 0x2040
callq 0x1180
testl %eax, %eax
je 0x158a
pushq $0x1
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x1140
movl %eax, %ebx
cmpl $-0x1, %eax
jne 0x1414
leaq 0xc47(%rip), %rdi       # 0x2054
xorl %eax, %eax
callq 0x1640
leaq 0x22(%rsp), %rsi
pushq $0x6e
popq %rdx
movl %ebx, 0xc(%rsp)
movl %ebx, %edi
callq 0x1130
cmpl $-0x1, %eax
jne 0x143a
leaq 0xc3f(%rip), %rdi       # 0x2072
xorl %eax, %eax
callq 0x1640
addq $0x8, %r14
movl $0x2000, %ebp           # imm = 0x2000
leaq 0xc4a(%rip), %r12       # 0x2094
xorl %r13d, %r13d
xorl %ebx, %ebx
cmpl $0x2, %r15d
jl 0x1489
testl %ebp, %ebp
jle 0x1489
decl %r15d
movslq %ebx, %rbx
leaq (%rsp,%rbx), %rdi
addq $0x2090, %rdi           # imm = 0x2090
movl %ebp, %esi
movq (%r14), %rcx
movq %r12, %rdx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x1030
addl %eax, %r13d
addl %eax, %ebx
subl %eax, %ebp
addq $0x8, %r14
jmp 0x144f
movslq %r13d, %rdx
leaq 0x2090(%rsp), %rsi
movl $0x0, 0x8(%rsp)
movl 0xc(%rsp), %ebx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x1190
cmpq $-0x1, %rax
jne 0x14bd
leaq 0xbe3(%rip), %rdi       # 0x2099
xorl %eax, %eax
callq 0x1640
leaq 0x10(%rsp), %r15
movl %ebx, (%r15)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x4(%r15)
movl $0x10, 0xc(%r15)
pushq $0x2
popq %r13
pushq $-0x1
popq %r12
leaq 0x90(%rsp), %rbp
movq 0x2b06(%rip), %r14      # 0x3ff8
movq %r15, %rdi
movq %r13, %rsi
movl %r12d, %edx
callq 0x1110
testl %eax, %eax
jle 0x157d
testb $0x1, 0x16(%rsp)
je 0x154d
movl $0x1fff, %edx           # imm = 0x1FFF
movl %ebx, %edi
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0x1090
testl %eax, %eax
jle 0x157d
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movb $0x0, 0x90(%rsp,%rax)
cmpb $0x7, 0x90(%rsp)
je 0x1556
movq (%r14), %rsi
movq %rbp, %rdi
callq 0x1120
movq %r14, %rax
movq (%rax), %rdi
callq 0x1210
testb $0x18, 0x1e(%rsp)
je 0x14f2
jmp 0x157d
movq 0x2a8b(%rip), %rax      # 0x3fe8
movq (%rax), %rsi
leaq 0x91(%rsp), %rdi
callq 0x1120
movq 0x2a74(%rip), %rax      # 0x3fe8
pushq $0x1
popq %rcx
movl %ecx, 0x8(%rsp)
jmp 0x1545
movl %ebx, %edi
callq 0x1040
movl 0x8(%rsp), %eax
jmp 0x1596
leaq 0x24(%rsp), %rdi
callq 0x1050
xorl %eax, %eax
addq $0x4098, %rsp           # imm = 0x4098
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq