// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40e0, %rsp           # imm = 0x40E0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x134d
leaq 0xcbe(%rip), %rdi       # 0x2004
movb $0x0, %al
callq 0x17d0
movw $0x1, -0x82(%rbp)
leaq 0xcbc(%rip), %rdi       # 0x2019
callq 0x1150
movq %rax, -0x4098(%rbp)
cmpq $0x0, -0x4098(%rbp)
je 0x139d
leaq -0x82(%rbp), %rdi
addq $0x2, %rdi
movq -0x4098(%rbp), %rcx
movl $0x6c, %esi
leaq 0xd9c(%rip), %rdx       # 0x212d
movb $0x0, %al
callq 0x1040
jmp 0x141e
movq $0x0, -0x40a0(%rbp)
movl $0x0, -0x40a4(%rbp)
movl $0x0, -0x40a8(%rbp)
xorl %eax, %eax
movl %eax, %edi
leaq -0x40a0(%rbp), %rsi
leaq -0x40a4(%rbp), %rdx
leaq -0x40a8(%rbp), %rcx
callq 0x11b0
cmpl $0x0, %eax
je 0x1412
leaq -0x82(%rbp), %rdi
addq $0x2, %rdi
movq -0x40a0(%rbp), %rcx
movl -0x40a4(%rbp), %r8d
movl -0x40a8(%rbp), %r9d
movl $0x6c, %esi
leaq 0xc1b(%rip), %rdx       # 0x2026
movb $0x0, %al
callq 0x1040
movq -0x40a0(%rbp), %rdi
callq 0x10c0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xc13(%rip), %rsi       # 0x2040
callq 0x1170
cmpl $0x0, %eax
jne 0x145c
leaq -0x82(%rbp), %rsi
addq $0x2, %rsi
leaq 0xc0b(%rip), %rdi       # 0x2054
movb $0x0, %al
callq 0x1030
movl $0x0, -0x4(%rbp)
jmp 0x1717
movl $0x1, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x1130
movl %eax, -0x14(%rbp)
cmpl $-0x1, %eax
jne 0x1480
leaq 0xbdf(%rip), %rdi       # 0x2058
movb $0x0, %al
callq 0x17d0
movl -0x14(%rbp), %edi
leaq -0x82(%rbp), %rsi
movl $0x6e, %edx
callq 0x1120
cmpl $-0x1, %eax
jne 0x14a7
leaq 0xbd6(%rip), %rdi       # 0x2076
movb $0x0, %al
callq 0x17d0
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
movl $0x0, -0x40ac(%rbp)
movl $0x0, -0x40b0(%rbp)
movl $0x2000, -0x40b4(%rbp)  # imm = 0x2000
movl $0x0, -0x40b8(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x8(%rbp)
movb %al, -0x40d1(%rbp)
jle 0x1502
cmpl $0x0, -0x40b4(%rbp)
setg %al
movb %al, -0x40d1(%rbp)
movb -0x40d1(%rbp), %al
testb $0x1, %al
jne 0x1511
jmp 0x1599
leaq -0x2090(%rbp), %rdi
movslq -0x40b0(%rbp), %rax
addq %rax, %rdi
movslq -0x40b4(%rbp), %rsi
movq -0x10(%rbp), %rax
movq (%rax), %rcx
leaq 0xb61(%rip), %rdx       # 0x2098
xorl %r8d, %r8d
movb $0x0, %al
callq 0x1040
movl %eax, -0x40b8(%rbp)
movl -0x40b8(%rbp), %eax
addl -0x40ac(%rbp), %eax
movl %eax, -0x40ac(%rbp)
movl -0x40b8(%rbp), %eax
addl -0x40b0(%rbp), %eax
movl %eax, -0x40b0(%rbp)
movl -0x40b8(%rbp), %ecx
movl -0x40b4(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x40b4(%rbp)
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
jmp 0x14e4
movl -0x14(%rbp), %edi
leaq -0x2090(%rbp), %rsi
movslq -0x40ac(%rbp), %rdx
xorl %ecx, %ecx
callq 0x1180
cmpq $-0x1, %rax
jne 0x15c5
leaq 0xadf(%rip), %rdi       # 0x209d
movb $0x0, %al
callq 0x17d0
movl $0x0, -0x40bc(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x40d0(%rbp)
movw $0x1, -0x40cc(%rbp)
movw $0x0, -0x40ca(%rbp)
movl $0x1, -0x40c8(%rbp)
movw $0x10, -0x40c4(%rbp)
movw $0x0, -0x40c2(%rbp)
leaq -0x40d0(%rbp), %rdi
movl $0x2, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x1110
cmpl $0x0, %eax
jle 0x1706
movswl -0x40ca(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x16f0
movl -0x14(%rbp), %edi
leaq -0x4090(%rbp), %rsi
movl $0x1fff, %edx           # imm = 0x1FFF
xorl %ecx, %ecx
callq 0x1090
movl %eax, -0x40c0(%rbp)
cmpl $0x0, %eax
jle 0x16ec
movslq -0x40c0(%rbp), %rax
movb $0x0, -0x4090(%rbp,%rax)
movsbl -0x4090(%rbp), %eax
movsbl 0xa3d(%rip), %ecx       # 0x20b7
cmpl %ecx, %eax
jne 0x16bc
movl $0x1, -0x40bc(%rbp)
movq 0x2959(%rip), %rax      # 0x3fe8
movq (%rax), %rdi
leaq -0x4090(%rbp), %rdx
addq $0x1, %rdx
leaq 0xa89(%rip), %rsi       # 0x212d
movb $0x0, %al
callq 0x11f0
movq 0x2936(%rip), %rax      # 0x3fe8
movq (%rax), %rdi
callq 0x1210
jmp 0x16ea
movq 0x2935(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq -0x4090(%rbp), %rdx
leaq 0xa59(%rip), %rsi       # 0x212d
movb $0x0, %al
callq 0x11f0
movq 0x2916(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
callq 0x1210
jmp 0x16ee
jmp 0x1706
jmp 0x16f0
movswl -0x40c2(%rbp), %eax
andl $0x18, %eax
cmpl $0x0, %eax
je 0x1701
jmp 0x1706
jmp 0x1606
movl -0x14(%rbp), %edi
callq 0x1050
movl -0x40bc(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40e0, %rsp           # imm = 0x40E0
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)