// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %r13
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %rbx
movl $0x400, %esi            # imm = 0x400
callq 0x2f21
movq %rax, %r15
testq %rax, %rax
je 0x2cfd
cmpq $0x0, 0x30(%r14)
jle 0x2b08
movq 0x18(%r14), %rdi
leaq 0x2bea(%rip), %rsi      # 0x55c9
callq 0x3b03
testl %eax, %eax
je 0x2b08
leaq 0x2bf8(%rip), %rdx      # 0x55eb
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2fcc
testq %rax, %rax
je 0x2cfd
leaq 0x2c1d(%rip), %rdx      # 0x562b
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2fcc
testq %rax, %rax
je 0x2cfd
cmpq $0x0, 0x30(%r14)
jle 0x2ab6
xorl %r13d, %r13d
leaq 0x2c2c(%rip), %rbp      # 0x5663
movq %r14, %rdi
movq %r13, %rsi
callq 0x4204
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x2fcc
testq %rax, %rax
je 0x2cfd
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x2fcc
testq %rax, %rax
je 0x2cfd
movq %r14, %rdi
movq %r13, %rsi
callq 0x41c4
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x2fcc
testq %rax, %rax
je 0x2cfd
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x2bca(%rip), %rdx      # 0x5669
callq 0x2fcc
testq %rax, %rax
je 0x2cfd
incq %r13
cmpq 0x30(%r14), %r13
jl 0x2a37
leaq 0x2fb4(%rip), %rdx      # 0x5a71
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2fcc
testq %rax, %rax
je 0x2cfd
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3051
leaq 0x302f(%rip), %rdx      # 0x5b12
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rcx
movq %r12, %r8
callq 0x32db
movl %eax, %ebp
movq %rbx, %rdi
movq %r15, %rsi
callq 0x307a
movl %ebp, %eax
jmp 0x2d0d
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
testq %r12, %r12
je 0x2d6d
leaq 0x2c47(%rip), %rbp      # 0x5769
movl $0x10, %r14d
leaq 0x60a1(%rip), %rbx      # 0x8bd0
movq %rbp, %rdi
callq 0x20b0
movq %r13, %rdi
subq %rax, %rdi
jb 0x2b51
addq %r12, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x2070
testl %eax, %eax
je 0x2b94
movq (%r14,%rbx), %rbp
addq $0x10, %r14
cmpq $0x150, %r14            # imm = 0x150
jne 0x2b2f
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x307a
leaq 0x2afa(%rip), %rdx      # 0x5673
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x31b1
movq -0x8(%r14,%rbx), %r13
testq %r13, %r13
je 0x2b62
leaq 0x2af0(%rip), %rsi      # 0x5695
movq %r12, %rdi
callq 0x2270
testq %rax, %rax
je 0x2c1f
movq %rax, %r12
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x2210
movl %eax, %ebp
movq %r12, %rdi
callq 0x21a0
movq %rax, %rbx
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2210
movq %rbx, %rcx
cmpq $-0x1, %rbx
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %r14
je 0x2c51
cmpl $-0x1, %ebp
je 0x2c51
cmpl $-0x1, %eax
je 0x2c51
cmpq $0x6400001, %rcx        # imm = 0x6400001
jl 0x2c76
movq %rbx, %rdi
movq %r15, %rsi
callq 0x307a
movq %r12, %rdi
callq 0x20a0
leaq 0x2aa0(%rip), %rdx      # 0x56bd
jmp 0x2c6b
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x307a
leaq 0x2a62(%rip), %rdx      # 0x5698
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3246
movq %rbx, %rdi
movq %r15, %rsi
callq 0x307a
movq %r12, %rdi
callq 0x20a0
leaq 0x2a41(%rip), %rdx      # 0x56ac
movq %rbx, %rdi
movq %r14, %rsi
jmp 0x2b81
movq %rcx, %rbp
leaq 0x2a60(%rip), %rdx      # 0x56e0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2fcc
testq %rax, %rax
je 0x2cf5
leaq 0x2a67(%rip), %rdx      # 0x56fe
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2fcc
testq %rax, %rax
je 0x2cf5
leaq 0x2a63(%rip), %rdx      # 0x5711
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2fcc
testq %rax, %rax
je 0x2cf5
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x2fcc
testq %rax, %rax
je 0x2cf5
movl 0x40(%r14), %r13d
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3051
movq %rbx, %rdi
movl %r13d, %esi
movq %rax, %rdx
movq %rbp, %rcx
callq 0x2d8c
testl %eax, %eax
je 0x2d31
movq %r12, %rdi
callq 0x20a0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x307a
movl $0x1, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x40(%r14), %esi
movq (%r15), %rdx
movq %rbx, %rdi
movq %rax, %rcx
callq *0x30(%rbx)
testq %rax, %rax
jle 0x2d4b
movq (%r15), %rdi
movl $0x1, %esi
movl $0x400, %edx            # imm = 0x400
movq %r12, %rcx
callq 0x2080
testq %rax, %rax
jg 0x2d1c
movq %rbx, %rdi
movq %r15, %rsi
callq 0x307a
movq %r12, %rdi
callq 0x20a0
movq %rbx, %rdi
movl $0x1, %esi
callq *0x48(%rbx)
xorl %eax, %eax
jmp 0x2d0d
leaq 0x2817(%rip), %rdi      # 0x558b
leaq 0x22d9(%rip), %rsi      # 0x5054
leaq 0x299e(%rip), %rcx      # 0x5720
movl $0x59, %edx
callq 0x20e0