// File: /workspace/github_c_makefile_star2_O3/somethingnew2-0[P]CS537-P4/server.c
// Function: consumer
void *consumer(void *arg) {
	thread worker;
	worker.id = -1;
	worker.count = 0;
	worker.statics = 0;
	worker.dynamics = 0;
	struct timeval dispatch;
	while(1) {
		pthread_mutex_lock(&lock);
		while(numfull == 0) {
			pthread_cond_wait(&fill, &lock);
		}

		gettimeofday(&dispatch, NULL);

		if(worker.id < 0) {
			worker.id = threadid;
			threadid++;
		}
		worker.count++;

		request *req;

		//FIFO
		if(algorithm == -2) {
			req = (request *)buffer[useptr];
			useptr = (useptr + 1) % max;
		}
		//SFF
		else if(algorithm == -1) {
			req = (request *)buffer[0];
			buffer[0] = buffer[fillptr - 1];
			if(fillptr > 1) {
				qsort(buffer, fillptr, sizeof(*buffer), requestcmp);
			}
			fillptr--;
		}
		//SFF-BD
		else {
			epoch *epo = (epoch *)epochs[0];
			req = (request *)epo->buffer[0];
			epo->buffer[0] = epo->buffer[epo->fillptr - 1];
			if(epo->fillptr > 1) {
				qsort(epo->buffer, epo->fillptr, sizeof(*epo->buffer), requestcmp);
			}
			epo->fillptr--;

			// Finished epoch
			if(epo->fillptr == 0 && epo->requests >= algorithm && fillptr > 0) {
				int i = 0;
				for(i = 0; i < fillptr; i++) {
					epochs[i] = epochs[i + 1];
				}
				fillptr--;
			}			
		}
		req->dispatch = ((dispatch.tv_sec) * 1000 + dispatch.tv_usec/1000.0) + 0.5;
	 	numfull--;

		pthread_cond_signal(&empty);
		pthread_mutex_unlock(&lock);

		requestHandle(req->fd, req->arrival, req->dispatch, &worker);
		Close(req->fd);

	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movss 0x19ed(%rip), %xmm0     # 0x4030
movq %rsp, %rbx
movapd %xmm0, (%rbx)
leaq 0x4b7f(%rip), %r14      # 0x71d0
leaq 0x4ba0(%rip), %r15      # 0x71f8
leaq 0x18(%rsp), %r12
leaq 0x4bc4(%rip), %rbp      # 0x7228
movq %r14, %rdi
callq 0x2350
cmpl $0x0, 0x4be5(%rip)      # 0x7258
jne 0x2682
movq %r15, %rdi
movq %r14, %rsi
callq 0x20d0
jmp 0x266c
movq %r12, %rdi
xorl %esi, %esi
callq 0x2130
cmpl $0x0, (%rsp)
jns 0x26a3
movl 0x4bc4(%rip), %eax      # 0x725c
movl %eax, (%rsp)
incl %eax
movl %eax, 0x4bb9(%rip)      # 0x725c
incl 0x4(%rsp)
movl 0x4bb3(%rip), %eax      # 0x7260
cmpl $-0x1, %eax
je 0x26de
cmpl $-0x2, %eax
jne 0x2728
movq 0x4baa(%rip), %rax      # 0x7268
movslq 0x4bab(%rip), %rcx      # 0x7270
movq (%rax,%rcx,8), %r13
leal 0x1(%rcx), %eax
cltd
idivl 0x4ba1(%rip)            # 0x7274
movl %edx, 0x4b97(%rip)      # 0x7270
jmp 0x27c7
movq 0x4b83(%rip), %rax      # 0x7268
movq (%rax), %r13
movslq 0x4b89(%rip), %rsi      # 0x7278
movq -0x8(%rax,%rsi,8), %rcx
movq %rcx, (%rax)
cmpq $0x2, %rsi
jl 0x271b
movq 0x4b64(%rip), %rdi      # 0x7268
movl $0x8, %edx
leaq -0xf0(%rip), %rcx       # 0x2620
callq 0x2080
movl 0x4b5d(%rip), %esi      # 0x7278
decl %esi
movl %esi, 0x4b55(%rip)      # 0x7278
jmp 0x27c7
movq %r12, %rbp
movq %rbx, %r12
movq 0x4b4b(%rip), %rax      # 0x7280
movq (%rax), %rbx
movq 0x8(%rbx), %rax
movq (%rax), %r13
movslq 0x4(%rbx), %rsi
movq -0x8(%rax,%rsi,8), %rcx
movq %rcx, (%rax)
cmpq $0x2, %rsi
jl 0x2769
movq 0x8(%rbx), %rdi
movl $0x8, %edx
leaq -0x141(%rip), %rcx      # 0x2620
callq 0x2080
movl 0x4(%rbx), %esi
decl %esi
movl %esi, 0x4(%rbx)
jne 0x27ba
movl (%rbx), %eax
cmpl 0x4ae8(%rip), %eax      # 0x7260
setge %cl
movl 0x4af7(%rip), %eax      # 0x7278
testl %eax, %eax
setg %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x27ba
movl %eax, %eax
xorl %ecx, %ecx
movq %r12, %rbx
movq 0x4ae5(%rip), %rdx      # 0x7280
movq 0x8(%rdx,%rcx,8), %rsi
movq %rsi, (%rdx,%rcx,8)
leaq 0x1(%rcx), %rdx
movq %rdx, %rcx
cmpq %rdx, %rax
jne 0x2794
decl %eax
movl %eax, 0x4ac0(%rip)      # 0x7278
jmp 0x27bd
movq %r12, %rbx
movq %rbp, %r12
leaq 0x4a61(%rip), %rbp      # 0x7228
imulq $0x3e8, 0x18(%rsp), %rax # imm = 0x3E8
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
xorps %xmm1, %xmm1
cvtsi2sdq 0x20(%rsp), %xmm1
divsd 0x1836(%rip), %xmm1     # 0x4020
addsd %xmm0, %xmm1
addsd 0x1832(%rip), %xmm1     # 0x4028
cvttsd2si %xmm1, %rax
movq %rax, 0x18(%r13)
decl 0x4a53(%rip)            # 0x7258
movq %rbp, %rdi
callq 0x2170
movq %r14, %rdi
callq 0x21f0
movl (%r13), %edi
movq 0x10(%r13), %rsi
movq 0x18(%r13), %rdx
movq %rbx, %rcx
callq 0x322c
movl (%r13), %edi
callq 0x363a
jmp 0x2664