// File: /workspace/github_c_makefile_star2_O1/containers[P]conmon/src/ctr_exit.c
// Function: check_child_processes
static void check_child_processes(GHashTable *pid_to_handler, GHashTable *cache)
{
	for (;;) {
		int status;
		pid_t pid = waitpid(-1, &status, WNOHANG);
		if (pid < 0 && errno == EINTR)
			continue;

		if (pid < 0 && errno == ECHILD) {
			/* Before quitting, check if container_pid is still alive.
			 * In some systemd configurations, the container process may not be
			 * a direct child, so we won't receive SIGCHLD when it exits.
			 * Use kill(pid, 0) to check if the process still exists. */
			if (container_pid > 0) {
				if (kill(container_pid, 0) == 0) {
					/* Container process is still alive but not our child.
					 * Don't quit the main loop yet. */
					ninfof("Container process %d is still alive but not a direct child", container_pid);
					return;
				} else if (errno == ESRCH) {
					/* Container process has exited */
					ninfof("Container process %d has exited (detected via kill probe)", container_pid);
					/* Simulate container exit callback */
					container_status = 0; /* We can't get the real exit status */
					container_pid = -1;
					/* Fall through to quit the main loop */
				}
			}
			g_main_loop_quit(main_loop);
			return;
		}
		if (pid < 0)
			pexit("Failed to read child process status");

		if (pid == 0)
			return;

		/* If we got here, pid > 0, so we have a valid pid to check.  */
		void (*cb)(GPid, int, gpointer) = g_hash_table_lookup(pid_to_handler, &pid);
		if (cb) {
			cb(pid, status, 0);
		} else if (cache) {
			pid_t *k = g_malloc(sizeof(pid_t));
			int *v = g_malloc(sizeof(int));
			if (k == NULL || v == NULL)
				pexit("Failed to allocate memory");
			*k = pid;
			*v = status;
			g_hash_table_insert(cache, k, v);
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x4(%rsp), %r15
movq %rsp, %r12
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %r15, %rsi
movl $0x1, %edx
callq 0x4560
movl %eax, %ebp
movl %eax, (%rsp)
testl %eax, %eax
js 0xc43e
testl %ebp, %ebp
je 0xc511
movq %r14, %rdi
movq %r12, %rsi
callq 0x4660
testq %rax, %rax
je 0xc45f
movl (%rsp), %edi
movl 0x4(%rsp), %esi
xorl %edx, %edx
callq *%rax
jmp 0xc3fe
callq 0x45b0
cmpl $0x4, (%rax)
je 0xc3fe
testl %ebp, %ebp
jns 0xc419
callq 0x45b0
cmpl $0xa, (%rax)
je 0xc4ad
testl %ebp, %ebp
jns 0xc419
jmp 0xc533
testq %rbx, %rbx
je 0xc3fe
movl $0x4, %edi
callq 0x42c0
movq %rax, %r13
movl $0x4, %edi
callq 0x42c0
testq %r13, %r13
je 0xc52e
testq %rax, %rax
je 0xc52e
movl (%rsp), %ecx
movl %ecx, (%r13)
movl 0x4(%rsp), %ecx
movl %ecx, (%rax)
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x4220
jmp 0xc3fe
cmpl $0x0, 0x199a0(%rip)     # 0x25e54
jle 0xc4f4
movq %rax, %r13
movl 0x19995(%rip), %edi     # 0x25e54
xorl %esi, %esi
callq 0x4490
testl %eax, %eax
je 0xc505
cmpl $0x3, (%r13)
jne 0xc4f4
leaq 0x8fbc(%rip), %rax      # 0x15494
cmpl $0x2, (%rax)
jae 0xc527
leaq 0x9850(%rip), %rax      # 0x15d34
movl $0x0, (%rax)
movl $0xffffffff, 0x19960(%rip) # imm = 0xFFFFFFFF
leaq 0x1dae5(%rip), %rax     # 0x29fe0
movq (%rax), %rdi
callq 0x4160
jmp 0xc511
leaq 0x8f88(%rip), %rax      # 0x15494
cmpl $0x2, (%rax)
jae 0xc520
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x7ba0
jmp 0xc511
callq 0x7b52
jmp 0xc4dd
callq 0x7aa6
callq 0x7afc