// File: /workspace/github_c_makefile_star2_O2/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 0x247c
andl $0x0, 0x4abd(%rip)      # 0x7278
andl $0x0, 0x4abe(%rip)      # 0x7280
movl (%rbx), %eax
andl $0x0, 0x4a95(%rip)      # 0x7260
movl %eax, 0x4aab(%rip)      # 0x727c
movslq (%r14), %rcx
movl %ecx, 0x4a8e(%rip)      # 0x7268
testq %rcx, %rcx
movq %rcx, 0x20(%rsp)
jle 0x27f0
cltd
idivl %ecx
leaq 0x4a9a(%rip), %rbx      # 0x7288
jmp 0x27f7
leaq 0x4a79(%rip), %rbx      # 0x7270
movslq %eax, %rdi
shlq $0x3, %rdi
callq 0x2200
movq %rax, (%rbx)
movslq 0x18(%rsp), %rbx
movq %rbx, %rdi
shlq $0x3, %rdi
callq 0x2200
movq %rax, 0x4a72(%rip)      # 0x7290
xorl %r12d, %r12d
testl %ebx, %ebx
movl $0x0, %r13d
cmovgl %ebx, %r13d
pushq $0x8
popq %r15
leaq -0x2c1(%rip), %r14      # 0x2577
movq 0x20(%rsp), %rbx
cmpq %r12, %r13
je 0x2871
movq %r15, %rdi
callq 0x2200
movq 0x4a3f(%rip), %rcx      # 0x7290
movq %rax, (%rcx,%r12,8)
movq 0x4a34(%rip), %rax      # 0x7290
movq (%rax,%r12,8), %rdi
xorl %esi, %esi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x2260
incq %r12
jmp 0x283d
movl 0x1c(%rsp), %edi
callq 0x3b0f
movl %eax, %r13d
leaq (,%rbx,8), %rax
movq %rax, 0x28(%rsp)
leaq 0x4947(%rip), %rbp      # 0x71d8
leaq 0x4998(%rip), %r14      # 0x7230
movl %r13d, 0x10(%rsp)
movl $0x10, 0x14(%rsp)
movl %r13d, %edi
leaq 0x48(%rsp), %rsi
leaq 0x14(%rsp), %rdx
callq 0x3670
movl %eax, 0xc(%rsp)
leaq 0x38(%rsp), %rdi
xorl %esi, %esi
callq 0x2130
movq %rbp, %rdi
callq 0x2360
movl 0x49a6(%rip), %r12d     # 0x727c
cmpl %r12d, 0x4983(%rip)     # 0x7260
jne 0x28ec
movq %r14, %rdi
movq %rbp, %rsi
callq 0x20d0
jmp 0x28cf
pushq $0x20
popq %rdi
callq 0x2200
movq %rax, %r15
cmpl $-0x1, %ebx
je 0x292a
cmpl $-0x2, %ebx
jne 0x297c
movq 0x4968(%rip), %rax      # 0x7270
movslq 0x4971(%rip), %rcx      # 0x7280
movq %r15, (%rax,%rcx,8)
leal 0x1(%rcx), %eax
cltd
idivl %r12d
movl %edx, 0x4960(%rip)      # 0x7280
movl 0xc(%rsp), %r12d
jmp 0x2a16
movl 0xc(%rsp), %r12d
movl %r12d, %edi
callq 0x333c
movq %rax, 0x8(%r15)
movq 0x492e(%rip), %rcx      # 0x7270
movslq 0x4937(%rip), %rdx      # 0x7280
leal 0x1(%rdx), %eax
movq %r15, (%rcx,%rdx,8)
movl %eax, 0x492a(%rip)      # 0x7280
testq %rdx, %rdx
jle 0x2a16
movq 0x490a(%rip), %rdi      # 0x7270
movl %eax, %esi
pushq $0x8
popq %rdx
leaq -0x408(%rip), %rcx      # 0x256a
callq 0x2080
jmp 0x2a16
movq 0x4905(%rip), %rbx      # 0x7288
movslq 0x48f6(%rip), %r13      # 0x7280
movq (%rbx,%r13,8), %r12
testq %r12, %r12
jne 0x29b1
pushq $0x10
popq %rdi
callq 0x2200
movq %rax, %r12
movq 0x28(%rsp), %rdi
callq 0x2200
movq %rax, 0x8(%r12)
movq %r12, (%rbx,%r13,8)
movl 0xc(%rsp), %edi
callq 0x333c
movq %rax, 0x8(%r15)
movq 0x8(%r12), %rax
movslq 0x4(%r12), %rdx
movq %r15, (%rax,%rdx,8)
leal 0x1(%rdx), %ecx
movl (%r12), %eax
incl %eax
movl %ecx, 0x4(%r12)
movl %eax, (%r12)
testq %rdx, %rdx
movl 0x10(%rsp), %r13d
jle 0x2a02
movq 0x8(%r12), %rdi
movl %ecx, %esi
pushq $0x8
popq %rdx
leaq -0x48f(%rip), %rcx      # 0x256a
callq 0x2080
movl (%r12), %eax
movq 0x20(%rsp), %rbx
cmpl %ebx, %eax
movl 0xc(%rsp), %r12d
jl 0x2a16
incl 0x486a(%rip)            # 0x7280
imulq $0x3e8, 0x38(%rsp), %rax # imm = 0x3E8
cvtsi2sd %rax, %xmm0
movl %r12d, (%r15)
cvtsi2sdq 0x40(%rsp), %xmm1
divsd 0x15ea(%rip), %xmm1     # 0x4020
addsd %xmm0, %xmm1
addsd 0x15e6(%rip), %xmm1     # 0x4028
cvttsd2si %xmm1, %rax
movq %rax, 0x10(%r15)
incl 0x480f(%rip)            # 0x7260
leaq 0x47a8(%rip), %rdi      # 0x7200
callq 0x2170
movq %rbp, %rdi
callq 0x21f0
jmp 0x289d
nop