Just to have a disclaimer about the setup:
The first step in converting a c file to wasm is to install the C language dependencies. Then it is necessary to make the following downloads in sequence:
Wasmer: is a WebAssembly runtime that enables super lightweight containers to run anywhere from Desktop to the Cloud, Edge and IoT devices.
Wasienv: Is a tool to make very easy to compile different programming languages to WebAssembly, so it is possible to run programs in the server.
Wasmtime: Is a Bytecode Alliance project that is a standalone wasm-only optimizing runtime for WebAssembly and WASI. It runs WebAssembly code outside of the Web, and can be used both as a command-line utility or as a library embedded in a larger application.
The links is at the end of the post.
Compiling the C Code:
To compile the file is use the following command:
Compile to WASM Binary using the following command:
Since the Code has been written in C, we need to figure out a way to generate a WebAssembly Binary. That's why we will be using wasienv in order to generate a .wasm binary from this c file.
When you have your c file created, you can execute wasicc:
wasicc NameOfTheFile.c -o NameOfTheFile.wasm
The example will be about find the minimum length unsorted subarray, sorting which makes the complete array sorted.
Given an unsorted array arr[0..n-1] of size n, find the minimum length subarray arr[s..e] such that sorting this subarray makes the whole array sorted.
If the input array is [0, 1, 15, 25, 6, 7, 30, 40, 50], your program should be able to find that the subarray lies between the indexes 2 and 5.
Using the commands to compile: