// File: /workspace/github_c_makefile_star2_O3/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 0x1bb4
leaq 0x10(%rsp), %rdi
callq 0x1110
testl %eax, %eax
js 0x1bb4
callq 0x1250
testl %eax, %eax
js 0x1bbd
je 0x1bc6
movl %eax, %ebp
movl 0x94(%rsp), %edi
callq 0x1100
movl 0x10(%rsp), %edi
callq 0x1100
movl 0x90(%rsp), %eax
movl %eax, 0x39a0(%rip)      # 0x515c
movl 0x14(%rsp), %eax
movl %eax, 0x399a(%rip)      # 0x5160
movq 0x397b(%rip), %rdi      # 0x5148
leaq 0x199f(%rip), %rdx      # 0x3173
movl %ebp, %esi
callq 0x152f
movq %rax, 0x3966(%rip)      # 0x5148
incl 0x3970(%rip)            # 0x5158
cmpl $0x2, %r14d
jl 0x1806
decl %r14d
xorl %r15d, %r15d
movq 0x8(%rbx,%r15,8), %rdi
incq %r15
callq 0x1c1f
cmpq %r15, %r14
jne 0x17f4
cmpl $0x0, 0x394b(%rip)      # 0x5158
je 0x1beb
movl 0x393f(%rip), %edi      # 0x5158
callq 0x201a
leaq 0x4ed(%rip), %rax       # 0x1d12
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 0x1c0d
leaq 0x67d(%rip), %rax       # 0x1f10
leaq 0x90(%rsp), %rsi
movq %rax, (%rsi)
movl $0xe, %edi
xorl %edx, %edx
callq 0x1080
testl %eax, %eax
js 0x1c16
movl $0x1, %esi
movl $0xd, %edi
callq 0x1140
movq 0x3880(%rip), %rax      # 0x5148
movl 0x4(%rax), %edi
movl $0x12, %esi
callq 0x1180
movl $0x2, %edi
callq 0x10f0
movl 0x3877(%rip), %ebp      # 0x515c
movl 0x3874(%rip), %r14d     # 0x5160
leaq 0x90(%rsp), %r13
leaq 0xc(%rsp), %rbx
leaq 0x1704(%rip), %r12      # 0x3004
movl $0x44, %edx
movl %ebp, %edi
leaq 0x10(%rsp), %rsi
callq 0x1120
cmpq $0x44, %rax
jne 0x1b34
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 0x1b95
movl 0x10(%rsp), %eax
cmpq $0x4, %rax
ja 0x1a18
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movq 0x37d9(%rip), %rdi      # 0x5148
callq 0x15d2
jmp 0x1a10
movl 0x14(%rsp), %esi
movq 0x37c4(%rip), %rcx      # 0x5148
movq %rcx, %rax
cmpl %esi, (%rax)
je 0x1a23
movq 0x18(%rax), %rax
cmpq %rcx, %rax
jne 0x1987
jmp 0x19c8
leaq 0x18(%rsp), %rdi
callq 0x1c1f
jmp 0x1a10
movl 0x14(%rsp), %esi
movq 0x3797(%rip), %rcx      # 0x5148
movq %rcx, %rax
movq 0x18(%rax), %rdx
cmpl %esi, (%rax)
je 0x1a74
movq %rdx, %rax
cmpq %rcx, %rdx
jne 0x19b4
xorl %r15d, %r15d
leaq 0x169f(%rip), %rdi      # 0x3071
xorl %eax, %eax
callq 0x10d0
jmp 0x1ad3
movl 0x14(%rsp), %r15d
movq 0x375e(%rip), %rcx      # 0x5148
movq %rcx, %rax
cmpl %r15d, (%rax)
je 0x1ac6
movq 0x18(%rax), %rax
cmpq %rcx, %rax
jne 0x19ed
leaq 0x166b(%rip), %rdi      # 0x3071
movl %r15d, %esi
xorl %eax, %eax
callq 0x10d0
xorl %r15d, %r15d
jmp 0x1ad3
movl $0xffffffda, %r15d      # imm = 0xFFFFFFDA
jmp 0x1ad3
movl $0x0, 0x10(%rax)
movq 0x371f(%rip), %rcx      # 0x5150
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)
xorl %r15d, %r15d
cmpq %rax, %rcx
jne 0x1ad3
movq 0x18(%rcx), %rax
cmpl $0x0, 0x10(%rax)
jne 0x1abd
movq $0x0, 0x36de(%rip) # 0x5150
jmp 0x1ad3
movq 0x20(%rax), %rsi
movq %rdx, 0x18(%rsi)
movq %rsi, 0x20(%rdx)
movl $0x1, 0x10(%rax)
movq %rcx, %rsi
movq %rsi, %rdx
movq 0x20(%rsi), %rsi
cmpq %rcx, %rsi
je 0x1a9c
cmpl $0x0, 0x10(%rsi)
je 0x1a8a
movq %rsi, 0x20(%rax)
movq %rdx, 0x18(%rax)
movq 0x20(%rdx), %rcx
movq %rax, 0x18(%rcx)
movq %rax, 0x20(%rdx)
xorl %r15d, %r15d
cmpq $0x0, 0x3695(%rip)      # 0x5150
jne 0x1ad3
movq %rax, 0x368c(%rip)      # 0x5150
jmp 0x1ad3
movl 0x4(%rax), %edi
movl $0x9, %esi
callq 0x1180
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 0x1b9e
movl $0x4, %edx
movl %r14d, %edi
movq %rbx, %rsi
callq 0x1090
cmpq $0x4, %rax
je 0x1900
leaq 0x167a(%rip), %rdi      # 0x31ac
jmp 0x1b3b
leaq 0x1623(%rip), %rdi      # 0x315e
callq 0x11d0
movq 0x34b1(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0x1628(%rip), %rdi      # 0x3179
movl $0x32, %esi
movl $0x1, %edx
callq 0x1210
decl 0x35f2(%rip)            # 0x5158
cmpl $0x0, 0x35eb(%rip)      # 0x5158
jle 0x1b8e
callq 0x1190
cmpl $-0x1, %eax
jne 0x1b6f
cmpl $0x0, 0x35d8(%rip)      # 0x5158
jne 0x1b6f
leaq 0x163d(%rip), %rdi      # 0x31c6
callq 0x1070
xorl %edi, %edi
callq 0x1200
leaq 0x157c(%rip), %rdi      # 0x3118
jmp 0x1ba5
leaq 0x159d(%rip), %rdi      # 0x3142
callq 0x11d0
movl $0x1, %edi
callq 0x1200
leaq 0x1504(%rip), %rdi      # 0x30bf
jmp 0x1ba5
leaq 0x1500(%rip), %rdi      # 0x30c4
jmp 0x1ba5
movl 0x90(%rsp), %edi
callq 0x1100
movl 0x14(%rsp), %edi
callq 0x1100
movl 0x94(%rsp), %edi
movl 0x10(%rsp), %esi
callq 0x1c79
movq 0x3406(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0x14a2(%rip), %rdi      # 0x309e
movl $0x20, %esi
movl $0x1, %edx
callq 0x1210
jmp 0x1baa
leaq 0x14de(%rip), %rdi      # 0x30f2
jmp 0x1ba5
leaq 0x14e8(%rip), %rdi      # 0x3105
jmp 0x1ba5