// File: /workspace/github_c_makefile_star2_O1/chrispanag[P]operating-systems-scheduler/scheduler.c
// Function: sigchld_handler
static void
sigchld_handler(int signum)
{
  // Create SIGSSET
  sigset_t sigset;
  sigemptyset(&sigset);
  sigaddset(&sigset, SIGALRM);
  
  // Disable SIGALRM
	if (sigprocmask(SIG_BLOCK, &sigset, NULL) < 0) {
		perror("signals_disable: sigprocmask");
		exit(1);
	}

  int status;
  for (;;) {
    if (nproc <= 0) break; // If there are no child processes just exit.
    pid_t pid = waitpid(-1, &status, WUNTRACED | WNOHANG); // If one of the children has changed status return pid
    if (pid < 0) {
      perror("waitpid");
      exit(1);
    }
    if (pid == 0) break; // No children have stopped

    explain_wait_status(pid, status);

    if (WIFEXITED(status) || WIFSIGNALED(status)) {
      /* A child has died */
      node* stopped = accessNode(proc_list, pid);
      // Check if the child is the one running now
      if (stopped == proc_list) {
        kill(stopped->next->pid, SIGCONT);
        /* Reset Alarm */
        alarm(SCHED_TQ_SEC);
      }
      proc_list = deleteNode(proc_list, pid);
      nproc--;
      printf("Parent: Received SIGCHLD, child is dead. Exiting.\n");
    }
    if (WIFSTOPPED(status)) {
      /* A child has stopped due to SIGSTOP/SIGTSTP, etc... */
      printf("Parent: Child has been stopped. Moving right along...\n");
      node* stopped = accessNode(proc_list, pid);
      // Check if the child is the one running now
      if (stopped == proc_list) {
        kill(stopped->next->pid, SIGCONT);
        proc_list = proc_list->next;
        /* Reset Alarm */
        alarm(SCHED_TQ_SEC);
      }
    }
  }
  // Enable SIGALRM
  if (sigprocmask(SIG_UNBLOCK, &sigset, NULL) < 0) {
		perror("signals_enable: sigprocmask");
		exit(1);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x90, %rsp
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x1120
movq %rbx, %rdi
movl $0xe, %esi
callq 0x11f0
xorl %edi, %edi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x1030
testl %eax, %eax
js 0x1982
leaq 0xc(%rsp), %rbx
leaq 0x8d6(%rip), %r14       # 0x20ee
leaq 0x7e9(%rip), %r15       # 0x2008
leaq 0x8fa(%rip), %r12       # 0x2120
cmpl $0x0, 0x28fb(%rip)      # 0x4128
jle 0x1958
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %rbx, %rsi
movl $0x3, %edx
callq 0x1180
testl %eax, %eax
js 0x197d
je 0x1958
movl %eax, %ebp
movl 0xc(%rsp), %esi
movl %eax, %edi
callq 0x19f2
movl 0xc(%rsp), %eax
andl $0x7f, %eax
je 0x1878
shll $0x18, %eax
addl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x2000000, %eax        # imm = 0x2000000
jl 0x18e5
movq 0x28a1(%rip), %rcx      # 0x4120
movq %rcx, %rax
cmpl %ebp, 0x4(%rax)
je 0x189e
movq 0x10(%rax), %rax
cmpq %rcx, %rax
jne 0x1882
movq %r15, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x10c0
xorl %eax, %eax
cmpq 0x287b(%rip), %rax      # 0x4120
jne 0x18c2
movq 0x10(%rax), %rax
movl 0x4(%rax), %edi
movl $0x12, %esi
callq 0x1140
movl $0x2, %edi
callq 0x10e0
movq 0x2857(%rip), %rdi      # 0x4120
movl %ebp, %esi
callq 0x156e
movq %rax, 0x2849(%rip)      # 0x4120
decl 0x284b(%rip)            # 0x4128
movq %r14, %rdi
callq 0x1070
cmpb $0x7f, 0xc(%rsp)
jne 0x1950
movq %r12, %rdi
callq 0x1070
movq 0x2825(%rip), %rcx      # 0x4120
movq %rcx, %rax
cmpl %ebp, 0x4(%rax)
je 0x191a
movq 0x10(%rax), %rax
cmpq %rcx, %rax
jne 0x18fe
movq %r15, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x10c0
xorl %eax, %eax
cmpq 0x27ff(%rip), %rax      # 0x4120
jne 0x1950
movq 0x10(%rax), %rax
movl 0x4(%rax), %edi
movl $0x12, %esi
callq 0x1140
movq 0x27e5(%rip), %rax      # 0x4120
movq 0x10(%rax), %rax
movq %rax, 0x27da(%rip)      # 0x4120
movl $0x2, %edi
callq 0x10e0
testl %ebp, %ebp
jne 0x1826
leaq 0x10(%rsp), %rsi
movl $0x1, %edi
xorl %edx, %edx
callq 0x1030
testl %eax, %eax
js 0x1987
addq $0x90, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
callq 0x1220
callq 0x124a
callq 0x1235