// File: /workspace/github_c_makefile_star2_O2/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
subq $0x18, %rsp
movq %rsi, %rbx
movq %rdi, %r14
pushq $-0x1
popq %rbp
leaq 0xc(%rsp), %r15
pushq $0x1
popq %r12
movq %rdi, 0x10(%rsp)
movl %ebp, %edi
movq %r15, %rsi
movl %r12d, %edx
callq 0x4570
movl %eax, 0x8(%rsp)
testl %eax, %eax
js 0xb2f7
je 0xb3de
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
callq 0x4670
testq %rax, %rax
je 0xb308
movl 0x8(%rsp), %edi
movl 0xc(%rsp), %esi
xorl %edx, %edx
callq *%rax
jmp 0xb2bc
callq 0x45c0
movq %rax, %r13
movl (%rax), %eax
cmpl $0x4, %eax
je 0xb2bc
jmp 0xb373
testq %rbx, %rbx
je 0xb2bc
movl %r12d, %r14d
movq %r15, %r12
movl %ebp, %r15d
movq %rbx, %rbp
pushq $0x4
popq %rbx
movq %rbx, %rdi
callq 0x42c0
movq %rax, %r13
movq %rbx, %rdi
callq 0x42c0
testq %r13, %r13
je 0xb432
testq %rax, %rax
je 0xb432
movl 0x8(%rsp), %ecx
movl %ecx, (%r13)
movl 0xc(%rsp), %ecx
movl %ecx, (%rax)
movq %rbp, %rbx
movq %rbp, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x4220
movl %r15d, %ebp
movq %r12, %r15
movl %r14d, %r12d
movq 0x10(%rsp), %r14
jmp 0xb2bc
cmpl $0xa, %eax
jne 0xb3ed
cmpl $0x0, 0x19ae5(%rip)     # 0x24e64
jle 0xb3bd
movl 0x19add(%rip), %edi     # 0x24e64
xorl %esi, %esi
callq 0x4490
testl %eax, %eax
je 0xb3ce
cmpl $0x3, (%r13)
jne 0xb3bd
leaq 0x9104(%rip), %rax      # 0x144a4
cmpl $0x2, (%rax)
jae 0xb4db
leaq 0x9994(%rip), %rax      # 0x14d44
andl $0x0, (%rax)
movl $0xffffffff, 0x19aa7(%rip) # imm = 0xFFFFFFFF
leaq 0x1dc2c(%rip), %rax     # 0x28ff0
movq (%rax), %rdi
callq 0x4160
jmp 0xb3de
leaq 0x90cf(%rip), %rax      # 0x144a4
cmpl $0x2, (%rax)
jae 0xb487
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8bfc(%rip), %rax      # 0x13ff0
movq (%rax), %rdi
leaq 0x1c15(%rip), %rsi      # 0xd013
leaq 0x55e3(%rip), %rdx      # 0x109e8
xorl %eax, %eax
callq 0x4830
leaq 0x1dac5(%rip), %rax     # 0x28ed8
cmpl $0x0, (%rax)
je 0xb47f
leaq 0x1dab1(%rip), %rax     # 0x28ed0
movq (%rax), %rdx
leaq 0x1c14(%rip), %rsi      # 0xd03d
leaq 0x55b8(%rip), %rcx      # 0x109e8
jmp 0xb475
movq 0x8bb7(%rip), %rax      # 0x13ff0
movq (%rax), %rdi
leaq 0x1bd0(%rip), %rsi      # 0xd013
leaq 0x3f3a(%rip), %rdx      # 0xf384
xorl %eax, %eax
callq 0x4830
leaq 0x1da80(%rip), %rax     # 0x28ed8
cmpl $0x0, (%rax)
je 0xb47f
leaq 0x1da6c(%rip), %rax     # 0x28ed0
movq (%rax), %rdx
leaq 0x1bcf(%rip), %rsi      # 0xd03d
leaq 0x3f0f(%rip), %rcx      # 0xf384
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x4120
pushq $0x1
popq %rdi
callq 0x41e0
movq 0x8b62(%rip), %rax      # 0x13ff0
movq (%rax), %rdi
movl 0x199cd(%rip), %edx     # 0x24e64
leaq 0x5416(%rip), %rsi      # 0x108b4
xorl %eax, %eax
callq 0x4830
leaq 0x1da2c(%rip), %rax     # 0x28ed8
cmpl $0x0, (%rax)
je 0xb3de
leaq 0x1da14(%rip), %rax     # 0x28ed0
movq (%rax), %rdx
movl 0x1999f(%rip), %ecx     # 0x24e64
leaq 0x5430(%rip), %rsi      # 0x108fc
pushq $0x6
popq %rdi
xorl %eax, %eax
callq 0x4120
jmp 0xb3de
movq 0x8b0e(%rip), %rax      # 0x13ff0
movq (%rax), %rdi
movl 0x19979(%rip), %edx     # 0x24e64
leaq 0x545d(%rip), %rsi      # 0x1094f
xorl %eax, %eax
callq 0x4830
leaq 0x1d9d8(%rip), %rax     # 0x28ed8
cmpl $0x0, (%rax)
je 0xb3a9
leaq 0x1d9c0(%rip), %rax     # 0x28ed0
movq (%rax), %rdx
movl 0x1994b(%rip), %ecx     # 0x24e64
leaq 0x5476(%rip), %rsi      # 0x10996
pushq $0x6
popq %rdi
xorl %eax, %eax
callq 0x4120
jmp 0xb3a9