// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xb0, %rsp
movl %edi, -0x4(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x1170
leaq -0x88(%rbp), %rdi
movl $0xe, %esi
callq 0x1250
xorl %edi, %edi
leaq -0x88(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x1030
cmpl $0x0, %eax
jge 0x1df8
leaq 0x13a7(%rip), %rdi      # 0x3190
callq 0x11f0
movl $0x1, %edi
callq 0x1220
jmp 0x1dfa
movl 0x3360(%rip), %eax      # 0x5160
cmpl $0x0, %eax
jg 0x1e0a
jmp 0x1fdc
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
leaq -0x8c(%rbp), %rsi
movl $0x3, %edx
callq 0x11e0
movl %eax, -0x90(%rbp)
cmpl $0x0, -0x90(%rbp)
jge 0x1e45
leaq 0x1377(%rip), %rdi      # 0x31ad
callq 0x11f0
movl $0x1, %edi
callq 0x1220
cmpl $0x0, -0x90(%rbp)
jne 0x1e53
jmp 0x1fdc
movl -0x8c(%rbp), %eax
andl $0x7f, %eax
cmpl $0x0, %eax
je 0x1e7b
movl -0x8c(%rbp), %eax
andl $0x7f, %eax
addl $0x1, %eax
movsbl %al, %eax
sarl %eax
cmpl $0x0, %eax
jle 0x1f53
movq 0x32ce(%rip), %rdi      # 0x5150
movl -0x90(%rbp), %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x16f0
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
cmpq 0x32a9(%rip), %rax      # 0x5150
jne 0x1f26
movq -0x98(%rbp), %rdi
movl $0x1, %esi
callq 0x17b0
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x1edb
movq $0x0, 0x327f(%rip) # 0x5158
jmp 0x1ee9
movq -0xa0(%rbp), %rax
movq %rax, 0x326f(%rip)      # 0x5158
movq -0xa0(%rbp), %rax
movl 0x4(%rax), %edi
movl $0x12, %esi
callq 0x11a0
cmpl $0x0, %eax
jge 0x1f0e
leaq 0x12ac(%rip), %rdi      # 0x31b5
callq 0x11f0
movq -0xa0(%rbp), %rax
movq %rax, 0x3234(%rip)      # 0x5150
movl $0x2, %edi
callq 0x1100
movq 0x3223(%rip), %rdi      # 0x5150
movl -0x90(%rbp), %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x1580
movq %rax, 0x320c(%rip)      # 0x5150
movl 0x3216(%rip), %eax      # 0x5160
addl $-0x1, %eax
movl %eax, 0x320d(%rip)      # 0x5160
movl -0x8c(%rbp), %eax
andl $0xff, %eax
cmpl $0x7f, %eax
jne 0x1fd7
movq 0x31e6(%rip), %rdi      # 0x5150
movl -0x90(%rbp), %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x16f0
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
cmpq 0x31c1(%rip), %rax      # 0x5150
jne 0x1fd5
movq 0x31b8(%rip), %rdi      # 0x5150
xorl %esi, %esi
callq 0x17b0
movq %rax, 0x31aa(%rip)      # 0x5150
movq 0x31a3(%rip), %rax      # 0x5150
movl 0x4(%rax), %edi
movl $0x12, %esi
callq 0x11a0
cmpl $0x0, %eax
jge 0x1fcb
leaq 0x11ef(%rip), %rdi      # 0x31b5
callq 0x11f0
movl $0x2, %edi
callq 0x1100
jmp 0x1fd7
jmp 0x1dfa
movl $0x1, %edi
leaq -0x88(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x1030
cmpl $0x0, %eax
jge 0x200c
leaq 0x11bd(%rip), %rdi      # 0x31ba
callq 0x11f0
movl $0x1, %edi
callq 0x1220
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)