// File: /workspace/github_c_makefile_star2_O1/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, %r14d
leaq 0x90(%rsp), %rdi
callq 0x1110
testl %eax, %eax
js 0x1bfe
leaq 0x10(%rsp), %rdi
callq 0x1110
testl %eax, %eax
js 0x1bfe
callq 0x1250
testl %eax, %eax
js 0x1c07
je 0x1c10
movl %eax, %ebp
movl 0x94(%rsp), %edi
callq 0x1100
movl 0x10(%rsp), %edi
callq 0x1100
movl 0x90(%rsp), %eax
movl %eax, 0x3987(%rip)      # 0x515c
movl 0x14(%rsp), %eax
movl %eax, 0x3981(%rip)      # 0x5160
movq 0x3962(%rip), %rdi      # 0x5148
leaq 0x1986(%rip), %rdx      # 0x3173
movl %ebp, %esi
callq 0x152f
movq %rax, 0x394d(%rip)      # 0x5148
incl 0x3957(%rip)            # 0x5158
cmpl $0x2, %r14d
jl 0x181f
decl %r14d
xorl %r15d, %r15d
movq 0x8(%rbx,%r15,8), %rdi
incq %r15
callq 0x1c69
cmpq %r15, %r14
jne 0x180d
cmpl $0x0, 0x3932(%rip)      # 0x5158
je 0x1c35
movl 0x3926(%rip), %edi      # 0x5158
callq 0x2056
leaq 0x521(%rip), %rax       # 0x1d5f
leaq 0x98(%rsp), %rbx
movq %rax, -0x8(%rbx)
movl $0x10000000, 0x80(%rbx) # imm = 0x10000000
leaq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x1160
movq %r14, %rdi
movl $0x11, %esi
callq 0x1240
movq %r14, %rdi
movl $0xe, %esi
callq 0x1240
movl $0x10, %ecx
movq %rbx, %rdi
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x90(%rsp), %rsi
movl $0x11, %edi
xorl %edx, %edx
callq 0x1080
testl %eax, %eax
js 0x1c57
leaq 0x69d(%rip), %rax       # 0x1f49
leaq 0x90(%rsp), %rsi
movq %rax, (%rsi)
movl $0xe, %edi
xorl %edx, %edx
callq 0x1080
testl %eax, %eax
js 0x1c60
movl $0x1, %esi
movl $0xd, %edi
callq 0x1140
movq 0x3867(%rip), %rax      # 0x5148
movl 0x4(%rax), %edi
movl $0x12, %esi
callq 0x1180
movl $0x2, %edi
callq 0x10f0
movl 0x385e(%rip), %ebp      # 0x515c
movl 0x385b(%rip), %r14d     # 0x5160
leaq 0x90(%rsp), %r13
leaq 0xc(%rsp), %rbx
leaq 0x16eb(%rip), %r12      # 0x3004
movl $0x44, %edx
movl %ebp, %edi
leaq 0x10(%rsp), %rsi
callq 0x1120
cmpq $0x44, %rax
jne 0x1b7e
movq %r13, %rdi
callq 0x1160
movq %r13, %rdi
movl $0xe, %esi
callq 0x1240
movq %r13, %rdi
movl $0x11, %esi
callq 0x1240
xorl %edi, %edi
movq %r13, %rsi
xorl %edx, %edx
callq 0x1030
testl %eax, %eax
js 0x1bdf
movl 0x10(%rsp), %eax
cmpq $0x4, %rax
ja 0x1b17
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movq 0x37c0(%rip), %rdi      # 0x5148
callq 0x15d7
jmp 0x1b12
movl 0x14(%rsp), %esi
movq 0x37ab(%rip), %rcx      # 0x5148
movq %rcx, %rax
cmpl %esi, (%rax)
je 0x19bd
movq 0x18(%rax), %rax
cmpq %rcx, %rax
jne 0x19a0
leaq 0x16bd(%rip), %rdi      # 0x3071
xorl %eax, %eax
callq 0x10d0
xorl %eax, %eax
xorl %r15d, %r15d
testq %rax, %rax
je 0x1b1d
movl $0x0, 0x10(%rax)
movq 0x3779(%rip), %rcx      # 0x5150
movq 0x18(%rax), %rdx
movq 0x20(%rax), %rsi
movq %rdx, 0x18(%rsi)
movq 0x18(%rax), %rdx
movq %rsi, 0x20(%rdx)
movq 0x20(%rcx), %rdx
movq %rdx, 0x20(%rax)
movq %rcx, 0x18(%rax)
movq 0x20(%rcx), %rdx
movq %rax, 0x18(%rdx)
movq %rax, 0x20(%rcx)
cmpq %rax, %rcx
jne 0x1b1d
movq 0x18(%rcx), %rax
cmpl $0x0, 0x10(%rax)
jne 0x1ac5
movq $0x0, 0x372b(%rip) # 0x5150
jmp 0x1b1d
leaq 0x18(%rsp), %rdi
callq 0x1c69
jmp 0x1b12
movl 0x14(%rsp), %esi
movq 0x3704(%rip), %rcx      # 0x5148
movq %rcx, %rax
cmpl %esi, (%rax)
je 0x1a64
movq 0x18(%rax), %rax
cmpq %rcx, %rax
jne 0x1a47
leaq 0x1616(%rip), %rdi      # 0x3071
xorl %eax, %eax
callq 0x10d0
xorl %eax, %eax
xorl %r15d, %r15d
testq %rax, %rax
je 0x1b1d
movq 0x18(%rax), %rcx
movq 0x20(%rax), %rdx
movq %rcx, 0x18(%rdx)
movq 0x18(%rax), %rcx
movq %rdx, 0x20(%rcx)
movl $0x1, 0x10(%rax)
movq 0x36b6(%rip), %rdx      # 0x5148
movq %rdx, %rsi
movq %rsi, %rcx
movq 0x20(%rsi), %rsi
cmpq %rdx, %rsi
je 0x1aa7
cmpl $0x0, 0x10(%rsi)
je 0x1a95
movq %rsi, 0x20(%rax)
movq %rcx, 0x18(%rax)
movq 0x20(%rcx), %rdx
movq %rax, 0x18(%rdx)
movq %rax, 0x20(%rcx)
cmpq $0x0, 0x368d(%rip)      # 0x5150
jne 0x1b1d
movq %rax, 0x3684(%rip)      # 0x5150
jmp 0x1b1d
movl 0x14(%rsp), %r15d
movq 0x366e(%rip), %rcx      # 0x5148
movq %rcx, %rax
cmpl %r15d, (%rax)
je 0x1afe
movq 0x18(%rax), %rax
cmpq %rcx, %rax
jne 0x1add
leaq 0x157f(%rip), %rdi      # 0x3071
movl %r15d, %esi
xorl %eax, %eax
callq 0x10d0
xorl %eax, %eax
testq %rax, %rax
je 0x1b12
movl 0x4(%rax), %edi
movl $0x9, %esi
callq 0x1180
jmp 0x1b1d
xorl %r15d, %r15d
jmp 0x1b1d
movl $0xffffffda, %r15d      # imm = 0xFFFFFFDA
movl %r15d, 0xc(%rsp)
movq %r13, %rdi
callq 0x1160
movq %r13, %rdi
movl $0xe, %esi
callq 0x1240
movq %r13, %rdi
movl $0x11, %esi
callq 0x1240
movl $0x1, %edi
movq %r13, %rsi
xorl %edx, %edx
callq 0x1030
testl %eax, %eax
js 0x1be8
movl $0x4, %edx
movl %r14d, %edi
movq %rbx, %rsi
callq 0x1090
cmpq $0x4, %rax
je 0x1919
leaq 0x1630(%rip), %rdi      # 0x31ac
jmp 0x1b85
leaq 0x15d9(%rip), %rdi      # 0x315e
callq 0x11d0
movq 0x3467(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0x15de(%rip), %rdi      # 0x3179
movl $0x32, %esi
movl $0x1, %edx
callq 0x1210
decl 0x35a8(%rip)            # 0x5158
cmpl $0x0, 0x35a1(%rip)      # 0x5158
jle 0x1bd8
callq 0x1190
cmpl $-0x1, %eax
jne 0x1bb9
cmpl $0x0, 0x358e(%rip)      # 0x5158
jne 0x1bb9
leaq 0x15f3(%rip), %rdi      # 0x31c6
callq 0x1070
xorl %edi, %edi
callq 0x1200
leaq 0x1532(%rip), %rdi      # 0x3118
jmp 0x1bef
leaq 0x1553(%rip), %rdi      # 0x3142
callq 0x11d0
movl $0x1, %edi
callq 0x1200
leaq 0x14ba(%rip), %rdi      # 0x30bf
jmp 0x1bef
leaq 0x14b6(%rip), %rdi      # 0x30c4
jmp 0x1bef
movl 0x90(%rsp), %edi
callq 0x1100
movl 0x14(%rsp), %edi
callq 0x1100
movl 0x94(%rsp), %edi
movl 0x10(%rsp), %esi
callq 0x1cc3
movq 0x33bc(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0x1458(%rip), %rdi      # 0x309e
movl $0x20, %esi
movl $0x1, %edx
callq 0x1210
jmp 0x1bf4
leaq 0x1494(%rip), %rdi      # 0x30f2
jmp 0x1bef
leaq 0x149e(%rip), %rdi      # 0x3105
jmp 0x1bef