// File: /workspace/github_c_makefile_star2_O1/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 0x19f9(%rip), %xmm0     # 0x4030
leaq 0x8(%rsp), %rbx
movupd %xmm0, (%rbx)
leaq 0x4b89(%rip), %r14      # 0x71d0
leaq 0x4baa(%rip), %r15      # 0x71f8
leaq 0x18(%rsp), %r12
leaq 0x4bce(%rip), %rbp      # 0x7228
movq %r14, %rdi
callq 0x2350
cmpl $0x0, 0x4bef(%rip)      # 0x7258
jne 0x2678
movq %r15, %rdi
movq %r14, %rsi
callq 0x20d0
jmp 0x2662
movq %r12, %rdi
xorl %esi, %esi
callq 0x2130
cmpl $0x0, 0x8(%rsp)
jns 0x269b
movl 0x4bcd(%rip), %eax      # 0x725c
movl %eax, 0x8(%rsp)
incl %eax
movl %eax, 0x4bc1(%rip)      # 0x725c
incl 0xc(%rsp)
movl 0x4bbb(%rip), %eax      # 0x7260
cmpl $-0x1, %eax
je 0x26d6
cmpl $-0x2, %eax
jne 0x2718
movq 0x4bb2(%rip), %rax      # 0x7268
movslq 0x4bb3(%rip), %rcx      # 0x7270
movq (%rax,%rcx,8), %r13
leal 0x1(%rcx), %eax
cltd
idivl 0x4ba9(%rip)            # 0x7274
movl %edx, 0x4b9f(%rip)      # 0x7270
jmp 0x27b2
movq 0x4b8b(%rip), %rax      # 0x7268
movq (%rax), %r13
movslq 0x4b91(%rip), %rsi      # 0x7278
movq -0x8(%rax,%rsi,8), %rcx
movq %rcx, (%rax)
cmpq $0x2, %rsi
jl 0x270d
movq 0x4b6c(%rip), %rdi      # 0x7268
movl $0x8, %edx
leaq -0xf4(%rip), %rcx       # 0x2614
callq 0x2080
decl 0x4b65(%rip)            # 0x7278
jmp 0x27b2
movq %r12, %rbp
movq %rbx, %r12
movq 0x4b5b(%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 0x2756
movq 0x8(%rbx), %rdi
movl $0x8, %edx
leaq -0x13d(%rip), %rcx      # 0x2614
callq 0x2080
decl 0x4(%rbx)
jne 0x27a5
movl (%rbx), %eax
cmpl 0x4afd(%rip), %eax      # 0x7260
setge %cl
movl 0x4b0c(%rip), %eax      # 0x7278
testl %eax, %eax
setg %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x27a5
movl %eax, %eax
xorl %ecx, %ecx
movq %r12, %rbx
movq 0x4afa(%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 0x277f
decl %eax
movl %eax, 0x4ad5(%rip)      # 0x7278
jmp 0x27a8
movq %r12, %rbx
movq %rbp, %r12
leaq 0x4a76(%rip), %rbp      # 0x7228
imulq $0x3e8, 0x18(%rsp), %rax # imm = 0x3E8
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
xorps %xmm1, %xmm1
cvtsi2sdq 0x20(%rsp), %xmm1
divsd 0x184b(%rip), %xmm1     # 0x4020
addsd %xmm0, %xmm1
addsd 0x1847(%rip), %xmm1     # 0x4028
cvttsd2si %xmm1, %rax
movq %rax, 0x18(%r13)
decl 0x4a68(%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 0x3210
movl (%r13), %edi
callq 0x361e
jmp 0x265a