// File: /workspace/github_c_makefile_star2_O3/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 0x198a
cmpl $0x0, 0x2924(%rip)      # 0x4128
jle 0x1960
leaq 0xc(%rsp), %rbx
leaq 0x8d8(%rip), %r14       # 0x20ee
leaq 0x7eb(%rip), %r15       # 0x2008
leaq 0x8fc(%rip), %r12       # 0x2120
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %rbx, %rsi
movl $0x3, %edx
callq 0x1180
testl %eax, %eax
js 0x1985
je 0x1960
movl %eax, %ebp
movl 0xc(%rsp), %esi
movl %eax, %edi
callq 0x19fa
movl 0xc(%rsp), %eax
movl %eax, %ecx
andl $0x7f, %ecx
je 0x186d
shll $0x18, %ecx
addl $0x1000000, %ecx        # imm = 0x1000000
cmpl $0x2000000, %ecx        # imm = 0x2000000
jl 0x18e4
movq 0x28ac(%rip), %rdi      # 0x4120
movq %rdi, %rax
cmpl %ebp, 0x4(%rax)
je 0x189a
movq 0x10(%rax), %rax
cmpq %rdi, %rax
jne 0x1877
movq %r15, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x10c0
movq 0x2888(%rip), %rdi      # 0x4120
xorl %eax, %eax
cmpq %rdi, %rax
jne 0x18c1
movq 0x10(%rax), %rax
movl 0x4(%rax), %edi
movl $0x12, %esi
callq 0x1140
movl $0x2, %edi
callq 0x10e0
movq 0x285f(%rip), %rdi      # 0x4120
movl %ebp, %esi
callq 0x156b
movq %rax, 0x2851(%rip)      # 0x4120
decl 0x2853(%rip)            # 0x4128
movq %r14, %rdi
callq 0x1070
movzbl 0xc(%rsp), %eax
jmp 0x18e7
movzbl %al, %eax
cmpl $0x7f, %eax
jne 0x1953
movq %r12, %rdi
callq 0x1070
movq 0x2825(%rip), %rcx      # 0x4120
movq %rcx, %rax
cmpl %ebp, 0x4(%rax)
je 0x1921
movq 0x10(%rax), %rax
cmpq %rcx, %rax
jne 0x18fe
movq %r15, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x10c0
movq 0x2801(%rip), %rcx      # 0x4120
xorl %eax, %eax
cmpq %rcx, %rax
jne 0x1953
movq 0x10(%rax), %rax
movl 0x4(%rax), %edi
movl $0x12, %esi
callq 0x1140
movq 0x27e2(%rip), %rax      # 0x4120
movq 0x10(%rax), %rax
movq %rax, 0x27d7(%rip)      # 0x4120
movl $0x2, %edi
callq 0x10e0
cmpl $0x0, 0x27ce(%rip)      # 0x4128
jg 0x1824
leaq 0x10(%rsp), %rsi
movl $0x1, %edi
xorl %edx, %edx
callq 0x1030
testl %eax, %eax
js 0x198f
addq $0x90, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
callq 0x1220
callq 0x124a
callq 0x1235