// File: /workspace/github_c_makefile_star2_O3/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, 0x4a02(%rip)      # 0x7274
xorl %ecx, %ecx
movl %ecx, 0x49f6(%rip)      # 0x7270
movl %ecx, 0x49f8(%rip)      # 0x7278
movl %ecx, 0x49d2(%rip)      # 0x7258
movslq (%r14), %rcx
movl %ecx, 0x49d1(%rip)      # 0x7260
testq %rcx, %rcx
movq %rcx, 0x20(%rsp)
jle 0x28a5
cltd
idivl %ecx
leaq 0x49dd(%rip), %rbx      # 0x7280
jmp 0x28ac
leaq 0x49bc(%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, 0x49b4(%rip)      # 0x7288
testq %r15, %r15
jle 0x2917
xorl %r12d, %r12d
leaq -0x2b6(%rip), %r14      # 0x262d
movl $0x8, %edi
callq 0x2200
movq 0x4994(%rip), %rcx      # 0x7288
movq %rax, (%rcx,%r12,8)
movq 0x4989(%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 0x28e3
movl 0x1c(%rsp), %edi
callq 0x3bd8
movl %eax, %r12d
movq 0x20(%rsp), %rbx
leaq (,%rbx,8), %rax
movq %rax, 0x28(%rsp)
leaq 0x4894(%rip), %rbp      # 0x71d0
leaq 0x48e5(%rip), %r14      # 0x7228
movl %r12d, 0x10(%rsp)
movl $0x10, 0x14(%rsp)
movl %r12d, %edi
leaq 0x48(%rsp), %rsi
leaq 0x14(%rsp), %rdx
callq 0x3744
movl %eax, 0xc(%rsp)
leaq 0x38(%rsp), %rdi
xorl %esi, %esi
callq 0x2130
movq %rbp, %rdi
callq 0x2350
movl 0x48f3(%rip), %r15d     # 0x7274
cmpl %r15d, 0x48d0(%rip)     # 0x7258
jne 0x2997
movq %r14, %rdi
movq %rbp, %rsi
callq 0x20d0
jmp 0x297a
movl $0x20, %edi
callq 0x2200
movq %rax, %r13
cmpl $-0x1, %ebx
je 0x29d7
cmpl $-0x2, %ebx
jne 0x2a2b
movq 0x48b3(%rip), %rax      # 0x7268
movslq 0x48bc(%rip), %rcx      # 0x7278
movq %r13, (%rax,%rcx,8)
leal 0x1(%rcx), %eax
cltd
idivl %r15d
movl %edx, 0x48ab(%rip)      # 0x7278
movl 0xc(%rsp), %r15d
jmp 0x2ac1
movl 0xc(%rsp), %r15d
movl %r15d, %edi
callq 0x3410
movq %rax, 0x8(%r13)
movq 0x4879(%rip), %rcx      # 0x7268
movslq 0x4882(%rip), %rdx      # 0x7278
leal 0x1(%rdx), %eax
movq %r13, (%rcx,%rdx,8)
movl %eax, 0x4875(%rip)      # 0x7278
testq %rdx, %rdx
jle 0x2ac1
movq 0x4855(%rip), %rdi      # 0x7268
movl %eax, %esi
movl $0x8, %edx
leaq -0x401(%rip), %rcx      # 0x2620
callq 0x2080
jmp 0x2ac1
movq 0x484e(%rip), %rbx      # 0x7280
movslq 0x483f(%rip), %r12      # 0x7278
movq (%rbx,%r12,8), %r15
testq %r15, %r15
jne 0x2a61
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 0x3410
movq %rax, 0x8(%r13)
movq 0x8(%r15), %rax
movslq 0x4(%r15), %rdx
movq %r13, (%rax,%rdx,8)
leal 0x1(%rdx), %ecx
movl (%r15), %eax
incl %eax
movl %ecx, 0x4(%r15)
movl %eax, (%r15)
testq %rdx, %rdx
movl 0x10(%rsp), %r12d
jle 0x2aad
movq 0x8(%r15), %rdi
movl %ecx, %esi
movl $0x8, %edx
leaq -0x485(%rip), %rcx      # 0x2620
callq 0x2080
movl (%r15), %eax
movq 0x20(%rsp), %rbx
cmpl %ebx, %eax
movl 0xc(%rsp), %r15d
jl 0x2ac1
incl 0x47b7(%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 0x1538(%rip), %xmm1     # 0x4020
addsd %xmm0, %xmm1
addsd 0x1534(%rip), %xmm1     # 0x4028
cvttsd2si %xmm1, %rax
movq %rax, 0x10(%r13)
incl 0x4755(%rip)            # 0x7258
leaq 0x46ee(%rip), %rdi      # 0x71f8
callq 0x2170
movq %rbp, %rdi
callq 0x21f0
jmp 0x2948