// File: /workspace/github_c_makefile_star2_O0/chrispanag[P]operating-systems-scheduler/scheduler-shell.c
// Function: main
int main(int argc, char *argv[]) {
	/* Two file descriptors for communication with the shell */
	static int request_fd, return_fd;

	/* Create the shell. */
	pid_t shell_pid = sched_create_shell(SHELL_EXECUTABLE_NAME, &request_fd, &return_fd);
  proc_list = addNode(proc_list, shell_pid, SHELL_EXECUTABLE_NAME);
	nproc++;

	/* TODO: add the shell to the scheduler's tasks */

	/*
	 * For each of argv[1] to argv[argc - 1],
	 * create a new child process, add it to the process list.
	 */
	int i;
	for (i = 0; i < argc - 1; i++) {
		sched_create_task(argv[i+1]);
  }

	if (nproc == 0) {
		fprintf(stderr, "Scheduler: No tasks. Exiting...\n");
		exit(1);
	}

	/* Wait for all children to raise SIGSTOP before exec()ing. */
	wait_for_ready_children(nproc);

	/* Install SIGALRM and SIGCHLD handlers. */
	install_signal_handlers();
	// Start the first process and set alarm
	kill(proc_list->pid, SIGCONT);
  alarm(SCHED_TQ_SEC);


	shell_request_loop(request_fd, return_fd);
  nproc--;
	/* Now that the shell is gone, just loop forever
	 * until we exit from inside a signal handler.
	 */
   if (nproc > 0) {
     while (1) {
       if (pause() == -1) {
         if (nproc == 0) {
           printf("No processes on the list. Exiting...\n");
           exit(0);
         }
       }
     }
   } else {
     exit(0);
   }


	/* Unreachable */
	fprintf(stderr, "Internal error: Reached unreachable point\n");
	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq 0x19ae(%rip), %rdi      # 0x31eb
leaq 0x3920(%rip), %rsi      # 0x5164
leaq 0x391d(%rip), %rdx      # 0x5168
callq 0x1980
movl %eax, -0x14(%rbp)
movq 0x38f6(%rip), %rdi      # 0x5150
movl -0x14(%rbp), %esi
leaq 0x1987(%rip), %rdx      # 0x31eb
callq 0x13d0
movq %rax, 0x38e0(%rip)      # 0x5150
movl 0x38ea(%rip), %eax      # 0x5160
addl $0x1, %eax
movl %eax, 0x38e1(%rip)      # 0x5160
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl -0x8(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x18b4
movq -0x10(%rbp), %rax
movl -0x18(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1a60
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x1886
movl 0x38a6(%rip), %eax      # 0x5160
cmpl $0x0, %eax
jne 0x18e1
movq 0x3732(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x17d2(%rip), %rsi      # 0x30a2
movb $0x0, %al
callq 0x1160
movl $0x1, %edi
callq 0x1220
movl 0x3879(%rip), %edi      # 0x5160
callq 0x2610
callq 0x1b30
movq 0x3858(%rip), %rax      # 0x5150
movl 0x4(%rax), %edi
movl $0x12, %esi
callq 0x11a0
movl $0x2, %edi
callq 0x1100
movl 0x384f(%rip), %edi      # 0x5164
movl 0x384d(%rip), %esi      # 0x5168
callq 0x1c40
movl 0x383a(%rip), %eax      # 0x5160
addl $-0x1, %eax
movl %eax, 0x3831(%rip)      # 0x5160
movl 0x382b(%rip), %eax      # 0x5160
cmpl $0x0, %eax
jle 0x196a
jmp 0x193c
callq 0x11b0
cmpl $-0x1, %eax
jne 0x1968
movl 0x3814(%rip), %eax      # 0x5160
cmpl $0x0, %eax
jne 0x1966
leaq 0x176b(%rip), %rdi      # 0x30c3
movb $0x0, %al
callq 0x10c0
xorl %edi, %edi
callq 0x1220
jmp 0x1968
jmp 0x193c
xorl %edi, %edi
callq 0x1220
nopw %cs:(%rax,%rax)