// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rsi, %rbx
movl %edi, %ebp
leaq 0x90(%rsp), %rdi
callq 0x1110
testl %eax, %eax
js 0x1989
leaq 0x10(%rsp), %rdi
callq 0x1110
testl %eax, %eax
js 0x1989
callq 0x1250
testl %eax, %eax
js 0x1992
je 0x199b
movl %eax, %r14d
movl 0x94(%rsp), %edi
callq 0x1100
movl 0x10(%rsp), %edi
callq 0x1100
movl 0x90(%rsp), %eax
movl %eax, 0x2b8a(%rip)      # 0x415c
movl 0x14(%rsp), %eax
movl %eax, 0x2b84(%rip)      # 0x4160
movq 0x2b65(%rip), %rdi      # 0x4148
leaq 0xb89(%rip), %rdx       # 0x2173
movl %r14d, %esi
callq 0x139a
movq %rax, 0x2b4f(%rip)      # 0x4148
incl 0x2b59(%rip)            # 0x4158
cmpl $0x2, %ebp
pushq $0x1
popq %r14
cmovgel %ebp, %r14d
decl %r14d
xorl %r15d, %r15d
cmpq %r15, %r14
je 0x1624
movq 0x8(%rbx,%r15,8), %rdi
incq %r15
callq 0x19f0
jmp 0x1610
cmpl $0x0, 0x2b2d(%rip)      # 0x4158
je 0x19c0
movl 0x2b21(%rip), %edi      # 0x4158
callq 0x1e54
leaq 0x4df(%rip), %rax       # 0x1b22
leaq 0x98(%rsp), %rbx
movq %rax, -0x8(%rbx)
movl $0x10000000, 0x80(%rbx) # imm = 0x10000000
leaq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x1160
pushq $0x11
popq %rbp
movq %r14, %rdi
movl %ebp, %esi
callq 0x1240
pushq $0xe
popq %rsi
movq %r14, %rdi
callq 0x1240
pushq $0x10
popq %rcx
movq %rbx, %rdi
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x90(%rsp), %rsi
movl %ebp, %edi
xorl %edx, %edx
callq 0x1080
testl %eax, %eax
js 0x19de
leaq 0x66a(%rip), %rax       # 0x1d14
leaq 0x90(%rsp), %rsi
movq %rax, (%rsi)
pushq $0xe
popq %rdi
xorl %edx, %edx
callq 0x1080
testl %eax, %eax
js 0x19e7
pushq $0xd
popq %rdi
pushq $0x1
popq %rsi
callq 0x1140
movq 0x2a6f(%rip), %rax      # 0x4148
movl 0x4(%rax), %edi
pushq $0x12
popq %rsi
callq 0x1180
pushq $0x2
popq %rdi
callq 0x10f0
movl 0x2a6a(%rip), %ebp      # 0x415c
movl 0x2a67(%rip), %r14d     # 0x4160
leaq 0x90(%rsp), %rbx
pushq $0xe
popq %r15
pushq $0x11
popq %r12
movl %ebp, %edi
leaq 0x10(%rsp), %rsi
pushq $0x44
popq %rdx
callq 0x1120
cmpq $0x44, %rax
jne 0x190f
movq %rbx, %rdi
callq 0x1160
movq %rbx, %rdi
movl %r15d, %esi
callq 0x1240
movq %rbx, %rdi
movl %r12d, %esi
callq 0x1240
xorl %edi, %edi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x1030
testl %eax, %eax
js 0x196c
movl 0x10(%rsp), %eax
pushq $-0x26
popq %rcx
movl %ecx, %r13d
cmpq $0x4, %rax
ja 0x18b4
leaq 0x895(%rip), %rcx       # 0x2004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x29c9(%rip), %rdi      # 0x4148
callq 0x13ea
jmp 0x18b1
movl 0x14(%rsp), %edx
movq 0x29b4(%rip), %rdi      # 0x4148
pushq $-0x1
popq %rsi
callq 0x14e2
xorl %r13d, %r13d
testq %rax, %rax
je 0x18b4
andl $0x0, 0x10(%rax)
movq 0x299d(%rip), %rcx      # 0x4150
movq 0x18(%rax), %rdx
movq 0x20(%rax), %rsi
movq %rdx, 0x18(%rsi)
movq %rsi, 0x20(%rdx)
movq 0x20(%rcx), %rdx
movq %rdx, 0x20(%rax)
movq %rcx, 0x18(%rax)
movq %rax, 0x18(%rdx)
movq %rax, 0x20(%rcx)
cmpq %rax, %rcx
jne 0x18b4
movq 0x18(%rcx), %rax
xorl %r13d, %r13d
cmpl $0x0, 0x10(%rax)
movl $0x0, %ecx
cmovneq %rax, %rcx
movq %rcx, 0x2955(%rip)      # 0x4150
jmp 0x18b4
leaq 0x18(%rsp), %rdi
callq 0x19f0
jmp 0x18b1
movl 0x14(%rsp), %edx
movq 0x292e(%rip), %rdi      # 0x4148
pushq $-0x1
popq %rsi
callq 0x14e2
xorl %r13d, %r13d
testq %rax, %rax
je 0x18b4
movq 0x18(%rax), %rcx
movq 0x20(%rax), %rdx
movq %rcx, 0x18(%rdx)
movq %rdx, 0x20(%rcx)
movl $0x1, 0x10(%rax)
movq 0x28fc(%rip), %rdx      # 0x4148
movq %rdx, %rsi
movq %rsi, %rcx
movq 0x20(%rsi), %rsi
cmpq %rdx, %rsi
je 0x1861
cmpl $0x0, 0x10(%rsi)
je 0x184f
movq %rsi, 0x20(%rax)
movq %rcx, 0x18(%rax)
movq 0x20(%rcx), %rdx
movq %rax, 0x18(%rdx)
movq %rax, 0x20(%rcx)
cmpq $0x0, 0x28d3(%rip)      # 0x4150
jne 0x18b4
movq %rax, 0x28ca(%rip)      # 0x4150
jmp 0x18b4
movl 0x14(%rsp), %r13d
movq 0x28b4(%rip), %rdi      # 0x4148
pushq $-0x1
popq %rsi
movl %r13d, %edx
callq 0x14e2
testq %rax, %rax
je 0x18b1
movl 0x4(%rax), %edi
pushq $0x9
popq %rsi
callq 0x1180
jmp 0x18b4
xorl %r13d, %r13d
movl %r13d, 0xc(%rsp)
movq %rbx, %rdi
callq 0x1160
movq %rbx, %rdi
movl %r15d, %esi
callq 0x1240
movq %rbx, %rdi
movl %r12d, %esi
callq 0x1240
pushq $0x1
popq %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x1030
testl %eax, %eax
js 0x1975
movl %r14d, %edi
leaq 0xc(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x1090
cmpq $0x4, %rax
je 0x1709
leaq 0x89f(%rip), %rdi       # 0x21ac
jmp 0x1916
leaq 0x848(%rip), %rdi       # 0x215e
callq 0x11d0
movq 0x26d6(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0x84d(%rip), %rdi       # 0x2179
pushq $0x32
popq %rsi
pushq $0x1
popq %rdx
callq 0x1210
decl 0x281b(%rip)            # 0x4158
cmpl $0x0, 0x2814(%rip)      # 0x4158
jle 0x1965
callq 0x1190
cmpl $-0x1, %eax
jne 0x1946
cmpl $0x0, 0x2801(%rip)      # 0x4158
jne 0x1946
leaq 0x866(%rip), %rdi       # 0x21c6
callq 0x1070
xorl %edi, %edi
callq 0x1200
leaq 0x7a5(%rip), %rdi       # 0x2118
jmp 0x197c
leaq 0x7c6(%rip), %rdi       # 0x2142
callq 0x11d0
pushq $0x1
popq %rdi
callq 0x1200
leaq 0x72f(%rip), %rdi       # 0x20bf
jmp 0x197c
leaq 0x72b(%rip), %rdi       # 0x20c4
jmp 0x197c
movl 0x90(%rsp), %edi
callq 0x1100
movl 0x14(%rsp), %edi
callq 0x1100
movl 0x94(%rsp), %edi
movl 0x10(%rsp), %esi
callq 0x1a90
movq 0x2631(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0x6cd(%rip), %rdi       # 0x209e
pushq $0x20
popq %rsi
pushq $0x1
popq %rdx
callq 0x1210
jmp 0x1981
leaq 0x70d(%rip), %rdi       # 0x20f2
jmp 0x197c
leaq 0x717(%rip), %rdi       # 0x2105
jmp 0x197c