// File: /workspace/github_c_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x1160
pushq $0xe
popq %rsi
movq %rbx, %rdi
callq 0x1240
xorl %edi, %edi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x1030
testl %eax, %eax
js 0x1d02
pushq $-0x1
popq %rbx
leaq 0x4(%rsp), %r14
pushq $0x3
popq %rbp
pushq $0x2
popq %r13
cmpl $0x0, 0x25e3(%rip)      # 0x4158
jle 0x1cc9
movl %ebx, %edi
movq %r14, %rsi
movl %ebp, %edx
callq 0x11c0
testl %eax, %eax
js 0x1cee
je 0x1cc9
movl %eax, %r15d
movl 0x4(%rsp), %eax
movl %eax, %ecx
andl $0x7f, %ecx
je 0x1bb8
shll $0x18, %ecx
addl $0x1000000, %ecx        # imm = 0x1000000
cmpl $0x2000000, %ecx        # imm = 0x2000000
jl 0x1c40
movq 0x2589(%rip), %rdi      # 0x4148
movl %r15d, %esi
movl %ebx, %edx
callq 0x14e2
movq 0x2578(%rip), %rdi      # 0x4148
cmpq %rdi, %rax
jne 0x1c22
movq %rax, %rdi
pushq $0x1
popq %rsi
callq 0x1532
movq %rax, %r12
cmpl $0x0, 0x10(%rax)
movl $0x0, %ecx
cmoveq %rcx, %rax
movq %rax, 0x2559(%rip)      # 0x4150
movl 0x4(%r12), %edi
pushq $0x12
popq %rsi
callq 0x1180
testl %eax, %eax
js 0x1caa
movq %r12, 0x2535(%rip)      # 0x4148
movl %r13d, %edi
callq 0x10f0
movq 0x2526(%rip), %rdi      # 0x4148
movl %r15d, %esi
movl %ebx, %edx
callq 0x1469
movq %rax, 0x2515(%rip)      # 0x4148
decl 0x251f(%rip)            # 0x4158
movzbl 0x4(%rsp), %eax
jmp 0x1c43
movzbl %al, %eax
cmpl $0x7f, %eax
jne 0x1b6e
movq 0x24f5(%rip), %rdi      # 0x4148
movl %r15d, %esi
movl %ebx, %edx
callq 0x14e2
movq 0x24e4(%rip), %rcx      # 0x4148
cmpq %rcx, %rax
jne 0x1b6e
movq 0x18(%rcx), %rax
movq 0x24d8(%rip), %rcx      # 0x4150
testq %rcx, %rcx
cmoveq %rax, %rcx
cmpl $0x0, 0x10(%rax)
cmovneq %rax, %rcx
movq %rcx, 0x24ba(%rip)      # 0x4148
movl 0x4(%rcx), %edi
pushq $0x12
popq %rsi
callq 0x1180
testl %eax, %eax
js 0x1cbb
movl %r13d, %edi
callq 0x10f0
jmp 0x1b6e
leaq 0x48c(%rip), %rdi       # 0x213d
callq 0x11d0
jmp 0x1c0c
leaq 0x47b(%rip), %rdi       # 0x213d
callq 0x11d0
jmp 0x1c9d
pushq $0x1
popq %rdi
leaq 0x8(%rsp), %rsi
xorl %edx, %edx
callq 0x1030
testl %eax, %eax
js 0x1d0b
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x440(%rip), %rdi       # 0x2135
callq 0x11d0
pushq $0x1
popq %rdi
callq 0x1200
leaq 0x40f(%rip), %rdi       # 0x2118
jmp 0x1cf5
leaq 0x430(%rip), %rdi       # 0x2142
jmp 0x1cf5