// File: /workspace/github_c_makefile_star2_O1/somethingnew2-0[P]CS537-P4/server.c
// Function: main
int main(int argc, char *argv[])
{
    int listenfd, connfd, port, threads, buffers, alg, clientlen;
    struct sockaddr_in clientaddr;
	struct timeval arrival;

    getargs(&port, &threads, &buffers, &alg, argc, argv);

	max = buffers;
	numfull = fillptr = useptr = 0;
	algorithm = alg;
	// SFF-BS
	if(alg > 0) {
		epochs = malloc((buffers / alg) * sizeof(*epochs));
	}
	// FIFO or SFF
	else {
		buffer = malloc(buffers * sizeof(*buffer));
	}


	cid = malloc(threads*sizeof(*cid));

	int i;
	for(i = 0; i< threads; i++) {
		cid[i] = malloc(sizeof(pthread_t));
		pthread_create(cid[i], NULL, consumer, NULL);
	}

    // 
    // CS537: Create some threads...
    //

    listenfd = Open_listenfd(port);
    while (1) {
		clientlen = sizeof(clientaddr);
		connfd = Accept(listenfd, (SA *)&clientaddr, (socklen_t *) &clientlen);
		gettimeofday(&arrival, NULL);

		pthread_mutex_lock(&lock);
		while(numfull == max) {
			pthread_cond_wait(&empty, &lock);
		}

		request *req = malloc(sizeof(request)); 
		//FIFO
		if(alg == -2) {
			buffer[fillptr] = req;	
			fillptr = (fillptr+1) % max;	
		}
		//SFF
		else if(alg == -1) {
			req->size = requestFileSize(connfd);
			buffer[fillptr] = req;
			fillptr++;
			if(fillptr > 1) {
				qsort(buffer, fillptr, sizeof(*buffer), requestcmp);
			}
		}
		//SFF-BD
		else {
			epoch *epo;
			if(epochs[fillptr] == NULL) {
				epo = malloc(sizeof(epoch));
				epo->buffer = malloc(alg * sizeof(*epo->buffer));
				epochs[fillptr] = epo;
			}
			else {
				epo = epochs[fillptr];
			}
			req->size = requestFileSize(connfd);
			epo->buffer[epo->fillptr] = req;
			epo->fillptr++;
			epo->requests++;
			if(epo->fillptr > 1) {
				qsort(epo->buffer, epo->fillptr, sizeof(*epo->buffer), requestcmp);
			}

			if(epo->requests >= alg) {
				fillptr++;
			}
		}
		req->fd = connfd;
		req->arrival = ((arrival.tv_sec) * 1000 + arrival.tv_usec/1000.0) + 0.5;
		numfull++;

		pthread_cond_signal(&fill);
		pthread_mutex_unlock(&lock);

    }

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r9
movl %edi, %r8d
leaq 0x1c(%rsp), %rdi
leaq 0x18(%rsp), %rsi
leaq 0x34(%rsp), %rbx
leaq 0x30(%rsp), %r14
movq %rbx, %rdx
movq %r14, %rcx
callq 0x253c
movl (%rbx), %eax
movl %eax, 0x4a17(%rip)      # 0x7274
xorl %ecx, %ecx
movl %ecx, 0x4a0b(%rip)      # 0x7270
movl %ecx, 0x4a0d(%rip)      # 0x7278
movl %ecx, 0x49e7(%rip)      # 0x7258
movslq (%r14), %rcx
movl %ecx, 0x49e6(%rip)      # 0x7260
testq %rcx, %rcx
movq %rcx, 0x20(%rsp)
jle 0x2890
cltd
idivl %ecx
leaq 0x49f2(%rip), %rbx      # 0x7280
jmp 0x2897
leaq 0x49d1(%rip), %rbx      # 0x7268
movslq %eax, %rdi
shlq $0x3, %rdi
callq 0x2200
movq %rax, (%rbx)
movslq 0x18(%rsp), %r15
leaq (,%r15,8), %rdi
callq 0x2200
movq %rax, 0x49c9(%rip)      # 0x7288
testq %r15, %r15
jle 0x2902
xorl %r12d, %r12d
leaq -0x2ad(%rip), %r14      # 0x2621
movl $0x8, %edi
callq 0x2200
movq 0x49a9(%rip), %rcx      # 0x7288
movq %rax, (%rcx,%r12,8)
movq 0x499e(%rip), %rax      # 0x7288
movq (%rax,%r12,8), %rdi
xorl %esi, %esi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x2250
incq %r12
cmpq %r12, %r15
jne 0x28ce
movl 0x1c(%rsp), %edi
callq 0x3bb3
movl %eax, %r12d
movq 0x20(%rsp), %rbx
leaq (,%rbx,8), %rax
movq %rax, 0x28(%rsp)
leaq 0x48a9(%rip), %rbp      # 0x71d0
leaq 0x48fa(%rip), %r14      # 0x7228
movl %r12d, 0x10(%rsp)
movl $0x10, 0x14(%rsp)
movl %r12d, %edi
leaq 0x48(%rsp), %rsi
leaq 0x14(%rsp), %rdx
callq 0x3728
movl %eax, 0xc(%rsp)
leaq 0x38(%rsp), %rdi
xorl %esi, %esi
callq 0x2130
movq %rbp, %rdi
callq 0x2350
movl 0x4908(%rip), %r15d     # 0x7274
cmpl %r15d, 0x48e5(%rip)     # 0x7258
jne 0x2982
movq %r14, %rdi
movq %rbp, %rsi
callq 0x20d0
jmp 0x2965
movl $0x20, %edi
callq 0x2200
movq %rax, %r13
cmpl $-0x1, %ebx
je 0x29c2
cmpl $-0x2, %ebx
jne 0x2a16
movq 0x48c8(%rip), %rax      # 0x7268
movslq 0x48d1(%rip), %rcx      # 0x7278
movq %r13, (%rax,%rcx,8)
leal 0x1(%rcx), %eax
cltd
idivl %r15d
movl %edx, 0x48c0(%rip)      # 0x7278
movl 0xc(%rsp), %r15d
jmp 0x2aa5
movl 0xc(%rsp), %r15d
movl %r15d, %edi
callq 0x33f4
movq %rax, 0x8(%r13)
movq 0x488e(%rip), %rcx      # 0x7268
movslq 0x4897(%rip), %rdx      # 0x7278
leal 0x1(%rdx), %eax
movq %r13, (%rcx,%rdx,8)
movl %eax, 0x488a(%rip)      # 0x7278
testq %rdx, %rdx
jle 0x2aa5
movq 0x486a(%rip), %rdi      # 0x7268
movl %eax, %esi
movl $0x8, %edx
leaq -0x3f8(%rip), %rcx      # 0x2614
callq 0x2080
jmp 0x2aa5
movq 0x4863(%rip), %rbx      # 0x7280
movslq 0x4854(%rip), %r12      # 0x7278
movq (%rbx,%r12,8), %r15
testq %r15, %r15
jne 0x2a4c
movl $0x10, %edi
callq 0x2200
movq %rax, %r15
movq 0x28(%rsp), %rdi
callq 0x2200
movq %rax, 0x8(%r15)
movq %r15, (%rbx,%r12,8)
movl 0xc(%rsp), %edi
callq 0x33f4
movq %rax, 0x8(%r13)
movq 0x8(%r15), %rax
movslq 0x4(%r15), %rcx
movq %r13, (%rax,%rcx,8)
incl (%r15)
leal 0x1(%rcx), %eax
movl %eax, 0x4(%r15)
testq %rcx, %rcx
movl 0x10(%rsp), %r12d
jle 0x2a90
movq 0x8(%r15), %rdi
movl %eax, %esi
movl $0x8, %edx
leaq -0x477(%rip), %rcx      # 0x2614
callq 0x2080
movq 0x20(%rsp), %rbx
cmpl %ebx, (%r15)
movl 0xc(%rsp), %r15d
jl 0x2aa5
incl 0x47d3(%rip)            # 0x7278
imulq $0x3e8, 0x38(%rsp), %rax # imm = 0x3E8
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movl %r15d, (%r13)
xorps %xmm1, %xmm1
cvtsi2sdq 0x40(%rsp), %xmm1
divsd 0x1554(%rip), %xmm1     # 0x4020
addsd %xmm0, %xmm1
addsd 0x1550(%rip), %xmm1     # 0x4028
cvttsd2si %xmm1, %rax
movq %rax, 0x10(%r13)
incl 0x4771(%rip)            # 0x7258
leaq 0x470a(%rip), %rdi      # 0x71f8
callq 0x2170
movq %rbp, %rdi
callq 0x21f0
jmp 0x2933