// File: /workspace/github_c_makefile_star2_O0/howerj[P]eweb/main.c
// Function: send_file_response
static int send_file_response(eweb_os_t *w, eweb_os_hit_args_t *args, const char *path, const char *request_body, long path_length) {
	assert(w);
	UNUSED(request_body);
	FILE *file = NULL;
	string_t *response = string_new(w, FILE_CHUNK_SIZE);
	if (!response)
		goto fail;

	if (args->form_value_counter > 0 && eweb_string_matches_value(args->content_type, "application/x-www-form-urlencoded")) {
		if (!string_add(w, response, "<!DOCTYPE html>\n<html><head><title>Response Page</title></head>"))
			goto fail;
		if (!string_add(w, response, "<body><h1>Thanks...</h1>You sent these values<br/><br/>"))
			goto fail;

		for (long v = 0; v < args->form_value_counter; v++) {
			if (!string_add(w, response, eweb_form_name(args, v)))
				goto fail;
			if (!string_add(w, response, ": <b>"))
				goto fail;
			if (!string_add(w, response, eweb_form_value(args, v)))
				goto fail;
			if (!string_add(w, response, "</b><br/>"))
				goto fail;
		}

		if (!string_add(w, response, "</body></html>"))
			goto fail;
		const int r = eweb_ok_200(w, args, "\nContent-Type: text/html", string_chars(w, response), path);
		string_free(w, response);
		return r;
	}
	/* work out the file type and check we support it */
	const char *content_type = find_file_type_by_file_extension(path, path_length);
	if (!content_type) {
		string_free(w, response);
		return eweb_forbidden_403(w, args, "file extension type not supported");
	}

	file = fopen(path, "rb");
	if (!file) {
		string_free(w, response);
		return eweb_not_found_404(w, args, "failed to open file");
	}

	const int r1 = fseek(file, 0, SEEK_END);
	long len = ftell(file);
	const int r2 = fseek(file, 0, SEEK_SET);
	if (r1 == -1 || r2 == -1 || len == -1) {
		string_free(w, response);
		fclose(file);
		return eweb_forbidden_403(w, args, "seek/tell failed");
	}

	if (len > BIGGEST_FILE) {
		string_free(w, response);
		fclose(file);
		return eweb_forbidden_403(w, args, "files this large are not supported");
	}

	if (!string_add(w, response, "HTTP/1.1 200 OK\nServer: eweb\n"))
		goto fail;
	if (!string_add(w, response, "Connection: close\n"))
		goto fail;
	if (!string_add(w, response, "Content-Type: "))
		goto fail;
	if (!string_add(w, response, content_type))
		goto fail;
	if (eweb_write_header(w, args->socketfd, string_chars(w, response), len) != EWEB_OK)
		goto fail;

	/* send file in blocks */
	while ((len = fread(response->ptr, 1, FILE_CHUNK_SIZE, file)) > 0) {
		if (w->write(w, args->socketfd, response->ptr, len) <= 0)
			break;
	}
	string_free(w, response);
	fclose(file);

	w->sleep(w, 1); /* allow socket to drain before closing */
	return EWEB_OK;
fail:
	if (file)
		fclose(file);
	string_free(w, response);
	return EWEB_ERROR;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x2d08
jmp 0x2d27
leaq 0x4826(%rip), %rdi      # 0x7535
leaq 0x4342(%rip), %rsi      # 0x7058
movl $0x91, %edx
leaq 0x4908(%rip), %rcx      # 0x762a
callq 0x2100
movq $0x0, -0x38(%rbp)
movq -0x10(%rbp), %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x34a0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x2d4d
jmp 0x3199
movq -0x18(%rbp), %rax
cmpq $0x0, 0x30(%rax)
jle 0x2f1c
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
leaq 0x491b(%rip), %rsi      # 0x7686
callq 0x4760
cmpl $0x0, %eax
je 0x2f1c
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
leaq 0x4920(%rip), %rdx      # 0x76a8
callq 0x3540
cmpq $0x0, %rax
jne 0x2d98
jmp 0x3199
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
leaq 0x4941(%rip), %rdx      # 0x76e8
callq 0x3540
cmpq $0x0, %rax
jne 0x2db7
jmp 0x3199
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x30(%rcx), %rax
jge 0x2e9e
movq -0x10(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x5210
movq -0x80(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq %rax, %rdx
callq 0x3540
cmpq $0x0, %rax
jne 0x2e09
jmp 0x3199
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
leaq 0x4908(%rip), %rdx      # 0x7720
callq 0x3540
cmpq $0x0, %rax
jne 0x2e28
jmp 0x3199
movq -0x10(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x5190
movq -0x90(%rbp), %rdi
movq -0x88(%rbp), %rsi
movq %rax, %rdx
callq 0x3540
cmpq $0x0, %rax
jne 0x2e6c
jmp 0x3199
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
leaq 0x48ab(%rip), %rdx      # 0x7726
callq 0x3540
cmpq $0x0, %rax
jne 0x2e8b
jmp 0x3199
jmp 0x2e8d
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x2dbf
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
leaq 0x4c81(%rip), %rdx      # 0x7b2e
callq 0x3540
cmpq $0x0, %rax
jne 0x2ebd
jmp 0x3199
movq -0x10(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x3610
movq -0xa0(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq %rax, %rcx
movq -0x20(%rbp), %r8
leaq 0x4cd3(%rip), %rdx      # 0x7bcf
callq 0x3960
movl %eax, -0x4c(%rbp)
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x3660
movl -0x4c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x31bd
movq -0x20(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x31d0
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x2f5d
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x3660
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq 0x47e0(%rip), %rdx      # 0x7730
callq 0x37e0
movl %eax, -0x4(%rbp)
jmp 0x31bd
movq -0x20(%rbp), %rdi
leaq 0x47ea(%rip), %rsi      # 0x7752
callq 0x22a0
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x2fa1
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x3660
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq 0x47c1(%rip), %rdx      # 0x7755
callq 0x38a0
movl %eax, -0x4(%rbp)
jmp 0x31bd
movq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x2240
movl %eax, -0x5c(%rbp)
movq -0x38(%rbp), %rdi
callq 0x21c0
movq %rax, -0x68(%rbp)
movq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x2240
movl %eax, -0x6c(%rbp)
cmpl $-0x1, -0x5c(%rbp)
je 0x2fe8
cmpl $-0x1, -0x6c(%rbp)
je 0x2fe8
cmpq $-0x1, -0x68(%rbp)
jne 0x301a
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x3660
movq -0x38(%rbp), %rdi
callq 0x20a0
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq 0x475c(%rip), %rdx      # 0x7769
callq 0x37e0
movl %eax, -0x4(%rbp)
jmp 0x31bd
cmpq $0x6400000, -0x68(%rbp) # imm = 0x6400000
jle 0x3056
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x3660
movq -0x38(%rbp), %rdi
callq 0x20a0
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq 0x4731(%rip), %rdx      # 0x777a
callq 0x37e0
movl %eax, -0x4(%rbp)
jmp 0x31bd
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
leaq 0x4738(%rip), %rdx      # 0x779d
callq 0x3540
cmpq $0x0, %rax
jne 0x3075
jmp 0x3199
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
leaq 0x4737(%rip), %rdx      # 0x77bb
callq 0x3540
cmpq $0x0, %rax
jne 0x3094
jmp 0x3199
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
leaq 0x472b(%rip), %rdx      # 0x77ce
callq 0x3540
cmpq $0x0, %rax
jne 0x30b3
jmp 0x3199
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x58(%rbp), %rdx
callq 0x3540
cmpq $0x0, %rax
jne 0x30cf
jmp 0x3199
movq -0x10(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x18(%rbp), %rax
movl 0x40(%rax), %eax
movl %eax, -0xa4(%rbp)
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x3610
movq -0xb0(%rbp), %rdi
movl -0xa4(%rbp), %esi
movq %rax, %rdx
movq -0x68(%rbp), %rcx
callq 0x32d0
cmpl $0x0, %eax
je 0x3117
jmp 0x3199
jmp 0x3119
movq -0x40(%rbp), %rax
movq (%rax), %rdi
movq -0x38(%rbp), %rcx
movl $0x1, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x2080
movq %rax, -0x68(%rbp)
cmpq $0x0, %rax
jle 0x3167
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rcx
movl 0x40(%rcx), %esi
movq -0x40(%rbp), %rcx
movq (%rcx), %rdx
movq -0x68(%rbp), %rcx
callq *%rax
cmpq $0x0, %rax
jg 0x3165
jmp 0x3167
jmp 0x3119
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x3660
movq -0x38(%rbp), %rdi
callq 0x20a0
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq *%rax
movl $0x0, -0x4(%rbp)
jmp 0x31bd
cmpq $0x0, -0x38(%rbp)
je 0x31a9
movq -0x38(%rbp), %rdi
callq 0x20a0
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x3660
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax)