// File: /workspace/github_c_makefile_star2_O3/containers[P]conmon/src/ctrl.c
// Function: terminal_accept_cb
gboolean terminal_accept_cb(int fd, G_GNUC_UNUSED GIOCondition condition, G_GNUC_UNUSED gpointer user_data)
{

	ndebugf("about to accept from console_socket_fd: %d", fd);

	int connfd;
	do {
		connfd = accept4(fd, NULL, NULL, SOCK_CLOEXEC);
	} while (connfd < 0 && errno == EINTR);
	if (connfd < 0) {
		pexit("Failed to accept console-socket connection");
	}

	/* Not accepting anything else. */
	const char *csname = user_data;
	if (unlink(csname) < 0)
		nwarnf("failed to unlink %s", csname);

	close(fd);

	/* We exit if this fails. */
	ndebugf("about to recvfd from connfd: %d", connfd);
	struct file_t console = recvfd(connfd);

	if (console.fd < 0) {
		pexit("Failed to receive console file descriptor");
	}

	ndebugf("console = {.name = '%s'; .fd = %d}", console.name, console.fd);
	free(console.name);

	/* We change the terminal settings to match kube settings */
	struct termios tset;
	if (tcgetattr(console.fd, &tset) == -1) {
		nwarn("Failed to get console terminal settings");
		goto exit;
	}

	tset.c_oflag |= ONLCR;

	if (tcsetattr(console.fd, TCSANOW, &tset) == -1)
		nwarn("Failed to set console terminal settings");

exit:
	/* We only have a single fd for both pipes, so we just treat it as
	 * stdout. stderr is ignored. */
	mainfd_stdin = console.fd;
	mainfd_stdout = dup(console.fd);
	if (mainfd_stdout < 0)
		pexit("Failed to dup console file descriptor");

	/* Now that we have a fd to the tty, make sure we handle any pending data
	 * that was already buffered. */
	schedule_main_stdin_write();

	/* now that we've set mainfd_stdout, we can register the ctrl_winsz_cb
	 * if we didn't set it here, we'd risk attempting to run ioctl on
	 * a negative fd, and fail to resize the window */
	g_unix_fd_add(winsz_fd_r, G_IO_IN, ctrl_winsz_cb, NULL);

	/* Clean up everything */
	close(connfd);

	/* Since we've gotten our console from the runtime, we no longer need to
	   be listening on this callback. */
	return G_SOURCE_REMOVE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x48, %rsp
movq %rdx, %r14
movl %edi, %ebp
leaq 0x9faa(%rip), %r15      # 0x15494
cmpl $0x3, (%r15)
jae 0xb60b
movl %ebp, %edi
xorl %esi, %esi
xorl %edx, %edx
movl $0x80000, %ecx          # imm = 0x80000
callq 0x45d0
testl %eax, %eax
jns 0xb517
callq 0x45b0
cmpl $0x4, (%rax)
je 0xb4f4
callq 0x7500
movl %eax, %ebx
movq %r14, %rdi
callq 0x4290
testl %eax, %eax
jns 0xb52f
cmpl $0x0, (%r15)
jne 0xb617
movl %ebp, %edi
callq 0x4100
cmpl $0x3, (%r15)
jae 0xb624
movl %ebx, %edi
callq 0x8fe5
testl %edx, %edx
js 0xb675
movq %rax, %r14
movl %edx, %ebp
cmpl $0x3, (%r15)
jae 0xb630
movq %r14, %rdi
callq 0x48e8
leaq 0xc(%rsp), %rsi
movl %ebp, %edi
callq 0x4520
cmpl $-0x1, %eax
je 0xb5a9
leaq 0xc(%rsp), %rdx
orb $0x4, 0x4(%rdx)
movl %ebp, %edi
xorl %esi, %esi
callq 0x4060
cmpl $-0x1, %eax
jne 0xb5b3
cmpl $0x0, (%r15)
je 0xb5b3
callq 0x73e6
testb $0x1, %al
jne 0xb5b3
leaq 0x48e1(%rip), %rcx      # 0xfe85
jmp 0xb653
cmpl $0x0, (%r15)
jne 0xb63f
leaq 0xa77e(%rip), %rax      # 0x15d38
movl %ebp, (%rax)
movl %ebp, %edi
callq 0x4270
leaq 0xa772(%rip), %rcx      # 0x15d3c
movl %eax, (%rcx)
testl %eax, %eax
js 0xb67a
xorl %eax, %eax
callq 0xb06f
leaq 0xa77a(%rip), %rax      # 0x15d5c
movl (%rax), %edi
leaq 0x94(%rip), %rdx        # 0xb67f
movl $0x1, %esi
xorl %ecx, %ecx
callq 0x4430
movl %ebx, %edi
callq 0x4100
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movl %ebp, %edi
callq 0x72a6
jmp 0xb4f4
movq %r14, %rdi
callq 0x72ef
jmp 0xb52f
movl %ebx, %edi
callq 0x733b
jmp 0xb540
movq %r14, %rdi
movl %ebp, %esi
callq 0x7384
jmp 0xb55e
callq 0x741d
testb $0x1, %al
jne 0xb5b3
leaq 0x480a(%rip), %rcx      # 0xfe5d
leaq 0x1e866(%rip), %rax     # 0x29ec0
movq (%rax), %rdx
leaq 0x1b6e(%rip), %rsi      # 0xd1d2
movl $0x6, %edi
xorl %eax, %eax
callq 0x4120
jmp 0xb5b3
callq 0x74aa
callq 0x7454