// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x4080, %rsp           # imm = 0x4080
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
leaq -0x4050(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2130
movq -0x10(%rbp), %rdi
leaq -0x2040(%rbp), %rsi
callq 0x3050
movq -0x10(%rbp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x3b00
movl %eax, -0x34(%rbp)
movslq -0x14(%rbp), %rsi
movl -0x34(%rbp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x1, %edx
movl $0x2, %ecx
movq %r9, %rdi
callq 0x3d70
movq %rax, -0x40(%rbp)
movl -0x34(%rbp), %edi
callq 0x3c30
leaq -0x4060(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2130
movq -0x4060(%rbp), %rax
subq -0x4050(%rbp), %rax
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
cvtsi2sd %rax, %xmm0
movq -0x4058(%rbp), %rax
subq -0x4048(%rbp), %rax
cvtsi2sd %rax, %xmm1
movsd 0x1c7f(%rip), %xmm2     # 0x5010
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd 0x1c67(%rip), %xmm1     # 0x5008
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x4064(%rbp)
imulq $0x3e8, -0x4060(%rbp), %rax # imm = 0x3E8
cvtsi2sd %rax, %xmm0
cvtsi2sdq -0x4058(%rbp), %xmm1
movsd 0x1c40(%rip), %xmm2     # 0x5010
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd 0x1c28(%rip), %xmm1     # 0x5008
addsd %xmm1, %xmm0
cvtsi2sdq -0x20(%rbp), %xmm1
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x4068(%rbp)
leaq -0x4040(%rbp), %rdi
leaq 0x1d6d(%rip), %rsi      # 0x5173
movb $0x0, %al
callq 0x22f0
leaq -0x4040(%rbp), %rdi
leaq -0x4040(%rbp), %rdx
leaq 0x1d63(%rip), %rsi      # 0x5185
movb $0x0, %al
callq 0x22f0
leaq -0x4040(%rbp), %rdi
leaq -0x4040(%rbp), %rdx
movq -0x20(%rbp), %rcx
leaq 0x1d60(%rip), %rsi      # 0x51a2
movb $0x0, %al
callq 0x22f0
leaq -0x4040(%rbp), %rdi
leaq -0x4040(%rbp), %rdx
movq -0x28(%rbp), %rax
subq -0x20(%rbp), %rax
movl %eax, %ecx
leaq 0x1d55(%rip), %rsi      # 0x51bd
movb $0x0, %al
callq 0x22f0
leaq -0x4040(%rbp), %rdi
leaq -0x4040(%rbp), %rdx
movl -0x4064(%rbp), %ecx
leaq 0x1dc7(%rip), %rsi      # 0x5251
movb $0x0, %al
callq 0x22f0
leaq -0x4040(%rbp), %rdi
leaq -0x4040(%rbp), %rdx
movl -0x4068(%rbp), %ecx
leaq 0x1dbc(%rip), %rsi      # 0x5268
movb $0x0, %al
callq 0x22f0
leaq -0x4040(%rbp), %rdi
leaq -0x4040(%rbp), %rdx
movq -0x30(%rbp), %rax
movl (%rax), %ecx
leaq 0x1d0a(%rip), %rsi      # 0x51d8
movb $0x0, %al
callq 0x22f0
leaq -0x4040(%rbp), %rdi
leaq -0x4040(%rbp), %rdx
movq -0x30(%rbp), %rax
movl 0x4(%rax), %ecx
leaq 0x1cff(%rip), %rsi      # 0x51f0
movb $0x0, %al
callq 0x22f0
leaq -0x4040(%rbp), %rdi
leaq -0x4040(%rbp), %rdx
movq -0x30(%rbp), %rax
movl 0x8(%rax), %ecx
leaq 0x1cf7(%rip), %rsi      # 0x520b
movb $0x0, %al
callq 0x22f0
leaq -0x4040(%rbp), %rdi
leaq -0x4040(%rbp), %rdx
movq -0x30(%rbp), %rax
movl 0xc(%rax), %ecx
leaq 0x1cf0(%rip), %rsi      # 0x5227
movb $0x0, %al
callq 0x22f0
leaq -0x4040(%rbp), %rdi
leaq -0x4040(%rbp), %rdx
movl -0x14(%rbp), %ecx
leaq 0x1d2d(%rip), %rsi      # 0x5283
movb $0x0, %al
callq 0x22f0
leaq -0x4040(%rbp), %rdi
leaq -0x4040(%rbp), %rdx
leaq -0x2040(%rbp), %rcx
leaq 0x1d21(%rip), %rsi      # 0x529a
movb $0x0, %al
callq 0x22f0
movl -0x4(%rbp), %eax
movl %eax, -0x4074(%rbp)
leaq -0x4040(%rbp), %rax
movq %rax, -0x4070(%rbp)
leaq -0x4040(%rbp), %rdi
callq 0x20d0
movl -0x4074(%rbp), %edi
movq -0x4070(%rbp), %rsi
movq %rax, %rdx
callq 0x4470
movl -0x4(%rbp), %edi
movq -0x40(%rbp), %rsi
movslq -0x14(%rbp), %rdx
callq 0x4470
movq -0x40(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x3dd0
addq $0x4080, %rsp           # imm = 0x4080
popq %rbp
retq
nop