// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %rsp
movl %edi, -0x4(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x1110
leaq -0x88(%rbp), %rdi
movl $0xe, %esi
callq 0x11e0
xorl %edi, %edi
leaq -0x88(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x1030
cmpl $0x0, %eax
jge 0x1908
leaq 0x7c5(%rip), %rdi       # 0x20be
callq 0x1190
movl $0x1, %edi
callq 0x11b0
jmp 0x190a
movl 0x2810(%rip), %eax      # 0x4120
cmpl $0x0, %eax
jg 0x191a
jmp 0x1a9f
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
leaq -0x8c(%rbp), %rsi
movl $0x3, %edx
callq 0x1180
movl %eax, -0x90(%rbp)
cmpl $0x0, -0x90(%rbp)
jge 0x1955
leaq 0x795(%rip), %rdi       # 0x20db
callq 0x1190
movl $0x1, %edi
callq 0x11b0
cmpl $0x0, -0x90(%rbp)
jne 0x1963
jmp 0x1a9f
movl -0x90(%rbp), %edi
movl -0x8c(%rbp), %esi
callq 0x1bd0
movl -0x8c(%rbp), %eax
andl $0x7f, %eax
cmpl $0x0, %eax
je 0x199c
movl -0x8c(%rbp), %eax
andl $0x7f, %eax
addl $0x1, %eax
movsbl %al, %eax
sarl %eax
cmpl $0x0, %eax
jle 0x1a1d
movq 0x2775(%rip), %rdi      # 0x4118
movl -0x90(%rbp), %esi
callq 0x1580
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
cmpq 0x2755(%rip), %rax      # 0x4118
jne 0x19e7
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rax
movl 0x4(%rax), %edi
movl $0x12, %esi
callq 0x1140
movl $0x2, %edi
callq 0x10d0
movq 0x272a(%rip), %rdi      # 0x4118
movl -0x90(%rbp), %esi
callq 0x14a0
movq %rax, 0x2718(%rip)      # 0x4118
movl 0x271a(%rip), %eax      # 0x4120
addl $-0x1, %eax
movl %eax, 0x2711(%rip)      # 0x4120
leaq 0x6cd(%rip), %rdi       # 0x20e3
movb $0x0, %al
callq 0x10a0
movl -0x8c(%rbp), %eax
andl $0xff, %eax
cmpl $0x7f, %eax
jne 0x1a9a
leaq 0x6e2(%rip), %rdi       # 0x2116
movb $0x0, %al
callq 0x10a0
movq 0x26d6(%rip), %rdi      # 0x4118
movl -0x90(%rbp), %esi
callq 0x1580
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
cmpq 0x26b6(%rip), %rax      # 0x4118
jne 0x1a98
movq -0xa0(%rbp), %rax
movq 0x10(%rax), %rax
movl 0x4(%rax), %edi
movl $0x12, %esi
callq 0x1140
movq 0x2695(%rip), %rax      # 0x4118
movq 0x10(%rax), %rax
movq %rax, 0x268a(%rip)      # 0x4118
movl $0x2, %edi
callq 0x10d0
jmp 0x1a9a
jmp 0x190a
movl $0x1, %edi
leaq -0x88(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x1030
cmpl $0x0, %eax
jge 0x1acf
leaq 0x68d(%rip), %rdi       # 0x214d
callq 0x1190
movl $0x1, %edi
callq 0x11b0
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax,%rax)