// File: /workspace/github_c_makefile_star2_O3/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 0x1f06
cmpl $0x0, 0x3402(%rip)      # 0x5158
jle 0x1edc
leaq 0xc(%rsp), %rbx
xorl %r15d, %r15d
leaq 0x13d2(%rip), %r14      # 0x313d
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %rbx, %rsi
movl $0x3, %edx
callq 0x11c0
testl %eax, %eax
js 0x1f01
je 0x1edc
movl %eax, %ebp
movl 0xc(%rsp), %eax
movl %eax, %ecx
andl $0x7f, %ecx
je 0x1dad
shll $0x18, %ecx
addl $0x1000000, %ecx        # imm = 0x1000000
cmpl $0x2000000, %ecx        # imm = 0x2000000
jl 0x1e53
movq 0x3394(%rip), %rdi      # 0x5148
movl %ebp, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x16cc
movq 0x3381(%rip), %rdi      # 0x5148
cmpq %rdi, %rax
jne 0x1e33
movq 0x18(%rax), %rcx
movq 0x3379(%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, 0x334c(%rip)      # 0x5150
movl 0x4(%r12), %edi
movl $0x12, %esi
callq 0x1180
testl %eax, %eax
js 0x1ec5
movq %r12, 0x3326(%rip)      # 0x5148
movl $0x2, %edi
callq 0x10f0
movq 0x3315(%rip), %rdi      # 0x5148
movl %ebp, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x1653
movq %rax, 0x3302(%rip)      # 0x5148
decl 0x330c(%rip)            # 0x5158
movzbl 0xc(%rsp), %eax
jmp 0x1e56
movzbl %al, %eax
cmpl $0x7f, %eax
jne 0x1eb6
movq 0x32e6(%rip), %rdi      # 0x5148
movl %ebp, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x16cc
movq 0x32d3(%rip), %rcx      # 0x5148
cmpq %rcx, %rax
jne 0x1eb6
movq 0x18(%rcx), %rax
movq 0x32cb(%rip), %rcx      # 0x5150
testq %rcx, %rcx
cmoveq %rax, %rcx
cmpl $0x0, 0x10(%rax)
cmovneq %rax, %rcx
movq %rcx, 0x32ad(%rip)      # 0x5148
movl 0x4(%rcx), %edi
movl $0x12, %esi
callq 0x1180
testl %eax, %eax
js 0x1ed2
movl $0x2, %edi
callq 0x10f0
cmpl $0x0, 0x329b(%rip)      # 0x5158
jg 0x1d6b
jmp 0x1edc
movq %r14, %rdi
callq 0x11d0
jmp 0x1e1b
movq %r14, %rdi
callq 0x11d0
jmp 0x1eac
leaq 0x10(%rsp), %rsi
movl $0x1, %edi
xorl %edx, %edx
callq 0x1030
testl %eax, %eax
js 0x1f0b
addq $0x90, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
callq 0x12c0
callq 0x12ea
callq 0x12d5