// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movl $0xffffffff, -0x18(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x14(%rbp)
movl $0x0, -0x10(%rbp)
movl $0x0, -0xc(%rbp)
leaq 0x5b71(%rip), %rdi      # 0x81e0
callq 0x2370
cmpl $0x0, 0x5bed(%rip)      # 0x8268
jne 0x2692
leaq 0x5b84(%rip), %rdi      # 0x8208
leaq 0x5b55(%rip), %rsi      # 0x81e0
callq 0x20c0
jmp 0x2674
leaq -0x28(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2130
cmpl $0x0, -0x18(%rbp)
jge 0x26bd
movl 0x5bc1(%rip), %eax      # 0x826c
movl %eax, -0x18(%rbp)
movl 0x5bb8(%rip), %eax      # 0x826c
addl $0x1, %eax
movl %eax, 0x5baf(%rip)      # 0x826c
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
cmpl $-0x2, 0x5ba3(%rip)     # 0x8270
jne 0x2700
movq 0x5ba2(%rip), %rax      # 0x8278
movslq 0x5ba3(%rip), %rcx      # 0x8280
movq (%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
movl 0x5b95(%rip), %eax      # 0x8280
addl $0x1, %eax
cltd
idivl 0x5b8f(%rip)            # 0x8284
movl %edx, 0x5b85(%rip)      # 0x8280
jmp 0x2868
cmpl $-0x1, 0x5b69(%rip)     # 0x8270
jne 0x2774
movq 0x5b68(%rip), %rax      # 0x8278
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq 0x5b5a(%rip), %rax      # 0x8278
movl 0x5b64(%rip), %ecx      # 0x8288
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rcx
movq 0x5b43(%rip), %rax      # 0x8278
movq %rcx, (%rax)
cmpl $0x1, 0x5b49(%rip)      # 0x8288
jle 0x2760
movq 0x5b30(%rip), %rdi      # 0x8278
movslq 0x5b39(%rip), %rsi      # 0x8288
movl $0x8, %edx
leaq -0x14b(%rip), %rcx      # 0x2610
callq 0x2070
movl 0x5b22(%rip), %eax      # 0x8288
addl $-0x1, %eax
movl %eax, 0x5b19(%rip)      # 0x8288
jmp 0x2866
movq 0x5b15(%rip), %rax      # 0x8290
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x38(%rbp), %rcx
movl 0x4(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rcx
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq %rcx, (%rax)
movq -0x38(%rbp), %rax
cmpl $0x1, 0x4(%rax)
jle 0x27e0
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x38(%rbp), %rax
movslq 0x4(%rax), %rsi
movl $0x8, %edx
leaq -0x1cb(%rip), %rcx      # 0x2610
callq 0x2070
movq -0x38(%rbp), %rax
movl 0x4(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x4(%rax)
movq -0x38(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jne 0x2864
movq -0x38(%rbp), %rax
movl (%rax), %eax
cmpl 0x5a6d(%rip), %eax      # 0x8270
jl 0x2864
cmpl $0x0, 0x5a7c(%rip)      # 0x8288
jle 0x2864
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl 0x5a63(%rip), %eax      # 0x8288
jge 0x2855
movq 0x5a62(%rip), %rax      # 0x8290
movl -0x3c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdx
movq 0x5a4e(%rip), %rax      # 0x8290
movslq -0x3c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x281c
movl 0x5a2d(%rip), %eax      # 0x8288
addl $-0x1, %eax
movl %eax, 0x5a24(%rip)      # 0x8288
jmp 0x2866
jmp 0x2868
imulq $0x3e8, -0x28(%rbp), %rax # imm = 0x3E8
cvtsi2sd %rax, %xmm0
cvtsi2sdq -0x20(%rbp), %xmm1
movsd 0x278d(%rip), %xmm2     # 0x5010
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd 0x2775(%rip), %xmm1     # 0x5008
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x18(%rax)
movl 0x59be(%rip), %eax      # 0x8268
addl $-0x1, %eax
movl %eax, 0x59b5(%rip)      # 0x8268
leaq 0x597e(%rip), %rdi      # 0x8238
callq 0x2190
leaq 0x591a(%rip), %rdi      # 0x81e0
callq 0x2210
movq -0x30(%rbp), %rax
movl (%rax), %edi
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rdx
leaq -0x18(%rbp), %rcx
callq 0x35e0
movq -0x30(%rbp), %rax
movl (%rax), %edi
callq 0x3c30
jmp 0x2668
nopw (%rax,%rax)