// File: /workspace/github_c_makefile_star2_O1/chrispanag[P]operating-systems-scheduler/scheduler-shell.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 (pid < 0) {
      perror("waitpid");
      exit(1);
    }
    if (pid == 0) break;

    if (WIFEXITED(status) || WIFSIGNALED(status)) {
      /* A child has died */
      /* Start the next process */
      node* stopped = accessNode(proc_list, pid, -1);
      if (stopped == proc_list) {
        node* next = getNextProcess(stopped, 1);

        // Change the proc_list_high pointer
        if (!next->priority) {
          proc_list_high = NULL;
        } else {
          proc_list_high = next;
        }

        if (kill(next->pid, SIGCONT) < 0) {
          perror("kill");
        }
        proc_list = next;
        alarm(SCHED_TQ_SEC);
      }
      /* Delete the killed process from the list */
      proc_list = deleteNode(proc_list, pid, -1);
      nproc--;
    }
    if (WIFSTOPPED(status)) {
      /* A child has stopped due to SIGSTOP/SIGTSTP, etc... */
      node* stopped = accessNode(proc_list, pid, -1);
      // Check if the child is the one running now
      if (stopped == proc_list) {
        proc_list = getNextProcess(proc_list, 0);
        if (kill(proc_list->pid, SIGCONT) < 0) {
          perror("kill");
        }
        /* 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 0x1160
movq %rbx, %rdi
movl $0xe, %esi
callq 0x1240
xorl %edi, %edi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x1030
testl %eax, %eax
js 0x1f3f
leaq 0xc(%rsp), %rbx
xorl %r15d, %r15d
leaq 0x1392(%rip), %r14      # 0x313d
cmpl $0x0, 0x33a6(%rip)      # 0x5158
jle 0x1f15
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %rbx, %rsi
movl $0x3, %edx
callq 0x11c0
testl %eax, %eax
js 0x1f3a
je 0x1f15
movl %eax, %ebp
movl 0xc(%rsp), %eax
andl $0x7f, %eax
je 0x1df6
shll $0x18, %eax
addl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x2000000, %eax        # imm = 0x2000000
jl 0x1e92
movq 0x334b(%rip), %rdi      # 0x5148
movl %ebp, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x16e5
cmpq 0x3338(%rip), %rax      # 0x5148
jne 0x1e72
movq 0x18(%rax), %rcx
movq 0x3333(%rip), %rdx      # 0x5150
cmpq %rax, %rdx
movq %rdx, %r12
cmoveq %rcx, %r12
testq %rdx, %rdx
cmoveq %rcx, %r12
cmpl $0x0, 0x10(%rcx)
cmovneq %rcx, %r12
cmpl $0x0, 0x10(%r12)
movq %r12, %rax
cmoveq %r15, %rax
movq %rax, 0x3306(%rip)      # 0x5150
movl 0x4(%r12), %edi
movl $0x12, %esi
callq 0x1180
testl %eax, %eax
js 0x1efe
movq %r12, 0x32e0(%rip)      # 0x5148
movl $0x2, %edi
callq 0x10f0
movq 0x32cf(%rip), %rdi      # 0x5148
movl %ebp, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x165c
movq %rax, 0x32bc(%rip)      # 0x5148
decl 0x32c6(%rip)            # 0x5158
cmpb $0x7f, 0xc(%rsp)
jne 0x1ef4
movq 0x32a8(%rip), %rdi      # 0x5148
movl %ebp, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x16e5
movq 0x3295(%rip), %rcx      # 0x5148
cmpq %rcx, %rax
jne 0x1ef4
movq 0x18(%rcx), %rax
movq 0x328d(%rip), %rcx      # 0x5150
testq %rcx, %rcx
cmoveq %rax, %rcx
cmpl $0x0, 0x10(%rax)
cmovneq %rax, %rcx
movq %rcx, 0x326f(%rip)      # 0x5148
movl 0x4(%rcx), %edi
movl $0x12, %esi
callq 0x1180
testl %eax, %eax
js 0x1f0b
movl $0x2, %edi
callq 0x10f0
testl %ebp, %ebp
jne 0x1dab
jmp 0x1f15
movq %r14, %rdi
callq 0x11d0
jmp 0x1e61
movq %r14, %rdi
callq 0x11d0
jmp 0x1eea
leaq 0x10(%rsp), %rsi
movl $0x1, %edi
xorl %edx, %edx
callq 0x1030
testl %eax, %eax
js 0x1f44
addq $0x90, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
callq 0x12c0
callq 0x12ea
callq 0x12d5