// File: /workspace/github_c_makefile_star2_O2/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 0x2e76
movq %rax, %r15
testq %rax, %rax
je 0x2c96
cmpq $0x0, 0x30(%r14)
jle 0x2a3f
movq 0x18(%r14), %rdi
leaq 0x2c5b(%rip), %rsi      # 0x55c9
callq 0x3bb3
testl %eax, %eax
je 0x2a3f
leaq 0x2c69(%rip), %rdx      # 0x55eb
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2f18
testq %rax, %rax
je 0x2c96
leaq 0x2c8e(%rip), %rdx      # 0x562b
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2f18
testq %rax, %rax
je 0x2c96
xorl %r13d, %r13d
leaq 0x2ca8(%rip), %rbp      # 0x5663
cmpq 0x30(%r14), %r13
jge 0x2bbd
movq %r14, %rdi
movq %r13, %rsi
callq 0x3ff9
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x2f18
testq %rax, %rax
je 0x2c96
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x2f18
testq %rax, %rax
je 0x2c96
movq %r14, %rdi
movq %r13, %rsi
callq 0x3fb9
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x2f18
testq %rax, %rax
je 0x2c96
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x2c3c(%rip), %rdx      # 0x5669
callq 0x2f18
incq %r13
testq %rax, %rax
jne 0x29bb
jmp 0x2c96
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
testq %r12, %r12
je 0x2d04
pushq $0x8
popq %r14
leaq 0x6173(%rip), %rbx      # 0x8bd0
cmpq $0x148, %r14            # imm = 0x148
je 0x2b37
movq -0x8(%r14,%rbx), %rbp
movq %rbp, %rdi
callq 0x20b0
movq %r13, %rdi
subq %rax, %rdi
jb 0x2a91
addq %r12, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x2070
testl %eax, %eax
je 0x2a97
addq $0x10, %r14
jmp 0x2a5d
movq (%r14,%rbx), %r13
testq %r13, %r13
je 0x2b37
leaq 0x2bea(%rip), %rsi      # 0x5695
movq %r12, %rdi
callq 0x2270
testq %rax, %rax
je 0x2b69
movq %rax, %r12
pushq $0x2
popq %rdx
movq %rax, %rdi
xorl %esi, %esi
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 0x2b9b
cmpl $-0x1, %ebp
je 0x2b9b
cmpl $-0x1, %eax
je 0x2b9b
cmpq $0x6400001, %rcx        # imm = 0x6400001
jl 0x2c0f
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2fc6
movq %r12, %rdi
callq 0x20a0
leaq 0x2b88(%rip), %rdx      # 0x56bd
jmp 0x2bb5
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2fc6
leaq 0x2b25(%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 0x30fb
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2fc6
leaq 0x2b18(%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 0x3190
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2fc6
movq %r12, %rdi
callq 0x20a0
leaq 0x2af7(%rip), %rdx      # 0x56ac
movq %rbx, %rdi
movq %r14, %rsi
jmp 0x2b56
leaq 0x2ead(%rip), %rdx      # 0x5a71
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2f18
testq %rax, %rax
je 0x2c96
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2f9d
leaq 0x2f28(%rip), %rdx      # 0x5b12
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rcx
movq %r12, %r8
callq 0x3225
movl %eax, %ebp
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2fc6
movl %ebp, %eax
jmp 0x2ca4
movq %rcx, %rbp
leaq 0x2ac7(%rip), %rdx      # 0x56e0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2f18
testq %rax, %rax
je 0x2c8e
leaq 0x2ace(%rip), %rdx      # 0x56fe
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2f18
testq %rax, %rax
je 0x2c8e
leaq 0x2aca(%rip), %rdx      # 0x5711
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2f18
testq %rax, %rax
je 0x2c8e
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x2f18
testq %rax, %rax
je 0x2c8e
movl 0x40(%r14), %r13d
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2f9d
movq %rbx, %rdi
movl %r13d, %esi
movq %rax, %rdx
movq %rbp, %rcx
callq 0x2d24
testl %eax, %eax
je 0x2cb3
movq %r12, %rdi
callq 0x20a0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2fc6
pushq $0x1
popq %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %r13
movq (%r15), %rdi
movl $0x400, %edx            # imm = 0x400
movq %r13, %rsi
movq %r12, %rcx
callq 0x2080
testq %rax, %rax
jle 0x2ce4
movl 0x40(%r14), %esi
movq (%r15), %rdx
movq %rbx, %rdi
movq %rax, %rcx
callq *0x30(%rbx)
testq %rax, %rax
jg 0x2cb7
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2fc6
movq %r12, %rdi
callq 0x20a0
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq *0x48(%rbx)
xorl %eax, %eax
jmp 0x2ca4
leaq 0x2880(%rip), %rdi      # 0x558b
leaq 0x2342(%rip), %rsi      # 0x5054
leaq 0x2a07(%rip), %rcx      # 0x5720
pushq $0x59
popq %rdx
callq 0x20e0
nopl (%rax)