// File: /workspace/github_c_makefile_star2_O1/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, %ebp
cmpl $0x1, %edi
jg 0x13e5
leaq 0xc26(%rip), %rdi       # 0x2004
xorl %eax, %eax
callq 0x164c
movw $0x1, 0x22(%rsp)
leaq 0xc26(%rip), %rdi       # 0x2019
callq 0x1160
testq %rax, %rax
je 0x141a
leaq 0x24(%rsp), %rdi
leaq 0xd1e(%rip), %rdx       # 0x2127
movl $0x6c, %esi
movq %rax, %rcx
xorl %eax, %eax
callq 0x1030
jmp 0x1481
leaq 0x2090(%rsp), %rsi
movq $0x0, (%rsi)
xorl %eax, %eax
leaq 0x90(%rsp), %rdx
movl %eax, (%rdx)
leaq 0x10(%rsp), %rcx
movl %eax, (%rcx)
xorl %edi, %edi
callq 0x11c0
testl %eax, %eax
je 0x1474
leaq 0x24(%rsp), %rdi
movq 0x2090(%rsp), %rcx
movl 0x90(%rsp), %r8d
movl 0x10(%rsp), %r9d
leaq 0xbbe(%rip), %rdx       # 0x2026
movl $0x6c, %esi
xorl %eax, %eax
callq 0x1030
movq 0x2090(%rsp), %rdi
callq 0x10c0
movq 0x8(%r14), %rdi
leaq 0xbb4(%rip), %rsi       # 0x2040
callq 0x1180
testl %eax, %eax
je 0x162b
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1140
movl %eax, %ebx
cmpl $-0x1, %eax
jne 0x14bf
leaq 0xb9c(%rip), %rdi       # 0x2054
xorl %eax, %eax
callq 0x164c
leaq 0x22(%rsp), %rsi
movl %ebx, %edi
movl $0x6e, %edx
callq 0x1130
cmpl $-0x1, %eax
jne 0x14e3
leaq 0xb96(%rip), %rdi       # 0x2072
xorl %eax, %eax
callq 0x164c
xorl %edx, %edx
cmpl $0x2, %ebp
jl 0x1544
movl %ebx, 0xc(%rsp)
addq $0x8, %r14
movl $0x2000, %r12d          # imm = 0x2000
leaq 0xb95(%rip), %r15       # 0x2094
xorl %ebx, %ebx
xorl %r13d, %r13d
movslq %ebx, %rax
leaq (%rsp,%rax), %rdi
addq $0x2090, %rdi           # imm = 0x2090
movl %r12d, %esi
movq (%r14), %rcx
movq %r15, %rdx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x1030
addl %eax, %r13d
cmpl $0x3, %ebp
jl 0x153d
decl %ebp
addl %eax, %ebx
subl %eax, %r12d
addq $0x8, %r14
testl %r12d, %r12d
jg 0x1504
movslq %r13d, %rdx
movl 0xc(%rsp), %ebx
leaq 0x2090(%rsp), %rsi
xorl %ebp, %ebp
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x1190
cmpq $-0x1, %rax
jne 0x156b
leaq 0xb35(%rip), %rdi       # 0x2099
xorl %eax, %eax
callq 0x164c
leaq 0x10(%rsp), %r14
movl %ebx, (%r14)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x4(%r14)
movl $0x10, 0xc(%r14)
leaq 0x90(%rsp), %r12
movq 0x2a50(%rip), %r13      # 0x3fe8
movq 0x2a59(%rip), %r15      # 0x3ff8
movl $0x2, %esi
movq %r14, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x1110
testl %eax, %eax
jle 0x1622
testb $0x1, 0x16(%rsp)
je 0x15fe
movl $0x1fff, %edx           # imm = 0x1FFF
movl %ebx, %edi
movq %r12, %rsi
xorl %ecx, %ecx
callq 0x1090
testl %eax, %eax
jle 0x1622
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movb $0x0, 0x90(%rsp,%rax)
cmpb $0x7, 0x90(%rsp)
je 0x1607
movq (%r15), %rsi
movq %r12, %rdi
callq 0x1120
movq %r15, %rax
movq (%rax), %rdi
callq 0x1210
testb $0x18, 0x1e(%rsp)
je 0x159f
jmp 0x1622
movq (%r13), %rsi
leaq 0x91(%rsp), %rdi
callq 0x1120
movl $0x1, %ebp
movq %r13, %rax
jmp 0x15f6
movl %ebx, %edi
callq 0x1040
jmp 0x1637
leaq 0x24(%rsp), %rdi
callq 0x1050
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x4098, %rsp           # imm = 0x4098
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop