// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl -0x8(%rbp), %r8d
movq -0x10(%rbp), %r9
leaq -0x1c(%rbp), %rdi
leaq -0x20(%rbp), %rsi
leaq -0x24(%rbp), %rdx
leaq -0x28(%rbp), %rcx
callq 0x2490
movl -0x24(%rbp), %eax
movl %eax, 0x5948(%rip)      # 0x8284
movl $0x0, 0x593a(%rip)      # 0x8280
movl $0x0, 0x5938(%rip)      # 0x8288
movl $0x0, 0x590e(%rip)      # 0x8268
movl -0x28(%rbp), %eax
movl %eax, 0x590d(%rip)      # 0x8270
cmpl $0x0, -0x28(%rbp)
jle 0x2985
movl -0x24(%rbp), %eax
cltd
idivl -0x28(%rbp)
movslq %eax, %rdi
shlq $0x3, %rdi
callq 0x2220
movq %rax, 0x590d(%rip)      # 0x8290
jmp 0x2999
movslq -0x24(%rbp), %rdi
shlq $0x3, %rdi
callq 0x2220
movq %rax, 0x58df(%rip)      # 0x8278
movslq -0x20(%rbp), %rdi
shlq $0x3, %rdi
callq 0x2220
movq %rax, 0x58eb(%rip)      # 0x8298
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x2a05
movl $0x8, %edi
callq 0x2220
movq %rax, %rdx
movq 0x58c8(%rip), %rax      # 0x8298
movslq -0x54(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x58b9(%rip), %rax      # 0x8298
movslq -0x54(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
xorl %eax, %eax
movl %eax, %ecx
leaq -0x3b2(%rip), %rdx      # 0x2640
movq %rcx, %rsi
callq 0x2270
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x29b4
movl -0x1c(%rbp), %edi
callq 0x47e0
movl %eax, -0x14(%rbp)
movl $0x10, -0x2c(%rbp)
movl -0x14(%rbp), %edi
leaq -0x3c(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
callq 0x3f20
movl %eax, -0x18(%rbp)
leaq -0x50(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2130
leaq 0x57a2(%rip), %rdi      # 0x81e0
callq 0x2370
movl 0x581f(%rip), %eax      # 0x8268
cmpl 0x5835(%rip), %eax      # 0x8284
jne 0x2a66
leaq 0x57e0(%rip), %rdi      # 0x8238
leaq 0x5781(%rip), %rsi      # 0x81e0
callq 0x20c0
jmp 0x2a43
movl $0x20, %edi
callq 0x2220
movq %rax, -0x60(%rbp)
cmpl $-0x2, -0x28(%rbp)
jne 0x2aab
movq -0x60(%rbp), %rdx
movq 0x57f3(%rip), %rax      # 0x8278
movslq 0x57fc(%rip), %rcx      # 0x8288
movq %rdx, (%rax,%rcx,8)
movl 0x57f2(%rip), %eax      # 0x8288
addl $0x1, %eax
cltd
idivl 0x57e4(%rip)            # 0x8284
movl %edx, 0x57e2(%rip)      # 0x8288
jmp 0x2c0b
cmpl $-0x1, -0x28(%rbp)
jne 0x2b16
movl -0x18(%rbp), %edi
callq 0x3820
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x60(%rbp), %rdx
movq 0x57a9(%rip), %rax      # 0x8278
movslq 0x57b2(%rip), %rcx      # 0x8288
movq %rdx, (%rax,%rcx,8)
movl 0x57a8(%rip), %eax      # 0x8288
addl $0x1, %eax
movl %eax, 0x579f(%rip)      # 0x8288
cmpl $0x1, 0x5798(%rip)      # 0x8288
jle 0x2b11
movq 0x577f(%rip), %rdi      # 0x8278
movslq 0x5788(%rip), %rsi      # 0x8288
movl $0x8, %edx
leaq -0x4fc(%rip), %rcx      # 0x2610
callq 0x2070
jmp 0x2c09
movq 0x5773(%rip), %rax      # 0x8290
movslq 0x5764(%rip), %rcx      # 0x8288
cmpq $0x0, (%rax,%rcx,8)
jne 0x2b69
movl $0x10, %edi
callq 0x2220
movq %rax, -0x68(%rbp)
movslq -0x28(%rbp), %rdi
shlq $0x3, %rdi
callq 0x2220
movq %rax, %rcx
movq -0x68(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x68(%rbp), %rdx
movq 0x5734(%rip), %rax      # 0x8290
movslq 0x5725(%rip), %rcx      # 0x8288
movq %rdx, (%rax,%rcx,8)
jmp 0x2b7f
movq 0x5720(%rip), %rax      # 0x8290
movslq 0x5711(%rip), %rcx      # 0x8288
movq (%rax,%rcx,8), %rax
movq %rax, -0x68(%rbp)
movl -0x18(%rbp), %edi
callq 0x3820
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x60(%rbp), %rdx
movq -0x68(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x68(%rbp), %rcx
movslq 0x4(%rcx), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x68(%rbp), %rax
movl 0x4(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x4(%rax)
movq -0x68(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq -0x68(%rbp), %rax
cmpl $0x1, 0x4(%rax)
jle 0x2bed
movq -0x68(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x68(%rbp), %rax
movslq 0x4(%rax), %rsi
movl $0x8, %edx
leaq -0x5d8(%rip), %rcx      # 0x2610
callq 0x2070
movq -0x68(%rbp), %rax
movl (%rax), %eax
cmpl -0x28(%rbp), %eax
jl 0x2c07
movl 0x568a(%rip), %eax      # 0x8288
addl $0x1, %eax
movl %eax, 0x5681(%rip)      # 0x8288
jmp 0x2c09
jmp 0x2c0b
movl -0x18(%rbp), %ecx
movq -0x60(%rbp), %rax
movl %ecx, (%rax)
imulq $0x3e8, -0x50(%rbp), %rax # imm = 0x3E8
cvtsi2sd %rax, %xmm0
cvtsi2sdq -0x48(%rbp), %xmm1
movsd 0x23e1(%rip), %xmm2     # 0x5010
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd 0x23c9(%rip), %xmm1     # 0x5008
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x10(%rax)
movl 0x5612(%rip), %eax      # 0x8268
addl $0x1, %eax
movl %eax, 0x5609(%rip)      # 0x8268
leaq 0x55a2(%rip), %rdi      # 0x8208
callq 0x2190
leaq 0x556e(%rip), %rdi      # 0x81e0
callq 0x2210
jmp 0x2a10
nopl (%rax)