// File: /workspace/github_c_makefile_star2_O2/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 0x1aa3(%rip), %xmm0     # 0x4030
movq %rsp, %rbx
movapd %xmm0, (%rbx)
leaq 0x4c3d(%rip), %r14      # 0x71d8
leaq 0x4c5e(%rip), %r15      # 0x7200
leaq 0x18(%rsp), %r12
leaq 0x4c82(%rip), %r13      # 0x7230
movq %r14, %rdi
callq 0x2360
cmpl $0x0, 0x4ca3(%rip)      # 0x7260
jne 0x25cc
movq %r15, %rdi
movq %r14, %rsi
callq 0x20d0
jmp 0x25b6
movq %r12, %rdi
xorl %esi, %esi
callq 0x2130
cmpl $0x0, (%rsp)
jns 0x25ed
movl 0x4c82(%rip), %eax      # 0x7264
movl %eax, (%rsp)
incl %eax
movl %eax, 0x4c77(%rip)      # 0x7264
incl 0x4(%rsp)
movl 0x4c71(%rip), %eax      # 0x7268
cmpl $-0x1, %eax
je 0x2628
cmpl $-0x2, %eax
jne 0x2670
movq 0x4c68(%rip), %rax      # 0x7270
movslq 0x4c69(%rip), %rcx      # 0x7278
movq (%rax,%rcx,8), %rbp
leal 0x1(%rcx), %eax
cltd
idivl 0x4c5f(%rip)            # 0x727c
movl %edx, 0x4c55(%rip)      # 0x7278
jmp 0x270f
movq 0x4c41(%rip), %rax      # 0x7270
movq (%rax), %rbp
movslq 0x4c47(%rip), %rsi      # 0x7280
movq -0x8(%rax,%rsi,8), %rcx
movq %rcx, (%rax)
cmpq $0x2, %rsi
jl 0x2663
movq 0x4c22(%rip), %rdi      # 0x7270
pushq $0x8
popq %rdx
leaq -0xee(%rip), %rcx       # 0x256a
callq 0x2080
movl 0x4c1d(%rip), %esi      # 0x7280
decl %esi
movl %esi, 0x4c15(%rip)      # 0x7280
jmp 0x270f
movq %r12, %r13
movq %rbx, %r12
movq 0x4c0b(%rip), %rax      # 0x7288
movq (%rax), %rbx
movq 0x8(%rbx), %rax
movq (%rax), %rbp
movslq 0x4(%rbx), %rsi
movq -0x8(%rax,%rsi,8), %rcx
movq %rcx, (%rax)
cmpq $0x2, %rsi
jl 0x26af
movq 0x8(%rbx), %rdi
pushq $0x8
popq %rdx
leaq -0x13d(%rip), %rcx      # 0x256a
callq 0x2080
movl 0x4(%rbx), %esi
decl %esi
movl %esi, 0x4(%rbx)
jne 0x2702
movl (%rbx), %eax
cmpl 0x4baa(%rip), %eax      # 0x7268
setge %cl
movl 0x4bb9(%rip), %eax      # 0x7280
testl %eax, %eax
setg %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x2702
movl %eax, %eax
xorl %ecx, %ecx
movq %r12, %rbx
movq %r13, %r12
leaq 0x4b4c(%rip), %r13      # 0x7230
cmpq %rcx, %rax
je 0x2777
movq 0x4b94(%rip), %rdx      # 0x7288
movq 0x8(%rdx,%rcx,8), %rsi
movq %rsi, (%rdx,%rcx,8)
incq %rcx
jmp 0x26e4
movq %r12, %rbx
movq %r13, %r12
leaq 0x4b21(%rip), %r13      # 0x7230
imulq $0x3e8, 0x18(%rsp), %rax # imm = 0x3E8
cvtsi2sd %rax, %xmm0
cvtsi2sdq 0x20(%rsp), %xmm1
divsd 0x18f4(%rip), %xmm1     # 0x4020
addsd %xmm0, %xmm1
addsd 0x18f0(%rip), %xmm1     # 0x4028
cvttsd2si %xmm1, %rax
movq %rax, 0x18(%rbp)
decl 0x4b19(%rip)            # 0x7260
movq %r13, %rdi
callq 0x2170
movq %r14, %rdi
callq 0x21f0
movl (%rbp), %edi
movq 0x10(%rbp), %rsi
movq 0x18(%rbp), %rdx
movq %rbx, %rcx
callq 0x3158
movl (%rbp), %edi
callq 0x3566
jmp 0x25ae
decl %eax
movl %eax, 0x4b01(%rip)      # 0x7280
jmp 0x270f