// File: /workspace/github_c_makefile_star2_O1/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 0x9cb2(%rip), %r15      # 0x15494
cmpl $0x3, (%r15)
jae 0xb903
movl %ebp, %edi
xorl %esi, %esi
xorl %edx, %edx
movl $0x80000, %ecx          # imm = 0x80000
callq 0x45d0
testl %eax, %eax
jns 0xb80f
callq 0x45b0
cmpl $0x4, (%rax)
je 0xb7ec
callq 0x7448
movl %eax, %ebx
movq %r14, %rdi
callq 0x4290
testl %eax, %eax
jns 0xb827
cmpl $0x0, (%r15)
jne 0xb90f
movl %ebp, %edi
callq 0x4100
cmpl $0x3, (%r15)
jae 0xb91c
movl %ebx, %edi
callq 0x9042
testl %edx, %edx
js 0xb96d
movq %rax, %r14
movl %edx, %ebp
cmpl $0x3, (%r15)
jae 0xb928
movq %r14, %rdi
callq 0x48e8
leaq 0xc(%rsp), %rsi
movl %ebp, %edi
callq 0x4520
cmpl $-0x1, %eax
je 0xb8a1
leaq 0xc(%rsp), %rdx
orb $0x4, 0x4(%rdx)
movl %ebp, %edi
xorl %esi, %esi
callq 0x4060
cmpl $-0x1, %eax
jne 0xb8ab
cmpl $0x0, (%r15)
je 0xb8ab
callq 0x732e
testb $0x1, %al
jne 0xb8ab
leaq 0x4609(%rip), %rcx      # 0xfea5
jmp 0xb94b
cmpl $0x0, (%r15)
jne 0xb937
leaq 0xa486(%rip), %rax      # 0x15d38
movl %ebp, (%rax)
movl %ebp, %edi
callq 0x4270
leaq 0xa47a(%rip), %rcx      # 0x15d3c
movl %eax, (%rcx)
testl %eax, %eax
js 0xb972
xorl %eax, %eax
callq 0xb28a
leaq 0xa482(%rip), %rax      # 0x15d5c
movl (%rax), %edi
leaq 0x94(%rip), %rdx        # 0xb977
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 0x71ee
jmp 0xb7ec
movq %r14, %rdi
callq 0x7237
jmp 0xb827
movl %ebx, %edi
callq 0x7283
jmp 0xb838
movq %r14, %rdi
movl %ebp, %esi
callq 0x72cc
jmp 0xb856
callq 0x7365
testb $0x1, %al
jne 0xb8ab
leaq 0x4532(%rip), %rcx      # 0xfe7d
leaq 0x1e56e(%rip), %rax     # 0x29ec0
movq (%rax), %rdx
leaq 0x1876(%rip), %rsi      # 0xd1d2
movl $0x6, %edi
xorl %eax, %eax
callq 0x4120
jmp 0xb8ab
callq 0x73f2
callq 0x739c