// File: /workspace/github_c_makefile_star2_O2/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, %r14
movl %edi, 0xc(%rsp)
leaq 0x30(%rsp), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x2130
leaq 0x40(%rsp), %rsi
movq %r14, %rdi
callq 0x2d0a
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3501
movl %eax, %r14d
movslq %ebp, %rsi
movq %rsi, 0x10(%rsp)
pushq $0x1
popq %rdx
pushq $0x2
popq %rcx
xorl %edi, %edi
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x35de
movq %rax, 0x18(%rsp)
movl %r14d, %edi
callq 0x3566
leaq 0x20(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x2130
movq (%r14), %rax
imulq $0x3e8, %rax, %rcx      # imm = 0x3E8
subq (%r12), %rax
movq 0x8(%r14), %rdx
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
cvtsi2sd %rax, %xmm0
cvtsi2sd %rdx, %xmm1
subq 0x8(%r12), %rdx
cvtsi2sd %rdx, %xmm2
movsd 0x1069(%rip), %xmm3     # 0x4020
divsd %xmm3, %xmm2
addsd %xmm0, %xmm2
movsd 0x1061(%rip), %xmm0     # 0x4028
addsd %xmm0, %xmm2
cvttsd2si %xmm2, %r12d
cvtsi2sd %rcx, %xmm2
divsd %xmm3, %xmm1
addsd %xmm2, %xmm1
cvtsi2sd %rbx, %xmm2
addsd %xmm0, %xmm1
subsd %xmm2, %xmm1
cvttsd2si %xmm1, %r14d
movups 0x118a(%rip), %xmm0     # 0x4180
leaq 0x2040(%rsp), %rbp
movaps %xmm0, (%rbp)
movw $0xa, 0x10(%rbp)
leaq 0x1183(%rip), %rsi      # 0x4192
movq %rbp, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x22d0
leaq 0x118c(%rip), %rsi      # 0x41af
movq %rbp, %rdi
movq %rbp, %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x22d0
subl %ebx, %r15d
leaq 0x118d(%rip), %rsi      # 0x41ca
movq %rbp, %rdi
movq %rbp, %rdx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x22d0
leaq 0x120a(%rip), %rsi      # 0x425e
movq %rbp, %rdi
movq %rbp, %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x22d0
leaq 0x120a(%rip), %rsi      # 0x4275
movq %rbp, %rdi
movq %rbp, %rdx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x22d0
movl (%r13), %ecx
leaq 0x115f(%rip), %rsi      # 0x41e5
movq %rbp, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x22d0
movl 0x4(%r13), %ecx
leaq 0x115f(%rip), %rsi      # 0x41fd
movq %rbp, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x22d0
movl 0x8(%r13), %ecx
leaq 0x1162(%rip), %rsi      # 0x4218
movq %rbp, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x22d0
movl 0xc(%r13), %ecx
leaq 0x1166(%rip), %rsi      # 0x4234
movq %rbp, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x22d0
leaq 0x11ae(%rip), %rsi      # 0x4290
movq %rbp, %rdi
movq %rbp, %rdx
movq 0x10(%rsp), %r14
movl %r14d, %ecx
xorl %eax, %eax
callq 0x22d0
leaq 0x11a9(%rip), %rsi      # 0x42a7
movq %rbp, %rdi
movq %rbp, %rdx
leaq 0x40(%rsp), %rcx
xorl %eax, %eax
callq 0x22d0
movq %rbp, %rdi
callq 0x20e0
movl 0xc(%rsp), %ebx
movl %ebx, %edi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x3929
movl %ebx, %edi
movq 0x18(%rsp), %r15
movq %r15, %rsi
movq %r14, %rdx
callq 0x3929
movq %r15, %rdi
movq %r14, %rsi
callq 0x35f8
addq $0x4048, %rsp           # imm = 0x4048
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq