// File: /workspace/github_c_makefile_star2_O2/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 0xa492(%rip), %r15      # 0x144a4
cmpl $0x3, (%r15)
jae 0xa180
movl %ebp, %edi
xorl %esi, %esi
xorl %edx, %edx
movl $0x80000, %ecx          # imm = 0x80000
callq 0x45e0
testl %eax, %eax
jns 0xa086
callq 0x45c0
cmpl $0x4, (%rax)
je 0xa01c
movq 0x9faf(%rip), %rax      # 0x13ff0
movq (%rax), %rdi
leaq 0x2fc8(%rip), %rsi      # 0xd013
leaq 0x5c97(%rip), %rdx      # 0xfce9
xorl %eax, %eax
callq 0x4830
leaq 0x1ee78(%rip), %rax     # 0x28ed8
cmpl $0x0, (%rax)
je 0xa352
leaq 0x1ee60(%rip), %rax     # 0x28ed0
movq (%rax), %rdx
leaq 0x2fc3(%rip), %rsi      # 0xd03d
leaq 0x5c68(%rip), %rcx      # 0xfce9
jmp 0xa348
movl %eax, %ebx
movq %r14, %rdi
callq 0x4290
testl %eax, %eax
jns 0xa09e
cmpl $0x0, (%r15)
jne 0xa1cc
movl %ebp, %edi
callq 0x4100
cmpl $0x3, (%r15)
jae 0xa21a
movl %ebx, %edi
callq 0x6513
testl %edx, %edx
js 0xa266
movq %rax, %r14
movl %edx, %ebp
cmpl $0x3, (%r15)
jae 0xa2b2
movq %r14, %rdi
callq 0x48f8
leaq 0xc(%rsp), %rsi
movl %ebp, %edi
callq 0x4530
cmpl $-0x1, %eax
je 0xa120
leaq 0xc(%rsp), %rdx
orb $0x4, 0x4(%rdx)
movl %ebp, %edi
xorl %esi, %esi
callq 0x4060
cmpl $-0x1, %eax
jne 0xa12a
cmpl $0x0, (%r15)
je 0xa12a
movq 0x9ee6(%rip), %rax      # 0x13ff0
movq (%rax), %rdi
leaq 0x3083(%rip), %rsi      # 0xd197
leaq 0x5d6a(%rip), %r14      # 0xfe85
jmp 0xa372
cmpl $0x0, (%r15)
jne 0xa35a
leaq 0xac17(%rip), %rax      # 0x14d48
movl %ebp, (%rax)
movl %ebp, %edi
callq 0x4270
leaq 0xac0b(%rip), %rcx      # 0x14d4c
movl %eax, (%rcx)
testl %eax, %eax
js 0xa305
xorl %eax, %eax
callq 0x9844
leaq 0xac13(%rip), %rax      # 0x14d6c
movl (%rax), %edi
leaq 0x24d(%rip), %rdx       # 0xa3af
pushq $0x1
popq %rsi
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
movq 0x9e69(%rip), %rax      # 0x13ff0
movq (%rax), %rdi
leaq 0x5adc(%rip), %rsi      # 0xfc6d
movl %ebp, %edx
xorl %eax, %eax
callq 0x4830
leaq 0x1ed37(%rip), %rax     # 0x28ed8
cmpl $0x0, (%rax)
je 0xa01c
leaq 0x1ed1f(%rip), %rax     # 0x28ed0
movq (%rax), %rdx
leaq 0x5aea(%rip), %rsi      # 0xfca5
pushq $0x6
popq %rdi
movl %ebp, %ecx
xorl %eax, %eax
callq 0x4120
jmp 0xa01c
movq 0x9e1d(%rip), %rax      # 0x13ff0
movq (%rax), %rdi
leaq 0x5b37(%rip), %rsi      # 0xfd14
movq %r14, %rdx
xorl %eax, %eax
callq 0x4830
leaq 0x1ecea(%rip), %rax     # 0x28ed8
cmpl $0x0, (%rax)
je 0xa09e
leaq 0x1ecd2(%rip), %rax     # 0x28ed0
movq (%rax), %rdx
leaq 0x5b2d(%rip), %rsi      # 0xfd35
pushq $0x6
popq %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x4120
jmp 0xa09e
movq 0x9dcf(%rip), %rax      # 0x13ff0
movq (%rax), %rdi
leaq 0x5b36(%rip), %rsi      # 0xfd61
movl %ebx, %edx
xorl %eax, %eax
callq 0x4830
leaq 0x1ec9d(%rip), %rax     # 0x28ed8
cmpl $0x0, (%rax)
je 0xa0af
leaq 0x1ec85(%rip), %rax     # 0x28ed0
movq (%rax), %rdx
leaq 0x5b39(%rip), %rsi      # 0xfd8e
pushq $0x6
popq %rdi
movl %ebx, %ecx
xorl %eax, %eax
callq 0x4120
jmp 0xa0af
movq 0x9d83(%rip), %rax      # 0x13ff0
movq (%rax), %rdi
leaq 0x2d9c(%rip), %rsi      # 0xd013
leaq 0x5b49(%rip), %rdx      # 0xfdc7
xorl %eax, %eax
callq 0x4830
leaq 0x1ec4c(%rip), %rax     # 0x28ed8
cmpl $0x0, (%rax)
je 0xa352
leaq 0x1ec34(%rip), %rax     # 0x28ed0
movq (%rax), %rdx
leaq 0x2d97(%rip), %rsi      # 0xd03d
leaq 0x5b1a(%rip), %rcx      # 0xfdc7
jmp 0xa348
movq 0x9d37(%rip), %rax      # 0x13ff0
movq (%rax), %rdi
leaq 0x5b2e(%rip), %rsi      # 0xfdf1
movq %r14, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x4830
leaq 0x1ec02(%rip), %rax     # 0x28ed8
cmpl $0x0, (%rax)
je 0xa0cd
leaq 0x1ebea(%rip), %rax     # 0x28ed0
movq (%rax), %rdx
leaq 0x5b31(%rip), %rsi      # 0xfe21
pushq $0x6
popq %rdi
movq %r14, %rcx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x4120
jmp 0xa0cd
movq 0x9ce4(%rip), %rax      # 0x13ff0
movq (%rax), %rdi
leaq 0x2cfd(%rip), %rsi      # 0xd013
leaq 0x5b90(%rip), %rdx      # 0xfead
xorl %eax, %eax
callq 0x4830
leaq 0x1ebad(%rip), %rax     # 0x28ed8
cmpl $0x0, (%rax)
je 0xa352
leaq 0x1eb99(%rip), %rax     # 0x28ed0
movq (%rax), %rdx
leaq 0x2cfc(%rip), %rsi      # 0xd03d
leaq 0x5b65(%rip), %rcx      # 0xfead
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x4120
pushq $0x1
popq %rdi
callq 0x41e0
movq 0x9c8f(%rip), %rax      # 0x13ff0
movq (%rax), %rdi
leaq 0x2e2c(%rip), %rsi      # 0xd197
leaq 0x5aeb(%rip), %r14      # 0xfe5d
movq %r14, %rdx
xorl %eax, %eax
callq 0x4830
leaq 0x1eb55(%rip), %rax     # 0x28ed8
cmpl $0x0, (%rax)
je 0xa12a
leaq 0x1eb3d(%rip), %rax     # 0x28ed0
movq (%rax), %rdx
leaq 0x2e35(%rip), %rsi      # 0xd1d2
pushq $0x6
popq %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x4120
jmp 0xa12a