// File: /workspace/github_c_makefile_star2_O3/somethingnew2-0[P]CS537-P4/request.c
// Function: requestServeStatic
void requestServeStatic(int fd, char *filename, int filesize, long arrival, long dispatch, thread *worker) 
{
   int srcfd;
   char *srcp, filetype[MAXLINE], buf[MAXBUF];
   struct timeval beforeread, afterread;
   int read, complete;

	gettimeofday(&beforeread, NULL);

   requestGetFiletype(filename, filetype);

   srcfd = Open(filename, O_RDONLY, 0);

   // Rather than call read() to read the file into memory, 
   // which would require that we allocate a buffer, we memory-map the file
   srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0);
   Close(srcfd);

	gettimeofday(&afterread, NULL);

	read = ((afterread.tv_sec - beforeread.tv_sec) * 1000 + (afterread.tv_usec - beforeread.tv_usec)/1000.0) + 0.5;
	complete = (((afterread.tv_sec) * 1000 + (afterread.tv_usec)/1000.0) + 0.5) - arrival;

   // put together response
   sprintf(buf, "HTTP/1.0 200 OK\r\n");
   sprintf(buf, "%sServer: CS537 Web Server\r\n", buf);

	 // CS537: Your statistics go here -- fill in the 0's with something useful!
	 sprintf(buf, "%s Stat-req-arrival: %ld\r\n", buf, arrival);
	 sprintf(buf, "%s Stat-req-dispatch: %d\r\n", buf, (int)(dispatch - arrival));
	 sprintf(buf, "%s Stat-req-read: %d\r\n", buf, read);
	 sprintf(buf, "%s Stat-req-complete: %d\r\n", buf, complete); 
	 sprintf(buf, "%s Stat-thread-id: %d\r\n", buf, worker->id);
	 sprintf(buf, "%s Stat-thread-count: %d\r\n", buf, worker->count);
	 sprintf(buf, "%s Stat-thread-static: %d\r\n", buf, worker->statics);
	 sprintf(buf, "%s Stat-thread-dynamic: %d\r\n", buf, worker->dynamics);

   sprintf(buf, "%sContent-Length: %d\r\n", buf, filesize);
   sprintf(buf, "%sContent-Type: %s\r\n\r\n", buf, filetype);

   Rio_writen(fd, buf, strlen(buf));

   //  Writes out to the client socket the memory-mapped file 
   Rio_writen(fd, srcp, filesize);
   Munmap(srcp, filesize);

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4048, %rsp           # imm = 0x4048
movq %r9, %r13
movq %r8, %r15
movq %rcx, %rbx
movl %edx, %ebp
movq %rsi, %r12
movl %edi, 0xc(%rsp)
leaq 0x30(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x2130
leaq 0x40(%rsp), %rsi
movq %r12, %rdi
callq 0x2dce
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x35d5
movl %eax, %r12d
movslq %ebp, %rsi
movq %rsi, 0x10(%rsp)
xorl %edi, %edi
movl $0x1, %edx
movl $0x2, %ecx
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x36b2
movq %rax, 0x18(%rsp)
movl %r12d, %edi
callq 0x363a
leaq 0x20(%rsp), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x2130
movq (%r12), %rax
imulq $0x3e8, %rax, %rcx      # imm = 0x3E8
subq (%r14), %rax
movq 0x8(%r12), %rdx
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
cvtsi2sd %rax, %xmm0
cvtsi2sd %rdx, %xmm1
subq 0x8(%r14), %rdx
cvtsi2sd %rdx, %xmm2
movsd 0xf9b(%rip), %xmm3      # 0x4020
divsd %xmm3, %xmm2
addsd %xmm0, %xmm2
movsd 0xf93(%rip), %xmm0      # 0x4028
addsd %xmm0, %xmm2
cvttsd2si %xmm2, %r12d
xorps %xmm2, %xmm2
cvtsi2sd %rcx, %xmm2
divsd %xmm3, %xmm1
addsd %xmm2, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %rbx, %xmm2
addsd %xmm0, %xmm1
subsd %xmm2, %xmm1
cvttsd2si %xmm1, %r14d
movups 0x10b6(%rip), %xmm0     # 0x4180
leaq 0x2040(%rsp), %rbp
movaps %xmm0, (%rbp)
movw $0xa, 0x10(%rbp)
leaq 0x10af(%rip), %rsi      # 0x4192
movq %rbp, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x22c0
leaq 0x10b8(%rip), %rsi      # 0x41af
movq %rbp, %rdi
movq %rbp, %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x22c0
subl %ebx, %r15d
leaq 0x10b9(%rip), %rsi      # 0x41ca
movq %rbp, %rdi
movq %rbp, %rdx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x22c0
leaq 0x1136(%rip), %rsi      # 0x425e
movq %rbp, %rdi
movq %rbp, %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x22c0
leaq 0x1136(%rip), %rsi      # 0x4275
movq %rbp, %rdi
movq %rbp, %rdx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x22c0
movl (%r13), %ecx
leaq 0x108b(%rip), %rsi      # 0x41e5
movq %rbp, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x22c0
movl 0x4(%r13), %ecx
leaq 0x108b(%rip), %rsi      # 0x41fd
movq %rbp, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x22c0
movl 0x8(%r13), %ecx
leaq 0x108e(%rip), %rsi      # 0x4218
movq %rbp, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x22c0
movl 0xc(%r13), %ecx
leaq 0x1092(%rip), %rsi      # 0x4234
movq %rbp, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x22c0
leaq 0x10da(%rip), %rsi      # 0x4290
movq %rbp, %rdi
movq %rbp, %rdx
movq 0x10(%rsp), %r14
movl %r14d, %ecx
xorl %eax, %eax
callq 0x22c0
leaq 0x10d5(%rip), %rsi      # 0x42a7
movq %rbp, %rdi
movq %rbp, %rdx
leaq 0x40(%rsp), %rcx
xorl %eax, %eax
callq 0x22c0
movq %rbp, %rdi
callq 0x20e0
movl 0xc(%rsp), %ebx
movl %ebx, %edi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x3a07
movl %ebx, %edi
movq 0x18(%rsp), %r15
movq %r15, %rsi
movq %r14, %rdx
callq 0x3a07
movq %r15, %rdi
movq %r14, %rsi
callq 0x36cc
addq $0x4048, %rsp           # imm = 0x4048
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq