From 337e095aeebbefe7bf9eab1839868aba32f19ac6 Mon Sep 17 00:00:00 2001 From: ahuoguo Date: Mon, 7 Oct 2024 22:43:48 -0400 Subject: [PATCH 1/3] should be able to run btree --- benchmarks/wasm/btree/2o1u-fd_write.wat | 2877 ++++++++++++++++++++ benchmarks/wasm/btree/2o1u-orig.wat | 2819 ++++++++++++++++++++ benchmarks/wasm/btree/2o1u-unlabeled.wat | 5 +- benchmarks/wasm/btree/2o1u.wat | 4 +- benchmarks/wasm/btree/2o2u-unlabeled.wat | 2672 +++++++++++++++++++ benchmarks/wasm/btree/2o2u.wat | 2861 ++++++++++++++++++++ benchmarks/wasm/btree/2o3u-unlabeled.wat | 2708 +++++++++++++++++++ benchmarks/wasm/btree/2o3u.wat | 2899 ++++++++++++++++++++ benchmarks/wasm/btree/3o1u-unlabeled.wat | 2668 +++++++++++++++++++ benchmarks/wasm/btree/3o1u.wat | 2858 ++++++++++++++++++++ benchmarks/wasm/btree/3o2u-unlabeled.wat | 2704 +++++++++++++++++++ benchmarks/wasm/btree/3o2u.wat | 2895 ++++++++++++++++++++ benchmarks/wasm/btree/3o3u-unlabeled.wat | 2744 +++++++++++++++++++ benchmarks/wasm/btree/3o3u.wat | 2937 ++++++++++++++++++++ benchmarks/wasm/btree/4o1u.wat | 2874 ++++++++++++++++++++ benchmarks/wasm/btree/4o2u.wat | 2915 ++++++++++++++++++++ benchmarks/wasm/btree/4o3u.wat | 2960 +++++++++++++++++++++ benchmarks/wasm/btree/5o1u.wat | 2903 ++++++++++++++++++++ benchmarks/wasm/btree/5o2u.wat | 2948 ++++++++++++++++++++ benchmarks/wasm/btree/5o3u.wat | 2997 +++++++++++++++++++++ benchmarks/wasm/btree/6o1u.wat | 2932 ++++++++++++++++++++ benchmarks/wasm/btree/6o2u.wat | 2981 +++++++++++++++++++++ benchmarks/wasm/btree/6o3u.wat | 3034 +++++++++++++++++++++ benchmarks/wasm/btree/7o1u.wat | 2961 +++++++++++++++++++++ benchmarks/wasm/btree/7o2u.wat | 3014 +++++++++++++++++++++ benchmarks/wasm/btree/7o3u.wat | 3071 +++++++++++++++++++++ benchmarks/wasm/btree/8o1u.wat | 2990 +++++++++++++++++++++ benchmarks/wasm/btree/9o1u.wat | 3019 +++++++++++++++++++++ benchmarks/wasm/btree/9o2u-unlabeled.wat | 2896 ++++++++++++++++++++ benchmarks/wasm/btree/9o2u.wat | 3099 ++++++++++++++++++++++ benchmarks/wasm/btree/unlabel-all.sh | 10 + benchmarks/wasm/btree/unlabel.sh | 8 + benchmarks/wasm/div_s.wat | 17 + benchmarks/wasm/load.wat | 1 - src/main/scala/wasm/MiniWasm.scala | 6 + src/test/scala/genwasym/TestEval.scala | 13 +- 36 files changed, 81296 insertions(+), 4 deletions(-) create mode 100644 benchmarks/wasm/btree/2o1u-fd_write.wat create mode 100644 benchmarks/wasm/btree/2o1u-orig.wat create mode 100644 benchmarks/wasm/btree/2o2u-unlabeled.wat create mode 100644 benchmarks/wasm/btree/2o2u.wat create mode 100644 benchmarks/wasm/btree/2o3u-unlabeled.wat create mode 100644 benchmarks/wasm/btree/2o3u.wat create mode 100644 benchmarks/wasm/btree/3o1u-unlabeled.wat create mode 100644 benchmarks/wasm/btree/3o1u.wat create mode 100644 benchmarks/wasm/btree/3o2u-unlabeled.wat create mode 100644 benchmarks/wasm/btree/3o2u.wat create mode 100644 benchmarks/wasm/btree/3o3u-unlabeled.wat create mode 100644 benchmarks/wasm/btree/3o3u.wat create mode 100644 benchmarks/wasm/btree/4o1u.wat create mode 100644 benchmarks/wasm/btree/4o2u.wat create mode 100644 benchmarks/wasm/btree/4o3u.wat create mode 100644 benchmarks/wasm/btree/5o1u.wat create mode 100644 benchmarks/wasm/btree/5o2u.wat create mode 100644 benchmarks/wasm/btree/5o3u.wat create mode 100644 benchmarks/wasm/btree/6o1u.wat create mode 100644 benchmarks/wasm/btree/6o2u.wat create mode 100644 benchmarks/wasm/btree/6o3u.wat create mode 100644 benchmarks/wasm/btree/7o1u.wat create mode 100644 benchmarks/wasm/btree/7o2u.wat create mode 100644 benchmarks/wasm/btree/7o3u.wat create mode 100644 benchmarks/wasm/btree/8o1u.wat create mode 100644 benchmarks/wasm/btree/9o1u.wat create mode 100644 benchmarks/wasm/btree/9o2u-unlabeled.wat create mode 100644 benchmarks/wasm/btree/9o2u.wat create mode 100755 benchmarks/wasm/btree/unlabel-all.sh create mode 100755 benchmarks/wasm/btree/unlabel.sh create mode 100644 benchmarks/wasm/div_s.wat diff --git a/benchmarks/wasm/btree/2o1u-fd_write.wat b/benchmarks/wasm/btree/2o1u-fd_write.wat new file mode 100644 index 00000000..e0fe0d79 --- /dev/null +++ b/benchmarks/wasm/btree/2o1u-fd_write.wat @@ -0,0 +1,2877 @@ +(module + (import "wasi_snapshot_preview1" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32))) + (memory $0 2) + (export "memory" (memory 0)) + (data (i32.const 1024) "hello world\n") + (func $createBtree (param i32) (result i32) ;; createBtree(t), where t: degree of the btree + ;; (i32.store (i32.const 0) (i32.const 1024)) ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string + ;; (i32.store (i32.const 4) (i32.const 12)) ;; iov.iov_len - The length of the 'hello world\n' string + ;; (call $fd_write + ;; (i32.const 1) ;; file_descriptor - 1 for stdout + ;; (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0 + ;; (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one. + ;; (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written + ;; ) + ;; drop + (i32.const 0) + (local.get 0) + (i32.store) ;; store t at address 0 + (i32.const 0) + (i32.const 1) + (i32.store offset=4) ;; store the number of nodes in the btree = 1 + (i32.const 0) + (i32.const 65536) + (i32.store offset=8) ;; store the root addr + (i32.const 1) ;; Now create root + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then + (i32.const 65536) ;; 64KiB = 65536 bytes + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + (i32.const 65536) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + ) + ) + (i32.const 65536) ;; returns the address of the root + ) + ;; btreeSearch(x, k), where x: node address; k: key to search. Returns address of the key or -1 if key is not in tree. + (func $btreeSearch (param i32) (param i32) (result i32) (local i32) + ;; (i32.store (i32.const 0) (i32.const 8)) ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string + ;; (i32.store (i32.const 4) (i32.const 12)) ;; iov.iov_len - The length of the 'hello world\n' string + ;; (call $fd_write + ;; (i32.const 1) ;; file_descriptor - 1 for stdout + ;; (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0 + ;; (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one. + ;; (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written + ;; ) + ;; drop + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n -1 + (i32.le_s) ;; i <= x.children-1 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + (if + (then + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.le_s) ;; i <= x.children - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.eq) + ;; k == x.keys[i] + (if (result i32) + (then + (local.get 0) ;; save the address, which is x+8+i*4 + (i32.const 8) + (i32.add) ;; x+8 + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x+8+i*4 + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + ;; btreeSplitChild(x, i), where x: addr of a non full internal node; i: index such that x.children[i] is a full child of x + (func $btreeSplitChild (param i32) (param i32) (local i32) (local i32) + ;; (i32.store (i32.const 0) (i32.const 8)) ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string + ;; (i32.store (i32.const 4) (i32.const 12)) ;; iov.iov_len - The length of the 'hello world\n' string + ;; (call $fd_write + ;; (i32.const 1) ;; file_descriptor - 1 for stdout + ;; (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0 + ;; (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one. + ;; (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written + ;; ) + ;; drop + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then ;; get the number of nodes in the tree + (i32.const 0) + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address is 64KiB*(number of nodes+1) + (local.set 2) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the addr of the child in index i : y + (i32.load) ;; check if x.children[i] is leaf ;; load the first i32 in the child + (i32.const 1) + (i32.eq) ;; is y leaf? + (if + (then ;; yes + (local.get 2) ;; addr of new node z + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + ) + (else ;; no + (local.get 2) ;; addr of new node z + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + ) + ) + (local.get 2) ;; now store number of children, which will be t/2 -1 ;; addr of z + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.store offset=4) ;; store number of children = (t/2)-1 + (i32.const 0) ;; now to store the keys + (local.set 3) ;; counter = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.eq) ;; counter == (t/2)-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 2) ;; addr of z ;; we will store in the new node's address. z.keys[counter] + (i32.const 4) + (local.get 3) ;; counter + (i32.mul) ;; counter * 4 + (i32.add) ;; addr + counter * 4 -> where the key is goint to be located + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 4) ;; now get x.children[i].keys[counter+(t/2)] + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + (t/2) + (i32.mul) ;; counter + (t/2) *4 + (i32.add) ;; y + counter + (t/2) *4 + (i32.load offset=8) ;; y.keys[counter + (t/2)] + (i32.store offset=8) ;; z.keys[counter] = y.keys[counter + (t/2)] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) + (local.set 3) ;; set counter back to 0 + (i32.const 0) ;; check if x.children[i] is leaf + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load) ;; get first i32 in child --> isLeaf + (i32.const 1) + (i32.ne) ;; is leaf? + (if + (then ;; not leaf so we have to update the children + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.eq) ;; counter == (t/2) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; now get z.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; counter*4 + (i32.add) ;; (t-1)*4 + counter*4 + (local.get 2) ;; z + (i32.add) ;; z + (t-1)*4 + counter*4 --> address where addr of child is stored + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) ;; get x.children[i].children[counter+(t/2)] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + t/2 + (i32.const 4) + (i32.mul) ;; counter+t/2 * 4 + (i32.add) ;; (t-1)*4 + (counter+(t/2))*4 + (i32.add) ;; x.children[i] + (t-1)*4 + (counter + (t/2))*4 + (i32.load offset=8) ;; load the address of the child in index counter + t/2 + (i32.store offset=8) ;; z.children[counter] = x.children[i].children[counter] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.store offset=4) ;; number of keys of x.children[i] is now t/2 -1 + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (local.set 3) ;; counter = x.n + (block $loop_break + (loop $loop + (local.get 1) ;; i + (local.get 3) ;; counter + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; get x.children[counter + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (i32.add) ;; (t-1)*4 + (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter + 1)*4 -->address where addr of child is stored + (i32.const 0) ;; get x.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (i32.add) ;; (t-1)*4 + (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter)*4 + (i32.load offset=8) ;; load the addr of the child in index (counter) + (i32.store offset=8) ;; x.children[counter+1] = x.children[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 1) + (i32.add) ;; i + 1 + (i32.const 4) + (i32.mul) ;; (i + 1)*4 + (i32.add) ;; (t-1)*4 + (i + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i + 1)*4 --> addr where we will store the new addr of the child + (local.get 2) ;; addr of z + (i32.store offset=8) ;; x.children[i+1] = z + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (i32.const 1) + (i32.sub) + (local.set 3) ;; counter = x.n -1 + (block $loop_break + (loop $loop + (local.get 1) ;; i + (i32.const 1) + (i32.sub) ;; i-1 + (local.get 3) ;; counter + (i32.eq) ;; counter == i-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 3) ;; get x.keys[counter + 1] ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter + 1)*4 --> addr where we will store the new key + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter)*4 + (i32.load offset=8) ;; load the key in index (counter) + (i32.store offset=8) ;; x.keys[counter+1] = x.keys[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (local.get 1) ;; get x.keys[i] + (i32.const 4) + (i32.mul) ;; i*4 + (local.get 0) ;; x + (i32.add) ;; x +i*4 --> addr of the key + (i32.const 0) ;; get x.children[i].keys[t/2] + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.const 4) + (i32.mul) ;; (t/2)-1 *4 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; (i)*4 + (i32.add) ;; (t-1)*4 + (i)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i)*4 + (i32.load offset=8) ;; load the address of the child in index (i) + (i32.add) ;; y + (t/2)-1 *4 + (i32.load offset=8) ;; load the key in index (t/2)-1 from node x.children[i] + (i32.store offset=8) ;; store the new key in the addr + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) ;; x.n + 1 + (i32.store offset=4) ;; x.n = x.n + 1 + ) + ) + ) + ;; btreeInsertNonFull(x, k), where x: addr of a non full internal node; k: the key to insert + (func $btreeInsertNonFull (param i32) (param i32) (local i32) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n -1 + (local.set 2) ;; i = x.n -1 + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + ;; (i32.store (i32.const 0) (i32.const 1024)) ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string + ;; (i32.store (i32.const 4) (i32.const 12)) ;; iov.iov_len - The length of the 'hello world\n' string + ;; (call $fd_write + ;; (i32.const 1) ;; file_descriptor - 1 for stdout + ;; (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0 + ;; (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one. + ;; (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written + ;; ) + ;; drop + (if + (then ;; x is leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (i32.and) + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i *4 + (i32.add) ;; x + i *4 --> addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.keys[i+1] = x.keys[i] + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 1) ;; k + (i32.store offset=8) ;; x.keys[i+1] = k + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; x.n = x.n +1 + ) + (else ;; x is not leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i >= 0 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i -1 + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 2) ;;i = i+1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address of the child in index i = y + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load offset=4) ;; get y.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; y.n == t-1 --> node is full + (if + (then + (local.get 0) ;; x + (local.get 2) ;; i + (call $btreeSplitChild) + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k>x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i+1 + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (local.get 1) ;; k + (call $btreeInsertNonFull) + ) + ) + ) + (func $btreeInsert (param i32) (result i32) (local i32) (local i32) + ;; (i32.store (i32.const 0) (i32.const 1024)) ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string + ;; (i32.store (i32.const 4) (i32.const 12)) ;; iov.iov_len - The length of the 'hello world\n' string + ;; (call $fd_write + ;; (i32.const 1) ;; file_descriptor - 1 for stdout + ;; (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0 + ;; (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one. + ;; (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written + ;; ) + ;; drop + (i32.const 0) + (i32.load offset=8) ;; root addr + (local.tee 2) ;; set r + (i32.load offset=4) ;; r.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; r.n == t-1 --> root is full + (if (result i32) + (then + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if (result i32) ;; if we can grow memory + (then + (i32.const 0) ;; get the number of nodes in the tree + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address of s is 64KiB*(number of nodes+1) + (local.set 1) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) + (local.get 1) ;; s + (i32.store offset=8) ;; root addr is now s + (local.get 1) ;; s + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + (local.get 1) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + (i32.const 0) ;; store children addresses + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 1) ;; x + (i32.add) ;; x + (t-1)*4 --> addre where addr of child number 0 is located + (local.get 2) + (i32.store offset=8) ;; s.children[0] = r + (local.get 1) ;; s + (i32.const 0) + (call $btreeSplitChild) + (local.get 1) ;; s + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 1) ;; return the address of the new root + ) + (else + (i32.const -1) ;; return -1 because we could not create a new node + ) + ) + ) + (else + (local.get 2) ;; r + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 2) ;; return the address of the root, which is the same + ) + ) + ) + ;; Returns the address of the new root (if applicable, otherwise returns the addres of the root) + (func $btreeDelete (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local i32) + ;; (i32.store (i32.const 0) (i32.const 8)) ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string + ;; (i32.store (i32.const 4) (i32.const 12)) ;; iov.iov_len - The length of the 'hello world\n' string + ;; (call $fd_write + ;; (i32.const 1) ;; file_descriptor - 1 for stdout + ;; (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0 + ;; (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one. + ;; (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written + ;; ) + ;; drop + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (local.get 2) ;; i + (i32.eq) ;; i == x.n + (if + (then + (br $loop_break) ;; break from loop + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + (if + (then + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $while_break + (loop $while + (local.get 3) ;; j + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n-1 + (i32.eq) ;; j == x.n-1 + (if + (then + (br $while_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 --> addr where x.keys[j] is stored + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.mul) ;; j+1 *4 + (i32.add) ;; x + j+1 *4 --> addr where x.keys[j+1] is stored + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $while) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n -1 + (br $loop_break) + ) + ) + ) + ) + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) ;; continue + ) + ) + ) + (else ;; x is not leaf + (i32.const 0) ;;get the index of the appropriate child/key + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (i32.and) ;; i <= x.n - 1 && k > x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i + 1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.lt_s) ;; i < x.n + (if (result i32) + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (if + (then ;; key is present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.tee 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (call $btreeDelete) ;; (x.c[i], x.c[i].keys[x.c[i].n - 1]) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i].keys[x.c[i].n - 1] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i+1] + (local.tee 5) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (local.get 5) ;; x.c[i+1] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (call $btreeDelete) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 --> addr where x.c[i].keys[x.c[i].n] will be stored + (local.get 1) ;; k + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = k + (i32.const 0) ;; now merge x.c[i] with x.c[i+1] + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if ;; j == x.c[i+1].n + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n + j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[index] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x.c[i+1] + i*4 ;; addr of x.c[i+1].keys[j] + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[index] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n +1 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + 1 + x.c[i+1].n + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; now adjust the children + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.get 3) ;; j + (i32.add) ;; index = x.c[i].n+j + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x.c[i] + (t-1)*4 --> address where x.c[i].c[x.c[i].n+j] is stored + (i32.const 0) ;;get x.c[i+1].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x.c[i+1] + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (i32.const 1) + (i32.sub) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) + (i32.mul) ;; j+1*4 + (i32.add) ;; x + j+1*4 ;; addr of x.keys[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = i+1 + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 0) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n-1 + (local.get 5) ;; x.c[i] + (local.get 1) ;; k + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) + (else ;; key not present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) + (i32.const 1) + (i32.sub) ;; t/2 - 1 + (i32.eq) + (if + (then + (i32.const -1) + (local.set 4) ;; changed = -1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.le_s) ;; i+1 <= x.n + (if + (then + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; index = t/2 -1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[t/2-1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[t/2-1] = x.keys[i] + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;;x.c[i].n = x.c[i].n +1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i+1].keys[0] + (i32.load offset=8) ;; x.c[i+1].keys[0] + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + (local.get 5) + (i32.load) ;; x.c[i] is leaf? + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) ;;get x.c[i].c[t/2] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; index = t/2 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[t/2] + (i32.const 0) ;;get x.c[i+1].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[0] + (i32.store offset=8) ;; x.c[i].c[t/2] = x.c[i+1].c[0] + ) + ) + (local.get 5) ;; x.c[i] + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.store offset=4) ;; x.c[i].n = t/2 + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 --> address where x.c[i+1].keys[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j+1] + (i32.store offset=8) ;; x.c[i+1].keys[j] = x.c[i+1].keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i+1].c[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j+1] + (i32.store offset=8) ;; x.c[i+1].c[j] = x.c[i+1].c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr where x.c[i+1] is located + (i32.load offset=8) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i+1].n = x.c[i+1].n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i -1 >= 0 + (i32.and) ;; changed == -1 && i -1 >= 0 + (if + (then + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i-1].n >= t/2 + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.set 3) ;; j = x.c[i].n + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j-1] + (i32.load offset=8) ;; x.c[i].keys[j-1] + (i32.store offset=8) ;; x.c[i].keys[j] = x.c[i].keys[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (local.set 3) ;; j = x.c[i].n + 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i].c[j] is located + (i32.const 0) ;;get x.c[i].c[j-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) + (i32.load offset=8) ;; x.c[i].c[j-1] + (i32.store offset=8) ;; x.c[i].c[j] = x.c[i].c[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; ;; x.c[i].n = x.c[i].n + 1 + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[0] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i-1] + (i32.store offset=8) ;; x.c[i].keys[0] = x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n - 1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[i-1] = x.c[i-1].keys[x.c[i-1].n] + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; get x.c[i].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 ;; addr where x.c[i].c[0] is located + (i32.const 0) ;; get x.c[i-1].c[x.c[i-1].n + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.add) ;; index = x.c[i-1.n + 1] + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1].c[x.c[i-1].n + 1] + (i32.store offset=8) ;; x.c[i].c[0] = x.c[i-1].c[x.c[i-1].n + 1] + ) + ) + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (if ;; changed == -1? + (then + (local.get 2) ;; we have to merge x.c[i] with one sibling;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.le_s) + (if ;; i+1 <= x.n + (then + (local.get 5) ;; merge with right sibling;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = x.keys[i] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n+j] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n+j] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i+1].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[x.c[i].n+j] + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) ;; x.c[i].n + x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + x.c[i+1].n + 1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.set 3) ;; j = i+1 + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) ;; changed == -1 ? + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i - 1 >= 0? + (i32.and) ;; changed == -1 && i - 1 >= 0 + (if + (then + (i32.const 0) ;; merge with left sibling ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n] = x.keys[i-1] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n+j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[j] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n+j] = x.c[i].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i-1].c[x.c[i-1].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i-1].c[x.c[i-1].n+j] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i].c[j] + (i32.store offset=8) ;; x.c[i-1].c[x.c[i-1].n+j] = x.c[i].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.add) ;; x.c[i-1].n + x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n + x.c[i].n + 1 + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = i - 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + ) + ) + ) + ) + ) + ) + (local.get 4) + (i32.const -1) + (i32.eq) + (if ;; changed == -1? if yes, we merged with left sibling + (then + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (local.get 1) + (call $btreeDelete) + (drop) + ) + (else + (local.get 5) ;; x.c[i] + (local.get 1) + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) ;; end of if x is not leaf + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.load offset=4) + (i32.const 0) + (i32.eq) + (if ;; if root is empty + (then + (i32.const 0) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; root.c[0] + (i32.store offset=8) ;; root = root.c[0] + ) + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + ) + (func $main (param $a i32) (param $b i32) (param $h i32) + (local $btree i32) + (local.get $a) + (local.get $b) + (i32.gt_s) ;; a > b + (local.get $a) + (local.get $h) + (i32.ne) ;; a != h + (local.get $b) + (local.get $h) + (i32.ne) ;; b != h + (i32.and) + (i32.and) + (i32.eqz) + (if (then (unreachable))) + (i32.const 4) ;;create a tree with degree 4 + (call $createBtree) + (local.set $btree) + ;; insert variables + (local.get $a) + (call $btreeInsert) + (local.set $btree) + (local.get $b) + (call $btreeInsert) + (local.set $btree) + (local.get $h) + (call $btreeInsert) + (local.set $btree) + ;; search for variables & check that they were inserted + (local.get $btree) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (i32.and) + (i32.and) + ;; delete & check that it was deleted + ;; a + (local.get $btree) + (local.get $a) + (call $btreeDelete) + (local.tee $btree) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; b + (local.get $btree) + (local.get $b) + (call $btreeDelete) + (local.tee $btree) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + (i32.and) + (i32.and) + (drop) + ) + (export "main" (func $main)) + (func $real_main + i32.const 3 + i32.const 2 + i32.const 1 + call $main + ) + (start $real_main) + ) diff --git a/benchmarks/wasm/btree/2o1u-orig.wat b/benchmarks/wasm/btree/2o1u-orig.wat new file mode 100644 index 00000000..a8536b0b --- /dev/null +++ b/benchmarks/wasm/btree/2o1u-orig.wat @@ -0,0 +1,2819 @@ +(module + (memory $0 2) + (func $createBtree (param i32) (result i32) ;; createBtree(t), where t: degree of the btree + (i32.const 0) + (local.get 0) + (i32.store) ;; store t at address 0 + (i32.const 0) + (i32.const 1) + (i32.store offset=4) ;; store the number of nodes in the btree = 1 + (i32.const 0) + (i32.const 65536) + (i32.store offset=8) ;; store the root addr + (i32.const 1) ;; Now create root + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then + (i32.const 65536) ;; 64KiB = 65536 bytes + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + (i32.const 65536) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + ) + ) + (i32.const 65536) ;; returns the address of the root + ) + ;; btreeSearch(x, k), where x: node address; k: key to search. Returns address of the key or -1 if key is not in tree. + (func $btreeSearch (param i32) (param i32) (result i32) (local i32) + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n -1 + (i32.le_s) ;; i <= x.children-1 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + (if + (then + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.le_s) ;; i <= x.children - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.eq) + ;; k == x.keys[i] + (if (result i32) + (then + (local.get 0) ;; save the address, which is x+8+i*4 + (i32.const 8) + (i32.add) ;; x+8 + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x+8+i*4 + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + ;; btreeSplitChild(x, i), where x: addr of a non full internal node; i: index such that x.children[i] is a full child of x + (func $btreeSplitChild (param i32) (param i32) (local i32) (local i32) + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then ;; get the number of nodes in the tree + (i32.const 0) + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address is 64KiB*(number of nodes+1) + (local.set 2) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the addr of the child in index i : y + (i32.load) ;; check if x.children[i] is leaf ;; load the first i32 in the child + (i32.const 1) + (i32.eq) ;; is y leaf? + (if + (then ;; yes + (local.get 2) ;; addr of new node z + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + ) + (else ;; no + (local.get 2) ;; addr of new node z + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + ) + ) + (local.get 2) ;; now store number of children, which will be t/2 -1 ;; addr of z + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.store offset=4) ;; store number of children = (t/2)-1 + (i32.const 0) ;; now to store the keys + (local.set 3) ;; counter = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.eq) ;; counter == (t/2)-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 2) ;; addr of z ;; we will store in the new node's address. z.keys[counter] + (i32.const 4) + (local.get 3) ;; counter + (i32.mul) ;; counter * 4 + (i32.add) ;; addr + counter * 4 -> where the key is goint to be located + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 4) ;; now get x.children[i].keys[counter+(t/2)] + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + (t/2) + (i32.mul) ;; counter + (t/2) *4 + (i32.add) ;; y + counter + (t/2) *4 + (i32.load offset=8) ;; y.keys[counter + (t/2)] + (i32.store offset=8) ;; z.keys[counter] = y.keys[counter + (t/2)] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) + (local.set 3) ;; set counter back to 0 + (i32.const 0) ;; check if x.children[i] is leaf + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load) ;; get first i32 in child --> isLeaf + (i32.const 1) + (i32.ne) ;; is leaf? + (if + (then ;; not leaf so we have to update the children + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.eq) ;; counter == (t/2) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; now get z.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; counter*4 + (i32.add) ;; (t-1)*4 + counter*4 + (local.get 2) ;; z + (i32.add) ;; z + (t-1)*4 + counter*4 --> address where addr of child is stored + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) ;; get x.children[i].children[counter+(t/2)] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + t/2 + (i32.const 4) + (i32.mul) ;; counter+t/2 * 4 + (i32.add) ;; (t-1)*4 + (counter+(t/2))*4 + (i32.add) ;; x.children[i] + (t-1)*4 + (counter + (t/2))*4 + (i32.load offset=8) ;; load the address of the child in index counter + t/2 + (i32.store offset=8) ;; z.children[counter] = x.children[i].children[counter] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.store offset=4) ;; number of keys of x.children[i] is now t/2 -1 + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (local.set 3) ;; counter = x.n + (block $loop_break + (loop $loop + (local.get 1) ;; i + (local.get 3) ;; counter + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; get x.children[counter + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (i32.add) ;; (t-1)*4 + (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter + 1)*4 -->address where addr of child is stored + (i32.const 0) ;; get x.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (i32.add) ;; (t-1)*4 + (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter)*4 + (i32.load offset=8) ;; load the addr of the child in index (counter) + (i32.store offset=8) ;; x.children[counter+1] = x.children[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 1) + (i32.add) ;; i + 1 + (i32.const 4) + (i32.mul) ;; (i + 1)*4 + (i32.add) ;; (t-1)*4 + (i + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i + 1)*4 --> addr where we will store the new addr of the child + (local.get 2) ;; addr of z + (i32.store offset=8) ;; x.children[i+1] = z + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (i32.const 1) + (i32.sub) + (local.set 3) ;; counter = x.n -1 + (block $loop_break + (loop $loop + (local.get 1) ;; i + (i32.const 1) + (i32.sub) ;; i-1 + (local.get 3) ;; counter + (i32.eq) ;; counter == i-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 3) ;; get x.keys[counter + 1] ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter + 1)*4 --> addr where we will store the new key + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter)*4 + (i32.load offset=8) ;; load the key in index (counter) + (i32.store offset=8) ;; x.keys[counter+1] = x.keys[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (local.get 1) ;; get x.keys[i] + (i32.const 4) + (i32.mul) ;; i*4 + (local.get 0) ;; x + (i32.add) ;; x +i*4 --> addr of the key + (i32.const 0) ;; get x.children[i].keys[t/2] + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.const 4) + (i32.mul) ;; (t/2)-1 *4 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; (i)*4 + (i32.add) ;; (t-1)*4 + (i)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i)*4 + (i32.load offset=8) ;; load the address of the child in index (i) + (i32.add) ;; y + (t/2)-1 *4 + (i32.load offset=8) ;; load the key in index (t/2)-1 from node x.children[i] + (i32.store offset=8) ;; store the new key in the addr + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) ;; x.n + 1 + (i32.store offset=4) ;; x.n = x.n + 1 + ) + ) + ) + ;; btreeInsertNonFull(x, k), where x: addr of a non full internal node; k: the key to insert + (func $btreeInsertNonFull (param i32) (param i32) (local i32) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n -1 + (local.set 2) ;; i = x.n -1 + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (i32.and) + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i *4 + (i32.add) ;; x + i *4 --> addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.keys[i+1] = x.keys[i] + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 1) ;; k + (i32.store offset=8) ;; x.keys[i+1] = k + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; x.n = x.n +1 + ) + (else ;; x is not leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i >= 0 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i -1 + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 2) ;;i = i+1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address of the child in index i = y + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load offset=4) ;; get y.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; y.n == t-1 --> node is full + (if + (then + (local.get 0) ;; x + (local.get 2) ;; i + (call $btreeSplitChild) + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k>x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i+1 + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (local.get 1) ;; k + (call $btreeInsertNonFull) + ) + ) + ) + (func $btreeInsert (param i32) (result i32) (local i32) (local i32) + (i32.const 0) + (i32.load offset=8) ;; root addr + (local.tee 2) ;; set r + (i32.load offset=4) ;; r.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; r.n == t-1 --> root is full + (if (result i32) + (then + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if (result i32) ;; if we can grow memory + (then + (i32.const 0) ;; get the number of nodes in the tree + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address of s is 64KiB*(number of nodes+1) + (local.set 1) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) + (local.get 1) ;; s + (i32.store offset=8) ;; root addr is now s + (local.get 1) ;; s + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + (local.get 1) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + (i32.const 0) ;; store children addresses + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 1) ;; x + (i32.add) ;; x + (t-1)*4 --> addre where addr of child number 0 is located + (local.get 2) + (i32.store offset=8) ;; s.children[0] = r + (local.get 1) ;; s + (i32.const 0) + (call $btreeSplitChild) + (local.get 1) ;; s + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 1) ;; return the address of the new root + ) + (else + (i32.const -1) ;; return -1 because we could not create a new node + ) + ) + ) + (else + (local.get 2) ;; r + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 2) ;; return the address of the root, which is the same + ) + ) + ) + ;; Returns the address of the new root (if applicable, otherwise returns the addres of the root) + (func $btreeDelete (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local i32) + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (local.get 2) ;; i + (i32.eq) ;; i == x.n + (if + (then + (br $loop_break) ;; break from loop + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + (if + (then + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $while_break + (loop $while + (local.get 3) ;; j + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n-1 + (i32.eq) ;; j == x.n-1 + (if + (then + (br $while_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 --> addr where x.keys[j] is stored + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.mul) ;; j+1 *4 + (i32.add) ;; x + j+1 *4 --> addr where x.keys[j+1] is stored + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $while) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n -1 + (br $loop_break) + ) + ) + ) + ) + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) ;; continue + ) + ) + ) + (else ;; x is not leaf + (i32.const 0) ;;get the index of the appropriate child/key + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (i32.and) ;; i <= x.n - 1 && k > x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i + 1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.lt_s) ;; i < x.n + (if (result i32) + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (if + (then ;; key is present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.tee 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (call $btreeDelete) ;; (x.c[i], x.c[i].keys[x.c[i].n - 1]) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i].keys[x.c[i].n - 1] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i+1] + (local.tee 5) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (local.get 5) ;; x.c[i+1] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (call $btreeDelete) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 --> addr where x.c[i].keys[x.c[i].n] will be stored + (local.get 1) ;; k + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = k + (i32.const 0) ;; now merge x.c[i] with x.c[i+1] + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if ;; j == x.c[i+1].n + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n + j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[index] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x.c[i+1] + i*4 ;; addr of x.c[i+1].keys[j] + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[index] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n +1 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + 1 + x.c[i+1].n + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; now adjust the children + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.get 3) ;; j + (i32.add) ;; index = x.c[i].n+j + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x.c[i] + (t-1)*4 --> address where x.c[i].c[x.c[i].n+j] is stored + (i32.const 0) ;;get x.c[i+1].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x.c[i+1] + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (i32.const 1) + (i32.sub) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) + (i32.mul) ;; j+1*4 + (i32.add) ;; x + j+1*4 ;; addr of x.keys[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = i+1 + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 0) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n-1 + (local.get 5) ;; x.c[i] + (local.get 1) ;; k + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) + (else ;; key not present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) + (i32.const 1) + (i32.sub) ;; t/2 - 1 + (i32.eq) + (if + (then + (i32.const -1) + (local.set 4) ;; changed = -1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.le_s) ;; i+1 <= x.n + (if + (then + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; index = t/2 -1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[t/2-1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[t/2-1] = x.keys[i] + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;;x.c[i].n = x.c[i].n +1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i+1].keys[0] + (i32.load offset=8) ;; x.c[i+1].keys[0] + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + (local.get 5) + (i32.load) ;; x.c[i] is leaf? + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) ;;get x.c[i].c[t/2] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; index = t/2 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[t/2] + (i32.const 0) ;;get x.c[i+1].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[0] + (i32.store offset=8) ;; x.c[i].c[t/2] = x.c[i+1].c[0] + ) + ) + (local.get 5) ;; x.c[i] + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.store offset=4) ;; x.c[i].n = t/2 + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 --> address where x.c[i+1].keys[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j+1] + (i32.store offset=8) ;; x.c[i+1].keys[j] = x.c[i+1].keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i+1].c[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j+1] + (i32.store offset=8) ;; x.c[i+1].c[j] = x.c[i+1].c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr where x.c[i+1] is located + (i32.load offset=8) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i+1].n = x.c[i+1].n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i -1 >= 0 + (i32.and) ;; changed == -1 && i -1 >= 0 + (if + (then + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i-1].n >= t/2 + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.set 3) ;; j = x.c[i].n + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j-1] + (i32.load offset=8) ;; x.c[i].keys[j-1] + (i32.store offset=8) ;; x.c[i].keys[j] = x.c[i].keys[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (local.set 3) ;; j = x.c[i].n + 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i].c[j] is located + (i32.const 0) ;;get x.c[i].c[j-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) + (i32.load offset=8) ;; x.c[i].c[j-1] + (i32.store offset=8) ;; x.c[i].c[j] = x.c[i].c[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; ;; x.c[i].n = x.c[i].n + 1 + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[0] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i-1] + (i32.store offset=8) ;; x.c[i].keys[0] = x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n - 1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[i-1] = x.c[i-1].keys[x.c[i-1].n] + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; get x.c[i].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 ;; addr where x.c[i].c[0] is located + (i32.const 0) ;; get x.c[i-1].c[x.c[i-1].n + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.add) ;; index = x.c[i-1.n + 1] + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1].c[x.c[i-1].n + 1] + (i32.store offset=8) ;; x.c[i].c[0] = x.c[i-1].c[x.c[i-1].n + 1] + ) + ) + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (if ;; changed == -1? + (then + (local.get 2) ;; we have to merge x.c[i] with one sibling;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.le_s) + (if ;; i+1 <= x.n + (then + (local.get 5) ;; merge with right sibling;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = x.keys[i] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n+j] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n+j] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i+1].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[x.c[i].n+j] + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) ;; x.c[i].n + x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + x.c[i+1].n + 1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.set 3) ;; j = i+1 + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) ;; changed == -1 ? + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i - 1 >= 0? + (i32.and) ;; changed == -1 && i - 1 >= 0 + (if + (then + (i32.const 0) ;; merge with left sibling ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n] = x.keys[i-1] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n+j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[j] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n+j] = x.c[i].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i-1].c[x.c[i-1].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i-1].c[x.c[i-1].n+j] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i].c[j] + (i32.store offset=8) ;; x.c[i-1].c[x.c[i-1].n+j] = x.c[i].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.add) ;; x.c[i-1].n + x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n + x.c[i].n + 1 + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = i - 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + ) + ) + ) + ) + ) + ) + (local.get 4) + (i32.const -1) + (i32.eq) + (if ;; changed == -1? if yes, we merged with left sibling + (then + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (local.get 1) + (call $btreeDelete) + (drop) + ) + (else + (local.get 5) ;; x.c[i] + (local.get 1) + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) ;; end of if x is not leaf + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.load offset=4) + (i32.const 0) + (i32.eq) + (if ;; if root is empty + (then + (i32.const 0) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; root.c[0] + (i32.store offset=8) ;; root = root.c[0] + ) + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + ) + (func $main (param $a i32) (param $b i32) (param $h i32) + (local $btree i32) + (local.get $a) + (local.get $b) + (i32.gt_s) + (local.get $a) + (local.get $h) + (i32.ne) + (local.get $b) + (local.get $h) + (i32.ne) + (i32.and) + (i32.and) + (i32.eqz) + (if (then (unreachable))) + (i32.const 4) ;;create a tree with degree 4 + (call $createBtree) + (local.set $btree) + ;; insert variables + (local.get $a) + (call $btreeInsert) + (local.set $btree) + (local.get $b) + (call $btreeInsert) + (local.set $btree) + (local.get $h) + (call $btreeInsert) + (local.set $btree) + ;; search for variables & check that they were inserted + (local.get $btree) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (i32.and) + (i32.and) + ;; delete & check that it was deleted + ;; a + (local.get $btree) + (local.get $a) + (call $btreeDelete) + (local.tee $btree) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; b + (local.get $btree) + (local.get $b) + (call $btreeDelete) + (local.tee $btree) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + (i32.and) + (i32.and) + (drop) + ) + (export "main" (func $real_main)) + (func $real_main + i32.const 3 + i32.const 2 + i32.const 1 + call $main + ) + ) diff --git a/benchmarks/wasm/btree/2o1u-unlabeled.wat b/benchmarks/wasm/btree/2o1u-unlabeled.wat index 096f1004..51cb6d09 100644 --- a/benchmarks/wasm/btree/2o1u-unlabeled.wat +++ b/benchmarks/wasm/btree/2o1u-unlabeled.wat @@ -2624,7 +2624,10 @@ i32.eq i32.and i32.and - drop) + i32.eqz + if ;; label = @1 + unreachable + end) (func (;7;) (type 4) i32.const 3 i32.const 2 diff --git a/benchmarks/wasm/btree/2o1u.wat b/benchmarks/wasm/btree/2o1u.wat index 880e175a..b2ba0bb4 100644 --- a/benchmarks/wasm/btree/2o1u.wat +++ b/benchmarks/wasm/btree/2o1u.wat @@ -2808,7 +2808,9 @@ (i32.eq) (i32.and) (i32.and) - (drop) + ;; last functional check + (i32.eqz) + (if (then (unreachable))) ) (export "main" (func $real_main)) (func $real_main diff --git a/benchmarks/wasm/btree/2o2u-unlabeled.wat b/benchmarks/wasm/btree/2o2u-unlabeled.wat new file mode 100644 index 00000000..35888eb3 --- /dev/null +++ b/benchmarks/wasm/btree/2o2u-unlabeled.wat @@ -0,0 +1,2672 @@ +(module + (type (;0;) (func (param i32) (result i32))) + (type (;1;) (func (param i32 i32) (result i32))) + (type (;2;) (func (param i32 i32))) + (type (;3;) (func (param i32 i32 i32 i32))) + (type (;4;) (func)) + (func (;0;) (type 0) (param i32) (result i32) + i32.const 0 + local.get 0 + i32.store + i32.const 0 + i32.const 1 + i32.store offset=4 + i32.const 0 + i32.const 65536 + i32.store offset=8 + i32.const 1 + memory.grow + i32.const -1 + i32.ne + if ;; label = @1 + i32.const 65536 + i32.const 1 + i32.store + i32.const 65536 + i32.const 0 + i32.store offset=4 + end + i32.const 65536) + (func (;1;) (type 1) (param i32 i32) (result i32) + (local i32) + i32.const 0 + local.set 2 + block ;; label = @1 + loop ;; label = @2 + local.get 2 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.le_s + if ;; label = @3 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.gt_s + if ;; label = @4 + i32.const 1 + local.get 2 + i32.add + local.set 2 + br 2 (;@2;) + else + br 3 (;@1;) + end + else + br 2 (;@1;) + end + end + end + local.get 2 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.le_s + if (result i32) ;; label = @1 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.eq + if (result i32) ;; label = @2 + local.get 0 + i32.const 8 + i32.add + i32.const 4 + local.get 2 + i32.mul + i32.add + else + local.get 0 + i32.load + i32.const 1 + i32.eq + if (result i32) ;; label = @3 + i32.const -1 + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.get 1 + call 1 + end + end + else + local.get 0 + i32.load + i32.const 1 + i32.eq + if (result i32) ;; label = @2 + i32.const -1 + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.get 1 + call 1 + end + end) + (func (;2;) (type 2) (param i32 i32) + (local i32 i32) + i32.const 1 + memory.grow + i32.const -1 + i32.ne + if ;; label = @1 + i32.const 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.const 65536 + i32.mul + local.set 2 + i32.const 0 + i32.const 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load + i32.const 1 + i32.eq + if ;; label = @2 + local.get 2 + i32.const 1 + i32.store + else + local.get 2 + i32.const 0 + i32.store + end + local.get 2 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.store offset=4 + i32.const 0 + local.set 3 + block ;; label = @2 + loop ;; label = @3 + local.get 3 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.eq + if ;; label = @4 + br 2 (;@2;) + else + local.get 2 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@3;) + end + end + end + i32.const 0 + local.set 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load + i32.const 1 + i32.ne + if ;; label = @2 + block ;; label = @3 + loop ;; label = @4 + local.get 3 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.eq + if ;; label = @5 + br 2 (;@3;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 2 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.add + i32.const 4 + i32.mul + i32.add + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@4;) + end + end + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.store offset=4 + local.get 0 + i32.load offset=4 + local.set 3 + block ;; label = @2 + loop ;; label = @3 + local.get 1 + local.get 3 + i32.eq + if ;; label = @4 + br 2 (;@2;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.sub + local.set 3 + br 1 (;@3;) + end + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + local.get 2 + i32.store offset=8 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + local.set 3 + block ;; label = @2 + loop ;; label = @3 + local.get 1 + i32.const 1 + i32.sub + local.get 3 + i32.eq + if ;; label = @4 + br 2 (;@2;) + else + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + local.get 0 + i32.add + local.get 3 + i32.const 4 + i32.mul + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.sub + local.set 3 + br 1 (;@3;) + end + end + end + local.get 1 + i32.const 4 + i32.mul + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + end) + (func (;3;) (type 2) (param i32 i32) + (local i32) + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + local.set 2 + local.get 0 + i32.load + i32.const 1 + i32.eq + if ;; label = @1 + block ;; label = @2 + loop ;; label = @3 + local.get 2 + i32.const 0 + i32.ge_s + if (result i32) ;; label = @4 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.lt_s + else + i32.const 0 + end + local.get 2 + i32.const 0 + i32.ge_s + i32.and + if ;; label = @4 + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.add + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 2 + i32.const 1 + i32.sub + local.set 2 + br 1 (;@3;) + else + br 2 (;@2;) + end + end + end + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.add + i32.mul + i32.add + local.get 1 + i32.store offset=8 + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + else + block ;; label = @2 + loop ;; label = @3 + local.get 2 + i32.const 0 + i32.ge_s + if ;; label = @4 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.lt_s + if ;; label = @5 + local.get 2 + i32.const 1 + i32.sub + local.set 2 + br 2 (;@3;) + else + br 3 (;@2;) + end + else + br 2 (;@2;) + end + end + end + local.get 2 + i32.const 1 + i32.add + local.set 2 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.eq + if ;; label = @2 + local.get 0 + local.get 2 + call 2 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.gt_s + if ;; label = @3 + local.get 2 + i32.const 1 + i32.add + local.set 2 + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.get 1 + call 3 + end) + (func (;4;) (type 0) (param i32) (result i32) + (local i32 i32) + i32.const 0 + i32.load offset=8 + local.tee 2 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.eq + if (result i32) ;; label = @1 + i32.const 1 + memory.grow + i32.const -1 + i32.ne + if (result i32) ;; label = @2 + i32.const 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.const 65536 + i32.mul + local.set 1 + i32.const 0 + i32.const 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + i32.const 0 + local.get 1 + i32.store offset=8 + local.get 1 + i32.const 0 + i32.store + local.get 1 + i32.const 0 + i32.store offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.const 4 + i32.mul + i32.add + local.get 1 + i32.add + local.get 2 + i32.store offset=8 + local.get 1 + i32.const 0 + call 2 + local.get 1 + local.get 0 + call 3 + local.get 1 + else + i32.const -1 + end + else + local.get 2 + local.get 0 + call 3 + local.get 2 + end) + (func (;5;) (type 1) (param i32 i32) (result i32) + (local i32 i32 i32 i32) + local.get 0 + i32.load + i32.const 1 + i32.eq + if ;; label = @1 + i32.const 0 + local.set 2 + block ;; label = @2 + loop ;; label = @3 + local.get 0 + i32.load offset=4 + local.get 2 + i32.eq + if ;; label = @4 + br 2 (;@2;) + else + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + local.get 1 + i32.eq + if ;; label = @5 + local.get 2 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 0 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + br 3 (;@2;) + end + end + i32.const 1 + local.get 2 + i32.add + local.set 2 + br 0 (;@3;) + end + end + else + i32.const 0 + local.set 2 + block ;; label = @2 + loop ;; label = @3 + local.get 2 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.le_s + if (result i32) ;; label = @4 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.gt_s + else + i32.const 0 + end + local.get 2 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.le_s + i32.and + if ;; label = @4 + local.get 2 + i32.const 1 + i32.add + local.set 2 + br 1 (;@3;) + else + br 2 (;@2;) + end + end + end + local.get 2 + local.get 0 + i32.load offset=4 + i32.lt_s + if (result i32) ;; label = @2 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + local.get 1 + i32.eq + else + i32.const 0 + end + if ;; label = @2 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.tee 5 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.ge_s + if ;; label = @3 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + local.get 5 + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + call 5 + drop + i32.store offset=8 + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.tee 5 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.ge_s + if ;; label = @4 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + local.get 5 + i32.const 4 + i32.const 0 + i32.mul + i32.add + i32.load offset=8 + local.get 5 + local.get 5 + i32.const 4 + i32.const 0 + i32.mul + i32.add + i32.load offset=8 + call 5 + drop + i32.store offset=8 + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.set 5 + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + i32.mul + i32.add + local.get 1 + i32.store offset=8 + i32.const 0 + local.set 3 + block ;; label = @5 + loop ;; label = @6 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.eq + if ;; label = @7 + br 2 (;@5;) + else + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@6;) + end + end + end + local.get 5 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.add + i32.store offset=4 + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @5 + i32.const 0 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.add + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 5 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + end + local.get 2 + local.set 3 + block ;; label = @5 + loop ;; label = @6 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + local.get 3 + i32.eq + if ;; label = @7 + br 2 (;@5;) + else + local.get 0 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@6;) + end + end + end + local.get 2 + i32.const 1 + i32.add + local.set 3 + block ;; label = @5 + loop ;; label = @6 + local.get 0 + i32.load offset=4 + local.get 3 + i32.eq + if ;; label = @7 + br 2 (;@5;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@6;) + end + end + end + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + local.get 5 + local.get 1 + call 5 + drop + end + end + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.set 5 + local.get 5 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.eq + if ;; label = @3 + i32.const -1 + local.set 4 + local.get 2 + i32.const 1 + i32.add + local.get 0 + i32.load offset=4 + i32.le_s + if ;; label = @4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.ge_s + if ;; label = @5 + local.get 5 + i32.const 4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 5 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + i32.const 0 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + end + local.get 5 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.store offset=4 + i32.const 0 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.sub + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + local.set 3 + block ;; label = @7 + loop ;; label = @8 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.eq + if ;; label = @9 + br 2 (;@7;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@8;) + end + end + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + i32.const 0 + local.set 4 + end + end + local.get 4 + i32.const -1 + i32.eq + local.get 2 + i32.const 1 + i32.sub + i32.const 0 + i32.ge_s + i32.and + if ;; label = @4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.ge_s + if ;; label = @5 + local.get 5 + i32.load offset=4 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + i32.const 0 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 5 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 5 + i32.const 4 + local.get 3 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.sub + local.set 3 + br 1 (;@7;) + end + end + end + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + local.set 3 + block ;; label = @7 + loop ;; label = @8 + local.get 3 + i32.const 0 + i32.eq + if ;; label = @9 + br 2 (;@7;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.sub + local.set 3 + br 1 (;@8;) + end + end + end + end + local.get 5 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + local.get 5 + i32.const 4 + i32.const 0 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + end + i32.const 0 + local.set 4 + end + end + local.get 4 + i32.const -1 + i32.eq + if ;; label = @4 + local.get 2 + i32.const 1 + i32.add + local.get 0 + i32.load offset=4 + i32.le_s + if ;; label = @5 + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + i32.const 0 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + local.set 3 + block ;; label = @7 + loop ;; label = @8 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.add + i32.eq + if ;; label = @9 + br 2 (;@7;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 5 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@8;) + end + end + end + end + local.get 5 + local.get 5 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.add + i32.const 1 + i32.add + i32.store offset=4 + local.get 2 + i32.const 1 + i32.add + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 2 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 0 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + i32.const 0 + local.set 4 + end + local.get 4 + i32.const -1 + i32.eq + local.get 2 + i32.const 1 + i32.sub + i32.const 0 + i32.ge_s + i32.and + if ;; label = @5 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + i32.const 0 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 5 + i32.load offset=4 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.mul + i32.add + local.get 5 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + local.set 3 + block ;; label = @7 + loop ;; label = @8 + local.get 3 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + i32.eq + if ;; label = @9 + br 2 (;@7;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@8;) + end + end + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + local.get 5 + i32.load offset=4 + i32.add + i32.const 1 + i32.add + i32.store offset=4 + local.get 2 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 2 + i32.const 1 + i32.sub + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 0 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + end + end + end + local.get 4 + i32.const -1 + i32.eq + if ;; label = @3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.get 1 + call 5 + drop + else + local.get 5 + local.get 1 + call 5 + drop + end + end + end + i32.const 0 + i32.load offset=8 + i32.load offset=4 + i32.const 0 + i32.eq + if ;; label = @1 + i32.const 0 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + end + i32.const 0 + i32.load offset=8) + (func (;6;) (type 3) (param i32 i32 i32 i32) + (local i32) + local.get 0 + local.get 1 + i32.gt_s + local.get 2 + local.get 0 + i32.ne + local.get 2 + local.get 1 + i32.ne + local.get 3 + local.get 0 + i32.ne + local.get 3 + local.get 1 + i32.ne + local.get 3 + local.get 2 + i32.ne + i32.and + i32.and + i32.and + i32.and + i32.and + i32.eqz + if ;; label = @1 + unreachable + end + i32.const 4 + call 0 + local.set 4 + local.get 0 + call 4 + local.set 4 + local.get 1 + call 4 + local.set 4 + local.get 2 + call 4 + local.set 4 + local.get 3 + call 4 + local.set 4 + local.get 4 + local.get 0 + call 1 + i32.const -1 + i32.ne + local.get 4 + local.get 1 + call 1 + i32.const -1 + i32.ne + local.get 4 + local.get 2 + call 1 + i32.const -1 + i32.ne + local.get 4 + local.get 3 + call 1 + i32.const -1 + i32.ne + i32.and + i32.and + i32.and + local.get 4 + local.get 0 + call 5 + local.set 4 + local.get 4 + local.get 0 + call 1 + i32.const -1 + i32.eq + local.get 4 + local.get 1 + call 5 + local.set 4 + local.get 4 + local.get 1 + call 1 + i32.const -1 + i32.eq + local.get 4 + local.get 2 + call 5 + local.set 4 + local.get 4 + local.get 2 + call 1 + i32.const -1 + i32.eq + i32.and + i32.and + i32.and + i32.eqz + if ;; label = @1 + unreachable + end) + (func (;7;) (type 4) + i32.const 4 + i32.const 3 + i32.const 2 + i32.const 1 + call 6) + (memory (;0;) 2) + (export "main" (func 7)) + (start 7)) diff --git a/benchmarks/wasm/btree/2o2u.wat b/benchmarks/wasm/btree/2o2u.wat new file mode 100644 index 00000000..da6b9787 --- /dev/null +++ b/benchmarks/wasm/btree/2o2u.wat @@ -0,0 +1,2861 @@ +(module + (memory $0 2) + (func $createBtree (param i32) (result i32) ;; createBtree(t), where t: degree of the btree + (i32.const 0) + (local.get 0) + (i32.store) ;; store t at address 0 + (i32.const 0) + (i32.const 1) + (i32.store offset=4) ;; store the number of nodes in the btree = 1 + (i32.const 0) + (i32.const 65536) + (i32.store offset=8) ;; store the root addr + (i32.const 1) ;; Now create root + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then + (i32.const 65536) ;; 64KiB = 65536 bytes + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + (i32.const 65536) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + ) + ) + (i32.const 65536) ;; returns the address of the root + ) + ;; btreeSearch(x, k), where x: node address; k: key to search. Returns address of the key or -1 if key is not in tree. + (func $btreeSearch (param i32) (param i32) (result i32) (local i32) + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n -1 + (i32.le_s) ;; i <= x.children-1 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + (if + (then + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.le_s) ;; i <= x.children - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.eq) + ;; k == x.keys[i] + (if (result i32) + (then + (local.get 0) ;; save the address, which is x+8+i*4 + (i32.const 8) + (i32.add) ;; x+8 + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x+8+i*4 + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + ;; btreeSplitChild(x, i), where x: addr of a non full internal node; i: index such that x.children[i] is a full child of x + (func $btreeSplitChild (param i32) (param i32) (local i32) (local i32) + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then ;; get the number of nodes in the tree + (i32.const 0) + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address is 64KiB*(number of nodes+1) + (local.set 2) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the addr of the child in index i : y + (i32.load) ;; check if x.children[i] is leaf ;; load the first i32 in the child + (i32.const 1) + (i32.eq) ;; is y leaf? + (if + (then ;; yes + (local.get 2) ;; addr of new node z + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + ) + (else ;; no + (local.get 2) ;; addr of new node z + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + ) + ) + (local.get 2) ;; now store number of children, which will be t/2 -1 ;; addr of z + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.store offset=4) ;; store number of children = (t/2)-1 + (i32.const 0) ;; now to store the keys + (local.set 3) ;; counter = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.eq) ;; counter == (t/2)-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 2) ;; addr of z ;; we will store in the new node's address. z.keys[counter] + (i32.const 4) + (local.get 3) ;; counter + (i32.mul) ;; counter * 4 + (i32.add) ;; addr + counter * 4 -> where the key is goint to be located + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 4) ;; now get x.children[i].keys[counter+(t/2)] + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + (t/2) + (i32.mul) ;; counter + (t/2) *4 + (i32.add) ;; y + counter + (t/2) *4 + (i32.load offset=8) ;; y.keys[counter + (t/2)] + (i32.store offset=8) ;; z.keys[counter] = y.keys[counter + (t/2)] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) + (local.set 3) ;; set counter back to 0 + (i32.const 0) ;; check if x.children[i] is leaf + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load) ;; get first i32 in child --> isLeaf + (i32.const 1) + (i32.ne) ;; is leaf? + (if + (then ;; not leaf so we have to update the children + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.eq) ;; counter == (t/2) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; now get z.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; counter*4 + (i32.add) ;; (t-1)*4 + counter*4 + (local.get 2) ;; z + (i32.add) ;; z + (t-1)*4 + counter*4 --> address where addr of child is stored + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) ;; get x.children[i].children[counter+(t/2)] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + t/2 + (i32.const 4) + (i32.mul) ;; counter+t/2 * 4 + (i32.add) ;; (t-1)*4 + (counter+(t/2))*4 + (i32.add) ;; x.children[i] + (t-1)*4 + (counter + (t/2))*4 + (i32.load offset=8) ;; load the address of the child in index counter + t/2 + (i32.store offset=8) ;; z.children[counter] = x.children[i].children[counter] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.store offset=4) ;; number of keys of x.children[i] is now t/2 -1 + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (local.set 3) ;; counter = x.n + (block $loop_break + (loop $loop + (local.get 1) ;; i + (local.get 3) ;; counter + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; get x.children[counter + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (i32.add) ;; (t-1)*4 + (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter + 1)*4 -->address where addr of child is stored + (i32.const 0) ;; get x.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (i32.add) ;; (t-1)*4 + (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter)*4 + (i32.load offset=8) ;; load the addr of the child in index (counter) + (i32.store offset=8) ;; x.children[counter+1] = x.children[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 1) + (i32.add) ;; i + 1 + (i32.const 4) + (i32.mul) ;; (i + 1)*4 + (i32.add) ;; (t-1)*4 + (i + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i + 1)*4 --> addr where we will store the new addr of the child + (local.get 2) ;; addr of z + (i32.store offset=8) ;; x.children[i+1] = z + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (i32.const 1) + (i32.sub) + (local.set 3) ;; counter = x.n -1 + (block $loop_break + (loop $loop + (local.get 1) ;; i + (i32.const 1) + (i32.sub) ;; i-1 + (local.get 3) ;; counter + (i32.eq) ;; counter == i-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 3) ;; get x.keys[counter + 1] ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter + 1)*4 --> addr where we will store the new key + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter)*4 + (i32.load offset=8) ;; load the key in index (counter) + (i32.store offset=8) ;; x.keys[counter+1] = x.keys[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (local.get 1) ;; get x.keys[i] + (i32.const 4) + (i32.mul) ;; i*4 + (local.get 0) ;; x + (i32.add) ;; x +i*4 --> addr of the key + (i32.const 0) ;; get x.children[i].keys[t/2] + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.const 4) + (i32.mul) ;; (t/2)-1 *4 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; (i)*4 + (i32.add) ;; (t-1)*4 + (i)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i)*4 + (i32.load offset=8) ;; load the address of the child in index (i) + (i32.add) ;; y + (t/2)-1 *4 + (i32.load offset=8) ;; load the key in index (t/2)-1 from node x.children[i] + (i32.store offset=8) ;; store the new key in the addr + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) ;; x.n + 1 + (i32.store offset=4) ;; x.n = x.n + 1 + ) + ) + ) + ;; btreeInsertNonFull(x, k), where x: addr of a non full internal node; k: the key to insert + (func $btreeInsertNonFull (param i32) (param i32) (local i32) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n -1 + (local.set 2) ;; i = x.n -1 + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (i32.and) + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i *4 + (i32.add) ;; x + i *4 --> addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.keys[i+1] = x.keys[i] + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 1) ;; k + (i32.store offset=8) ;; x.keys[i+1] = k + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; x.n = x.n +1 + ) + (else ;; x is not leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i >= 0 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i -1 + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 2) ;;i = i+1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address of the child in index i = y + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load offset=4) ;; get y.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; y.n == t-1 --> node is full + (if + (then + (local.get 0) ;; x + (local.get 2) ;; i + (call $btreeSplitChild) + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k>x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i+1 + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (local.get 1) ;; k + (call $btreeInsertNonFull) + ) + ) + ) + (func $btreeInsert (param i32) (result i32) (local i32) (local i32) + (i32.const 0) + (i32.load offset=8) ;; root addr + (local.tee 2) ;; set r + (i32.load offset=4) ;; r.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; r.n == t-1 --> root is full + (if (result i32) + (then + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if (result i32) ;; if we can grow memory + (then + (i32.const 0) ;; get the number of nodes in the tree + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address of s is 64KiB*(number of nodes+1) + (local.set 1) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) + (local.get 1) ;; s + (i32.store offset=8) ;; root addr is now s + (local.get 1) ;; s + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + (local.get 1) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + (i32.const 0) ;; store children addresses + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 1) ;; x + (i32.add) ;; x + (t-1)*4 --> addre where addr of child number 0 is located + (local.get 2) + (i32.store offset=8) ;; s.children[0] = r + (local.get 1) ;; s + (i32.const 0) + (call $btreeSplitChild) + (local.get 1) ;; s + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 1) ;; return the address of the new root + ) + (else + (i32.const -1) ;; return -1 because we could not create a new node + ) + ) + ) + (else + (local.get 2) ;; r + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 2) ;; return the address of the root, which is the same + ) + ) + ) + ;; Returns the address of the new root (if applicable, otherwise returns the addres of the root) + (func $btreeDelete (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local i32) + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (local.get 2) ;; i + (i32.eq) ;; i == x.n + (if + (then + (br $loop_break) ;; break from loop + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + (if + (then + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $while_break + (loop $while + (local.get 3) ;; j + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n-1 + (i32.eq) ;; j == x.n-1 + (if + (then + (br $while_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 --> addr where x.keys[j] is stored + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.mul) ;; j+1 *4 + (i32.add) ;; x + j+1 *4 --> addr where x.keys[j+1] is stored + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $while) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n -1 + (br $loop_break) + ) + ) + ) + ) + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) ;; continue + ) + ) + ) + (else ;; x is not leaf + (i32.const 0) ;;get the index of the appropriate child/key + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (i32.and) ;; i <= x.n - 1 && k > x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i + 1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.lt_s) ;; i < x.n + (if (result i32) + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (if + (then ;; key is present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.tee 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (call $btreeDelete) ;; (x.c[i], x.c[i].keys[x.c[i].n - 1]) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i].keys[x.c[i].n - 1] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i+1] + (local.tee 5) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (local.get 5) ;; x.c[i+1] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (call $btreeDelete) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 --> addr where x.c[i].keys[x.c[i].n] will be stored + (local.get 1) ;; k + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = k + (i32.const 0) ;; now merge x.c[i] with x.c[i+1] + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if ;; j == x.c[i+1].n + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n + j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[index] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x.c[i+1] + i*4 ;; addr of x.c[i+1].keys[j] + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[index] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n +1 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + 1 + x.c[i+1].n + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; now adjust the children + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.get 3) ;; j + (i32.add) ;; index = x.c[i].n+j + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x.c[i] + (t-1)*4 --> address where x.c[i].c[x.c[i].n+j] is stored + (i32.const 0) ;;get x.c[i+1].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x.c[i+1] + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (i32.const 1) + (i32.sub) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) + (i32.mul) ;; j+1*4 + (i32.add) ;; x + j+1*4 ;; addr of x.keys[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = i+1 + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 0) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n-1 + (local.get 5) ;; x.c[i] + (local.get 1) ;; k + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) + (else ;; key not present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) + (i32.const 1) + (i32.sub) ;; t/2 - 1 + (i32.eq) + (if + (then + (i32.const -1) + (local.set 4) ;; changed = -1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.le_s) ;; i+1 <= x.n + (if + (then + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; index = t/2 -1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[t/2-1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[t/2-1] = x.keys[i] + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;;x.c[i].n = x.c[i].n +1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i+1].keys[0] + (i32.load offset=8) ;; x.c[i+1].keys[0] + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + (local.get 5) + (i32.load) ;; x.c[i] is leaf? + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) ;;get x.c[i].c[t/2] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; index = t/2 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[t/2] + (i32.const 0) ;;get x.c[i+1].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[0] + (i32.store offset=8) ;; x.c[i].c[t/2] = x.c[i+1].c[0] + ) + ) + (local.get 5) ;; x.c[i] + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.store offset=4) ;; x.c[i].n = t/2 + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 --> address where x.c[i+1].keys[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j+1] + (i32.store offset=8) ;; x.c[i+1].keys[j] = x.c[i+1].keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i+1].c[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j+1] + (i32.store offset=8) ;; x.c[i+1].c[j] = x.c[i+1].c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr where x.c[i+1] is located + (i32.load offset=8) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i+1].n = x.c[i+1].n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i -1 >= 0 + (i32.and) ;; changed == -1 && i -1 >= 0 + (if + (then + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i-1].n >= t/2 + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.set 3) ;; j = x.c[i].n + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j-1] + (i32.load offset=8) ;; x.c[i].keys[j-1] + (i32.store offset=8) ;; x.c[i].keys[j] = x.c[i].keys[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (local.set 3) ;; j = x.c[i].n + 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i].c[j] is located + (i32.const 0) ;;get x.c[i].c[j-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) + (i32.load offset=8) ;; x.c[i].c[j-1] + (i32.store offset=8) ;; x.c[i].c[j] = x.c[i].c[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; ;; x.c[i].n = x.c[i].n + 1 + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[0] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i-1] + (i32.store offset=8) ;; x.c[i].keys[0] = x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n - 1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[i-1] = x.c[i-1].keys[x.c[i-1].n] + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; get x.c[i].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 ;; addr where x.c[i].c[0] is located + (i32.const 0) ;; get x.c[i-1].c[x.c[i-1].n + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.add) ;; index = x.c[i-1.n + 1] + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1].c[x.c[i-1].n + 1] + (i32.store offset=8) ;; x.c[i].c[0] = x.c[i-1].c[x.c[i-1].n + 1] + ) + ) + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (if ;; changed == -1? + (then + (local.get 2) ;; we have to merge x.c[i] with one sibling;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.le_s) + (if ;; i+1 <= x.n + (then + (local.get 5) ;; merge with right sibling;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = x.keys[i] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n+j] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n+j] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i+1].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[x.c[i].n+j] + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) ;; x.c[i].n + x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + x.c[i+1].n + 1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.set 3) ;; j = i+1 + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) ;; changed == -1 ? + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i - 1 >= 0? + (i32.and) ;; changed == -1 && i - 1 >= 0 + (if + (then + (i32.const 0) ;; merge with left sibling ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n] = x.keys[i-1] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n+j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[j] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n+j] = x.c[i].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i-1].c[x.c[i-1].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i-1].c[x.c[i-1].n+j] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i].c[j] + (i32.store offset=8) ;; x.c[i-1].c[x.c[i-1].n+j] = x.c[i].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.add) ;; x.c[i-1].n + x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n + x.c[i].n + 1 + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = i - 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + ) + ) + ) + ) + ) + ) + (local.get 4) + (i32.const -1) + (i32.eq) + (if ;; changed == -1? if yes, we merged with left sibling + (then + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (local.get 1) + (call $btreeDelete) + (drop) + ) + (else + (local.get 5) ;; x.c[i] + (local.get 1) + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) ;; end of if x is not leaf + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.load offset=4) + (i32.const 0) + (i32.eq) + (if ;; if root is empty + (then + (i32.const 0) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; root.c[0] + (i32.store offset=8) ;; root = root.c[0] + ) + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + ) + (func $main + (param $a i32) + (param $b i32) + (param $h i32) + (param $i i32) + (local $btree i32) + (local.get $a) + (local.get $b) + (i32.gt_s) + (local.get $h) + (local.get $a) + (i32.ne) + (local.get $h) + (local.get $b) + (i32.ne) + (local.get $i) + (local.get $a) + (i32.ne) + (local.get $i) + (local.get $b) + (i32.ne) + (local.get $i) + (local.get $h) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.eqz) + (if (then (unreachable))) + (i32.const 4) ;;create a tree with degree 4 + (call $createBtree) + (local.set $btree) + ;; insert variables + (local.get $a) + (call $btreeInsert) + (local.set $btree) + (local.get $b) + (call $btreeInsert) + (local.set $btree) + (local.get $h) + (call $btreeInsert) + (local.set $btree) + (local.get $i) + (call $btreeInsert) + (local.set $btree) + ;; search for variables & check that they were inserted + (local.get $btree) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $i) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + ;; delete & check that it was deleted + ;; a + (local.get $btree) + (local.get $a) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; b + (local.get $btree) + (local.get $b) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; h + (local.get $btree) + (local.get $h) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + (i32.and) + (i32.and) + (i32.and) + ;; last functional check + (i32.eqz) + (if (then (unreachable))) + ) + (export "main" (func $real_main)) + (func $real_main + i32.const 4 + i32.const 3 + i32.const 2 + i32.const 1 + call $main + ) + (start $real_main) +) diff --git a/benchmarks/wasm/btree/2o3u-unlabeled.wat b/benchmarks/wasm/btree/2o3u-unlabeled.wat new file mode 100644 index 00000000..a14c893f --- /dev/null +++ b/benchmarks/wasm/btree/2o3u-unlabeled.wat @@ -0,0 +1,2708 @@ +(module + (type (;0;) (func (param i32) (result i32))) + (type (;1;) (func (param i32 i32) (result i32))) + (type (;2;) (func (param i32 i32))) + (type (;3;) (func (param i32 i32 i32 i32 i32))) + (type (;4;) (func)) + (func (;0;) (type 0) (param i32) (result i32) + i32.const 0 + local.get 0 + i32.store + i32.const 0 + i32.const 1 + i32.store offset=4 + i32.const 0 + i32.const 65536 + i32.store offset=8 + i32.const 1 + memory.grow + i32.const -1 + i32.ne + if ;; label = @1 + i32.const 65536 + i32.const 1 + i32.store + i32.const 65536 + i32.const 0 + i32.store offset=4 + end + i32.const 65536) + (func (;1;) (type 1) (param i32 i32) (result i32) + (local i32) + i32.const 0 + local.set 2 + block ;; label = @1 + loop ;; label = @2 + local.get 2 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.le_s + if ;; label = @3 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.gt_s + if ;; label = @4 + i32.const 1 + local.get 2 + i32.add + local.set 2 + br 2 (;@2;) + else + br 3 (;@1;) + end + else + br 2 (;@1;) + end + end + end + local.get 2 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.le_s + if (result i32) ;; label = @1 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.eq + if (result i32) ;; label = @2 + local.get 0 + i32.const 8 + i32.add + i32.const 4 + local.get 2 + i32.mul + i32.add + else + local.get 0 + i32.load + i32.const 1 + i32.eq + if (result i32) ;; label = @3 + i32.const -1 + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.get 1 + call 1 + end + end + else + local.get 0 + i32.load + i32.const 1 + i32.eq + if (result i32) ;; label = @2 + i32.const -1 + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.get 1 + call 1 + end + end) + (func (;2;) (type 2) (param i32 i32) + (local i32 i32) + i32.const 1 + memory.grow + i32.const -1 + i32.ne + if ;; label = @1 + i32.const 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.const 65536 + i32.mul + local.set 2 + i32.const 0 + i32.const 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load + i32.const 1 + i32.eq + if ;; label = @2 + local.get 2 + i32.const 1 + i32.store + else + local.get 2 + i32.const 0 + i32.store + end + local.get 2 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.store offset=4 + i32.const 0 + local.set 3 + block ;; label = @2 + loop ;; label = @3 + local.get 3 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.eq + if ;; label = @4 + br 2 (;@2;) + else + local.get 2 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@3;) + end + end + end + i32.const 0 + local.set 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load + i32.const 1 + i32.ne + if ;; label = @2 + block ;; label = @3 + loop ;; label = @4 + local.get 3 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.eq + if ;; label = @5 + br 2 (;@3;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 2 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.add + i32.const 4 + i32.mul + i32.add + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@4;) + end + end + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.store offset=4 + local.get 0 + i32.load offset=4 + local.set 3 + block ;; label = @2 + loop ;; label = @3 + local.get 1 + local.get 3 + i32.eq + if ;; label = @4 + br 2 (;@2;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.sub + local.set 3 + br 1 (;@3;) + end + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + local.get 2 + i32.store offset=8 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + local.set 3 + block ;; label = @2 + loop ;; label = @3 + local.get 1 + i32.const 1 + i32.sub + local.get 3 + i32.eq + if ;; label = @4 + br 2 (;@2;) + else + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + local.get 0 + i32.add + local.get 3 + i32.const 4 + i32.mul + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.sub + local.set 3 + br 1 (;@3;) + end + end + end + local.get 1 + i32.const 4 + i32.mul + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + end) + (func (;3;) (type 2) (param i32 i32) + (local i32) + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + local.set 2 + local.get 0 + i32.load + i32.const 1 + i32.eq + if ;; label = @1 + block ;; label = @2 + loop ;; label = @3 + local.get 2 + i32.const 0 + i32.ge_s + if (result i32) ;; label = @4 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.lt_s + else + i32.const 0 + end + local.get 2 + i32.const 0 + i32.ge_s + i32.and + if ;; label = @4 + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.add + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 2 + i32.const 1 + i32.sub + local.set 2 + br 1 (;@3;) + else + br 2 (;@2;) + end + end + end + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.add + i32.mul + i32.add + local.get 1 + i32.store offset=8 + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + else + block ;; label = @2 + loop ;; label = @3 + local.get 2 + i32.const 0 + i32.ge_s + if ;; label = @4 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.lt_s + if ;; label = @5 + local.get 2 + i32.const 1 + i32.sub + local.set 2 + br 2 (;@3;) + else + br 3 (;@2;) + end + else + br 2 (;@2;) + end + end + end + local.get 2 + i32.const 1 + i32.add + local.set 2 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.eq + if ;; label = @2 + local.get 0 + local.get 2 + call 2 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.gt_s + if ;; label = @3 + local.get 2 + i32.const 1 + i32.add + local.set 2 + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.get 1 + call 3 + end) + (func (;4;) (type 0) (param i32) (result i32) + (local i32 i32) + i32.const 0 + i32.load offset=8 + local.tee 2 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.eq + if (result i32) ;; label = @1 + i32.const 1 + memory.grow + i32.const -1 + i32.ne + if (result i32) ;; label = @2 + i32.const 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.const 65536 + i32.mul + local.set 1 + i32.const 0 + i32.const 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + i32.const 0 + local.get 1 + i32.store offset=8 + local.get 1 + i32.const 0 + i32.store + local.get 1 + i32.const 0 + i32.store offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.const 4 + i32.mul + i32.add + local.get 1 + i32.add + local.get 2 + i32.store offset=8 + local.get 1 + i32.const 0 + call 2 + local.get 1 + local.get 0 + call 3 + local.get 1 + else + i32.const -1 + end + else + local.get 2 + local.get 0 + call 3 + local.get 2 + end) + (func (;5;) (type 1) (param i32 i32) (result i32) + (local i32 i32 i32 i32) + local.get 0 + i32.load + i32.const 1 + i32.eq + if ;; label = @1 + i32.const 0 + local.set 2 + block ;; label = @2 + loop ;; label = @3 + local.get 0 + i32.load offset=4 + local.get 2 + i32.eq + if ;; label = @4 + br 2 (;@2;) + else + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + local.get 1 + i32.eq + if ;; label = @5 + local.get 2 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 0 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + br 3 (;@2;) + end + end + i32.const 1 + local.get 2 + i32.add + local.set 2 + br 0 (;@3;) + end + end + else + i32.const 0 + local.set 2 + block ;; label = @2 + loop ;; label = @3 + local.get 2 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.le_s + if (result i32) ;; label = @4 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.gt_s + else + i32.const 0 + end + local.get 2 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.le_s + i32.and + if ;; label = @4 + local.get 2 + i32.const 1 + i32.add + local.set 2 + br 1 (;@3;) + else + br 2 (;@2;) + end + end + end + local.get 2 + local.get 0 + i32.load offset=4 + i32.lt_s + if (result i32) ;; label = @2 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + local.get 1 + i32.eq + else + i32.const 0 + end + if ;; label = @2 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.tee 5 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.ge_s + if ;; label = @3 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + local.get 5 + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + call 5 + drop + i32.store offset=8 + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.tee 5 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.ge_s + if ;; label = @4 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + local.get 5 + i32.const 4 + i32.const 0 + i32.mul + i32.add + i32.load offset=8 + local.get 5 + local.get 5 + i32.const 4 + i32.const 0 + i32.mul + i32.add + i32.load offset=8 + call 5 + drop + i32.store offset=8 + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.set 5 + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + i32.mul + i32.add + local.get 1 + i32.store offset=8 + i32.const 0 + local.set 3 + block ;; label = @5 + loop ;; label = @6 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.eq + if ;; label = @7 + br 2 (;@5;) + else + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@6;) + end + end + end + local.get 5 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.add + i32.store offset=4 + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @5 + i32.const 0 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.add + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 5 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + end + local.get 2 + local.set 3 + block ;; label = @5 + loop ;; label = @6 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + local.get 3 + i32.eq + if ;; label = @7 + br 2 (;@5;) + else + local.get 0 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@6;) + end + end + end + local.get 2 + i32.const 1 + i32.add + local.set 3 + block ;; label = @5 + loop ;; label = @6 + local.get 0 + i32.load offset=4 + local.get 3 + i32.eq + if ;; label = @7 + br 2 (;@5;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@6;) + end + end + end + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + local.get 5 + local.get 1 + call 5 + drop + end + end + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.set 5 + local.get 5 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.eq + if ;; label = @3 + i32.const -1 + local.set 4 + local.get 2 + i32.const 1 + i32.add + local.get 0 + i32.load offset=4 + i32.le_s + if ;; label = @4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.ge_s + if ;; label = @5 + local.get 5 + i32.const 4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 5 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + i32.const 0 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + end + local.get 5 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.store offset=4 + i32.const 0 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.sub + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + local.set 3 + block ;; label = @7 + loop ;; label = @8 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.eq + if ;; label = @9 + br 2 (;@7;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@8;) + end + end + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + i32.const 0 + local.set 4 + end + end + local.get 4 + i32.const -1 + i32.eq + local.get 2 + i32.const 1 + i32.sub + i32.const 0 + i32.ge_s + i32.and + if ;; label = @4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.ge_s + if ;; label = @5 + local.get 5 + i32.load offset=4 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + i32.const 0 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 5 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 5 + i32.const 4 + local.get 3 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.sub + local.set 3 + br 1 (;@7;) + end + end + end + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + local.set 3 + block ;; label = @7 + loop ;; label = @8 + local.get 3 + i32.const 0 + i32.eq + if ;; label = @9 + br 2 (;@7;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.sub + local.set 3 + br 1 (;@8;) + end + end + end + end + local.get 5 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + local.get 5 + i32.const 4 + i32.const 0 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + end + i32.const 0 + local.set 4 + end + end + local.get 4 + i32.const -1 + i32.eq + if ;; label = @4 + local.get 2 + i32.const 1 + i32.add + local.get 0 + i32.load offset=4 + i32.le_s + if ;; label = @5 + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + i32.const 0 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + local.set 3 + block ;; label = @7 + loop ;; label = @8 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.add + i32.eq + if ;; label = @9 + br 2 (;@7;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 5 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@8;) + end + end + end + end + local.get 5 + local.get 5 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.add + i32.const 1 + i32.add + i32.store offset=4 + local.get 2 + i32.const 1 + i32.add + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 2 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 0 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + i32.const 0 + local.set 4 + end + local.get 4 + i32.const -1 + i32.eq + local.get 2 + i32.const 1 + i32.sub + i32.const 0 + i32.ge_s + i32.and + if ;; label = @5 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + i32.const 0 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 5 + i32.load offset=4 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.mul + i32.add + local.get 5 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + local.set 3 + block ;; label = @7 + loop ;; label = @8 + local.get 3 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + i32.eq + if ;; label = @9 + br 2 (;@7;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@8;) + end + end + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + local.get 5 + i32.load offset=4 + i32.add + i32.const 1 + i32.add + i32.store offset=4 + local.get 2 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 2 + i32.const 1 + i32.sub + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 0 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + end + end + end + local.get 4 + i32.const -1 + i32.eq + if ;; label = @3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.get 1 + call 5 + drop + else + local.get 5 + local.get 1 + call 5 + drop + end + end + end + i32.const 0 + i32.load offset=8 + i32.load offset=4 + i32.const 0 + i32.eq + if ;; label = @1 + i32.const 0 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + end + i32.const 0 + i32.load offset=8) + (func (;6;) (type 3) (param i32 i32 i32 i32 i32) + (local i32) + local.get 0 + local.get 1 + i32.gt_s + local.get 2 + local.get 0 + i32.ne + local.get 2 + local.get 1 + i32.ne + local.get 3 + local.get 0 + i32.ne + local.get 3 + local.get 1 + i32.ne + local.get 3 + local.get 2 + i32.ne + local.get 4 + local.get 0 + i32.ne + local.get 4 + local.get 1 + i32.ne + local.get 4 + local.get 2 + i32.ne + local.get 4 + local.get 3 + i32.ne + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.eqz + if ;; label = @1 + unreachable + end + i32.const 4 + call 0 + local.set 5 + local.get 0 + call 4 + local.set 5 + local.get 1 + call 4 + local.set 5 + local.get 2 + call 4 + local.set 5 + local.get 3 + call 4 + local.set 5 + local.get 4 + call 4 + local.set 5 + local.get 5 + local.get 0 + call 1 + i32.const -1 + i32.ne + local.get 5 + local.get 1 + call 1 + i32.const -1 + i32.ne + local.get 5 + local.get 2 + call 1 + i32.const -1 + i32.ne + local.get 5 + local.get 3 + call 1 + i32.const -1 + i32.ne + local.get 5 + local.get 4 + call 1 + i32.const -1 + i32.ne + i32.and + i32.and + i32.and + i32.and + local.get 5 + local.get 0 + call 5 + local.set 5 + local.get 5 + local.get 0 + call 1 + i32.const -1 + i32.eq + local.get 5 + local.get 1 + call 5 + local.set 5 + local.get 5 + local.get 1 + call 1 + i32.const -1 + i32.eq + local.get 5 + local.get 2 + call 5 + local.set 5 + local.get 5 + local.get 2 + call 1 + i32.const -1 + i32.eq + local.get 5 + local.get 3 + call 5 + local.set 5 + local.get 5 + local.get 3 + call 1 + i32.const -1 + i32.eq + i32.and + i32.and + i32.and + i32.and + i32.eqz + if ;; label = @1 + unreachable + end) + (func (;7;) (type 4) + i32.const 5 + i32.const 4 + i32.const 3 + i32.const 2 + i32.const 1 + call 6) + (memory (;0;) 2) + (export "main" (func 7)) + (start 7)) diff --git a/benchmarks/wasm/btree/2o3u.wat b/benchmarks/wasm/btree/2o3u.wat new file mode 100644 index 00000000..681698d4 --- /dev/null +++ b/benchmarks/wasm/btree/2o3u.wat @@ -0,0 +1,2899 @@ +(module + (memory $0 2) + (func $createBtree (param i32) (result i32) ;; createBtree(t), where t: degree of the btree + (i32.const 0) + (local.get 0) + (i32.store) ;; store t at address 0 + (i32.const 0) + (i32.const 1) + (i32.store offset=4) ;; store the number of nodes in the btree = 1 + (i32.const 0) + (i32.const 65536) + (i32.store offset=8) ;; store the root addr + (i32.const 1) ;; Now create root + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then + (i32.const 65536) ;; 64KiB = 65536 bytes + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + (i32.const 65536) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + ) + ) + (i32.const 65536) ;; returns the address of the root + ) + ;; btreeSearch(x, k), where x: node address; k: key to search. Returns address of the key or -1 if key is not in tree. + (func $btreeSearch (param i32) (param i32) (result i32) (local i32) + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n -1 + (i32.le_s) ;; i <= x.children-1 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + (if + (then + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.le_s) ;; i <= x.children - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.eq) + ;; k == x.keys[i] + (if (result i32) + (then + (local.get 0) ;; save the address, which is x+8+i*4 + (i32.const 8) + (i32.add) ;; x+8 + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x+8+i*4 + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + ;; btreeSplitChild(x, i), where x: addr of a non full internal node; i: index such that x.children[i] is a full child of x + (func $btreeSplitChild (param i32) (param i32) (local i32) (local i32) + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then ;; get the number of nodes in the tree + (i32.const 0) + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address is 64KiB*(number of nodes+1) + (local.set 2) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the addr of the child in index i : y + (i32.load) ;; check if x.children[i] is leaf ;; load the first i32 in the child + (i32.const 1) + (i32.eq) ;; is y leaf? + (if + (then ;; yes + (local.get 2) ;; addr of new node z + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + ) + (else ;; no + (local.get 2) ;; addr of new node z + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + ) + ) + (local.get 2) ;; now store number of children, which will be t/2 -1 ;; addr of z + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.store offset=4) ;; store number of children = (t/2)-1 + (i32.const 0) ;; now to store the keys + (local.set 3) ;; counter = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.eq) ;; counter == (t/2)-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 2) ;; addr of z ;; we will store in the new node's address. z.keys[counter] + (i32.const 4) + (local.get 3) ;; counter + (i32.mul) ;; counter * 4 + (i32.add) ;; addr + counter * 4 -> where the key is goint to be located + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 4) ;; now get x.children[i].keys[counter+(t/2)] + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + (t/2) + (i32.mul) ;; counter + (t/2) *4 + (i32.add) ;; y + counter + (t/2) *4 + (i32.load offset=8) ;; y.keys[counter + (t/2)] + (i32.store offset=8) ;; z.keys[counter] = y.keys[counter + (t/2)] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) + (local.set 3) ;; set counter back to 0 + (i32.const 0) ;; check if x.children[i] is leaf + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load) ;; get first i32 in child --> isLeaf + (i32.const 1) + (i32.ne) ;; is leaf? + (if + (then ;; not leaf so we have to update the children + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.eq) ;; counter == (t/2) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; now get z.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; counter*4 + (i32.add) ;; (t-1)*4 + counter*4 + (local.get 2) ;; z + (i32.add) ;; z + (t-1)*4 + counter*4 --> address where addr of child is stored + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) ;; get x.children[i].children[counter+(t/2)] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + t/2 + (i32.const 4) + (i32.mul) ;; counter+t/2 * 4 + (i32.add) ;; (t-1)*4 + (counter+(t/2))*4 + (i32.add) ;; x.children[i] + (t-1)*4 + (counter + (t/2))*4 + (i32.load offset=8) ;; load the address of the child in index counter + t/2 + (i32.store offset=8) ;; z.children[counter] = x.children[i].children[counter] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.store offset=4) ;; number of keys of x.children[i] is now t/2 -1 + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (local.set 3) ;; counter = x.n + (block $loop_break + (loop $loop + (local.get 1) ;; i + (local.get 3) ;; counter + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; get x.children[counter + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (i32.add) ;; (t-1)*4 + (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter + 1)*4 -->address where addr of child is stored + (i32.const 0) ;; get x.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (i32.add) ;; (t-1)*4 + (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter)*4 + (i32.load offset=8) ;; load the addr of the child in index (counter) + (i32.store offset=8) ;; x.children[counter+1] = x.children[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 1) + (i32.add) ;; i + 1 + (i32.const 4) + (i32.mul) ;; (i + 1)*4 + (i32.add) ;; (t-1)*4 + (i + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i + 1)*4 --> addr where we will store the new addr of the child + (local.get 2) ;; addr of z + (i32.store offset=8) ;; x.children[i+1] = z + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (i32.const 1) + (i32.sub) + (local.set 3) ;; counter = x.n -1 + (block $loop_break + (loop $loop + (local.get 1) ;; i + (i32.const 1) + (i32.sub) ;; i-1 + (local.get 3) ;; counter + (i32.eq) ;; counter == i-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 3) ;; get x.keys[counter + 1] ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter + 1)*4 --> addr where we will store the new key + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter)*4 + (i32.load offset=8) ;; load the key in index (counter) + (i32.store offset=8) ;; x.keys[counter+1] = x.keys[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (local.get 1) ;; get x.keys[i] + (i32.const 4) + (i32.mul) ;; i*4 + (local.get 0) ;; x + (i32.add) ;; x +i*4 --> addr of the key + (i32.const 0) ;; get x.children[i].keys[t/2] + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.const 4) + (i32.mul) ;; (t/2)-1 *4 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; (i)*4 + (i32.add) ;; (t-1)*4 + (i)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i)*4 + (i32.load offset=8) ;; load the address of the child in index (i) + (i32.add) ;; y + (t/2)-1 *4 + (i32.load offset=8) ;; load the key in index (t/2)-1 from node x.children[i] + (i32.store offset=8) ;; store the new key in the addr + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) ;; x.n + 1 + (i32.store offset=4) ;; x.n = x.n + 1 + ) + ) + ) + ;; btreeInsertNonFull(x, k), where x: addr of a non full internal node; k: the key to insert + (func $btreeInsertNonFull (param i32) (param i32) (local i32) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n -1 + (local.set 2) ;; i = x.n -1 + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (i32.and) + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i *4 + (i32.add) ;; x + i *4 --> addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.keys[i+1] = x.keys[i] + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 1) ;; k + (i32.store offset=8) ;; x.keys[i+1] = k + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; x.n = x.n +1 + ) + (else ;; x is not leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i >= 0 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i -1 + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 2) ;;i = i+1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address of the child in index i = y + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load offset=4) ;; get y.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; y.n == t-1 --> node is full + (if + (then + (local.get 0) ;; x + (local.get 2) ;; i + (call $btreeSplitChild) + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k>x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i+1 + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (local.get 1) ;; k + (call $btreeInsertNonFull) + ) + ) + ) + (func $btreeInsert (param i32) (result i32) (local i32) (local i32) + (i32.const 0) + (i32.load offset=8) ;; root addr + (local.tee 2) ;; set r + (i32.load offset=4) ;; r.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; r.n == t-1 --> root is full + (if (result i32) + (then + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if (result i32) ;; if we can grow memory + (then + (i32.const 0) ;; get the number of nodes in the tree + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address of s is 64KiB*(number of nodes+1) + (local.set 1) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) + (local.get 1) ;; s + (i32.store offset=8) ;; root addr is now s + (local.get 1) ;; s + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + (local.get 1) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + (i32.const 0) ;; store children addresses + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 1) ;; x + (i32.add) ;; x + (t-1)*4 --> addre where addr of child number 0 is located + (local.get 2) + (i32.store offset=8) ;; s.children[0] = r + (local.get 1) ;; s + (i32.const 0) + (call $btreeSplitChild) + (local.get 1) ;; s + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 1) ;; return the address of the new root + ) + (else + (i32.const -1) ;; return -1 because we could not create a new node + ) + ) + ) + (else + (local.get 2) ;; r + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 2) ;; return the address of the root, which is the same + ) + ) + ) + ;; Returns the address of the new root (if applicable, otherwise returns the addres of the root) + (func $btreeDelete (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local i32) + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (local.get 2) ;; i + (i32.eq) ;; i == x.n + (if + (then + (br $loop_break) ;; break from loop + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + (if + (then + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $while_break + (loop $while + (local.get 3) ;; j + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n-1 + (i32.eq) ;; j == x.n-1 + (if + (then + (br $while_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 --> addr where x.keys[j] is stored + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.mul) ;; j+1 *4 + (i32.add) ;; x + j+1 *4 --> addr where x.keys[j+1] is stored + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $while) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n -1 + (br $loop_break) + ) + ) + ) + ) + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) ;; continue + ) + ) + ) + (else ;; x is not leaf + (i32.const 0) ;;get the index of the appropriate child/key + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (i32.and) ;; i <= x.n - 1 && k > x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i + 1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.lt_s) ;; i < x.n + (if (result i32) + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (if + (then ;; key is present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.tee 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (call $btreeDelete) ;; (x.c[i], x.c[i].keys[x.c[i].n - 1]) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i].keys[x.c[i].n - 1] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i+1] + (local.tee 5) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (local.get 5) ;; x.c[i+1] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (call $btreeDelete) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 --> addr where x.c[i].keys[x.c[i].n] will be stored + (local.get 1) ;; k + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = k + (i32.const 0) ;; now merge x.c[i] with x.c[i+1] + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if ;; j == x.c[i+1].n + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n + j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[index] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x.c[i+1] + i*4 ;; addr of x.c[i+1].keys[j] + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[index] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n +1 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + 1 + x.c[i+1].n + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; now adjust the children + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.get 3) ;; j + (i32.add) ;; index = x.c[i].n+j + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x.c[i] + (t-1)*4 --> address where x.c[i].c[x.c[i].n+j] is stored + (i32.const 0) ;;get x.c[i+1].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x.c[i+1] + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (i32.const 1) + (i32.sub) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) + (i32.mul) ;; j+1*4 + (i32.add) ;; x + j+1*4 ;; addr of x.keys[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = i+1 + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 0) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n-1 + (local.get 5) ;; x.c[i] + (local.get 1) ;; k + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) + (else ;; key not present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) + (i32.const 1) + (i32.sub) ;; t/2 - 1 + (i32.eq) + (if + (then + (i32.const -1) + (local.set 4) ;; changed = -1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.le_s) ;; i+1 <= x.n + (if + (then + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; index = t/2 -1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[t/2-1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[t/2-1] = x.keys[i] + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;;x.c[i].n = x.c[i].n +1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i+1].keys[0] + (i32.load offset=8) ;; x.c[i+1].keys[0] + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + (local.get 5) + (i32.load) ;; x.c[i] is leaf? + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) ;;get x.c[i].c[t/2] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; index = t/2 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[t/2] + (i32.const 0) ;;get x.c[i+1].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[0] + (i32.store offset=8) ;; x.c[i].c[t/2] = x.c[i+1].c[0] + ) + ) + (local.get 5) ;; x.c[i] + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.store offset=4) ;; x.c[i].n = t/2 + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 --> address where x.c[i+1].keys[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j+1] + (i32.store offset=8) ;; x.c[i+1].keys[j] = x.c[i+1].keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i+1].c[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j+1] + (i32.store offset=8) ;; x.c[i+1].c[j] = x.c[i+1].c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr where x.c[i+1] is located + (i32.load offset=8) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i+1].n = x.c[i+1].n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i -1 >= 0 + (i32.and) ;; changed == -1 && i -1 >= 0 + (if + (then + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i-1].n >= t/2 + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.set 3) ;; j = x.c[i].n + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j-1] + (i32.load offset=8) ;; x.c[i].keys[j-1] + (i32.store offset=8) ;; x.c[i].keys[j] = x.c[i].keys[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (local.set 3) ;; j = x.c[i].n + 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i].c[j] is located + (i32.const 0) ;;get x.c[i].c[j-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) + (i32.load offset=8) ;; x.c[i].c[j-1] + (i32.store offset=8) ;; x.c[i].c[j] = x.c[i].c[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; ;; x.c[i].n = x.c[i].n + 1 + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[0] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i-1] + (i32.store offset=8) ;; x.c[i].keys[0] = x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n - 1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[i-1] = x.c[i-1].keys[x.c[i-1].n] + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; get x.c[i].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 ;; addr where x.c[i].c[0] is located + (i32.const 0) ;; get x.c[i-1].c[x.c[i-1].n + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.add) ;; index = x.c[i-1.n + 1] + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1].c[x.c[i-1].n + 1] + (i32.store offset=8) ;; x.c[i].c[0] = x.c[i-1].c[x.c[i-1].n + 1] + ) + ) + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (if ;; changed == -1? + (then + (local.get 2) ;; we have to merge x.c[i] with one sibling;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.le_s) + (if ;; i+1 <= x.n + (then + (local.get 5) ;; merge with right sibling;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = x.keys[i] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n+j] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n+j] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i+1].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[x.c[i].n+j] + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) ;; x.c[i].n + x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + x.c[i+1].n + 1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.set 3) ;; j = i+1 + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) ;; changed == -1 ? + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i - 1 >= 0? + (i32.and) ;; changed == -1 && i - 1 >= 0 + (if + (then + (i32.const 0) ;; merge with left sibling ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n] = x.keys[i-1] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n+j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[j] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n+j] = x.c[i].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i-1].c[x.c[i-1].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i-1].c[x.c[i-1].n+j] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i].c[j] + (i32.store offset=8) ;; x.c[i-1].c[x.c[i-1].n+j] = x.c[i].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.add) ;; x.c[i-1].n + x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n + x.c[i].n + 1 + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = i - 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + ) + ) + ) + ) + ) + ) + (local.get 4) + (i32.const -1) + (i32.eq) + (if ;; changed == -1? if yes, we merged with left sibling + (then + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (local.get 1) + (call $btreeDelete) + (drop) + ) + (else + (local.get 5) ;; x.c[i] + (local.get 1) + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) ;; end of if x is not leaf + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.load offset=4) + (i32.const 0) + (i32.eq) + (if ;; if root is empty + (then + (i32.const 0) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; root.c[0] + (i32.store offset=8) ;; root = root.c[0] + ) + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + ) + (func $main + (param $a i32) + (param $b i32) + (param $h i32) + (param $i i32) + (param $j i32) + (local $btree i32) + (local.get $a) + (local.get $b) + (i32.gt_s) + (local.get $h) + (local.get $a) + (i32.ne) + (local.get $h) + (local.get $b) + (i32.ne) + (local.get $i) + (local.get $a) + (i32.ne) + (local.get $i) + (local.get $b) + (i32.ne) + (local.get $i) + (local.get $h) + (i32.ne) + (local.get $j) + (local.get $a) + (i32.ne) + (local.get $j) + (local.get $b) + (i32.ne) + (local.get $j) + (local.get $h) + (i32.ne) + (local.get $j) + (local.get $i) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.eqz) + (if (then (unreachable))) + (i32.const 4) ;;create a tree with degree 4 + (call $createBtree) + (local.set $btree) + ;; insert variables + (local.get $a) + (call $btreeInsert) + (local.set $btree) + (local.get $b) + (call $btreeInsert) + (local.set $btree) + (local.get $h) + (call $btreeInsert) + (local.set $btree) + (local.get $i) + (call $btreeInsert) + (local.set $btree) + (local.get $j) + (call $btreeInsert) + (local.set $btree) + ;; search for variables & check that they were inserted + (local.get $btree) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $i) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $j) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + ;; delete & check that it was deleted + ;; a + (local.get $btree) + (local.get $a) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; b + (local.get $btree) + (local.get $b) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; h + (local.get $btree) + (local.get $h) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; i + (local.get $btree) + (local.get $i) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $i) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + ;; last functional check + (i32.eqz) + (if (then (unreachable))) + ) + (export "main" (func $real_main)) + (func $real_main + i32.const 5 + i32.const 4 + i32.const 3 + i32.const 2 + i32.const 1 + call $main + ) + (start $real_main) +) diff --git a/benchmarks/wasm/btree/3o1u-unlabeled.wat b/benchmarks/wasm/btree/3o1u-unlabeled.wat new file mode 100644 index 00000000..033a24d7 --- /dev/null +++ b/benchmarks/wasm/btree/3o1u-unlabeled.wat @@ -0,0 +1,2668 @@ +(module + (type (;0;) (func (param i32) (result i32))) + (type (;1;) (func (param i32 i32) (result i32))) + (type (;2;) (func (param i32 i32))) + (type (;3;) (func (param i32 i32 i32 i32))) + (type (;4;) (func)) + (func (;0;) (type 0) (param i32) (result i32) + i32.const 0 + local.get 0 + i32.store + i32.const 0 + i32.const 1 + i32.store offset=4 + i32.const 0 + i32.const 65536 + i32.store offset=8 + i32.const 1 + memory.grow + i32.const -1 + i32.ne + if ;; label = @1 + i32.const 65536 + i32.const 1 + i32.store + i32.const 65536 + i32.const 0 + i32.store offset=4 + end + i32.const 65536) + (func (;1;) (type 1) (param i32 i32) (result i32) + (local i32) + i32.const 0 + local.set 2 + block ;; label = @1 + loop ;; label = @2 + local.get 2 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.le_s + if ;; label = @3 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.gt_s + if ;; label = @4 + i32.const 1 + local.get 2 + i32.add + local.set 2 + br 2 (;@2;) + else + br 3 (;@1;) + end + else + br 2 (;@1;) + end + end + end + local.get 2 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.le_s + if (result i32) ;; label = @1 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.eq + if (result i32) ;; label = @2 + local.get 0 + i32.const 8 + i32.add + i32.const 4 + local.get 2 + i32.mul + i32.add + else + local.get 0 + i32.load + i32.const 1 + i32.eq + if (result i32) ;; label = @3 + i32.const -1 + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.get 1 + call 1 + end + end + else + local.get 0 + i32.load + i32.const 1 + i32.eq + if (result i32) ;; label = @2 + i32.const -1 + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.get 1 + call 1 + end + end) + (func (;2;) (type 2) (param i32 i32) + (local i32 i32) + i32.const 1 + memory.grow + i32.const -1 + i32.ne + if ;; label = @1 + i32.const 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.const 65536 + i32.mul + local.set 2 + i32.const 0 + i32.const 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load + i32.const 1 + i32.eq + if ;; label = @2 + local.get 2 + i32.const 1 + i32.store + else + local.get 2 + i32.const 0 + i32.store + end + local.get 2 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.store offset=4 + i32.const 0 + local.set 3 + block ;; label = @2 + loop ;; label = @3 + local.get 3 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.eq + if ;; label = @4 + br 2 (;@2;) + else + local.get 2 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@3;) + end + end + end + i32.const 0 + local.set 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load + i32.const 1 + i32.ne + if ;; label = @2 + block ;; label = @3 + loop ;; label = @4 + local.get 3 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.eq + if ;; label = @5 + br 2 (;@3;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 2 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.add + i32.const 4 + i32.mul + i32.add + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@4;) + end + end + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.store offset=4 + local.get 0 + i32.load offset=4 + local.set 3 + block ;; label = @2 + loop ;; label = @3 + local.get 1 + local.get 3 + i32.eq + if ;; label = @4 + br 2 (;@2;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.sub + local.set 3 + br 1 (;@3;) + end + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + local.get 2 + i32.store offset=8 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + local.set 3 + block ;; label = @2 + loop ;; label = @3 + local.get 1 + i32.const 1 + i32.sub + local.get 3 + i32.eq + if ;; label = @4 + br 2 (;@2;) + else + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + local.get 0 + i32.add + local.get 3 + i32.const 4 + i32.mul + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.sub + local.set 3 + br 1 (;@3;) + end + end + end + local.get 1 + i32.const 4 + i32.mul + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + end) + (func (;3;) (type 2) (param i32 i32) + (local i32) + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + local.set 2 + local.get 0 + i32.load + i32.const 1 + i32.eq + if ;; label = @1 + block ;; label = @2 + loop ;; label = @3 + local.get 2 + i32.const 0 + i32.ge_s + if (result i32) ;; label = @4 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.lt_s + else + i32.const 0 + end + local.get 2 + i32.const 0 + i32.ge_s + i32.and + if ;; label = @4 + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.add + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 2 + i32.const 1 + i32.sub + local.set 2 + br 1 (;@3;) + else + br 2 (;@2;) + end + end + end + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.add + i32.mul + i32.add + local.get 1 + i32.store offset=8 + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + else + block ;; label = @2 + loop ;; label = @3 + local.get 2 + i32.const 0 + i32.ge_s + if ;; label = @4 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.lt_s + if ;; label = @5 + local.get 2 + i32.const 1 + i32.sub + local.set 2 + br 2 (;@3;) + else + br 3 (;@2;) + end + else + br 2 (;@2;) + end + end + end + local.get 2 + i32.const 1 + i32.add + local.set 2 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.eq + if ;; label = @2 + local.get 0 + local.get 2 + call 2 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.gt_s + if ;; label = @3 + local.get 2 + i32.const 1 + i32.add + local.set 2 + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.get 1 + call 3 + end) + (func (;4;) (type 0) (param i32) (result i32) + (local i32 i32) + i32.const 0 + i32.load offset=8 + local.tee 2 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.eq + if (result i32) ;; label = @1 + i32.const 1 + memory.grow + i32.const -1 + i32.ne + if (result i32) ;; label = @2 + i32.const 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.const 65536 + i32.mul + local.set 1 + i32.const 0 + i32.const 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + i32.const 0 + local.get 1 + i32.store offset=8 + local.get 1 + i32.const 0 + i32.store + local.get 1 + i32.const 0 + i32.store offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.const 4 + i32.mul + i32.add + local.get 1 + i32.add + local.get 2 + i32.store offset=8 + local.get 1 + i32.const 0 + call 2 + local.get 1 + local.get 0 + call 3 + local.get 1 + else + i32.const -1 + end + else + local.get 2 + local.get 0 + call 3 + local.get 2 + end) + (func (;5;) (type 1) (param i32 i32) (result i32) + (local i32 i32 i32 i32) + local.get 0 + i32.load + i32.const 1 + i32.eq + if ;; label = @1 + i32.const 0 + local.set 2 + block ;; label = @2 + loop ;; label = @3 + local.get 0 + i32.load offset=4 + local.get 2 + i32.eq + if ;; label = @4 + br 2 (;@2;) + else + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + local.get 1 + i32.eq + if ;; label = @5 + local.get 2 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 0 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + br 3 (;@2;) + end + end + i32.const 1 + local.get 2 + i32.add + local.set 2 + br 0 (;@3;) + end + end + else + i32.const 0 + local.set 2 + block ;; label = @2 + loop ;; label = @3 + local.get 2 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.le_s + if (result i32) ;; label = @4 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.gt_s + else + i32.const 0 + end + local.get 2 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.le_s + i32.and + if ;; label = @4 + local.get 2 + i32.const 1 + i32.add + local.set 2 + br 1 (;@3;) + else + br 2 (;@2;) + end + end + end + local.get 2 + local.get 0 + i32.load offset=4 + i32.lt_s + if (result i32) ;; label = @2 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + local.get 1 + i32.eq + else + i32.const 0 + end + if ;; label = @2 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.tee 5 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.ge_s + if ;; label = @3 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + local.get 5 + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + call 5 + drop + i32.store offset=8 + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.tee 5 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.ge_s + if ;; label = @4 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + local.get 5 + i32.const 4 + i32.const 0 + i32.mul + i32.add + i32.load offset=8 + local.get 5 + local.get 5 + i32.const 4 + i32.const 0 + i32.mul + i32.add + i32.load offset=8 + call 5 + drop + i32.store offset=8 + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.set 5 + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + i32.mul + i32.add + local.get 1 + i32.store offset=8 + i32.const 0 + local.set 3 + block ;; label = @5 + loop ;; label = @6 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.eq + if ;; label = @7 + br 2 (;@5;) + else + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@6;) + end + end + end + local.get 5 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.add + i32.store offset=4 + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @5 + i32.const 0 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.add + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 5 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + end + local.get 2 + local.set 3 + block ;; label = @5 + loop ;; label = @6 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + local.get 3 + i32.eq + if ;; label = @7 + br 2 (;@5;) + else + local.get 0 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@6;) + end + end + end + local.get 2 + i32.const 1 + i32.add + local.set 3 + block ;; label = @5 + loop ;; label = @6 + local.get 0 + i32.load offset=4 + local.get 3 + i32.eq + if ;; label = @7 + br 2 (;@5;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@6;) + end + end + end + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + local.get 5 + local.get 1 + call 5 + drop + end + end + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.set 5 + local.get 5 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.eq + if ;; label = @3 + i32.const -1 + local.set 4 + local.get 2 + i32.const 1 + i32.add + local.get 0 + i32.load offset=4 + i32.le_s + if ;; label = @4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.ge_s + if ;; label = @5 + local.get 5 + i32.const 4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 5 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + i32.const 0 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + end + local.get 5 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.store offset=4 + i32.const 0 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.sub + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + local.set 3 + block ;; label = @7 + loop ;; label = @8 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.eq + if ;; label = @9 + br 2 (;@7;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@8;) + end + end + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + i32.const 0 + local.set 4 + end + end + local.get 4 + i32.const -1 + i32.eq + local.get 2 + i32.const 1 + i32.sub + i32.const 0 + i32.ge_s + i32.and + if ;; label = @4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.ge_s + if ;; label = @5 + local.get 5 + i32.load offset=4 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + i32.const 0 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 5 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 5 + i32.const 4 + local.get 3 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.sub + local.set 3 + br 1 (;@7;) + end + end + end + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + local.set 3 + block ;; label = @7 + loop ;; label = @8 + local.get 3 + i32.const 0 + i32.eq + if ;; label = @9 + br 2 (;@7;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.sub + local.set 3 + br 1 (;@8;) + end + end + end + end + local.get 5 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + local.get 5 + i32.const 4 + i32.const 0 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + end + i32.const 0 + local.set 4 + end + end + local.get 4 + i32.const -1 + i32.eq + if ;; label = @4 + local.get 2 + i32.const 1 + i32.add + local.get 0 + i32.load offset=4 + i32.le_s + if ;; label = @5 + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + i32.const 0 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + local.set 3 + block ;; label = @7 + loop ;; label = @8 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.add + i32.eq + if ;; label = @9 + br 2 (;@7;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 5 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@8;) + end + end + end + end + local.get 5 + local.get 5 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.add + i32.const 1 + i32.add + i32.store offset=4 + local.get 2 + i32.const 1 + i32.add + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 2 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 0 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + i32.const 0 + local.set 4 + end + local.get 4 + i32.const -1 + i32.eq + local.get 2 + i32.const 1 + i32.sub + i32.const 0 + i32.ge_s + i32.and + if ;; label = @5 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + i32.const 0 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 5 + i32.load offset=4 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.mul + i32.add + local.get 5 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + local.set 3 + block ;; label = @7 + loop ;; label = @8 + local.get 3 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + i32.eq + if ;; label = @9 + br 2 (;@7;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@8;) + end + end + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + local.get 5 + i32.load offset=4 + i32.add + i32.const 1 + i32.add + i32.store offset=4 + local.get 2 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 2 + i32.const 1 + i32.sub + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 0 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + end + end + end + local.get 4 + i32.const -1 + i32.eq + if ;; label = @3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.get 1 + call 5 + drop + else + local.get 5 + local.get 1 + call 5 + drop + end + end + end + i32.const 0 + i32.load offset=8 + i32.load offset=4 + i32.const 0 + i32.eq + if ;; label = @1 + i32.const 0 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + end + i32.const 0 + i32.load offset=8) + (func (;6;) (type 3) (param i32 i32 i32 i32) + (local i32) + local.get 0 + local.get 1 + i32.gt_s + local.get 1 + local.get 2 + i32.gt_s + local.get 0 + local.get 3 + i32.ne + local.get 1 + local.get 3 + i32.ne + local.get 2 + local.get 3 + i32.ne + i32.and + i32.and + i32.and + i32.and + i32.eqz + if ;; label = @1 + unreachable + end + i32.const 4 + call 0 + local.set 4 + local.get 0 + call 4 + local.set 4 + local.get 1 + call 4 + local.set 4 + local.get 2 + call 4 + local.set 4 + local.get 3 + call 4 + local.set 4 + local.get 4 + local.get 0 + call 1 + i32.const -1 + i32.ne + local.get 4 + local.get 1 + call 1 + i32.const -1 + i32.ne + local.get 4 + local.get 2 + call 1 + i32.const -1 + i32.ne + local.get 4 + local.get 3 + call 1 + i32.const -1 + i32.ne + i32.and + i32.and + i32.and + local.get 4 + local.get 0 + call 5 + local.set 4 + local.get 4 + local.get 0 + call 1 + i32.const -1 + i32.eq + local.get 4 + local.get 1 + call 5 + local.set 4 + local.get 4 + local.get 1 + call 1 + i32.const -1 + i32.eq + local.get 4 + local.get 2 + call 5 + local.set 4 + local.get 4 + local.get 2 + call 1 + i32.const -1 + i32.eq + i32.and + i32.and + i32.and + i32.eqz + if ;; label = @1 + unreachable + end) + (func (;7;) (type 4) + i32.const 4 + i32.const 3 + i32.const 2 + i32.const 1 + call 6) + (memory (;0;) 2) + (export "main" (func 7)) + (start 7)) diff --git a/benchmarks/wasm/btree/3o1u.wat b/benchmarks/wasm/btree/3o1u.wat new file mode 100644 index 00000000..6f6fb68b --- /dev/null +++ b/benchmarks/wasm/btree/3o1u.wat @@ -0,0 +1,2858 @@ +(module + (memory $0 2) + (func $createBtree (param i32) (result i32) ;; createBtree(t), where t: degree of the btree + (i32.const 0) + (local.get 0) + (i32.store) ;; store t at address 0 + (i32.const 0) + (i32.const 1) + (i32.store offset=4) ;; store the number of nodes in the btree = 1 + (i32.const 0) + (i32.const 65536) + (i32.store offset=8) ;; store the root addr + (i32.const 1) ;; Now create root + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then + (i32.const 65536) ;; 64KiB = 65536 bytes + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + (i32.const 65536) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + ) + ) + (i32.const 65536) ;; returns the address of the root + ) + ;; btreeSearch(x, k), where x: node address; k: key to search. Returns address of the key or -1 if key is not in tree. + (func $btreeSearch (param i32) (param i32) (result i32) (local i32) + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n -1 + (i32.le_s) ;; i <= x.children-1 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + (if + (then + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.le_s) ;; i <= x.children - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.eq) + ;; k == x.keys[i] + (if (result i32) + (then + (local.get 0) ;; save the address, which is x+8+i*4 + (i32.const 8) + (i32.add) ;; x+8 + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x+8+i*4 + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + ;; btreeSplitChild(x, i), where x: addr of a non full internal node; i: index such that x.children[i] is a full child of x + (func $btreeSplitChild (param i32) (param i32) (local i32) (local i32) + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then ;; get the number of nodes in the tree + (i32.const 0) + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address is 64KiB*(number of nodes+1) + (local.set 2) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the addr of the child in index i : y + (i32.load) ;; check if x.children[i] is leaf ;; load the first i32 in the child + (i32.const 1) + (i32.eq) ;; is y leaf? + (if + (then ;; yes + (local.get 2) ;; addr of new node z + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + ) + (else ;; no + (local.get 2) ;; addr of new node z + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + ) + ) + (local.get 2) ;; now store number of children, which will be t/2 -1 ;; addr of z + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.store offset=4) ;; store number of children = (t/2)-1 + (i32.const 0) ;; now to store the keys + (local.set 3) ;; counter = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.eq) ;; counter == (t/2)-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 2) ;; addr of z ;; we will store in the new node's address. z.keys[counter] + (i32.const 4) + (local.get 3) ;; counter + (i32.mul) ;; counter * 4 + (i32.add) ;; addr + counter * 4 -> where the key is goint to be located + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 4) ;; now get x.children[i].keys[counter+(t/2)] + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + (t/2) + (i32.mul) ;; counter + (t/2) *4 + (i32.add) ;; y + counter + (t/2) *4 + (i32.load offset=8) ;; y.keys[counter + (t/2)] + (i32.store offset=8) ;; z.keys[counter] = y.keys[counter + (t/2)] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) + (local.set 3) ;; set counter back to 0 + (i32.const 0) ;; check if x.children[i] is leaf + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load) ;; get first i32 in child --> isLeaf + (i32.const 1) + (i32.ne) ;; is leaf? + (if + (then ;; not leaf so we have to update the children + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.eq) ;; counter == (t/2) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; now get z.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; counter*4 + (i32.add) ;; (t-1)*4 + counter*4 + (local.get 2) ;; z + (i32.add) ;; z + (t-1)*4 + counter*4 --> address where addr of child is stored + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) ;; get x.children[i].children[counter+(t/2)] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + t/2 + (i32.const 4) + (i32.mul) ;; counter+t/2 * 4 + (i32.add) ;; (t-1)*4 + (counter+(t/2))*4 + (i32.add) ;; x.children[i] + (t-1)*4 + (counter + (t/2))*4 + (i32.load offset=8) ;; load the address of the child in index counter + t/2 + (i32.store offset=8) ;; z.children[counter] = x.children[i].children[counter] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.store offset=4) ;; number of keys of x.children[i] is now t/2 -1 + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (local.set 3) ;; counter = x.n + (block $loop_break + (loop $loop + (local.get 1) ;; i + (local.get 3) ;; counter + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; get x.children[counter + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (i32.add) ;; (t-1)*4 + (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter + 1)*4 -->address where addr of child is stored + (i32.const 0) ;; get x.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (i32.add) ;; (t-1)*4 + (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter)*4 + (i32.load offset=8) ;; load the addr of the child in index (counter) + (i32.store offset=8) ;; x.children[counter+1] = x.children[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 1) + (i32.add) ;; i + 1 + (i32.const 4) + (i32.mul) ;; (i + 1)*4 + (i32.add) ;; (t-1)*4 + (i + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i + 1)*4 --> addr where we will store the new addr of the child + (local.get 2) ;; addr of z + (i32.store offset=8) ;; x.children[i+1] = z + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (i32.const 1) + (i32.sub) + (local.set 3) ;; counter = x.n -1 + (block $loop_break + (loop $loop + (local.get 1) ;; i + (i32.const 1) + (i32.sub) ;; i-1 + (local.get 3) ;; counter + (i32.eq) ;; counter == i-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 3) ;; get x.keys[counter + 1] ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter + 1)*4 --> addr where we will store the new key + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter)*4 + (i32.load offset=8) ;; load the key in index (counter) + (i32.store offset=8) ;; x.keys[counter+1] = x.keys[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (local.get 1) ;; get x.keys[i] + (i32.const 4) + (i32.mul) ;; i*4 + (local.get 0) ;; x + (i32.add) ;; x +i*4 --> addr of the key + (i32.const 0) ;; get x.children[i].keys[t/2] + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.const 4) + (i32.mul) ;; (t/2)-1 *4 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; (i)*4 + (i32.add) ;; (t-1)*4 + (i)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i)*4 + (i32.load offset=8) ;; load the address of the child in index (i) + (i32.add) ;; y + (t/2)-1 *4 + (i32.load offset=8) ;; load the key in index (t/2)-1 from node x.children[i] + (i32.store offset=8) ;; store the new key in the addr + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) ;; x.n + 1 + (i32.store offset=4) ;; x.n = x.n + 1 + ) + ) + ) + ;; btreeInsertNonFull(x, k), where x: addr of a non full internal node; k: the key to insert + (func $btreeInsertNonFull (param i32) (param i32) (local i32) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n -1 + (local.set 2) ;; i = x.n -1 + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (i32.and) + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i *4 + (i32.add) ;; x + i *4 --> addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.keys[i+1] = x.keys[i] + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 1) ;; k + (i32.store offset=8) ;; x.keys[i+1] = k + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; x.n = x.n +1 + ) + (else ;; x is not leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i >= 0 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i -1 + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 2) ;;i = i+1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address of the child in index i = y + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load offset=4) ;; get y.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; y.n == t-1 --> node is full + (if + (then + (local.get 0) ;; x + (local.get 2) ;; i + (call $btreeSplitChild) + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k>x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i+1 + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (local.get 1) ;; k + (call $btreeInsertNonFull) + ) + ) + ) + (func $btreeInsert (param i32) (result i32) (local i32) (local i32) + (i32.const 0) + (i32.load offset=8) ;; root addr + (local.tee 2) ;; set r + (i32.load offset=4) ;; r.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; r.n == t-1 --> root is full + (if (result i32) + (then + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if (result i32) ;; if we can grow memory + (then + (i32.const 0) ;; get the number of nodes in the tree + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address of s is 64KiB*(number of nodes+1) + (local.set 1) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) + (local.get 1) ;; s + (i32.store offset=8) ;; root addr is now s + (local.get 1) ;; s + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + (local.get 1) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + (i32.const 0) ;; store children addresses + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 1) ;; x + (i32.add) ;; x + (t-1)*4 --> addre where addr of child number 0 is located + (local.get 2) + (i32.store offset=8) ;; s.children[0] = r + (local.get 1) ;; s + (i32.const 0) + (call $btreeSplitChild) + (local.get 1) ;; s + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 1) ;; return the address of the new root + ) + (else + (i32.const -1) ;; return -1 because we could not create a new node + ) + ) + ) + (else + (local.get 2) ;; r + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 2) ;; return the address of the root, which is the same + ) + ) + ) + ;; Returns the address of the new root (if applicable, otherwise returns the addres of the root) + (func $btreeDelete (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local i32) + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (local.get 2) ;; i + (i32.eq) ;; i == x.n + (if + (then + (br $loop_break) ;; break from loop + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + (if + (then + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $while_break + (loop $while + (local.get 3) ;; j + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n-1 + (i32.eq) ;; j == x.n-1 + (if + (then + (br $while_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 --> addr where x.keys[j] is stored + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.mul) ;; j+1 *4 + (i32.add) ;; x + j+1 *4 --> addr where x.keys[j+1] is stored + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $while) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n -1 + (br $loop_break) + ) + ) + ) + ) + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) ;; continue + ) + ) + ) + (else ;; x is not leaf + (i32.const 0) ;;get the index of the appropriate child/key + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (i32.and) ;; i <= x.n - 1 && k > x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i + 1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.lt_s) ;; i < x.n + (if (result i32) + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (if + (then ;; key is present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.tee 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (call $btreeDelete) ;; (x.c[i], x.c[i].keys[x.c[i].n - 1]) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i].keys[x.c[i].n - 1] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i+1] + (local.tee 5) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (local.get 5) ;; x.c[i+1] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (call $btreeDelete) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 --> addr where x.c[i].keys[x.c[i].n] will be stored + (local.get 1) ;; k + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = k + (i32.const 0) ;; now merge x.c[i] with x.c[i+1] + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if ;; j == x.c[i+1].n + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n + j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[index] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x.c[i+1] + i*4 ;; addr of x.c[i+1].keys[j] + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[index] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n +1 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + 1 + x.c[i+1].n + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; now adjust the children + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.get 3) ;; j + (i32.add) ;; index = x.c[i].n+j + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x.c[i] + (t-1)*4 --> address where x.c[i].c[x.c[i].n+j] is stored + (i32.const 0) ;;get x.c[i+1].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x.c[i+1] + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (i32.const 1) + (i32.sub) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) + (i32.mul) ;; j+1*4 + (i32.add) ;; x + j+1*4 ;; addr of x.keys[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = i+1 + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 0) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n-1 + (local.get 5) ;; x.c[i] + (local.get 1) ;; k + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) + (else ;; key not present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) + (i32.const 1) + (i32.sub) ;; t/2 - 1 + (i32.eq) + (if + (then + (i32.const -1) + (local.set 4) ;; changed = -1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.le_s) ;; i+1 <= x.n + (if + (then + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; index = t/2 -1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[t/2-1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[t/2-1] = x.keys[i] + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;;x.c[i].n = x.c[i].n +1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i+1].keys[0] + (i32.load offset=8) ;; x.c[i+1].keys[0] + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + (local.get 5) + (i32.load) ;; x.c[i] is leaf? + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) ;;get x.c[i].c[t/2] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; index = t/2 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[t/2] + (i32.const 0) ;;get x.c[i+1].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[0] + (i32.store offset=8) ;; x.c[i].c[t/2] = x.c[i+1].c[0] + ) + ) + (local.get 5) ;; x.c[i] + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.store offset=4) ;; x.c[i].n = t/2 + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 --> address where x.c[i+1].keys[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j+1] + (i32.store offset=8) ;; x.c[i+1].keys[j] = x.c[i+1].keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i+1].c[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j+1] + (i32.store offset=8) ;; x.c[i+1].c[j] = x.c[i+1].c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr where x.c[i+1] is located + (i32.load offset=8) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i+1].n = x.c[i+1].n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i -1 >= 0 + (i32.and) ;; changed == -1 && i -1 >= 0 + (if + (then + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i-1].n >= t/2 + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.set 3) ;; j = x.c[i].n + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j-1] + (i32.load offset=8) ;; x.c[i].keys[j-1] + (i32.store offset=8) ;; x.c[i].keys[j] = x.c[i].keys[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (local.set 3) ;; j = x.c[i].n + 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i].c[j] is located + (i32.const 0) ;;get x.c[i].c[j-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) + (i32.load offset=8) ;; x.c[i].c[j-1] + (i32.store offset=8) ;; x.c[i].c[j] = x.c[i].c[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; ;; x.c[i].n = x.c[i].n + 1 + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[0] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i-1] + (i32.store offset=8) ;; x.c[i].keys[0] = x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n - 1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[i-1] = x.c[i-1].keys[x.c[i-1].n] + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; get x.c[i].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 ;; addr where x.c[i].c[0] is located + (i32.const 0) ;; get x.c[i-1].c[x.c[i-1].n + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.add) ;; index = x.c[i-1.n + 1] + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1].c[x.c[i-1].n + 1] + (i32.store offset=8) ;; x.c[i].c[0] = x.c[i-1].c[x.c[i-1].n + 1] + ) + ) + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (if ;; changed == -1? + (then + (local.get 2) ;; we have to merge x.c[i] with one sibling;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.le_s) + (if ;; i+1 <= x.n + (then + (local.get 5) ;; merge with right sibling;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = x.keys[i] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n+j] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n+j] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i+1].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[x.c[i].n+j] + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) ;; x.c[i].n + x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + x.c[i+1].n + 1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.set 3) ;; j = i+1 + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) ;; changed == -1 ? + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i - 1 >= 0? + (i32.and) ;; changed == -1 && i - 1 >= 0 + (if + (then + (i32.const 0) ;; merge with left sibling ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n] = x.keys[i-1] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n+j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[j] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n+j] = x.c[i].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i-1].c[x.c[i-1].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i-1].c[x.c[i-1].n+j] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i].c[j] + (i32.store offset=8) ;; x.c[i-1].c[x.c[i-1].n+j] = x.c[i].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.add) ;; x.c[i-1].n + x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n + x.c[i].n + 1 + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = i - 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + ) + ) + ) + ) + ) + ) + (local.get 4) + (i32.const -1) + (i32.eq) + (if ;; changed == -1? if yes, we merged with left sibling + (then + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (local.get 1) + (call $btreeDelete) + (drop) + ) + (else + (local.get 5) ;; x.c[i] + (local.get 1) + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) ;; end of if x is not leaf + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.load offset=4) + (i32.const 0) + (i32.eq) + (if ;; if root is empty + (then + (i32.const 0) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; root.c[0] + (i32.store offset=8) ;; root = root.c[0] + ) + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + ) + (func $main + (param $a i32) + (param $b i32) + (param $c i32) + (param $h i32) + (local $btree i32) + (local.get $a) + (local.get $b) + (i32.gt_s) + (local.get $b) + (local.get $c) + (i32.gt_s) + (local.get $a) + (local.get $h) + (i32.ne) + (local.get $b) + (local.get $h) + (i32.ne) + (local.get $c) + (local.get $h) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.eqz) + (if (then (unreachable))) + (i32.const 4) ;;create a tree with degree 4 + (call $createBtree) + (local.set $btree) + ;; insert variables + (local.get $a) + (call $btreeInsert) + (local.set $btree) + (local.get $b) + (call $btreeInsert) + (local.set $btree) + (local.get $c) + (call $btreeInsert) + (local.set $btree) + (local.get $h) + (call $btreeInsert) + (local.set $btree) + ;; search for variables & check that they were inserted + (local.get $btree) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + ;; delete & check that it was deleted + ;; a + (local.get $btree) + (local.get $a) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; b + (local.get $btree) + (local.get $b) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; c + (local.get $btree) + (local.get $c) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + (i32.and) + (i32.and) + (i32.and) + ;; last functional check + (i32.eqz) + (if (then (unreachable))) + ) + (export "main" (func $real_main)) + (func $real_main + i32.const 4 + i32.const 3 + i32.const 2 + i32.const 1 + call $main + ) + (start $real_main) +) + diff --git a/benchmarks/wasm/btree/3o2u-unlabeled.wat b/benchmarks/wasm/btree/3o2u-unlabeled.wat new file mode 100644 index 00000000..f19d829a --- /dev/null +++ b/benchmarks/wasm/btree/3o2u-unlabeled.wat @@ -0,0 +1,2704 @@ +(module + (type (;0;) (func (param i32) (result i32))) + (type (;1;) (func (param i32 i32) (result i32))) + (type (;2;) (func (param i32 i32))) + (type (;3;) (func (param i32 i32 i32 i32 i32))) + (type (;4;) (func)) + (func (;0;) (type 0) (param i32) (result i32) + i32.const 0 + local.get 0 + i32.store + i32.const 0 + i32.const 1 + i32.store offset=4 + i32.const 0 + i32.const 65536 + i32.store offset=8 + i32.const 1 + memory.grow + i32.const -1 + i32.ne + if ;; label = @1 + i32.const 65536 + i32.const 1 + i32.store + i32.const 65536 + i32.const 0 + i32.store offset=4 + end + i32.const 65536) + (func (;1;) (type 1) (param i32 i32) (result i32) + (local i32) + i32.const 0 + local.set 2 + block ;; label = @1 + loop ;; label = @2 + local.get 2 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.le_s + if ;; label = @3 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.gt_s + if ;; label = @4 + i32.const 1 + local.get 2 + i32.add + local.set 2 + br 2 (;@2;) + else + br 3 (;@1;) + end + else + br 2 (;@1;) + end + end + end + local.get 2 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.le_s + if (result i32) ;; label = @1 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.eq + if (result i32) ;; label = @2 + local.get 0 + i32.const 8 + i32.add + i32.const 4 + local.get 2 + i32.mul + i32.add + else + local.get 0 + i32.load + i32.const 1 + i32.eq + if (result i32) ;; label = @3 + i32.const -1 + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.get 1 + call 1 + end + end + else + local.get 0 + i32.load + i32.const 1 + i32.eq + if (result i32) ;; label = @2 + i32.const -1 + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.get 1 + call 1 + end + end) + (func (;2;) (type 2) (param i32 i32) + (local i32 i32) + i32.const 1 + memory.grow + i32.const -1 + i32.ne + if ;; label = @1 + i32.const 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.const 65536 + i32.mul + local.set 2 + i32.const 0 + i32.const 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load + i32.const 1 + i32.eq + if ;; label = @2 + local.get 2 + i32.const 1 + i32.store + else + local.get 2 + i32.const 0 + i32.store + end + local.get 2 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.store offset=4 + i32.const 0 + local.set 3 + block ;; label = @2 + loop ;; label = @3 + local.get 3 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.eq + if ;; label = @4 + br 2 (;@2;) + else + local.get 2 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@3;) + end + end + end + i32.const 0 + local.set 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load + i32.const 1 + i32.ne + if ;; label = @2 + block ;; label = @3 + loop ;; label = @4 + local.get 3 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.eq + if ;; label = @5 + br 2 (;@3;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 2 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.add + i32.const 4 + i32.mul + i32.add + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@4;) + end + end + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.store offset=4 + local.get 0 + i32.load offset=4 + local.set 3 + block ;; label = @2 + loop ;; label = @3 + local.get 1 + local.get 3 + i32.eq + if ;; label = @4 + br 2 (;@2;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.sub + local.set 3 + br 1 (;@3;) + end + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + local.get 2 + i32.store offset=8 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + local.set 3 + block ;; label = @2 + loop ;; label = @3 + local.get 1 + i32.const 1 + i32.sub + local.get 3 + i32.eq + if ;; label = @4 + br 2 (;@2;) + else + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + local.get 0 + i32.add + local.get 3 + i32.const 4 + i32.mul + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.sub + local.set 3 + br 1 (;@3;) + end + end + end + local.get 1 + i32.const 4 + i32.mul + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + end) + (func (;3;) (type 2) (param i32 i32) + (local i32) + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + local.set 2 + local.get 0 + i32.load + i32.const 1 + i32.eq + if ;; label = @1 + block ;; label = @2 + loop ;; label = @3 + local.get 2 + i32.const 0 + i32.ge_s + if (result i32) ;; label = @4 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.lt_s + else + i32.const 0 + end + local.get 2 + i32.const 0 + i32.ge_s + i32.and + if ;; label = @4 + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.add + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 2 + i32.const 1 + i32.sub + local.set 2 + br 1 (;@3;) + else + br 2 (;@2;) + end + end + end + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.add + i32.mul + i32.add + local.get 1 + i32.store offset=8 + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + else + block ;; label = @2 + loop ;; label = @3 + local.get 2 + i32.const 0 + i32.ge_s + if ;; label = @4 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.lt_s + if ;; label = @5 + local.get 2 + i32.const 1 + i32.sub + local.set 2 + br 2 (;@3;) + else + br 3 (;@2;) + end + else + br 2 (;@2;) + end + end + end + local.get 2 + i32.const 1 + i32.add + local.set 2 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.eq + if ;; label = @2 + local.get 0 + local.get 2 + call 2 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.gt_s + if ;; label = @3 + local.get 2 + i32.const 1 + i32.add + local.set 2 + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.get 1 + call 3 + end) + (func (;4;) (type 0) (param i32) (result i32) + (local i32 i32) + i32.const 0 + i32.load offset=8 + local.tee 2 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.eq + if (result i32) ;; label = @1 + i32.const 1 + memory.grow + i32.const -1 + i32.ne + if (result i32) ;; label = @2 + i32.const 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.const 65536 + i32.mul + local.set 1 + i32.const 0 + i32.const 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + i32.const 0 + local.get 1 + i32.store offset=8 + local.get 1 + i32.const 0 + i32.store + local.get 1 + i32.const 0 + i32.store offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.const 4 + i32.mul + i32.add + local.get 1 + i32.add + local.get 2 + i32.store offset=8 + local.get 1 + i32.const 0 + call 2 + local.get 1 + local.get 0 + call 3 + local.get 1 + else + i32.const -1 + end + else + local.get 2 + local.get 0 + call 3 + local.get 2 + end) + (func (;5;) (type 1) (param i32 i32) (result i32) + (local i32 i32 i32 i32) + local.get 0 + i32.load + i32.const 1 + i32.eq + if ;; label = @1 + i32.const 0 + local.set 2 + block ;; label = @2 + loop ;; label = @3 + local.get 0 + i32.load offset=4 + local.get 2 + i32.eq + if ;; label = @4 + br 2 (;@2;) + else + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + local.get 1 + i32.eq + if ;; label = @5 + local.get 2 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 0 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + br 3 (;@2;) + end + end + i32.const 1 + local.get 2 + i32.add + local.set 2 + br 0 (;@3;) + end + end + else + i32.const 0 + local.set 2 + block ;; label = @2 + loop ;; label = @3 + local.get 2 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.le_s + if (result i32) ;; label = @4 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.gt_s + else + i32.const 0 + end + local.get 2 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.le_s + i32.and + if ;; label = @4 + local.get 2 + i32.const 1 + i32.add + local.set 2 + br 1 (;@3;) + else + br 2 (;@2;) + end + end + end + local.get 2 + local.get 0 + i32.load offset=4 + i32.lt_s + if (result i32) ;; label = @2 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + local.get 1 + i32.eq + else + i32.const 0 + end + if ;; label = @2 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.tee 5 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.ge_s + if ;; label = @3 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + local.get 5 + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + call 5 + drop + i32.store offset=8 + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.tee 5 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.ge_s + if ;; label = @4 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + local.get 5 + i32.const 4 + i32.const 0 + i32.mul + i32.add + i32.load offset=8 + local.get 5 + local.get 5 + i32.const 4 + i32.const 0 + i32.mul + i32.add + i32.load offset=8 + call 5 + drop + i32.store offset=8 + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.set 5 + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + i32.mul + i32.add + local.get 1 + i32.store offset=8 + i32.const 0 + local.set 3 + block ;; label = @5 + loop ;; label = @6 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.eq + if ;; label = @7 + br 2 (;@5;) + else + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@6;) + end + end + end + local.get 5 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.add + i32.store offset=4 + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @5 + i32.const 0 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.add + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 5 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + end + local.get 2 + local.set 3 + block ;; label = @5 + loop ;; label = @6 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + local.get 3 + i32.eq + if ;; label = @7 + br 2 (;@5;) + else + local.get 0 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@6;) + end + end + end + local.get 2 + i32.const 1 + i32.add + local.set 3 + block ;; label = @5 + loop ;; label = @6 + local.get 0 + i32.load offset=4 + local.get 3 + i32.eq + if ;; label = @7 + br 2 (;@5;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@6;) + end + end + end + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + local.get 5 + local.get 1 + call 5 + drop + end + end + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.set 5 + local.get 5 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.eq + if ;; label = @3 + i32.const -1 + local.set 4 + local.get 2 + i32.const 1 + i32.add + local.get 0 + i32.load offset=4 + i32.le_s + if ;; label = @4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.ge_s + if ;; label = @5 + local.get 5 + i32.const 4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 5 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + i32.const 0 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + end + local.get 5 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.store offset=4 + i32.const 0 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.sub + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + local.set 3 + block ;; label = @7 + loop ;; label = @8 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.eq + if ;; label = @9 + br 2 (;@7;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@8;) + end + end + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + i32.const 0 + local.set 4 + end + end + local.get 4 + i32.const -1 + i32.eq + local.get 2 + i32.const 1 + i32.sub + i32.const 0 + i32.ge_s + i32.and + if ;; label = @4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.ge_s + if ;; label = @5 + local.get 5 + i32.load offset=4 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + i32.const 0 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 5 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 5 + i32.const 4 + local.get 3 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.sub + local.set 3 + br 1 (;@7;) + end + end + end + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + local.set 3 + block ;; label = @7 + loop ;; label = @8 + local.get 3 + i32.const 0 + i32.eq + if ;; label = @9 + br 2 (;@7;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.sub + local.set 3 + br 1 (;@8;) + end + end + end + end + local.get 5 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + local.get 5 + i32.const 4 + i32.const 0 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + end + i32.const 0 + local.set 4 + end + end + local.get 4 + i32.const -1 + i32.eq + if ;; label = @4 + local.get 2 + i32.const 1 + i32.add + local.get 0 + i32.load offset=4 + i32.le_s + if ;; label = @5 + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + i32.const 0 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + local.set 3 + block ;; label = @7 + loop ;; label = @8 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.add + i32.eq + if ;; label = @9 + br 2 (;@7;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 5 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@8;) + end + end + end + end + local.get 5 + local.get 5 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.add + i32.const 1 + i32.add + i32.store offset=4 + local.get 2 + i32.const 1 + i32.add + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 2 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 0 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + i32.const 0 + local.set 4 + end + local.get 4 + i32.const -1 + i32.eq + local.get 2 + i32.const 1 + i32.sub + i32.const 0 + i32.ge_s + i32.and + if ;; label = @5 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + i32.const 0 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 5 + i32.load offset=4 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.mul + i32.add + local.get 5 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + local.set 3 + block ;; label = @7 + loop ;; label = @8 + local.get 3 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + i32.eq + if ;; label = @9 + br 2 (;@7;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@8;) + end + end + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + local.get 5 + i32.load offset=4 + i32.add + i32.const 1 + i32.add + i32.store offset=4 + local.get 2 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 2 + i32.const 1 + i32.sub + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 0 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + end + end + end + local.get 4 + i32.const -1 + i32.eq + if ;; label = @3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.get 1 + call 5 + drop + else + local.get 5 + local.get 1 + call 5 + drop + end + end + end + i32.const 0 + i32.load offset=8 + i32.load offset=4 + i32.const 0 + i32.eq + if ;; label = @1 + i32.const 0 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + end + i32.const 0 + i32.load offset=8) + (func (;6;) (type 3) (param i32 i32 i32 i32 i32) + (local i32) + local.get 0 + local.get 1 + i32.gt_s + local.get 1 + local.get 2 + i32.gt_s + local.get 3 + local.get 0 + i32.ne + local.get 3 + local.get 1 + i32.ne + local.get 3 + local.get 2 + i32.ne + local.get 4 + local.get 0 + i32.ne + local.get 4 + local.get 1 + i32.ne + local.get 4 + local.get 2 + i32.ne + local.get 4 + local.get 3 + i32.ne + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.eqz + if ;; label = @1 + unreachable + end + i32.const 4 + call 0 + local.set 5 + local.get 0 + call 4 + local.set 5 + local.get 1 + call 4 + local.set 5 + local.get 2 + call 4 + local.set 5 + local.get 3 + call 4 + local.set 5 + local.get 4 + call 4 + local.set 5 + local.get 5 + local.get 0 + call 1 + i32.const -1 + i32.ne + local.get 5 + local.get 1 + call 1 + i32.const -1 + i32.ne + local.get 5 + local.get 2 + call 1 + i32.const -1 + i32.ne + local.get 5 + local.get 3 + call 1 + i32.const -1 + i32.ne + local.get 5 + local.get 4 + call 1 + i32.const -1 + i32.ne + i32.and + i32.and + i32.and + i32.and + local.get 5 + local.get 0 + call 5 + local.set 5 + local.get 5 + local.get 0 + call 1 + i32.const -1 + i32.eq + local.get 5 + local.get 1 + call 5 + local.set 5 + local.get 5 + local.get 1 + call 1 + i32.const -1 + i32.eq + local.get 5 + local.get 2 + call 5 + local.set 5 + local.get 5 + local.get 2 + call 1 + i32.const -1 + i32.eq + local.get 5 + local.get 3 + call 5 + local.set 5 + local.get 5 + local.get 3 + call 1 + i32.const -1 + i32.eq + i32.and + i32.and + i32.and + i32.and + i32.eqz + if ;; label = @1 + unreachable + end) + (func (;7;) (type 4) + i32.const 5 + i32.const 4 + i32.const 3 + i32.const 2 + i32.const 1 + call 6) + (memory (;0;) 2) + (export "main" (func 7)) + (start 7)) diff --git a/benchmarks/wasm/btree/3o2u.wat b/benchmarks/wasm/btree/3o2u.wat new file mode 100644 index 00000000..73e47913 --- /dev/null +++ b/benchmarks/wasm/btree/3o2u.wat @@ -0,0 +1,2895 @@ +(module + (memory $0 2) + (func $createBtree (param i32) (result i32) ;; createBtree(t), where t: degree of the btree + (i32.const 0) + (local.get 0) + (i32.store) ;; store t at address 0 + (i32.const 0) + (i32.const 1) + (i32.store offset=4) ;; store the number of nodes in the btree = 1 + (i32.const 0) + (i32.const 65536) + (i32.store offset=8) ;; store the root addr + (i32.const 1) ;; Now create root + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then + (i32.const 65536) ;; 64KiB = 65536 bytes + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + (i32.const 65536) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + ) + ) + (i32.const 65536) ;; returns the address of the root + ) + ;; btreeSearch(x, k), where x: node address; k: key to search. Returns address of the key or -1 if key is not in tree. + (func $btreeSearch (param i32) (param i32) (result i32) (local i32) + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n -1 + (i32.le_s) ;; i <= x.children-1 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + (if + (then + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.le_s) ;; i <= x.children - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.eq) + ;; k == x.keys[i] + (if (result i32) + (then + (local.get 0) ;; save the address, which is x+8+i*4 + (i32.const 8) + (i32.add) ;; x+8 + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x+8+i*4 + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + ;; btreeSplitChild(x, i), where x: addr of a non full internal node; i: index such that x.children[i] is a full child of x + (func $btreeSplitChild (param i32) (param i32) (local i32) (local i32) + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then ;; get the number of nodes in the tree + (i32.const 0) + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address is 64KiB*(number of nodes+1) + (local.set 2) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the addr of the child in index i : y + (i32.load) ;; check if x.children[i] is leaf ;; load the first i32 in the child + (i32.const 1) + (i32.eq) ;; is y leaf? + (if + (then ;; yes + (local.get 2) ;; addr of new node z + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + ) + (else ;; no + (local.get 2) ;; addr of new node z + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + ) + ) + (local.get 2) ;; now store number of children, which will be t/2 -1 ;; addr of z + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.store offset=4) ;; store number of children = (t/2)-1 + (i32.const 0) ;; now to store the keys + (local.set 3) ;; counter = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.eq) ;; counter == (t/2)-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 2) ;; addr of z ;; we will store in the new node's address. z.keys[counter] + (i32.const 4) + (local.get 3) ;; counter + (i32.mul) ;; counter * 4 + (i32.add) ;; addr + counter * 4 -> where the key is goint to be located + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 4) ;; now get x.children[i].keys[counter+(t/2)] + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + (t/2) + (i32.mul) ;; counter + (t/2) *4 + (i32.add) ;; y + counter + (t/2) *4 + (i32.load offset=8) ;; y.keys[counter + (t/2)] + (i32.store offset=8) ;; z.keys[counter] = y.keys[counter + (t/2)] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) + (local.set 3) ;; set counter back to 0 + (i32.const 0) ;; check if x.children[i] is leaf + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load) ;; get first i32 in child --> isLeaf + (i32.const 1) + (i32.ne) ;; is leaf? + (if + (then ;; not leaf so we have to update the children + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.eq) ;; counter == (t/2) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; now get z.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; counter*4 + (i32.add) ;; (t-1)*4 + counter*4 + (local.get 2) ;; z + (i32.add) ;; z + (t-1)*4 + counter*4 --> address where addr of child is stored + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) ;; get x.children[i].children[counter+(t/2)] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + t/2 + (i32.const 4) + (i32.mul) ;; counter+t/2 * 4 + (i32.add) ;; (t-1)*4 + (counter+(t/2))*4 + (i32.add) ;; x.children[i] + (t-1)*4 + (counter + (t/2))*4 + (i32.load offset=8) ;; load the address of the child in index counter + t/2 + (i32.store offset=8) ;; z.children[counter] = x.children[i].children[counter] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.store offset=4) ;; number of keys of x.children[i] is now t/2 -1 + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (local.set 3) ;; counter = x.n + (block $loop_break + (loop $loop + (local.get 1) ;; i + (local.get 3) ;; counter + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; get x.children[counter + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (i32.add) ;; (t-1)*4 + (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter + 1)*4 -->address where addr of child is stored + (i32.const 0) ;; get x.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (i32.add) ;; (t-1)*4 + (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter)*4 + (i32.load offset=8) ;; load the addr of the child in index (counter) + (i32.store offset=8) ;; x.children[counter+1] = x.children[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 1) + (i32.add) ;; i + 1 + (i32.const 4) + (i32.mul) ;; (i + 1)*4 + (i32.add) ;; (t-1)*4 + (i + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i + 1)*4 --> addr where we will store the new addr of the child + (local.get 2) ;; addr of z + (i32.store offset=8) ;; x.children[i+1] = z + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (i32.const 1) + (i32.sub) + (local.set 3) ;; counter = x.n -1 + (block $loop_break + (loop $loop + (local.get 1) ;; i + (i32.const 1) + (i32.sub) ;; i-1 + (local.get 3) ;; counter + (i32.eq) ;; counter == i-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 3) ;; get x.keys[counter + 1] ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter + 1)*4 --> addr where we will store the new key + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter)*4 + (i32.load offset=8) ;; load the key in index (counter) + (i32.store offset=8) ;; x.keys[counter+1] = x.keys[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (local.get 1) ;; get x.keys[i] + (i32.const 4) + (i32.mul) ;; i*4 + (local.get 0) ;; x + (i32.add) ;; x +i*4 --> addr of the key + (i32.const 0) ;; get x.children[i].keys[t/2] + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.const 4) + (i32.mul) ;; (t/2)-1 *4 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; (i)*4 + (i32.add) ;; (t-1)*4 + (i)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i)*4 + (i32.load offset=8) ;; load the address of the child in index (i) + (i32.add) ;; y + (t/2)-1 *4 + (i32.load offset=8) ;; load the key in index (t/2)-1 from node x.children[i] + (i32.store offset=8) ;; store the new key in the addr + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) ;; x.n + 1 + (i32.store offset=4) ;; x.n = x.n + 1 + ) + ) + ) + ;; btreeInsertNonFull(x, k), where x: addr of a non full internal node; k: the key to insert + (func $btreeInsertNonFull (param i32) (param i32) (local i32) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n -1 + (local.set 2) ;; i = x.n -1 + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (i32.and) + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i *4 + (i32.add) ;; x + i *4 --> addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.keys[i+1] = x.keys[i] + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 1) ;; k + (i32.store offset=8) ;; x.keys[i+1] = k + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; x.n = x.n +1 + ) + (else ;; x is not leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i >= 0 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i -1 + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 2) ;;i = i+1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address of the child in index i = y + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load offset=4) ;; get y.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; y.n == t-1 --> node is full + (if + (then + (local.get 0) ;; x + (local.get 2) ;; i + (call $btreeSplitChild) + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k>x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i+1 + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (local.get 1) ;; k + (call $btreeInsertNonFull) + ) + ) + ) + (func $btreeInsert (param i32) (result i32) (local i32) (local i32) + (i32.const 0) + (i32.load offset=8) ;; root addr + (local.tee 2) ;; set r + (i32.load offset=4) ;; r.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; r.n == t-1 --> root is full + (if (result i32) + (then + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if (result i32) ;; if we can grow memory + (then + (i32.const 0) ;; get the number of nodes in the tree + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address of s is 64KiB*(number of nodes+1) + (local.set 1) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) + (local.get 1) ;; s + (i32.store offset=8) ;; root addr is now s + (local.get 1) ;; s + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + (local.get 1) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + (i32.const 0) ;; store children addresses + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 1) ;; x + (i32.add) ;; x + (t-1)*4 --> addre where addr of child number 0 is located + (local.get 2) + (i32.store offset=8) ;; s.children[0] = r + (local.get 1) ;; s + (i32.const 0) + (call $btreeSplitChild) + (local.get 1) ;; s + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 1) ;; return the address of the new root + ) + (else + (i32.const -1) ;; return -1 because we could not create a new node + ) + ) + ) + (else + (local.get 2) ;; r + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 2) ;; return the address of the root, which is the same + ) + ) + ) + ;; Returns the address of the new root (if applicable, otherwise returns the addres of the root) + (func $btreeDelete (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local i32) + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (local.get 2) ;; i + (i32.eq) ;; i == x.n + (if + (then + (br $loop_break) ;; break from loop + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + (if + (then + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $while_break + (loop $while + (local.get 3) ;; j + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n-1 + (i32.eq) ;; j == x.n-1 + (if + (then + (br $while_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 --> addr where x.keys[j] is stored + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.mul) ;; j+1 *4 + (i32.add) ;; x + j+1 *4 --> addr where x.keys[j+1] is stored + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $while) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n -1 + (br $loop_break) + ) + ) + ) + ) + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) ;; continue + ) + ) + ) + (else ;; x is not leaf + (i32.const 0) ;;get the index of the appropriate child/key + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (i32.and) ;; i <= x.n - 1 && k > x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i + 1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.lt_s) ;; i < x.n + (if (result i32) + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (if + (then ;; key is present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.tee 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (call $btreeDelete) ;; (x.c[i], x.c[i].keys[x.c[i].n - 1]) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i].keys[x.c[i].n - 1] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i+1] + (local.tee 5) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (local.get 5) ;; x.c[i+1] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (call $btreeDelete) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 --> addr where x.c[i].keys[x.c[i].n] will be stored + (local.get 1) ;; k + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = k + (i32.const 0) ;; now merge x.c[i] with x.c[i+1] + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if ;; j == x.c[i+1].n + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n + j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[index] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x.c[i+1] + i*4 ;; addr of x.c[i+1].keys[j] + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[index] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n +1 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + 1 + x.c[i+1].n + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; now adjust the children + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.get 3) ;; j + (i32.add) ;; index = x.c[i].n+j + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x.c[i] + (t-1)*4 --> address where x.c[i].c[x.c[i].n+j] is stored + (i32.const 0) ;;get x.c[i+1].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x.c[i+1] + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (i32.const 1) + (i32.sub) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) + (i32.mul) ;; j+1*4 + (i32.add) ;; x + j+1*4 ;; addr of x.keys[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = i+1 + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 0) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n-1 + (local.get 5) ;; x.c[i] + (local.get 1) ;; k + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) + (else ;; key not present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) + (i32.const 1) + (i32.sub) ;; t/2 - 1 + (i32.eq) + (if + (then + (i32.const -1) + (local.set 4) ;; changed = -1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.le_s) ;; i+1 <= x.n + (if + (then + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; index = t/2 -1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[t/2-1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[t/2-1] = x.keys[i] + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;;x.c[i].n = x.c[i].n +1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i+1].keys[0] + (i32.load offset=8) ;; x.c[i+1].keys[0] + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + (local.get 5) + (i32.load) ;; x.c[i] is leaf? + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) ;;get x.c[i].c[t/2] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; index = t/2 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[t/2] + (i32.const 0) ;;get x.c[i+1].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[0] + (i32.store offset=8) ;; x.c[i].c[t/2] = x.c[i+1].c[0] + ) + ) + (local.get 5) ;; x.c[i] + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.store offset=4) ;; x.c[i].n = t/2 + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 --> address where x.c[i+1].keys[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j+1] + (i32.store offset=8) ;; x.c[i+1].keys[j] = x.c[i+1].keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i+1].c[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j+1] + (i32.store offset=8) ;; x.c[i+1].c[j] = x.c[i+1].c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr where x.c[i+1] is located + (i32.load offset=8) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i+1].n = x.c[i+1].n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i -1 >= 0 + (i32.and) ;; changed == -1 && i -1 >= 0 + (if + (then + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i-1].n >= t/2 + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.set 3) ;; j = x.c[i].n + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j-1] + (i32.load offset=8) ;; x.c[i].keys[j-1] + (i32.store offset=8) ;; x.c[i].keys[j] = x.c[i].keys[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (local.set 3) ;; j = x.c[i].n + 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i].c[j] is located + (i32.const 0) ;;get x.c[i].c[j-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) + (i32.load offset=8) ;; x.c[i].c[j-1] + (i32.store offset=8) ;; x.c[i].c[j] = x.c[i].c[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; ;; x.c[i].n = x.c[i].n + 1 + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[0] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i-1] + (i32.store offset=8) ;; x.c[i].keys[0] = x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n - 1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[i-1] = x.c[i-1].keys[x.c[i-1].n] + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; get x.c[i].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 ;; addr where x.c[i].c[0] is located + (i32.const 0) ;; get x.c[i-1].c[x.c[i-1].n + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.add) ;; index = x.c[i-1.n + 1] + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1].c[x.c[i-1].n + 1] + (i32.store offset=8) ;; x.c[i].c[0] = x.c[i-1].c[x.c[i-1].n + 1] + ) + ) + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (if ;; changed == -1? + (then + (local.get 2) ;; we have to merge x.c[i] with one sibling;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.le_s) + (if ;; i+1 <= x.n + (then + (local.get 5) ;; merge with right sibling;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = x.keys[i] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n+j] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n+j] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i+1].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[x.c[i].n+j] + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) ;; x.c[i].n + x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + x.c[i+1].n + 1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.set 3) ;; j = i+1 + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) ;; changed == -1 ? + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i - 1 >= 0? + (i32.and) ;; changed == -1 && i - 1 >= 0 + (if + (then + (i32.const 0) ;; merge with left sibling ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n] = x.keys[i-1] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n+j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[j] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n+j] = x.c[i].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i-1].c[x.c[i-1].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i-1].c[x.c[i-1].n+j] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i].c[j] + (i32.store offset=8) ;; x.c[i-1].c[x.c[i-1].n+j] = x.c[i].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.add) ;; x.c[i-1].n + x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n + x.c[i].n + 1 + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = i - 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + ) + ) + ) + ) + ) + ) + (local.get 4) + (i32.const -1) + (i32.eq) + (if ;; changed == -1? if yes, we merged with left sibling + (then + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (local.get 1) + (call $btreeDelete) + (drop) + ) + (else + (local.get 5) ;; x.c[i] + (local.get 1) + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) ;; end of if x is not leaf + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.load offset=4) + (i32.const 0) + (i32.eq) + (if ;; if root is empty + (then + (i32.const 0) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; root.c[0] + (i32.store offset=8) ;; root = root.c[0] + ) + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + ) + (func $main + (param $a i32) + (param $b i32) + (param $c i32) + (param $h i32) + (param $i i32) + (local $btree i32) + (local.get $a) + (local.get $b) + (i32.gt_s) + (local.get $b) + (local.get $c) + (i32.gt_s) + (local.get $h) + (local.get $a) + (i32.ne) + (local.get $h) + (local.get $b) + (i32.ne) + (local.get $h) + (local.get $c) + (i32.ne) + (local.get $i) + (local.get $a) + (i32.ne) + (local.get $i) + (local.get $b) + (i32.ne) + (local.get $i) + (local.get $c) + (i32.ne) + (local.get $i) + (local.get $h) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.eqz) + (if (then (unreachable))) + (i32.const 4) ;;create a tree with degree 4 + (call $createBtree) + (local.set $btree) + ;; insert variables + (local.get $a) + (call $btreeInsert) + (local.set $btree) + (local.get $b) + (call $btreeInsert) + (local.set $btree) + (local.get $c) + (call $btreeInsert) + (local.set $btree) + (local.get $h) + (call $btreeInsert) + (local.set $btree) + (local.get $i) + (call $btreeInsert) + (local.set $btree) + ;; search for variables & check that they were inserted + (local.get $btree) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $i) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + ;; delete & check that it was deleted + ;; a + (local.get $btree) + (local.get $a) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; b + (local.get $btree) + (local.get $b) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; c + (local.get $btree) + (local.get $c) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; h + (local.get $btree) + (local.get $h) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + ;; last functional check + (i32.eqz) + (if (then (unreachable))) + ) + (export "main" (func $real_main)) + (func $real_main + i32.const 5 + i32.const 4 + i32.const 3 + i32.const 2 + i32.const 1 + call $main + ) + (start $real_main) +) diff --git a/benchmarks/wasm/btree/3o3u-unlabeled.wat b/benchmarks/wasm/btree/3o3u-unlabeled.wat new file mode 100644 index 00000000..f61185bb --- /dev/null +++ b/benchmarks/wasm/btree/3o3u-unlabeled.wat @@ -0,0 +1,2744 @@ +(module + (type (;0;) (func (param i32) (result i32))) + (type (;1;) (func (param i32 i32) (result i32))) + (type (;2;) (func (param i32 i32))) + (type (;3;) (func (param i32 i32 i32 i32 i32 i32))) + (type (;4;) (func)) + (func (;0;) (type 0) (param i32) (result i32) + i32.const 0 + local.get 0 + i32.store + i32.const 0 + i32.const 1 + i32.store offset=4 + i32.const 0 + i32.const 65536 + i32.store offset=8 + i32.const 1 + memory.grow + i32.const -1 + i32.ne + if ;; label = @1 + i32.const 65536 + i32.const 1 + i32.store + i32.const 65536 + i32.const 0 + i32.store offset=4 + end + i32.const 65536) + (func (;1;) (type 1) (param i32 i32) (result i32) + (local i32) + i32.const 0 + local.set 2 + block ;; label = @1 + loop ;; label = @2 + local.get 2 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.le_s + if ;; label = @3 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.gt_s + if ;; label = @4 + i32.const 1 + local.get 2 + i32.add + local.set 2 + br 2 (;@2;) + else + br 3 (;@1;) + end + else + br 2 (;@1;) + end + end + end + local.get 2 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.le_s + if (result i32) ;; label = @1 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.eq + if (result i32) ;; label = @2 + local.get 0 + i32.const 8 + i32.add + i32.const 4 + local.get 2 + i32.mul + i32.add + else + local.get 0 + i32.load + i32.const 1 + i32.eq + if (result i32) ;; label = @3 + i32.const -1 + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.get 1 + call 1 + end + end + else + local.get 0 + i32.load + i32.const 1 + i32.eq + if (result i32) ;; label = @2 + i32.const -1 + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.get 1 + call 1 + end + end) + (func (;2;) (type 2) (param i32 i32) + (local i32 i32) + i32.const 1 + memory.grow + i32.const -1 + i32.ne + if ;; label = @1 + i32.const 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.const 65536 + i32.mul + local.set 2 + i32.const 0 + i32.const 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load + i32.const 1 + i32.eq + if ;; label = @2 + local.get 2 + i32.const 1 + i32.store + else + local.get 2 + i32.const 0 + i32.store + end + local.get 2 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.store offset=4 + i32.const 0 + local.set 3 + block ;; label = @2 + loop ;; label = @3 + local.get 3 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.eq + if ;; label = @4 + br 2 (;@2;) + else + local.get 2 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@3;) + end + end + end + i32.const 0 + local.set 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load + i32.const 1 + i32.ne + if ;; label = @2 + block ;; label = @3 + loop ;; label = @4 + local.get 3 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.eq + if ;; label = @5 + br 2 (;@3;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 2 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.add + i32.const 4 + i32.mul + i32.add + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@4;) + end + end + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.store offset=4 + local.get 0 + i32.load offset=4 + local.set 3 + block ;; label = @2 + loop ;; label = @3 + local.get 1 + local.get 3 + i32.eq + if ;; label = @4 + br 2 (;@2;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.sub + local.set 3 + br 1 (;@3;) + end + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + local.get 2 + i32.store offset=8 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + local.set 3 + block ;; label = @2 + loop ;; label = @3 + local.get 1 + i32.const 1 + i32.sub + local.get 3 + i32.eq + if ;; label = @4 + br 2 (;@2;) + else + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + local.get 0 + i32.add + local.get 3 + i32.const 4 + i32.mul + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.sub + local.set 3 + br 1 (;@3;) + end + end + end + local.get 1 + i32.const 4 + i32.mul + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + end) + (func (;3;) (type 2) (param i32 i32) + (local i32) + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + local.set 2 + local.get 0 + i32.load + i32.const 1 + i32.eq + if ;; label = @1 + block ;; label = @2 + loop ;; label = @3 + local.get 2 + i32.const 0 + i32.ge_s + if (result i32) ;; label = @4 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.lt_s + else + i32.const 0 + end + local.get 2 + i32.const 0 + i32.ge_s + i32.and + if ;; label = @4 + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.add + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 2 + i32.const 1 + i32.sub + local.set 2 + br 1 (;@3;) + else + br 2 (;@2;) + end + end + end + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.add + i32.mul + i32.add + local.get 1 + i32.store offset=8 + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + else + block ;; label = @2 + loop ;; label = @3 + local.get 2 + i32.const 0 + i32.ge_s + if ;; label = @4 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.lt_s + if ;; label = @5 + local.get 2 + i32.const 1 + i32.sub + local.set 2 + br 2 (;@3;) + else + br 3 (;@2;) + end + else + br 2 (;@2;) + end + end + end + local.get 2 + i32.const 1 + i32.add + local.set 2 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.eq + if ;; label = @2 + local.get 0 + local.get 2 + call 2 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.gt_s + if ;; label = @3 + local.get 2 + i32.const 1 + i32.add + local.set 2 + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.get 1 + call 3 + end) + (func (;4;) (type 0) (param i32) (result i32) + (local i32 i32) + i32.const 0 + i32.load offset=8 + local.tee 2 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.eq + if (result i32) ;; label = @1 + i32.const 1 + memory.grow + i32.const -1 + i32.ne + if (result i32) ;; label = @2 + i32.const 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.const 65536 + i32.mul + local.set 1 + i32.const 0 + i32.const 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + i32.const 0 + local.get 1 + i32.store offset=8 + local.get 1 + i32.const 0 + i32.store + local.get 1 + i32.const 0 + i32.store offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.const 4 + i32.mul + i32.add + local.get 1 + i32.add + local.get 2 + i32.store offset=8 + local.get 1 + i32.const 0 + call 2 + local.get 1 + local.get 0 + call 3 + local.get 1 + else + i32.const -1 + end + else + local.get 2 + local.get 0 + call 3 + local.get 2 + end) + (func (;5;) (type 1) (param i32 i32) (result i32) + (local i32 i32 i32 i32) + local.get 0 + i32.load + i32.const 1 + i32.eq + if ;; label = @1 + i32.const 0 + local.set 2 + block ;; label = @2 + loop ;; label = @3 + local.get 0 + i32.load offset=4 + local.get 2 + i32.eq + if ;; label = @4 + br 2 (;@2;) + else + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + local.get 1 + i32.eq + if ;; label = @5 + local.get 2 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 0 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + br 3 (;@2;) + end + end + i32.const 1 + local.get 2 + i32.add + local.set 2 + br 0 (;@3;) + end + end + else + i32.const 0 + local.set 2 + block ;; label = @2 + loop ;; label = @3 + local.get 2 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.le_s + if (result i32) ;; label = @4 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.gt_s + else + i32.const 0 + end + local.get 2 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.le_s + i32.and + if ;; label = @4 + local.get 2 + i32.const 1 + i32.add + local.set 2 + br 1 (;@3;) + else + br 2 (;@2;) + end + end + end + local.get 2 + local.get 0 + i32.load offset=4 + i32.lt_s + if (result i32) ;; label = @2 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + local.get 1 + i32.eq + else + i32.const 0 + end + if ;; label = @2 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.tee 5 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.ge_s + if ;; label = @3 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + local.get 5 + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + call 5 + drop + i32.store offset=8 + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.tee 5 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.ge_s + if ;; label = @4 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + local.get 5 + i32.const 4 + i32.const 0 + i32.mul + i32.add + i32.load offset=8 + local.get 5 + local.get 5 + i32.const 4 + i32.const 0 + i32.mul + i32.add + i32.load offset=8 + call 5 + drop + i32.store offset=8 + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.set 5 + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + i32.mul + i32.add + local.get 1 + i32.store offset=8 + i32.const 0 + local.set 3 + block ;; label = @5 + loop ;; label = @6 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.eq + if ;; label = @7 + br 2 (;@5;) + else + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@6;) + end + end + end + local.get 5 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.add + i32.store offset=4 + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @5 + i32.const 0 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.add + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 5 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + end + local.get 2 + local.set 3 + block ;; label = @5 + loop ;; label = @6 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + local.get 3 + i32.eq + if ;; label = @7 + br 2 (;@5;) + else + local.get 0 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@6;) + end + end + end + local.get 2 + i32.const 1 + i32.add + local.set 3 + block ;; label = @5 + loop ;; label = @6 + local.get 0 + i32.load offset=4 + local.get 3 + i32.eq + if ;; label = @7 + br 2 (;@5;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@6;) + end + end + end + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + local.get 5 + local.get 1 + call 5 + drop + end + end + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.set 5 + local.get 5 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.eq + if ;; label = @3 + i32.const -1 + local.set 4 + local.get 2 + i32.const 1 + i32.add + local.get 0 + i32.load offset=4 + i32.le_s + if ;; label = @4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.ge_s + if ;; label = @5 + local.get 5 + i32.const 4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 5 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + i32.const 0 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + end + local.get 5 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.store offset=4 + i32.const 0 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.sub + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + local.set 3 + block ;; label = @7 + loop ;; label = @8 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.eq + if ;; label = @9 + br 2 (;@7;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@8;) + end + end + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + i32.const 0 + local.set 4 + end + end + local.get 4 + i32.const -1 + i32.eq + local.get 2 + i32.const 1 + i32.sub + i32.const 0 + i32.ge_s + i32.and + if ;; label = @4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.ge_s + if ;; label = @5 + local.get 5 + i32.load offset=4 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + i32.const 0 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 5 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 5 + i32.const 4 + local.get 3 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.sub + local.set 3 + br 1 (;@7;) + end + end + end + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + local.set 3 + block ;; label = @7 + loop ;; label = @8 + local.get 3 + i32.const 0 + i32.eq + if ;; label = @9 + br 2 (;@7;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.sub + local.set 3 + br 1 (;@8;) + end + end + end + end + local.get 5 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + local.get 5 + i32.const 4 + i32.const 0 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + end + i32.const 0 + local.set 4 + end + end + local.get 4 + i32.const -1 + i32.eq + if ;; label = @4 + local.get 2 + i32.const 1 + i32.add + local.get 0 + i32.load offset=4 + i32.le_s + if ;; label = @5 + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + i32.const 0 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + local.set 3 + block ;; label = @7 + loop ;; label = @8 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.add + i32.eq + if ;; label = @9 + br 2 (;@7;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 5 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@8;) + end + end + end + end + local.get 5 + local.get 5 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.add + i32.const 1 + i32.add + i32.store offset=4 + local.get 2 + i32.const 1 + i32.add + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 2 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 0 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + i32.const 0 + local.set 4 + end + local.get 4 + i32.const -1 + i32.eq + local.get 2 + i32.const 1 + i32.sub + i32.const 0 + i32.ge_s + i32.and + if ;; label = @5 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + i32.const 0 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 5 + i32.load offset=4 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.mul + i32.add + local.get 5 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + local.set 3 + block ;; label = @7 + loop ;; label = @8 + local.get 3 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + i32.eq + if ;; label = @9 + br 2 (;@7;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@8;) + end + end + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + local.get 5 + i32.load offset=4 + i32.add + i32.const 1 + i32.add + i32.store offset=4 + local.get 2 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 2 + i32.const 1 + i32.sub + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 0 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + end + end + end + local.get 4 + i32.const -1 + i32.eq + if ;; label = @3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.get 1 + call 5 + drop + else + local.get 5 + local.get 1 + call 5 + drop + end + end + end + i32.const 0 + i32.load offset=8 + i32.load offset=4 + i32.const 0 + i32.eq + if ;; label = @1 + i32.const 0 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + end + i32.const 0 + i32.load offset=8) + (func (;6;) (type 3) (param i32 i32 i32 i32 i32 i32) + (local i32) + local.get 0 + local.get 1 + i32.gt_s + local.get 1 + local.get 2 + i32.gt_s + local.get 3 + local.get 0 + i32.ne + local.get 3 + local.get 1 + i32.ne + local.get 3 + local.get 2 + i32.ne + local.get 4 + local.get 0 + i32.ne + local.get 4 + local.get 1 + i32.ne + local.get 4 + local.get 2 + i32.ne + local.get 4 + local.get 3 + i32.ne + local.get 5 + local.get 0 + i32.ne + local.get 5 + local.get 1 + i32.ne + local.get 5 + local.get 2 + i32.ne + local.get 5 + local.get 3 + i32.ne + local.get 5 + local.get 4 + i32.ne + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.eqz + if ;; label = @1 + unreachable + end + i32.const 4 + call 0 + local.set 6 + local.get 0 + call 4 + local.set 6 + local.get 1 + call 4 + local.set 6 + local.get 2 + call 4 + local.set 6 + local.get 3 + call 4 + local.set 6 + local.get 4 + call 4 + local.set 6 + local.get 5 + call 4 + local.set 6 + local.get 6 + local.get 0 + call 1 + i32.const -1 + i32.ne + local.get 6 + local.get 1 + call 1 + i32.const -1 + i32.ne + local.get 6 + local.get 2 + call 1 + i32.const -1 + i32.ne + local.get 6 + local.get 3 + call 1 + i32.const -1 + i32.ne + local.get 6 + local.get 4 + call 1 + i32.const -1 + i32.ne + local.get 6 + local.get 5 + call 1 + i32.const -1 + i32.ne + i32.and + i32.and + i32.and + i32.and + i32.and + local.get 6 + local.get 0 + call 5 + local.set 6 + local.get 6 + local.get 0 + call 1 + i32.const -1 + i32.eq + local.get 6 + local.get 1 + call 5 + local.set 6 + local.get 6 + local.get 1 + call 1 + i32.const -1 + i32.eq + local.get 6 + local.get 2 + call 5 + local.set 6 + local.get 6 + local.get 2 + call 1 + i32.const -1 + i32.eq + local.get 6 + local.get 3 + call 5 + local.set 6 + local.get 6 + local.get 3 + call 1 + i32.const -1 + i32.eq + local.get 6 + local.get 4 + call 5 + local.set 6 + local.get 6 + local.get 4 + call 1 + i32.const -1 + i32.eq + i32.and + i32.and + i32.and + i32.and + i32.and + i32.eqz + if ;; label = @1 + unreachable + end) + (func (;7;) (type 4) + i32.const 6 + i32.const 5 + i32.const 4 + i32.const 3 + i32.const 2 + i32.const 1 + call 6) + (memory (;0;) 2) + (export "main" (func 7)) + (start 7)) diff --git a/benchmarks/wasm/btree/3o3u.wat b/benchmarks/wasm/btree/3o3u.wat new file mode 100644 index 00000000..63f25f5f --- /dev/null +++ b/benchmarks/wasm/btree/3o3u.wat @@ -0,0 +1,2937 @@ +(module + (memory $0 2) + (func $createBtree (param i32) (result i32) ;; createBtree(t), where t: degree of the btree + (i32.const 0) + (local.get 0) + (i32.store) ;; store t at address 0 + (i32.const 0) + (i32.const 1) + (i32.store offset=4) ;; store the number of nodes in the btree = 1 + (i32.const 0) + (i32.const 65536) + (i32.store offset=8) ;; store the root addr + (i32.const 1) ;; Now create root + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then + (i32.const 65536) ;; 64KiB = 65536 bytes + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + (i32.const 65536) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + ) + ) + (i32.const 65536) ;; returns the address of the root + ) + ;; btreeSearch(x, k), where x: node address; k: key to search. Returns address of the key or -1 if key is not in tree. + (func $btreeSearch (param i32) (param i32) (result i32) (local i32) + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n -1 + (i32.le_s) ;; i <= x.children-1 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + (if + (then + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.le_s) ;; i <= x.children - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.eq) + ;; k == x.keys[i] + (if (result i32) + (then + (local.get 0) ;; save the address, which is x+8+i*4 + (i32.const 8) + (i32.add) ;; x+8 + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x+8+i*4 + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + ;; btreeSplitChild(x, i), where x: addr of a non full internal node; i: index such that x.children[i] is a full child of x + (func $btreeSplitChild (param i32) (param i32) (local i32) (local i32) + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then ;; get the number of nodes in the tree + (i32.const 0) + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address is 64KiB*(number of nodes+1) + (local.set 2) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the addr of the child in index i : y + (i32.load) ;; check if x.children[i] is leaf ;; load the first i32 in the child + (i32.const 1) + (i32.eq) ;; is y leaf? + (if + (then ;; yes + (local.get 2) ;; addr of new node z + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + ) + (else ;; no + (local.get 2) ;; addr of new node z + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + ) + ) + (local.get 2) ;; now store number of children, which will be t/2 -1 ;; addr of z + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.store offset=4) ;; store number of children = (t/2)-1 + (i32.const 0) ;; now to store the keys + (local.set 3) ;; counter = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.eq) ;; counter == (t/2)-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 2) ;; addr of z ;; we will store in the new node's address. z.keys[counter] + (i32.const 4) + (local.get 3) ;; counter + (i32.mul) ;; counter * 4 + (i32.add) ;; addr + counter * 4 -> where the key is goint to be located + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 4) ;; now get x.children[i].keys[counter+(t/2)] + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + (t/2) + (i32.mul) ;; counter + (t/2) *4 + (i32.add) ;; y + counter + (t/2) *4 + (i32.load offset=8) ;; y.keys[counter + (t/2)] + (i32.store offset=8) ;; z.keys[counter] = y.keys[counter + (t/2)] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) + (local.set 3) ;; set counter back to 0 + (i32.const 0) ;; check if x.children[i] is leaf + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load) ;; get first i32 in child --> isLeaf + (i32.const 1) + (i32.ne) ;; is leaf? + (if + (then ;; not leaf so we have to update the children + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.eq) ;; counter == (t/2) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; now get z.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; counter*4 + (i32.add) ;; (t-1)*4 + counter*4 + (local.get 2) ;; z + (i32.add) ;; z + (t-1)*4 + counter*4 --> address where addr of child is stored + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) ;; get x.children[i].children[counter+(t/2)] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + t/2 + (i32.const 4) + (i32.mul) ;; counter+t/2 * 4 + (i32.add) ;; (t-1)*4 + (counter+(t/2))*4 + (i32.add) ;; x.children[i] + (t-1)*4 + (counter + (t/2))*4 + (i32.load offset=8) ;; load the address of the child in index counter + t/2 + (i32.store offset=8) ;; z.children[counter] = x.children[i].children[counter] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.store offset=4) ;; number of keys of x.children[i] is now t/2 -1 + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (local.set 3) ;; counter = x.n + (block $loop_break + (loop $loop + (local.get 1) ;; i + (local.get 3) ;; counter + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; get x.children[counter + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (i32.add) ;; (t-1)*4 + (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter + 1)*4 -->address where addr of child is stored + (i32.const 0) ;; get x.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (i32.add) ;; (t-1)*4 + (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter)*4 + (i32.load offset=8) ;; load the addr of the child in index (counter) + (i32.store offset=8) ;; x.children[counter+1] = x.children[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 1) + (i32.add) ;; i + 1 + (i32.const 4) + (i32.mul) ;; (i + 1)*4 + (i32.add) ;; (t-1)*4 + (i + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i + 1)*4 --> addr where we will store the new addr of the child + (local.get 2) ;; addr of z + (i32.store offset=8) ;; x.children[i+1] = z + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (i32.const 1) + (i32.sub) + (local.set 3) ;; counter = x.n -1 + (block $loop_break + (loop $loop + (local.get 1) ;; i + (i32.const 1) + (i32.sub) ;; i-1 + (local.get 3) ;; counter + (i32.eq) ;; counter == i-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 3) ;; get x.keys[counter + 1] ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter + 1)*4 --> addr where we will store the new key + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter)*4 + (i32.load offset=8) ;; load the key in index (counter) + (i32.store offset=8) ;; x.keys[counter+1] = x.keys[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (local.get 1) ;; get x.keys[i] + (i32.const 4) + (i32.mul) ;; i*4 + (local.get 0) ;; x + (i32.add) ;; x +i*4 --> addr of the key + (i32.const 0) ;; get x.children[i].keys[t/2] + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.const 4) + (i32.mul) ;; (t/2)-1 *4 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; (i)*4 + (i32.add) ;; (t-1)*4 + (i)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i)*4 + (i32.load offset=8) ;; load the address of the child in index (i) + (i32.add) ;; y + (t/2)-1 *4 + (i32.load offset=8) ;; load the key in index (t/2)-1 from node x.children[i] + (i32.store offset=8) ;; store the new key in the addr + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) ;; x.n + 1 + (i32.store offset=4) ;; x.n = x.n + 1 + ) + ) + ) + ;; btreeInsertNonFull(x, k), where x: addr of a non full internal node; k: the key to insert + (func $btreeInsertNonFull (param i32) (param i32) (local i32) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n -1 + (local.set 2) ;; i = x.n -1 + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (i32.and) + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i *4 + (i32.add) ;; x + i *4 --> addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.keys[i+1] = x.keys[i] + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 1) ;; k + (i32.store offset=8) ;; x.keys[i+1] = k + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; x.n = x.n +1 + ) + (else ;; x is not leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i >= 0 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i -1 + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 2) ;;i = i+1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address of the child in index i = y + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load offset=4) ;; get y.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; y.n == t-1 --> node is full + (if + (then + (local.get 0) ;; x + (local.get 2) ;; i + (call $btreeSplitChild) + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k>x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i+1 + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (local.get 1) ;; k + (call $btreeInsertNonFull) + ) + ) + ) + (func $btreeInsert (param i32) (result i32) (local i32) (local i32) + (i32.const 0) + (i32.load offset=8) ;; root addr + (local.tee 2) ;; set r + (i32.load offset=4) ;; r.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; r.n == t-1 --> root is full + (if (result i32) + (then + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if (result i32) ;; if we can grow memory + (then + (i32.const 0) ;; get the number of nodes in the tree + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address of s is 64KiB*(number of nodes+1) + (local.set 1) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) + (local.get 1) ;; s + (i32.store offset=8) ;; root addr is now s + (local.get 1) ;; s + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + (local.get 1) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + (i32.const 0) ;; store children addresses + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 1) ;; x + (i32.add) ;; x + (t-1)*4 --> addre where addr of child number 0 is located + (local.get 2) + (i32.store offset=8) ;; s.children[0] = r + (local.get 1) ;; s + (i32.const 0) + (call $btreeSplitChild) + (local.get 1) ;; s + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 1) ;; return the address of the new root + ) + (else + (i32.const -1) ;; return -1 because we could not create a new node + ) + ) + ) + (else + (local.get 2) ;; r + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 2) ;; return the address of the root, which is the same + ) + ) + ) + ;; Returns the address of the new root (if applicable, otherwise returns the addres of the root) + (func $btreeDelete (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local i32) + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (local.get 2) ;; i + (i32.eq) ;; i == x.n + (if + (then + (br $loop_break) ;; break from loop + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + (if + (then + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $while_break + (loop $while + (local.get 3) ;; j + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n-1 + (i32.eq) ;; j == x.n-1 + (if + (then + (br $while_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 --> addr where x.keys[j] is stored + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.mul) ;; j+1 *4 + (i32.add) ;; x + j+1 *4 --> addr where x.keys[j+1] is stored + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $while) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n -1 + (br $loop_break) + ) + ) + ) + ) + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) ;; continue + ) + ) + ) + (else ;; x is not leaf + (i32.const 0) ;;get the index of the appropriate child/key + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (i32.and) ;; i <= x.n - 1 && k > x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i + 1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.lt_s) ;; i < x.n + (if (result i32) + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (if + (then ;; key is present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.tee 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (call $btreeDelete) ;; (x.c[i], x.c[i].keys[x.c[i].n - 1]) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i].keys[x.c[i].n - 1] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i+1] + (local.tee 5) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (local.get 5) ;; x.c[i+1] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (call $btreeDelete) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 --> addr where x.c[i].keys[x.c[i].n] will be stored + (local.get 1) ;; k + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = k + (i32.const 0) ;; now merge x.c[i] with x.c[i+1] + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if ;; j == x.c[i+1].n + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n + j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[index] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x.c[i+1] + i*4 ;; addr of x.c[i+1].keys[j] + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[index] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n +1 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + 1 + x.c[i+1].n + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; now adjust the children + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.get 3) ;; j + (i32.add) ;; index = x.c[i].n+j + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x.c[i] + (t-1)*4 --> address where x.c[i].c[x.c[i].n+j] is stored + (i32.const 0) ;;get x.c[i+1].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x.c[i+1] + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (i32.const 1) + (i32.sub) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) + (i32.mul) ;; j+1*4 + (i32.add) ;; x + j+1*4 ;; addr of x.keys[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = i+1 + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 0) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n-1 + (local.get 5) ;; x.c[i] + (local.get 1) ;; k + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) + (else ;; key not present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) + (i32.const 1) + (i32.sub) ;; t/2 - 1 + (i32.eq) + (if + (then + (i32.const -1) + (local.set 4) ;; changed = -1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.le_s) ;; i+1 <= x.n + (if + (then + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; index = t/2 -1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[t/2-1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[t/2-1] = x.keys[i] + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;;x.c[i].n = x.c[i].n +1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i+1].keys[0] + (i32.load offset=8) ;; x.c[i+1].keys[0] + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + (local.get 5) + (i32.load) ;; x.c[i] is leaf? + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) ;;get x.c[i].c[t/2] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; index = t/2 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[t/2] + (i32.const 0) ;;get x.c[i+1].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[0] + (i32.store offset=8) ;; x.c[i].c[t/2] = x.c[i+1].c[0] + ) + ) + (local.get 5) ;; x.c[i] + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.store offset=4) ;; x.c[i].n = t/2 + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 --> address where x.c[i+1].keys[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j+1] + (i32.store offset=8) ;; x.c[i+1].keys[j] = x.c[i+1].keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i+1].c[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j+1] + (i32.store offset=8) ;; x.c[i+1].c[j] = x.c[i+1].c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr where x.c[i+1] is located + (i32.load offset=8) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i+1].n = x.c[i+1].n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i -1 >= 0 + (i32.and) ;; changed == -1 && i -1 >= 0 + (if + (then + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i-1].n >= t/2 + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.set 3) ;; j = x.c[i].n + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j-1] + (i32.load offset=8) ;; x.c[i].keys[j-1] + (i32.store offset=8) ;; x.c[i].keys[j] = x.c[i].keys[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (local.set 3) ;; j = x.c[i].n + 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i].c[j] is located + (i32.const 0) ;;get x.c[i].c[j-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) + (i32.load offset=8) ;; x.c[i].c[j-1] + (i32.store offset=8) ;; x.c[i].c[j] = x.c[i].c[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; ;; x.c[i].n = x.c[i].n + 1 + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[0] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i-1] + (i32.store offset=8) ;; x.c[i].keys[0] = x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n - 1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[i-1] = x.c[i-1].keys[x.c[i-1].n] + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; get x.c[i].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 ;; addr where x.c[i].c[0] is located + (i32.const 0) ;; get x.c[i-1].c[x.c[i-1].n + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.add) ;; index = x.c[i-1.n + 1] + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1].c[x.c[i-1].n + 1] + (i32.store offset=8) ;; x.c[i].c[0] = x.c[i-1].c[x.c[i-1].n + 1] + ) + ) + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (if ;; changed == -1? + (then + (local.get 2) ;; we have to merge x.c[i] with one sibling;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.le_s) + (if ;; i+1 <= x.n + (then + (local.get 5) ;; merge with right sibling;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = x.keys[i] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n+j] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n+j] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i+1].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[x.c[i].n+j] + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) ;; x.c[i].n + x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + x.c[i+1].n + 1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.set 3) ;; j = i+1 + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) ;; changed == -1 ? + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i - 1 >= 0? + (i32.and) ;; changed == -1 && i - 1 >= 0 + (if + (then + (i32.const 0) ;; merge with left sibling ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n] = x.keys[i-1] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n+j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[j] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n+j] = x.c[i].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i-1].c[x.c[i-1].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i-1].c[x.c[i-1].n+j] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i].c[j] + (i32.store offset=8) ;; x.c[i-1].c[x.c[i-1].n+j] = x.c[i].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.add) ;; x.c[i-1].n + x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n + x.c[i].n + 1 + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = i - 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + ) + ) + ) + ) + ) + ) + (local.get 4) + (i32.const -1) + (i32.eq) + (if ;; changed == -1? if yes, we merged with left sibling + (then + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (local.get 1) + (call $btreeDelete) + (drop) + ) + (else + (local.get 5) ;; x.c[i] + (local.get 1) + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) ;; end of if x is not leaf + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.load offset=4) + (i32.const 0) + (i32.eq) + (if ;; if root is empty + (then + (i32.const 0) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; root.c[0] + (i32.store offset=8) ;; root = root.c[0] + ) + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + ) + (func $main + (param $a i32) + (param $b i32) + (param $c i32) + (param $h i32) + (param $i i32) + (param $j i32) + (local $btree i32) + (local.get $a) + (local.get $b) + (i32.gt_s) + (local.get $b) + (local.get $c) + (i32.gt_s) + (local.get $h) + (local.get $a) + (i32.ne) + (local.get $h) + (local.get $b) + (i32.ne) + (local.get $h) + (local.get $c) + (i32.ne) + (local.get $i) + (local.get $a) + (i32.ne) + (local.get $i) + (local.get $b) + (i32.ne) + (local.get $i) + (local.get $c) + (i32.ne) + (local.get $i) + (local.get $h) + (i32.ne) + (local.get $j) + (local.get $a) + (i32.ne) + (local.get $j) + (local.get $b) + (i32.ne) + (local.get $j) + (local.get $c) + (i32.ne) + (local.get $j) + (local.get $h) + (i32.ne) + (local.get $j) + (local.get $i) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.eqz) + (if (then (unreachable))) + (i32.const 4) ;;create a tree with degree 4 + (call $createBtree) + (local.set $btree) + ;; insert variables + (local.get $a) + (call $btreeInsert) + (local.set $btree) + (local.get $b) + (call $btreeInsert) + (local.set $btree) + (local.get $c) + (call $btreeInsert) + (local.set $btree) + (local.get $h) + (call $btreeInsert) + (local.set $btree) + (local.get $i) + (call $btreeInsert) + (local.set $btree) + (local.get $j) + (call $btreeInsert) + (local.set $btree) + ;; search for variables & check that they were inserted + (local.get $btree) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $i) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $j) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + ;; delete & check that it was deleted + ;; a + (local.get $btree) + (local.get $a) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; b + (local.get $btree) + (local.get $b) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; c + (local.get $btree) + (local.get $c) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; h + (local.get $btree) + (local.get $h) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; i + (local.get $btree) + (local.get $i) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $i) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + ;; last functional check + (i32.eqz) + (if (then (unreachable))) + ) + (export "main" (func $real_main)) + (func $real_main + i32.const 6 + i32.const 5 + i32.const 4 + i32.const 3 + i32.const 2 + i32.const 1 + call $main + ) + (start $real_main) +) diff --git a/benchmarks/wasm/btree/4o1u.wat b/benchmarks/wasm/btree/4o1u.wat new file mode 100644 index 00000000..e457f8c4 --- /dev/null +++ b/benchmarks/wasm/btree/4o1u.wat @@ -0,0 +1,2874 @@ +(module + (memory $0 2) + (func $createBtree (param i32) (result i32) ;; createBtree(t), where t: degree of the btree + (i32.const 0) + (local.get 0) + (i32.store) ;; store t at address 0 + (i32.const 0) + (i32.const 1) + (i32.store offset=4) ;; store the number of nodes in the btree = 1 + (i32.const 0) + (i32.const 65536) + (i32.store offset=8) ;; store the root addr + (i32.const 1) ;; Now create root + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then + (i32.const 65536) ;; 64KiB = 65536 bytes + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + (i32.const 65536) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + ) + ) + (i32.const 65536) ;; returns the address of the root + ) + ;; btreeSearch(x, k), where x: node address; k: key to search. Returns address of the key or -1 if key is not in tree. + (func $btreeSearch (param i32) (param i32) (result i32) (local i32) + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n -1 + (i32.le_s) ;; i <= x.children-1 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + (if + (then + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.le_s) ;; i <= x.children - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.eq) + ;; k == x.keys[i] + (if (result i32) + (then + (local.get 0) ;; save the address, which is x+8+i*4 + (i32.const 8) + (i32.add) ;; x+8 + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x+8+i*4 + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + ;; btreeSplitChild(x, i), where x: addr of a non full internal node; i: index such that x.children[i] is a full child of x + (func $btreeSplitChild (param i32) (param i32) (local i32) (local i32) + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then ;; get the number of nodes in the tree + (i32.const 0) + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address is 64KiB*(number of nodes+1) + (local.set 2) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the addr of the child in index i : y + (i32.load) ;; check if x.children[i] is leaf ;; load the first i32 in the child + (i32.const 1) + (i32.eq) ;; is y leaf? + (if + (then ;; yes + (local.get 2) ;; addr of new node z + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + ) + (else ;; no + (local.get 2) ;; addr of new node z + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + ) + ) + (local.get 2) ;; now store number of children, which will be t/2 -1 ;; addr of z + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.store offset=4) ;; store number of children = (t/2)-1 + (i32.const 0) ;; now to store the keys + (local.set 3) ;; counter = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.eq) ;; counter == (t/2)-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 2) ;; addr of z ;; we will store in the new node's address. z.keys[counter] + (i32.const 4) + (local.get 3) ;; counter + (i32.mul) ;; counter * 4 + (i32.add) ;; addr + counter * 4 -> where the key is goint to be located + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 4) ;; now get x.children[i].keys[counter+(t/2)] + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + (t/2) + (i32.mul) ;; counter + (t/2) *4 + (i32.add) ;; y + counter + (t/2) *4 + (i32.load offset=8) ;; y.keys[counter + (t/2)] + (i32.store offset=8) ;; z.keys[counter] = y.keys[counter + (t/2)] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) + (local.set 3) ;; set counter back to 0 + (i32.const 0) ;; check if x.children[i] is leaf + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load) ;; get first i32 in child --> isLeaf + (i32.const 1) + (i32.ne) ;; is leaf? + (if + (then ;; not leaf so we have to update the children + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.eq) ;; counter == (t/2) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; now get z.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; counter*4 + (i32.add) ;; (t-1)*4 + counter*4 + (local.get 2) ;; z + (i32.add) ;; z + (t-1)*4 + counter*4 --> address where addr of child is stored + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) ;; get x.children[i].children[counter+(t/2)] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + t/2 + (i32.const 4) + (i32.mul) ;; counter+t/2 * 4 + (i32.add) ;; (t-1)*4 + (counter+(t/2))*4 + (i32.add) ;; x.children[i] + (t-1)*4 + (counter + (t/2))*4 + (i32.load offset=8) ;; load the address of the child in index counter + t/2 + (i32.store offset=8) ;; z.children[counter] = x.children[i].children[counter] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.store offset=4) ;; number of keys of x.children[i] is now t/2 -1 + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (local.set 3) ;; counter = x.n + (block $loop_break + (loop $loop + (local.get 1) ;; i + (local.get 3) ;; counter + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; get x.children[counter + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (i32.add) ;; (t-1)*4 + (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter + 1)*4 -->address where addr of child is stored + (i32.const 0) ;; get x.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (i32.add) ;; (t-1)*4 + (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter)*4 + (i32.load offset=8) ;; load the addr of the child in index (counter) + (i32.store offset=8) ;; x.children[counter+1] = x.children[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 1) + (i32.add) ;; i + 1 + (i32.const 4) + (i32.mul) ;; (i + 1)*4 + (i32.add) ;; (t-1)*4 + (i + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i + 1)*4 --> addr where we will store the new addr of the child + (local.get 2) ;; addr of z + (i32.store offset=8) ;; x.children[i+1] = z + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (i32.const 1) + (i32.sub) + (local.set 3) ;; counter = x.n -1 + (block $loop_break + (loop $loop + (local.get 1) ;; i + (i32.const 1) + (i32.sub) ;; i-1 + (local.get 3) ;; counter + (i32.eq) ;; counter == i-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 3) ;; get x.keys[counter + 1] ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter + 1)*4 --> addr where we will store the new key + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter)*4 + (i32.load offset=8) ;; load the key in index (counter) + (i32.store offset=8) ;; x.keys[counter+1] = x.keys[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (local.get 1) ;; get x.keys[i] + (i32.const 4) + (i32.mul) ;; i*4 + (local.get 0) ;; x + (i32.add) ;; x +i*4 --> addr of the key + (i32.const 0) ;; get x.children[i].keys[t/2] + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.const 4) + (i32.mul) ;; (t/2)-1 *4 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; (i)*4 + (i32.add) ;; (t-1)*4 + (i)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i)*4 + (i32.load offset=8) ;; load the address of the child in index (i) + (i32.add) ;; y + (t/2)-1 *4 + (i32.load offset=8) ;; load the key in index (t/2)-1 from node x.children[i] + (i32.store offset=8) ;; store the new key in the addr + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) ;; x.n + 1 + (i32.store offset=4) ;; x.n = x.n + 1 + ) + ) + ) + ;; btreeInsertNonFull(x, k), where x: addr of a non full internal node; k: the key to insert + (func $btreeInsertNonFull (param i32) (param i32) (local i32) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n -1 + (local.set 2) ;; i = x.n -1 + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (i32.and) + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i *4 + (i32.add) ;; x + i *4 --> addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.keys[i+1] = x.keys[i] + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 1) ;; k + (i32.store offset=8) ;; x.keys[i+1] = k + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; x.n = x.n +1 + ) + (else ;; x is not leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i >= 0 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i -1 + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 2) ;;i = i+1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address of the child in index i = y + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load offset=4) ;; get y.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; y.n == t-1 --> node is full + (if + (then + (local.get 0) ;; x + (local.get 2) ;; i + (call $btreeSplitChild) + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k>x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i+1 + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (local.get 1) ;; k + (call $btreeInsertNonFull) + ) + ) + ) + (func $btreeInsert (param i32) (result i32) (local i32) (local i32) + (i32.const 0) + (i32.load offset=8) ;; root addr + (local.tee 2) ;; set r + (i32.load offset=4) ;; r.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; r.n == t-1 --> root is full + (if (result i32) + (then + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if (result i32) ;; if we can grow memory + (then + (i32.const 0) ;; get the number of nodes in the tree + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address of s is 64KiB*(number of nodes+1) + (local.set 1) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) + (local.get 1) ;; s + (i32.store offset=8) ;; root addr is now s + (local.get 1) ;; s + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + (local.get 1) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + (i32.const 0) ;; store children addresses + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 1) ;; x + (i32.add) ;; x + (t-1)*4 --> addre where addr of child number 0 is located + (local.get 2) + (i32.store offset=8) ;; s.children[0] = r + (local.get 1) ;; s + (i32.const 0) + (call $btreeSplitChild) + (local.get 1) ;; s + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 1) ;; return the address of the new root + ) + (else + (i32.const -1) ;; return -1 because we could not create a new node + ) + ) + ) + (else + (local.get 2) ;; r + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 2) ;; return the address of the root, which is the same + ) + ) + ) + ;; Returns the address of the new root (if applicable, otherwise returns the addres of the root) + (func $btreeDelete (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local i32) + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (local.get 2) ;; i + (i32.eq) ;; i == x.n + (if + (then + (br $loop_break) ;; break from loop + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + (if + (then + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $while_break + (loop $while + (local.get 3) ;; j + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n-1 + (i32.eq) ;; j == x.n-1 + (if + (then + (br $while_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 --> addr where x.keys[j] is stored + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.mul) ;; j+1 *4 + (i32.add) ;; x + j+1 *4 --> addr where x.keys[j+1] is stored + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $while) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n -1 + (br $loop_break) + ) + ) + ) + ) + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) ;; continue + ) + ) + ) + (else ;; x is not leaf + (i32.const 0) ;;get the index of the appropriate child/key + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (i32.and) ;; i <= x.n - 1 && k > x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i + 1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.lt_s) ;; i < x.n + (if (result i32) + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (if + (then ;; key is present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.tee 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (call $btreeDelete) ;; (x.c[i], x.c[i].keys[x.c[i].n - 1]) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i].keys[x.c[i].n - 1] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i+1] + (local.tee 5) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (local.get 5) ;; x.c[i+1] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (call $btreeDelete) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 --> addr where x.c[i].keys[x.c[i].n] will be stored + (local.get 1) ;; k + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = k + (i32.const 0) ;; now merge x.c[i] with x.c[i+1] + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if ;; j == x.c[i+1].n + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n + j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[index] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x.c[i+1] + i*4 ;; addr of x.c[i+1].keys[j] + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[index] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n +1 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + 1 + x.c[i+1].n + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; now adjust the children + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.get 3) ;; j + (i32.add) ;; index = x.c[i].n+j + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x.c[i] + (t-1)*4 --> address where x.c[i].c[x.c[i].n+j] is stored + (i32.const 0) ;;get x.c[i+1].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x.c[i+1] + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (i32.const 1) + (i32.sub) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) + (i32.mul) ;; j+1*4 + (i32.add) ;; x + j+1*4 ;; addr of x.keys[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = i+1 + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 0) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n-1 + (local.get 5) ;; x.c[i] + (local.get 1) ;; k + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) + (else ;; key not present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) + (i32.const 1) + (i32.sub) ;; t/2 - 1 + (i32.eq) + (if + (then + (i32.const -1) + (local.set 4) ;; changed = -1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.le_s) ;; i+1 <= x.n + (if + (then + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; index = t/2 -1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[t/2-1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[t/2-1] = x.keys[i] + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;;x.c[i].n = x.c[i].n +1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i+1].keys[0] + (i32.load offset=8) ;; x.c[i+1].keys[0] + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + (local.get 5) + (i32.load) ;; x.c[i] is leaf? + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) ;;get x.c[i].c[t/2] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; index = t/2 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[t/2] + (i32.const 0) ;;get x.c[i+1].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[0] + (i32.store offset=8) ;; x.c[i].c[t/2] = x.c[i+1].c[0] + ) + ) + (local.get 5) ;; x.c[i] + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.store offset=4) ;; x.c[i].n = t/2 + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 --> address where x.c[i+1].keys[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j+1] + (i32.store offset=8) ;; x.c[i+1].keys[j] = x.c[i+1].keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i+1].c[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j+1] + (i32.store offset=8) ;; x.c[i+1].c[j] = x.c[i+1].c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr where x.c[i+1] is located + (i32.load offset=8) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i+1].n = x.c[i+1].n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i -1 >= 0 + (i32.and) ;; changed == -1 && i -1 >= 0 + (if + (then + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i-1].n >= t/2 + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.set 3) ;; j = x.c[i].n + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j-1] + (i32.load offset=8) ;; x.c[i].keys[j-1] + (i32.store offset=8) ;; x.c[i].keys[j] = x.c[i].keys[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (local.set 3) ;; j = x.c[i].n + 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i].c[j] is located + (i32.const 0) ;;get x.c[i].c[j-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) + (i32.load offset=8) ;; x.c[i].c[j-1] + (i32.store offset=8) ;; x.c[i].c[j] = x.c[i].c[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; ;; x.c[i].n = x.c[i].n + 1 + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[0] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i-1] + (i32.store offset=8) ;; x.c[i].keys[0] = x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n - 1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[i-1] = x.c[i-1].keys[x.c[i-1].n] + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; get x.c[i].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 ;; addr where x.c[i].c[0] is located + (i32.const 0) ;; get x.c[i-1].c[x.c[i-1].n + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.add) ;; index = x.c[i-1.n + 1] + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1].c[x.c[i-1].n + 1] + (i32.store offset=8) ;; x.c[i].c[0] = x.c[i-1].c[x.c[i-1].n + 1] + ) + ) + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (if ;; changed == -1? + (then + (local.get 2) ;; we have to merge x.c[i] with one sibling;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.le_s) + (if ;; i+1 <= x.n + (then + (local.get 5) ;; merge with right sibling;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = x.keys[i] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n+j] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n+j] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i+1].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[x.c[i].n+j] + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) ;; x.c[i].n + x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + x.c[i+1].n + 1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.set 3) ;; j = i+1 + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) ;; changed == -1 ? + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i - 1 >= 0? + (i32.and) ;; changed == -1 && i - 1 >= 0 + (if + (then + (i32.const 0) ;; merge with left sibling ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n] = x.keys[i-1] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n+j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[j] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n+j] = x.c[i].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i-1].c[x.c[i-1].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i-1].c[x.c[i-1].n+j] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i].c[j] + (i32.store offset=8) ;; x.c[i-1].c[x.c[i-1].n+j] = x.c[i].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.add) ;; x.c[i-1].n + x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n + x.c[i].n + 1 + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = i - 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + ) + ) + ) + ) + ) + ) + (local.get 4) + (i32.const -1) + (i32.eq) + (if ;; changed == -1? if yes, we merged with left sibling + (then + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (local.get 1) + (call $btreeDelete) + (drop) + ) + (else + (local.get 5) ;; x.c[i] + (local.get 1) + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) ;; end of if x is not leaf + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.load offset=4) + (i32.const 0) + (i32.eq) + (if ;; if root is empty + (then + (i32.const 0) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; root.c[0] + (i32.store offset=8) ;; root = root.c[0] + ) + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + ) + (func $main + (param $a i32) + (param $b i32) + (param $c i32) + (param $d i32) + (param $h i32) + (local $btree i32) + (local.get $a) + (local.get $b) + (i32.gt_s) + (local.get $b) + (local.get $c) + (i32.gt_s) + (local.get $c) + (local.get $d) + (i32.gt_s) + (local.get $h) + (local.get $a) + (i32.ne) + (local.get $h) + (local.get $b) + (i32.ne) + (local.get $h) + (local.get $c) + (i32.ne) + (local.get $h) + (local.get $d) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.eqz) + (if (then (unreachable))) + (i32.const 4) ;;create a tree with degree 4 + (call $createBtree) + (local.set $btree) + ;; insert variables + (local.get $a) + (call $btreeInsert) + (local.set $btree) + (local.get $b) + (call $btreeInsert) + (local.set $btree) + (local.get $c) + (call $btreeInsert) + (local.set $btree) + (local.get $d) + (call $btreeInsert) + (local.set $btree) + (local.get $h) + (call $btreeInsert) + (local.set $btree) + ;; search for variables & check that they were inserted + (local.get $btree) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + ;; delete & check that it was deleted + ;; a + (local.get $btree) + (local.get $a) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; b + (local.get $btree) + (local.get $b) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; c + (local.get $btree) + (local.get $c) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; d + (local.get $btree) + (local.get $d) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (drop)) + (export "main" (func $main))) diff --git a/benchmarks/wasm/btree/4o2u.wat b/benchmarks/wasm/btree/4o2u.wat new file mode 100644 index 00000000..e98842bf --- /dev/null +++ b/benchmarks/wasm/btree/4o2u.wat @@ -0,0 +1,2915 @@ +(module + (memory $0 2) + (func $createBtree (param i32) (result i32) ;; createBtree(t), where t: degree of the btree + (i32.const 0) + (local.get 0) + (i32.store) ;; store t at address 0 + (i32.const 0) + (i32.const 1) + (i32.store offset=4) ;; store the number of nodes in the btree = 1 + (i32.const 0) + (i32.const 65536) + (i32.store offset=8) ;; store the root addr + (i32.const 1) ;; Now create root + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then + (i32.const 65536) ;; 64KiB = 65536 bytes + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + (i32.const 65536) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + ) + ) + (i32.const 65536) ;; returns the address of the root + ) + ;; btreeSearch(x, k), where x: node address; k: key to search. Returns address of the key or -1 if key is not in tree. + (func $btreeSearch (param i32) (param i32) (result i32) (local i32) + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n -1 + (i32.le_s) ;; i <= x.children-1 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + (if + (then + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.le_s) ;; i <= x.children - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.eq) + ;; k == x.keys[i] + (if (result i32) + (then + (local.get 0) ;; save the address, which is x+8+i*4 + (i32.const 8) + (i32.add) ;; x+8 + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x+8+i*4 + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + ;; btreeSplitChild(x, i), where x: addr of a non full internal node; i: index such that x.children[i] is a full child of x + (func $btreeSplitChild (param i32) (param i32) (local i32) (local i32) + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then ;; get the number of nodes in the tree + (i32.const 0) + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address is 64KiB*(number of nodes+1) + (local.set 2) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the addr of the child in index i : y + (i32.load) ;; check if x.children[i] is leaf ;; load the first i32 in the child + (i32.const 1) + (i32.eq) ;; is y leaf? + (if + (then ;; yes + (local.get 2) ;; addr of new node z + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + ) + (else ;; no + (local.get 2) ;; addr of new node z + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + ) + ) + (local.get 2) ;; now store number of children, which will be t/2 -1 ;; addr of z + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.store offset=4) ;; store number of children = (t/2)-1 + (i32.const 0) ;; now to store the keys + (local.set 3) ;; counter = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.eq) ;; counter == (t/2)-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 2) ;; addr of z ;; we will store in the new node's address. z.keys[counter] + (i32.const 4) + (local.get 3) ;; counter + (i32.mul) ;; counter * 4 + (i32.add) ;; addr + counter * 4 -> where the key is goint to be located + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 4) ;; now get x.children[i].keys[counter+(t/2)] + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + (t/2) + (i32.mul) ;; counter + (t/2) *4 + (i32.add) ;; y + counter + (t/2) *4 + (i32.load offset=8) ;; y.keys[counter + (t/2)] + (i32.store offset=8) ;; z.keys[counter] = y.keys[counter + (t/2)] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) + (local.set 3) ;; set counter back to 0 + (i32.const 0) ;; check if x.children[i] is leaf + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load) ;; get first i32 in child --> isLeaf + (i32.const 1) + (i32.ne) ;; is leaf? + (if + (then ;; not leaf so we have to update the children + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.eq) ;; counter == (t/2) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; now get z.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; counter*4 + (i32.add) ;; (t-1)*4 + counter*4 + (local.get 2) ;; z + (i32.add) ;; z + (t-1)*4 + counter*4 --> address where addr of child is stored + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) ;; get x.children[i].children[counter+(t/2)] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + t/2 + (i32.const 4) + (i32.mul) ;; counter+t/2 * 4 + (i32.add) ;; (t-1)*4 + (counter+(t/2))*4 + (i32.add) ;; x.children[i] + (t-1)*4 + (counter + (t/2))*4 + (i32.load offset=8) ;; load the address of the child in index counter + t/2 + (i32.store offset=8) ;; z.children[counter] = x.children[i].children[counter] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.store offset=4) ;; number of keys of x.children[i] is now t/2 -1 + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (local.set 3) ;; counter = x.n + (block $loop_break + (loop $loop + (local.get 1) ;; i + (local.get 3) ;; counter + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; get x.children[counter + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (i32.add) ;; (t-1)*4 + (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter + 1)*4 -->address where addr of child is stored + (i32.const 0) ;; get x.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (i32.add) ;; (t-1)*4 + (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter)*4 + (i32.load offset=8) ;; load the addr of the child in index (counter) + (i32.store offset=8) ;; x.children[counter+1] = x.children[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 1) + (i32.add) ;; i + 1 + (i32.const 4) + (i32.mul) ;; (i + 1)*4 + (i32.add) ;; (t-1)*4 + (i + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i + 1)*4 --> addr where we will store the new addr of the child + (local.get 2) ;; addr of z + (i32.store offset=8) ;; x.children[i+1] = z + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (i32.const 1) + (i32.sub) + (local.set 3) ;; counter = x.n -1 + (block $loop_break + (loop $loop + (local.get 1) ;; i + (i32.const 1) + (i32.sub) ;; i-1 + (local.get 3) ;; counter + (i32.eq) ;; counter == i-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 3) ;; get x.keys[counter + 1] ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter + 1)*4 --> addr where we will store the new key + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter)*4 + (i32.load offset=8) ;; load the key in index (counter) + (i32.store offset=8) ;; x.keys[counter+1] = x.keys[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (local.get 1) ;; get x.keys[i] + (i32.const 4) + (i32.mul) ;; i*4 + (local.get 0) ;; x + (i32.add) ;; x +i*4 --> addr of the key + (i32.const 0) ;; get x.children[i].keys[t/2] + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.const 4) + (i32.mul) ;; (t/2)-1 *4 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; (i)*4 + (i32.add) ;; (t-1)*4 + (i)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i)*4 + (i32.load offset=8) ;; load the address of the child in index (i) + (i32.add) ;; y + (t/2)-1 *4 + (i32.load offset=8) ;; load the key in index (t/2)-1 from node x.children[i] + (i32.store offset=8) ;; store the new key in the addr + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) ;; x.n + 1 + (i32.store offset=4) ;; x.n = x.n + 1 + ) + ) + ) + ;; btreeInsertNonFull(x, k), where x: addr of a non full internal node; k: the key to insert + (func $btreeInsertNonFull (param i32) (param i32) (local i32) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n -1 + (local.set 2) ;; i = x.n -1 + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (i32.and) + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i *4 + (i32.add) ;; x + i *4 --> addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.keys[i+1] = x.keys[i] + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 1) ;; k + (i32.store offset=8) ;; x.keys[i+1] = k + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; x.n = x.n +1 + ) + (else ;; x is not leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i >= 0 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i -1 + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 2) ;;i = i+1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address of the child in index i = y + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load offset=4) ;; get y.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; y.n == t-1 --> node is full + (if + (then + (local.get 0) ;; x + (local.get 2) ;; i + (call $btreeSplitChild) + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k>x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i+1 + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (local.get 1) ;; k + (call $btreeInsertNonFull) + ) + ) + ) + (func $btreeInsert (param i32) (result i32) (local i32) (local i32) + (i32.const 0) + (i32.load offset=8) ;; root addr + (local.tee 2) ;; set r + (i32.load offset=4) ;; r.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; r.n == t-1 --> root is full + (if (result i32) + (then + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if (result i32) ;; if we can grow memory + (then + (i32.const 0) ;; get the number of nodes in the tree + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address of s is 64KiB*(number of nodes+1) + (local.set 1) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) + (local.get 1) ;; s + (i32.store offset=8) ;; root addr is now s + (local.get 1) ;; s + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + (local.get 1) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + (i32.const 0) ;; store children addresses + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 1) ;; x + (i32.add) ;; x + (t-1)*4 --> addre where addr of child number 0 is located + (local.get 2) + (i32.store offset=8) ;; s.children[0] = r + (local.get 1) ;; s + (i32.const 0) + (call $btreeSplitChild) + (local.get 1) ;; s + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 1) ;; return the address of the new root + ) + (else + (i32.const -1) ;; return -1 because we could not create a new node + ) + ) + ) + (else + (local.get 2) ;; r + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 2) ;; return the address of the root, which is the same + ) + ) + ) + ;; Returns the address of the new root (if applicable, otherwise returns the addres of the root) + (func $btreeDelete (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local i32) + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (local.get 2) ;; i + (i32.eq) ;; i == x.n + (if + (then + (br $loop_break) ;; break from loop + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + (if + (then + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $while_break + (loop $while + (local.get 3) ;; j + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n-1 + (i32.eq) ;; j == x.n-1 + (if + (then + (br $while_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 --> addr where x.keys[j] is stored + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.mul) ;; j+1 *4 + (i32.add) ;; x + j+1 *4 --> addr where x.keys[j+1] is stored + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $while) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n -1 + (br $loop_break) + ) + ) + ) + ) + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) ;; continue + ) + ) + ) + (else ;; x is not leaf + (i32.const 0) ;;get the index of the appropriate child/key + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (i32.and) ;; i <= x.n - 1 && k > x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i + 1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.lt_s) ;; i < x.n + (if (result i32) + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (if + (then ;; key is present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.tee 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (call $btreeDelete) ;; (x.c[i], x.c[i].keys[x.c[i].n - 1]) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i].keys[x.c[i].n - 1] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i+1] + (local.tee 5) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (local.get 5) ;; x.c[i+1] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (call $btreeDelete) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 --> addr where x.c[i].keys[x.c[i].n] will be stored + (local.get 1) ;; k + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = k + (i32.const 0) ;; now merge x.c[i] with x.c[i+1] + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if ;; j == x.c[i+1].n + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n + j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[index] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x.c[i+1] + i*4 ;; addr of x.c[i+1].keys[j] + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[index] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n +1 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + 1 + x.c[i+1].n + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; now adjust the children + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.get 3) ;; j + (i32.add) ;; index = x.c[i].n+j + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x.c[i] + (t-1)*4 --> address where x.c[i].c[x.c[i].n+j] is stored + (i32.const 0) ;;get x.c[i+1].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x.c[i+1] + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (i32.const 1) + (i32.sub) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) + (i32.mul) ;; j+1*4 + (i32.add) ;; x + j+1*4 ;; addr of x.keys[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = i+1 + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 0) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n-1 + (local.get 5) ;; x.c[i] + (local.get 1) ;; k + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) + (else ;; key not present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) + (i32.const 1) + (i32.sub) ;; t/2 - 1 + (i32.eq) + (if + (then + (i32.const -1) + (local.set 4) ;; changed = -1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.le_s) ;; i+1 <= x.n + (if + (then + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; index = t/2 -1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[t/2-1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[t/2-1] = x.keys[i] + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;;x.c[i].n = x.c[i].n +1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i+1].keys[0] + (i32.load offset=8) ;; x.c[i+1].keys[0] + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + (local.get 5) + (i32.load) ;; x.c[i] is leaf? + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) ;;get x.c[i].c[t/2] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; index = t/2 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[t/2] + (i32.const 0) ;;get x.c[i+1].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[0] + (i32.store offset=8) ;; x.c[i].c[t/2] = x.c[i+1].c[0] + ) + ) + (local.get 5) ;; x.c[i] + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.store offset=4) ;; x.c[i].n = t/2 + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 --> address where x.c[i+1].keys[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j+1] + (i32.store offset=8) ;; x.c[i+1].keys[j] = x.c[i+1].keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i+1].c[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j+1] + (i32.store offset=8) ;; x.c[i+1].c[j] = x.c[i+1].c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr where x.c[i+1] is located + (i32.load offset=8) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i+1].n = x.c[i+1].n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i -1 >= 0 + (i32.and) ;; changed == -1 && i -1 >= 0 + (if + (then + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i-1].n >= t/2 + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.set 3) ;; j = x.c[i].n + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j-1] + (i32.load offset=8) ;; x.c[i].keys[j-1] + (i32.store offset=8) ;; x.c[i].keys[j] = x.c[i].keys[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (local.set 3) ;; j = x.c[i].n + 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i].c[j] is located + (i32.const 0) ;;get x.c[i].c[j-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) + (i32.load offset=8) ;; x.c[i].c[j-1] + (i32.store offset=8) ;; x.c[i].c[j] = x.c[i].c[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; ;; x.c[i].n = x.c[i].n + 1 + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[0] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i-1] + (i32.store offset=8) ;; x.c[i].keys[0] = x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n - 1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[i-1] = x.c[i-1].keys[x.c[i-1].n] + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; get x.c[i].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 ;; addr where x.c[i].c[0] is located + (i32.const 0) ;; get x.c[i-1].c[x.c[i-1].n + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.add) ;; index = x.c[i-1.n + 1] + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1].c[x.c[i-1].n + 1] + (i32.store offset=8) ;; x.c[i].c[0] = x.c[i-1].c[x.c[i-1].n + 1] + ) + ) + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (if ;; changed == -1? + (then + (local.get 2) ;; we have to merge x.c[i] with one sibling;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.le_s) + (if ;; i+1 <= x.n + (then + (local.get 5) ;; merge with right sibling;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = x.keys[i] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n+j] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n+j] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i+1].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[x.c[i].n+j] + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) ;; x.c[i].n + x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + x.c[i+1].n + 1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.set 3) ;; j = i+1 + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) ;; changed == -1 ? + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i - 1 >= 0? + (i32.and) ;; changed == -1 && i - 1 >= 0 + (if + (then + (i32.const 0) ;; merge with left sibling ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n] = x.keys[i-1] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n+j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[j] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n+j] = x.c[i].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i-1].c[x.c[i-1].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i-1].c[x.c[i-1].n+j] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i].c[j] + (i32.store offset=8) ;; x.c[i-1].c[x.c[i-1].n+j] = x.c[i].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.add) ;; x.c[i-1].n + x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n + x.c[i].n + 1 + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = i - 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + ) + ) + ) + ) + ) + ) + (local.get 4) + (i32.const -1) + (i32.eq) + (if ;; changed == -1? if yes, we merged with left sibling + (then + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (local.get 1) + (call $btreeDelete) + (drop) + ) + (else + (local.get 5) ;; x.c[i] + (local.get 1) + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) ;; end of if x is not leaf + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.load offset=4) + (i32.const 0) + (i32.eq) + (if ;; if root is empty + (then + (i32.const 0) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; root.c[0] + (i32.store offset=8) ;; root = root.c[0] + ) + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + ) + (func $main + (param $a i32) + (param $b i32) + (param $c i32) + (param $d i32) + (param $h i32) + (param $i i32) + (local $btree i32) + (local.get $a) + (local.get $b) + (i32.gt_s) + (local.get $b) + (local.get $c) + (i32.gt_s) + (local.get $c) + (local.get $d) + (i32.gt_s) + (local.get $h) + (local.get $a) + (i32.ne) + (local.get $h) + (local.get $b) + (i32.ne) + (local.get $h) + (local.get $c) + (i32.ne) + (local.get $h) + (local.get $d) + (i32.ne) + (local.get $i) + (local.get $a) + (i32.ne) + (local.get $i) + (local.get $b) + (i32.ne) + (local.get $i) + (local.get $c) + (i32.ne) + (local.get $i) + (local.get $d) + (i32.ne) + (local.get $i) + (local.get $h) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.eqz) + (if (then (unreachable))) + (i32.const 4) ;;create a tree with degree 4 + (call $createBtree) + (local.set $btree) + ;; insert variables + (local.get $a) + (call $btreeInsert) + (local.set $btree) + (local.get $b) + (call $btreeInsert) + (local.set $btree) + (local.get $c) + (call $btreeInsert) + (local.set $btree) + (local.get $d) + (call $btreeInsert) + (local.set $btree) + (local.get $h) + (call $btreeInsert) + (local.set $btree) + (local.get $i) + (call $btreeInsert) + (local.set $btree) + ;; search for variables & check that they were inserted + (local.get $btree) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $i) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + ;; delete & check that it was deleted + ;; a + (local.get $btree) + (local.get $a) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; b + (local.get $btree) + (local.get $b) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; c + (local.get $btree) + (local.get $c) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; d + (local.get $btree) + (local.get $d) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; h + (local.get $btree) + (local.get $h) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (drop)) + (export "main" (func $main))) diff --git a/benchmarks/wasm/btree/4o3u.wat b/benchmarks/wasm/btree/4o3u.wat new file mode 100644 index 00000000..39100b45 --- /dev/null +++ b/benchmarks/wasm/btree/4o3u.wat @@ -0,0 +1,2960 @@ +(module + (memory $0 2) + (func $createBtree (param i32) (result i32) ;; createBtree(t), where t: degree of the btree + (i32.const 0) + (local.get 0) + (i32.store) ;; store t at address 0 + (i32.const 0) + (i32.const 1) + (i32.store offset=4) ;; store the number of nodes in the btree = 1 + (i32.const 0) + (i32.const 65536) + (i32.store offset=8) ;; store the root addr + (i32.const 1) ;; Now create root + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then + (i32.const 65536) ;; 64KiB = 65536 bytes + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + (i32.const 65536) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + ) + ) + (i32.const 65536) ;; returns the address of the root + ) + ;; btreeSearch(x, k), where x: node address; k: key to search. Returns address of the key or -1 if key is not in tree. + (func $btreeSearch (param i32) (param i32) (result i32) (local i32) + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n -1 + (i32.le_s) ;; i <= x.children-1 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + (if + (then + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.le_s) ;; i <= x.children - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.eq) + ;; k == x.keys[i] + (if (result i32) + (then + (local.get 0) ;; save the address, which is x+8+i*4 + (i32.const 8) + (i32.add) ;; x+8 + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x+8+i*4 + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + ;; btreeSplitChild(x, i), where x: addr of a non full internal node; i: index such that x.children[i] is a full child of x + (func $btreeSplitChild (param i32) (param i32) (local i32) (local i32) + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then ;; get the number of nodes in the tree + (i32.const 0) + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address is 64KiB*(number of nodes+1) + (local.set 2) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the addr of the child in index i : y + (i32.load) ;; check if x.children[i] is leaf ;; load the first i32 in the child + (i32.const 1) + (i32.eq) ;; is y leaf? + (if + (then ;; yes + (local.get 2) ;; addr of new node z + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + ) + (else ;; no + (local.get 2) ;; addr of new node z + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + ) + ) + (local.get 2) ;; now store number of children, which will be t/2 -1 ;; addr of z + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.store offset=4) ;; store number of children = (t/2)-1 + (i32.const 0) ;; now to store the keys + (local.set 3) ;; counter = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.eq) ;; counter == (t/2)-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 2) ;; addr of z ;; we will store in the new node's address. z.keys[counter] + (i32.const 4) + (local.get 3) ;; counter + (i32.mul) ;; counter * 4 + (i32.add) ;; addr + counter * 4 -> where the key is goint to be located + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 4) ;; now get x.children[i].keys[counter+(t/2)] + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + (t/2) + (i32.mul) ;; counter + (t/2) *4 + (i32.add) ;; y + counter + (t/2) *4 + (i32.load offset=8) ;; y.keys[counter + (t/2)] + (i32.store offset=8) ;; z.keys[counter] = y.keys[counter + (t/2)] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) + (local.set 3) ;; set counter back to 0 + (i32.const 0) ;; check if x.children[i] is leaf + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load) ;; get first i32 in child --> isLeaf + (i32.const 1) + (i32.ne) ;; is leaf? + (if + (then ;; not leaf so we have to update the children + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.eq) ;; counter == (t/2) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; now get z.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; counter*4 + (i32.add) ;; (t-1)*4 + counter*4 + (local.get 2) ;; z + (i32.add) ;; z + (t-1)*4 + counter*4 --> address where addr of child is stored + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) ;; get x.children[i].children[counter+(t/2)] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + t/2 + (i32.const 4) + (i32.mul) ;; counter+t/2 * 4 + (i32.add) ;; (t-1)*4 + (counter+(t/2))*4 + (i32.add) ;; x.children[i] + (t-1)*4 + (counter + (t/2))*4 + (i32.load offset=8) ;; load the address of the child in index counter + t/2 + (i32.store offset=8) ;; z.children[counter] = x.children[i].children[counter] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.store offset=4) ;; number of keys of x.children[i] is now t/2 -1 + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (local.set 3) ;; counter = x.n + (block $loop_break + (loop $loop + (local.get 1) ;; i + (local.get 3) ;; counter + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; get x.children[counter + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (i32.add) ;; (t-1)*4 + (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter + 1)*4 -->address where addr of child is stored + (i32.const 0) ;; get x.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (i32.add) ;; (t-1)*4 + (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter)*4 + (i32.load offset=8) ;; load the addr of the child in index (counter) + (i32.store offset=8) ;; x.children[counter+1] = x.children[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 1) + (i32.add) ;; i + 1 + (i32.const 4) + (i32.mul) ;; (i + 1)*4 + (i32.add) ;; (t-1)*4 + (i + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i + 1)*4 --> addr where we will store the new addr of the child + (local.get 2) ;; addr of z + (i32.store offset=8) ;; x.children[i+1] = z + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (i32.const 1) + (i32.sub) + (local.set 3) ;; counter = x.n -1 + (block $loop_break + (loop $loop + (local.get 1) ;; i + (i32.const 1) + (i32.sub) ;; i-1 + (local.get 3) ;; counter + (i32.eq) ;; counter == i-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 3) ;; get x.keys[counter + 1] ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter + 1)*4 --> addr where we will store the new key + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter)*4 + (i32.load offset=8) ;; load the key in index (counter) + (i32.store offset=8) ;; x.keys[counter+1] = x.keys[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (local.get 1) ;; get x.keys[i] + (i32.const 4) + (i32.mul) ;; i*4 + (local.get 0) ;; x + (i32.add) ;; x +i*4 --> addr of the key + (i32.const 0) ;; get x.children[i].keys[t/2] + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.const 4) + (i32.mul) ;; (t/2)-1 *4 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; (i)*4 + (i32.add) ;; (t-1)*4 + (i)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i)*4 + (i32.load offset=8) ;; load the address of the child in index (i) + (i32.add) ;; y + (t/2)-1 *4 + (i32.load offset=8) ;; load the key in index (t/2)-1 from node x.children[i] + (i32.store offset=8) ;; store the new key in the addr + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) ;; x.n + 1 + (i32.store offset=4) ;; x.n = x.n + 1 + ) + ) + ) + ;; btreeInsertNonFull(x, k), where x: addr of a non full internal node; k: the key to insert + (func $btreeInsertNonFull (param i32) (param i32) (local i32) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n -1 + (local.set 2) ;; i = x.n -1 + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (i32.and) + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i *4 + (i32.add) ;; x + i *4 --> addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.keys[i+1] = x.keys[i] + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 1) ;; k + (i32.store offset=8) ;; x.keys[i+1] = k + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; x.n = x.n +1 + ) + (else ;; x is not leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i >= 0 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i -1 + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 2) ;;i = i+1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address of the child in index i = y + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load offset=4) ;; get y.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; y.n == t-1 --> node is full + (if + (then + (local.get 0) ;; x + (local.get 2) ;; i + (call $btreeSplitChild) + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k>x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i+1 + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (local.get 1) ;; k + (call $btreeInsertNonFull) + ) + ) + ) + (func $btreeInsert (param i32) (result i32) (local i32) (local i32) + (i32.const 0) + (i32.load offset=8) ;; root addr + (local.tee 2) ;; set r + (i32.load offset=4) ;; r.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; r.n == t-1 --> root is full + (if (result i32) + (then + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if (result i32) ;; if we can grow memory + (then + (i32.const 0) ;; get the number of nodes in the tree + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address of s is 64KiB*(number of nodes+1) + (local.set 1) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) + (local.get 1) ;; s + (i32.store offset=8) ;; root addr is now s + (local.get 1) ;; s + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + (local.get 1) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + (i32.const 0) ;; store children addresses + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 1) ;; x + (i32.add) ;; x + (t-1)*4 --> addre where addr of child number 0 is located + (local.get 2) + (i32.store offset=8) ;; s.children[0] = r + (local.get 1) ;; s + (i32.const 0) + (call $btreeSplitChild) + (local.get 1) ;; s + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 1) ;; return the address of the new root + ) + (else + (i32.const -1) ;; return -1 because we could not create a new node + ) + ) + ) + (else + (local.get 2) ;; r + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 2) ;; return the address of the root, which is the same + ) + ) + ) + ;; Returns the address of the new root (if applicable, otherwise returns the addres of the root) + (func $btreeDelete (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local i32) + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (local.get 2) ;; i + (i32.eq) ;; i == x.n + (if + (then + (br $loop_break) ;; break from loop + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + (if + (then + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $while_break + (loop $while + (local.get 3) ;; j + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n-1 + (i32.eq) ;; j == x.n-1 + (if + (then + (br $while_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 --> addr where x.keys[j] is stored + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.mul) ;; j+1 *4 + (i32.add) ;; x + j+1 *4 --> addr where x.keys[j+1] is stored + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $while) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n -1 + (br $loop_break) + ) + ) + ) + ) + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) ;; continue + ) + ) + ) + (else ;; x is not leaf + (i32.const 0) ;;get the index of the appropriate child/key + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (i32.and) ;; i <= x.n - 1 && k > x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i + 1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.lt_s) ;; i < x.n + (if (result i32) + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (if + (then ;; key is present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.tee 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (call $btreeDelete) ;; (x.c[i], x.c[i].keys[x.c[i].n - 1]) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i].keys[x.c[i].n - 1] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i+1] + (local.tee 5) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (local.get 5) ;; x.c[i+1] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (call $btreeDelete) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 --> addr where x.c[i].keys[x.c[i].n] will be stored + (local.get 1) ;; k + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = k + (i32.const 0) ;; now merge x.c[i] with x.c[i+1] + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if ;; j == x.c[i+1].n + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n + j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[index] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x.c[i+1] + i*4 ;; addr of x.c[i+1].keys[j] + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[index] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n +1 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + 1 + x.c[i+1].n + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; now adjust the children + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.get 3) ;; j + (i32.add) ;; index = x.c[i].n+j + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x.c[i] + (t-1)*4 --> address where x.c[i].c[x.c[i].n+j] is stored + (i32.const 0) ;;get x.c[i+1].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x.c[i+1] + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (i32.const 1) + (i32.sub) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) + (i32.mul) ;; j+1*4 + (i32.add) ;; x + j+1*4 ;; addr of x.keys[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = i+1 + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 0) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n-1 + (local.get 5) ;; x.c[i] + (local.get 1) ;; k + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) + (else ;; key not present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) + (i32.const 1) + (i32.sub) ;; t/2 - 1 + (i32.eq) + (if + (then + (i32.const -1) + (local.set 4) ;; changed = -1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.le_s) ;; i+1 <= x.n + (if + (then + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; index = t/2 -1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[t/2-1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[t/2-1] = x.keys[i] + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;;x.c[i].n = x.c[i].n +1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i+1].keys[0] + (i32.load offset=8) ;; x.c[i+1].keys[0] + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + (local.get 5) + (i32.load) ;; x.c[i] is leaf? + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) ;;get x.c[i].c[t/2] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; index = t/2 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[t/2] + (i32.const 0) ;;get x.c[i+1].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[0] + (i32.store offset=8) ;; x.c[i].c[t/2] = x.c[i+1].c[0] + ) + ) + (local.get 5) ;; x.c[i] + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.store offset=4) ;; x.c[i].n = t/2 + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 --> address where x.c[i+1].keys[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j+1] + (i32.store offset=8) ;; x.c[i+1].keys[j] = x.c[i+1].keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i+1].c[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j+1] + (i32.store offset=8) ;; x.c[i+1].c[j] = x.c[i+1].c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr where x.c[i+1] is located + (i32.load offset=8) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i+1].n = x.c[i+1].n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i -1 >= 0 + (i32.and) ;; changed == -1 && i -1 >= 0 + (if + (then + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i-1].n >= t/2 + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.set 3) ;; j = x.c[i].n + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j-1] + (i32.load offset=8) ;; x.c[i].keys[j-1] + (i32.store offset=8) ;; x.c[i].keys[j] = x.c[i].keys[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (local.set 3) ;; j = x.c[i].n + 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i].c[j] is located + (i32.const 0) ;;get x.c[i].c[j-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) + (i32.load offset=8) ;; x.c[i].c[j-1] + (i32.store offset=8) ;; x.c[i].c[j] = x.c[i].c[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; ;; x.c[i].n = x.c[i].n + 1 + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[0] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i-1] + (i32.store offset=8) ;; x.c[i].keys[0] = x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n - 1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[i-1] = x.c[i-1].keys[x.c[i-1].n] + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; get x.c[i].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 ;; addr where x.c[i].c[0] is located + (i32.const 0) ;; get x.c[i-1].c[x.c[i-1].n + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.add) ;; index = x.c[i-1.n + 1] + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1].c[x.c[i-1].n + 1] + (i32.store offset=8) ;; x.c[i].c[0] = x.c[i-1].c[x.c[i-1].n + 1] + ) + ) + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (if ;; changed == -1? + (then + (local.get 2) ;; we have to merge x.c[i] with one sibling;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.le_s) + (if ;; i+1 <= x.n + (then + (local.get 5) ;; merge with right sibling;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = x.keys[i] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n+j] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n+j] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i+1].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[x.c[i].n+j] + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) ;; x.c[i].n + x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + x.c[i+1].n + 1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.set 3) ;; j = i+1 + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) ;; changed == -1 ? + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i - 1 >= 0? + (i32.and) ;; changed == -1 && i - 1 >= 0 + (if + (then + (i32.const 0) ;; merge with left sibling ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n] = x.keys[i-1] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n+j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[j] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n+j] = x.c[i].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i-1].c[x.c[i-1].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i-1].c[x.c[i-1].n+j] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i].c[j] + (i32.store offset=8) ;; x.c[i-1].c[x.c[i-1].n+j] = x.c[i].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.add) ;; x.c[i-1].n + x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n + x.c[i].n + 1 + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = i - 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + ) + ) + ) + ) + ) + ) + (local.get 4) + (i32.const -1) + (i32.eq) + (if ;; changed == -1? if yes, we merged with left sibling + (then + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (local.get 1) + (call $btreeDelete) + (drop) + ) + (else + (local.get 5) ;; x.c[i] + (local.get 1) + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) ;; end of if x is not leaf + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.load offset=4) + (i32.const 0) + (i32.eq) + (if ;; if root is empty + (then + (i32.const 0) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; root.c[0] + (i32.store offset=8) ;; root = root.c[0] + ) + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + ) + (func $main + (param $a i32) + (param $b i32) + (param $c i32) + (param $d i32) + (param $h i32) + (param $i i32) + (param $j i32) + (local $btree i32) + (local.get $a) + (local.get $b) + (i32.gt_s) + (local.get $b) + (local.get $c) + (i32.gt_s) + (local.get $c) + (local.get $d) + (i32.gt_s) + (local.get $h) + (local.get $a) + (i32.ne) + (local.get $h) + (local.get $b) + (i32.ne) + (local.get $h) + (local.get $c) + (i32.ne) + (local.get $h) + (local.get $d) + (i32.ne) + (local.get $i) + (local.get $a) + (i32.ne) + (local.get $i) + (local.get $b) + (i32.ne) + (local.get $i) + (local.get $c) + (i32.ne) + (local.get $i) + (local.get $d) + (i32.ne) + (local.get $i) + (local.get $h) + (i32.ne) + (local.get $j) + (local.get $a) + (i32.ne) + (local.get $j) + (local.get $b) + (i32.ne) + (local.get $j) + (local.get $c) + (i32.ne) + (local.get $j) + (local.get $d) + (i32.ne) + (local.get $j) + (local.get $h) + (i32.ne) + (local.get $j) + (local.get $i) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.eqz) + (if (then (unreachable))) + (i32.const 4) ;;create a tree with degree 4 + (call $createBtree) + (local.set $btree) + ;; insert variables + (local.get $a) + (call $btreeInsert) + (local.set $btree) + (local.get $b) + (call $btreeInsert) + (local.set $btree) + (local.get $c) + (call $btreeInsert) + (local.set $btree) + (local.get $d) + (call $btreeInsert) + (local.set $btree) + (local.get $h) + (call $btreeInsert) + (local.set $btree) + (local.get $i) + (call $btreeInsert) + (local.set $btree) + (local.get $j) + (call $btreeInsert) + (local.set $btree) + ;; search for variables & check that they were inserted + (local.get $btree) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $i) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $j) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + ;; delete & check that it was deleted + ;; a + (local.get $btree) + (local.get $a) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; b + (local.get $btree) + (local.get $b) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; c + (local.get $btree) + (local.get $c) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; d + (local.get $btree) + (local.get $d) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; h + (local.get $btree) + (local.get $h) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; i + (local.get $btree) + (local.get $i) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $i) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (drop)) + (export "main" (func $main))) diff --git a/benchmarks/wasm/btree/5o1u.wat b/benchmarks/wasm/btree/5o1u.wat new file mode 100644 index 00000000..f75d27bd --- /dev/null +++ b/benchmarks/wasm/btree/5o1u.wat @@ -0,0 +1,2903 @@ +(module + (memory $0 2) + (func $createBtree (param i32) (result i32) ;; createBtree(t), where t: degree of the btree + (i32.const 0) + (local.get 0) + (i32.store) ;; store t at address 0 + (i32.const 0) + (i32.const 1) + (i32.store offset=4) ;; store the number of nodes in the btree = 1 + (i32.const 0) + (i32.const 65536) + (i32.store offset=8) ;; store the root addr + (i32.const 1) ;; Now create root + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then + (i32.const 65536) ;; 64KiB = 65536 bytes + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + (i32.const 65536) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + ) + ) + (i32.const 65536) ;; returns the address of the root + ) + ;; btreeSearch(x, k), where x: node address; k: key to search. Returns address of the key or -1 if key is not in tree. + (func $btreeSearch (param i32) (param i32) (result i32) (local i32) + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n -1 + (i32.le_s) ;; i <= x.children-1 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + (if + (then + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.le_s) ;; i <= x.children - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.eq) + ;; k == x.keys[i] + (if (result i32) + (then + (local.get 0) ;; save the address, which is x+8+i*4 + (i32.const 8) + (i32.add) ;; x+8 + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x+8+i*4 + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + ;; btreeSplitChild(x, i), where x: addr of a non full internal node; i: index such that x.children[i] is a full child of x + (func $btreeSplitChild (param i32) (param i32) (local i32) (local i32) + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then ;; get the number of nodes in the tree + (i32.const 0) + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address is 64KiB*(number of nodes+1) + (local.set 2) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the addr of the child in index i : y + (i32.load) ;; check if x.children[i] is leaf ;; load the first i32 in the child + (i32.const 1) + (i32.eq) ;; is y leaf? + (if + (then ;; yes + (local.get 2) ;; addr of new node z + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + ) + (else ;; no + (local.get 2) ;; addr of new node z + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + ) + ) + (local.get 2) ;; now store number of children, which will be t/2 -1 ;; addr of z + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.store offset=4) ;; store number of children = (t/2)-1 + (i32.const 0) ;; now to store the keys + (local.set 3) ;; counter = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.eq) ;; counter == (t/2)-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 2) ;; addr of z ;; we will store in the new node's address. z.keys[counter] + (i32.const 4) + (local.get 3) ;; counter + (i32.mul) ;; counter * 4 + (i32.add) ;; addr + counter * 4 -> where the key is goint to be located + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 4) ;; now get x.children[i].keys[counter+(t/2)] + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + (t/2) + (i32.mul) ;; counter + (t/2) *4 + (i32.add) ;; y + counter + (t/2) *4 + (i32.load offset=8) ;; y.keys[counter + (t/2)] + (i32.store offset=8) ;; z.keys[counter] = y.keys[counter + (t/2)] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) + (local.set 3) ;; set counter back to 0 + (i32.const 0) ;; check if x.children[i] is leaf + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load) ;; get first i32 in child --> isLeaf + (i32.const 1) + (i32.ne) ;; is leaf? + (if + (then ;; not leaf so we have to update the children + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.eq) ;; counter == (t/2) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; now get z.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; counter*4 + (i32.add) ;; (t-1)*4 + counter*4 + (local.get 2) ;; z + (i32.add) ;; z + (t-1)*4 + counter*4 --> address where addr of child is stored + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) ;; get x.children[i].children[counter+(t/2)] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + t/2 + (i32.const 4) + (i32.mul) ;; counter+t/2 * 4 + (i32.add) ;; (t-1)*4 + (counter+(t/2))*4 + (i32.add) ;; x.children[i] + (t-1)*4 + (counter + (t/2))*4 + (i32.load offset=8) ;; load the address of the child in index counter + t/2 + (i32.store offset=8) ;; z.children[counter] = x.children[i].children[counter] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.store offset=4) ;; number of keys of x.children[i] is now t/2 -1 + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (local.set 3) ;; counter = x.n + (block $loop_break + (loop $loop + (local.get 1) ;; i + (local.get 3) ;; counter + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; get x.children[counter + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (i32.add) ;; (t-1)*4 + (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter + 1)*4 -->address where addr of child is stored + (i32.const 0) ;; get x.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (i32.add) ;; (t-1)*4 + (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter)*4 + (i32.load offset=8) ;; load the addr of the child in index (counter) + (i32.store offset=8) ;; x.children[counter+1] = x.children[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 1) + (i32.add) ;; i + 1 + (i32.const 4) + (i32.mul) ;; (i + 1)*4 + (i32.add) ;; (t-1)*4 + (i + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i + 1)*4 --> addr where we will store the new addr of the child + (local.get 2) ;; addr of z + (i32.store offset=8) ;; x.children[i+1] = z + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (i32.const 1) + (i32.sub) + (local.set 3) ;; counter = x.n -1 + (block $loop_break + (loop $loop + (local.get 1) ;; i + (i32.const 1) + (i32.sub) ;; i-1 + (local.get 3) ;; counter + (i32.eq) ;; counter == i-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 3) ;; get x.keys[counter + 1] ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter + 1)*4 --> addr where we will store the new key + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter)*4 + (i32.load offset=8) ;; load the key in index (counter) + (i32.store offset=8) ;; x.keys[counter+1] = x.keys[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (local.get 1) ;; get x.keys[i] + (i32.const 4) + (i32.mul) ;; i*4 + (local.get 0) ;; x + (i32.add) ;; x +i*4 --> addr of the key + (i32.const 0) ;; get x.children[i].keys[t/2] + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.const 4) + (i32.mul) ;; (t/2)-1 *4 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; (i)*4 + (i32.add) ;; (t-1)*4 + (i)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i)*4 + (i32.load offset=8) ;; load the address of the child in index (i) + (i32.add) ;; y + (t/2)-1 *4 + (i32.load offset=8) ;; load the key in index (t/2)-1 from node x.children[i] + (i32.store offset=8) ;; store the new key in the addr + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) ;; x.n + 1 + (i32.store offset=4) ;; x.n = x.n + 1 + ) + ) + ) + ;; btreeInsertNonFull(x, k), where x: addr of a non full internal node; k: the key to insert + (func $btreeInsertNonFull (param i32) (param i32) (local i32) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n -1 + (local.set 2) ;; i = x.n -1 + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (i32.and) + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i *4 + (i32.add) ;; x + i *4 --> addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.keys[i+1] = x.keys[i] + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 1) ;; k + (i32.store offset=8) ;; x.keys[i+1] = k + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; x.n = x.n +1 + ) + (else ;; x is not leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i >= 0 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i -1 + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 2) ;;i = i+1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address of the child in index i = y + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load offset=4) ;; get y.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; y.n == t-1 --> node is full + (if + (then + (local.get 0) ;; x + (local.get 2) ;; i + (call $btreeSplitChild) + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k>x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i+1 + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (local.get 1) ;; k + (call $btreeInsertNonFull) + ) + ) + ) + (func $btreeInsert (param i32) (result i32) (local i32) (local i32) + (i32.const 0) + (i32.load offset=8) ;; root addr + (local.tee 2) ;; set r + (i32.load offset=4) ;; r.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; r.n == t-1 --> root is full + (if (result i32) + (then + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if (result i32) ;; if we can grow memory + (then + (i32.const 0) ;; get the number of nodes in the tree + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address of s is 64KiB*(number of nodes+1) + (local.set 1) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) + (local.get 1) ;; s + (i32.store offset=8) ;; root addr is now s + (local.get 1) ;; s + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + (local.get 1) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + (i32.const 0) ;; store children addresses + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 1) ;; x + (i32.add) ;; x + (t-1)*4 --> addre where addr of child number 0 is located + (local.get 2) + (i32.store offset=8) ;; s.children[0] = r + (local.get 1) ;; s + (i32.const 0) + (call $btreeSplitChild) + (local.get 1) ;; s + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 1) ;; return the address of the new root + ) + (else + (i32.const -1) ;; return -1 because we could not create a new node + ) + ) + ) + (else + (local.get 2) ;; r + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 2) ;; return the address of the root, which is the same + ) + ) + ) + ;; Returns the address of the new root (if applicable, otherwise returns the addres of the root) + (func $btreeDelete (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local i32) + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (local.get 2) ;; i + (i32.eq) ;; i == x.n + (if + (then + (br $loop_break) ;; break from loop + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + (if + (then + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $while_break + (loop $while + (local.get 3) ;; j + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n-1 + (i32.eq) ;; j == x.n-1 + (if + (then + (br $while_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 --> addr where x.keys[j] is stored + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.mul) ;; j+1 *4 + (i32.add) ;; x + j+1 *4 --> addr where x.keys[j+1] is stored + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $while) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n -1 + (br $loop_break) + ) + ) + ) + ) + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) ;; continue + ) + ) + ) + (else ;; x is not leaf + (i32.const 0) ;;get the index of the appropriate child/key + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (i32.and) ;; i <= x.n - 1 && k > x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i + 1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.lt_s) ;; i < x.n + (if (result i32) + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (if + (then ;; key is present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.tee 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (call $btreeDelete) ;; (x.c[i], x.c[i].keys[x.c[i].n - 1]) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i].keys[x.c[i].n - 1] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i+1] + (local.tee 5) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (local.get 5) ;; x.c[i+1] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (call $btreeDelete) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 --> addr where x.c[i].keys[x.c[i].n] will be stored + (local.get 1) ;; k + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = k + (i32.const 0) ;; now merge x.c[i] with x.c[i+1] + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if ;; j == x.c[i+1].n + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n + j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[index] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x.c[i+1] + i*4 ;; addr of x.c[i+1].keys[j] + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[index] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n +1 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + 1 + x.c[i+1].n + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; now adjust the children + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.get 3) ;; j + (i32.add) ;; index = x.c[i].n+j + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x.c[i] + (t-1)*4 --> address where x.c[i].c[x.c[i].n+j] is stored + (i32.const 0) ;;get x.c[i+1].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x.c[i+1] + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (i32.const 1) + (i32.sub) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) + (i32.mul) ;; j+1*4 + (i32.add) ;; x + j+1*4 ;; addr of x.keys[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = i+1 + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 0) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n-1 + (local.get 5) ;; x.c[i] + (local.get 1) ;; k + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) + (else ;; key not present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) + (i32.const 1) + (i32.sub) ;; t/2 - 1 + (i32.eq) + (if + (then + (i32.const -1) + (local.set 4) ;; changed = -1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.le_s) ;; i+1 <= x.n + (if + (then + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; index = t/2 -1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[t/2-1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[t/2-1] = x.keys[i] + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;;x.c[i].n = x.c[i].n +1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i+1].keys[0] + (i32.load offset=8) ;; x.c[i+1].keys[0] + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + (local.get 5) + (i32.load) ;; x.c[i] is leaf? + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) ;;get x.c[i].c[t/2] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; index = t/2 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[t/2] + (i32.const 0) ;;get x.c[i+1].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[0] + (i32.store offset=8) ;; x.c[i].c[t/2] = x.c[i+1].c[0] + ) + ) + (local.get 5) ;; x.c[i] + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.store offset=4) ;; x.c[i].n = t/2 + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 --> address where x.c[i+1].keys[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j+1] + (i32.store offset=8) ;; x.c[i+1].keys[j] = x.c[i+1].keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i+1].c[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j+1] + (i32.store offset=8) ;; x.c[i+1].c[j] = x.c[i+1].c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr where x.c[i+1] is located + (i32.load offset=8) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i+1].n = x.c[i+1].n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i -1 >= 0 + (i32.and) ;; changed == -1 && i -1 >= 0 + (if + (then + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i-1].n >= t/2 + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.set 3) ;; j = x.c[i].n + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j-1] + (i32.load offset=8) ;; x.c[i].keys[j-1] + (i32.store offset=8) ;; x.c[i].keys[j] = x.c[i].keys[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (local.set 3) ;; j = x.c[i].n + 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i].c[j] is located + (i32.const 0) ;;get x.c[i].c[j-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) + (i32.load offset=8) ;; x.c[i].c[j-1] + (i32.store offset=8) ;; x.c[i].c[j] = x.c[i].c[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; ;; x.c[i].n = x.c[i].n + 1 + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[0] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i-1] + (i32.store offset=8) ;; x.c[i].keys[0] = x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n - 1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[i-1] = x.c[i-1].keys[x.c[i-1].n] + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; get x.c[i].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 ;; addr where x.c[i].c[0] is located + (i32.const 0) ;; get x.c[i-1].c[x.c[i-1].n + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.add) ;; index = x.c[i-1.n + 1] + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1].c[x.c[i-1].n + 1] + (i32.store offset=8) ;; x.c[i].c[0] = x.c[i-1].c[x.c[i-1].n + 1] + ) + ) + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (if ;; changed == -1? + (then + (local.get 2) ;; we have to merge x.c[i] with one sibling;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.le_s) + (if ;; i+1 <= x.n + (then + (local.get 5) ;; merge with right sibling;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = x.keys[i] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n+j] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n+j] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i+1].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[x.c[i].n+j] + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) ;; x.c[i].n + x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + x.c[i+1].n + 1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.set 3) ;; j = i+1 + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) ;; changed == -1 ? + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i - 1 >= 0? + (i32.and) ;; changed == -1 && i - 1 >= 0 + (if + (then + (i32.const 0) ;; merge with left sibling ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n] = x.keys[i-1] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n+j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[j] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n+j] = x.c[i].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i-1].c[x.c[i-1].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i-1].c[x.c[i-1].n+j] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i].c[j] + (i32.store offset=8) ;; x.c[i-1].c[x.c[i-1].n+j] = x.c[i].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.add) ;; x.c[i-1].n + x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n + x.c[i].n + 1 + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = i - 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + ) + ) + ) + ) + ) + ) + (local.get 4) + (i32.const -1) + (i32.eq) + (if ;; changed == -1? if yes, we merged with left sibling + (then + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (local.get 1) + (call $btreeDelete) + (drop) + ) + (else + (local.get 5) ;; x.c[i] + (local.get 1) + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) ;; end of if x is not leaf + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.load offset=4) + (i32.const 0) + (i32.eq) + (if ;; if root is empty + (then + (i32.const 0) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; root.c[0] + (i32.store offset=8) ;; root = root.c[0] + ) + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + ) + (func $main + (param $a i32) + (param $b i32) + (param $c i32) + (param $d i32) + (param $e i32) + (param $h i32) + (local $btree i32) + (local.get $a) + (local.get $b) + (i32.gt_s) + (local.get $b) + (local.get $c) + (i32.gt_s) + (local.get $c) + (local.get $d) + (i32.gt_s) + (local.get $d) + (local.get $e) + (i32.gt_s) + (local.get $h) + (local.get $a) + (i32.ne) + (local.get $h) + (local.get $b) + (i32.ne) + (local.get $h) + (local.get $c) + (i32.ne) + (local.get $h) + (local.get $d) + (i32.ne) + (local.get $h) + (local.get $e) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.eqz) + (if (then (unreachable))) + (i32.const 4) ;;create a tree with degree 4 + (call $createBtree) + (local.set $btree) + ;; insert variables + (local.get $a) + (call $btreeInsert) + (local.set $btree) + (local.get $b) + (call $btreeInsert) + (local.set $btree) + (local.get $c) + (call $btreeInsert) + (local.set $btree) + (local.get $d) + (call $btreeInsert) + (local.set $btree) + (local.get $e) + (call $btreeInsert) + (local.set $btree) + (local.get $h) + (call $btreeInsert) + (local.set $btree) + ;; search for variables & check that they were inserted + (local.get $btree) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $e) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get $btree) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + ;; delete & check that it was deleted + ;; a + (local.get $btree) + (local.get $a) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; b + (local.get $btree) + (local.get $b) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; c + (local.get $btree) + (local.get $c) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; d + (local.get $btree) + (local.get $d) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; e + (local.get $btree) + (local.get $e) + (call $btreeDelete) + (local.set $btree) + (local.get $btree) + (local.get $e) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (drop)) + (export "main" (func $main))) diff --git a/benchmarks/wasm/btree/5o2u.wat b/benchmarks/wasm/btree/5o2u.wat new file mode 100644 index 00000000..0d8e6151 --- /dev/null +++ b/benchmarks/wasm/btree/5o2u.wat @@ -0,0 +1,2948 @@ +(module + (memory $0 2) + (func $createBtree (param i32) (result i32) ;; createBtree(t), where t: degree of the btree + (i32.const 0) + (local.get 0) + (i32.store) ;; store t at address 0 + (i32.const 0) + (i32.const 1) + (i32.store offset=4) ;; store the number of nodes in the btree = 1 + (i32.const 0) + (i32.const 65536) + (i32.store offset=8) ;; store the root addr + (i32.const 1) ;; Now create root + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then + (i32.const 65536) ;; 64KiB = 65536 bytes + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + (i32.const 65536) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + ) + ) + (i32.const 65536) ;; returns the address of the root + ) + ;; btreeSearch(x, k), where x: node address; k: key to search. Returns address of the key or -1 if key is not in tree. + (func $btreeSearch (param i32) (param i32) (result i32) (local i32) + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n -1 + (i32.le_s) ;; i <= x.children-1 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + (if + (then + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.le_s) ;; i <= x.children - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.eq) + ;; k == x.keys[i] + (if (result i32) + (then + (local.get 0) ;; save the address, which is x+8+i*4 + (i32.const 8) + (i32.add) ;; x+8 + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x+8+i*4 + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + ;; btreeSplitChild(x, i), where x: addr of a non full internal node; i: index such that x.children[i] is a full child of x + (func $btreeSplitChild (param i32) (param i32) (local i32) (local i32) + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then ;; get the number of nodes in the tree + (i32.const 0) + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address is 64KiB*(number of nodes+1) + (local.set 2) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the addr of the child in index i : y + (i32.load) ;; check if x.children[i] is leaf ;; load the first i32 in the child + (i32.const 1) + (i32.eq) ;; is y leaf? + (if + (then ;; yes + (local.get 2) ;; addr of new node z + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + ) + (else ;; no + (local.get 2) ;; addr of new node z + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + ) + ) + (local.get 2) ;; now store number of children, which will be t/2 -1 ;; addr of z + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.store offset=4) ;; store number of children = (t/2)-1 + (i32.const 0) ;; now to store the keys + (local.set 3) ;; counter = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.eq) ;; counter == (t/2)-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 2) ;; addr of z ;; we will store in the new node's address. z.keys[counter] + (i32.const 4) + (local.get 3) ;; counter + (i32.mul) ;; counter * 4 + (i32.add) ;; addr + counter * 4 -> where the key is goint to be located + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 4) ;; now get x.children[i].keys[counter+(t/2)] + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + (t/2) + (i32.mul) ;; counter + (t/2) *4 + (i32.add) ;; y + counter + (t/2) *4 + (i32.load offset=8) ;; y.keys[counter + (t/2)] + (i32.store offset=8) ;; z.keys[counter] = y.keys[counter + (t/2)] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) + (local.set 3) ;; set counter back to 0 + (i32.const 0) ;; check if x.children[i] is leaf + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load) ;; get first i32 in child --> isLeaf + (i32.const 1) + (i32.ne) ;; is leaf? + (if + (then ;; not leaf so we have to update the children + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.eq) ;; counter == (t/2) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; now get z.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; counter*4 + (i32.add) ;; (t-1)*4 + counter*4 + (local.get 2) ;; z + (i32.add) ;; z + (t-1)*4 + counter*4 --> address where addr of child is stored + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) ;; get x.children[i].children[counter+(t/2)] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + t/2 + (i32.const 4) + (i32.mul) ;; counter+t/2 * 4 + (i32.add) ;; (t-1)*4 + (counter+(t/2))*4 + (i32.add) ;; x.children[i] + (t-1)*4 + (counter + (t/2))*4 + (i32.load offset=8) ;; load the address of the child in index counter + t/2 + (i32.store offset=8) ;; z.children[counter] = x.children[i].children[counter] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.store offset=4) ;; number of keys of x.children[i] is now t/2 -1 + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (local.set 3) ;; counter = x.n + (block $loop_break + (loop $loop + (local.get 1) ;; i + (local.get 3) ;; counter + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; get x.children[counter + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (i32.add) ;; (t-1)*4 + (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter + 1)*4 -->address where addr of child is stored + (i32.const 0) ;; get x.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (i32.add) ;; (t-1)*4 + (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter)*4 + (i32.load offset=8) ;; load the addr of the child in index (counter) + (i32.store offset=8) ;; x.children[counter+1] = x.children[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 1) + (i32.add) ;; i + 1 + (i32.const 4) + (i32.mul) ;; (i + 1)*4 + (i32.add) ;; (t-1)*4 + (i + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i + 1)*4 --> addr where we will store the new addr of the child + (local.get 2) ;; addr of z + (i32.store offset=8) ;; x.children[i+1] = z + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (i32.const 1) + (i32.sub) + (local.set 3) ;; counter = x.n -1 + (block $loop_break + (loop $loop + (local.get 1) ;; i + (i32.const 1) + (i32.sub) ;; i-1 + (local.get 3) ;; counter + (i32.eq) ;; counter == i-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 3) ;; get x.keys[counter + 1] ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter + 1)*4 --> addr where we will store the new key + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter)*4 + (i32.load offset=8) ;; load the key in index (counter) + (i32.store offset=8) ;; x.keys[counter+1] = x.keys[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (local.get 1) ;; get x.keys[i] + (i32.const 4) + (i32.mul) ;; i*4 + (local.get 0) ;; x + (i32.add) ;; x +i*4 --> addr of the key + (i32.const 0) ;; get x.children[i].keys[t/2] + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.const 4) + (i32.mul) ;; (t/2)-1 *4 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; (i)*4 + (i32.add) ;; (t-1)*4 + (i)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i)*4 + (i32.load offset=8) ;; load the address of the child in index (i) + (i32.add) ;; y + (t/2)-1 *4 + (i32.load offset=8) ;; load the key in index (t/2)-1 from node x.children[i] + (i32.store offset=8) ;; store the new key in the addr + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) ;; x.n + 1 + (i32.store offset=4) ;; x.n = x.n + 1 + ) + ) + ) + ;; btreeInsertNonFull(x, k), where x: addr of a non full internal node; k: the key to insert + (func $btreeInsertNonFull (param i32) (param i32) (local i32) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n -1 + (local.set 2) ;; i = x.n -1 + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (i32.and) + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i *4 + (i32.add) ;; x + i *4 --> addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.keys[i+1] = x.keys[i] + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 1) ;; k + (i32.store offset=8) ;; x.keys[i+1] = k + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; x.n = x.n +1 + ) + (else ;; x is not leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i >= 0 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i -1 + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 2) ;;i = i+1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address of the child in index i = y + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load offset=4) ;; get y.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; y.n == t-1 --> node is full + (if + (then + (local.get 0) ;; x + (local.get 2) ;; i + (call $btreeSplitChild) + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k>x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i+1 + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (local.get 1) ;; k + (call $btreeInsertNonFull) + ) + ) + ) + (func $btreeInsert (param i32) (result i32) (local i32) (local i32) + (i32.const 0) + (i32.load offset=8) ;; root addr + (local.tee 2) ;; set r + (i32.load offset=4) ;; r.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; r.n == t-1 --> root is full + (if (result i32) + (then + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if (result i32) ;; if we can grow memory + (then + (i32.const 0) ;; get the number of nodes in the tree + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address of s is 64KiB*(number of nodes+1) + (local.set 1) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) + (local.get 1) ;; s + (i32.store offset=8) ;; root addr is now s + (local.get 1) ;; s + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + (local.get 1) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + (i32.const 0) ;; store children addresses + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 1) ;; x + (i32.add) ;; x + (t-1)*4 --> addre where addr of child number 0 is located + (local.get 2) + (i32.store offset=8) ;; s.children[0] = r + (local.get 1) ;; s + (i32.const 0) + (call $btreeSplitChild) + (local.get 1) ;; s + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 1) ;; return the address of the new root + ) + (else + (i32.const -1) ;; return -1 because we could not create a new node + ) + ) + ) + (else + (local.get 2) ;; r + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 2) ;; return the address of the root, which is the same + ) + ) + ) + ;; Returns the address of the new root (if applicable, otherwise returns the addres of the root) + (func $btreeDelete (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local i32) + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (local.get 2) ;; i + (i32.eq) ;; i == x.n + (if + (then + (br $loop_break) ;; break from loop + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + (if + (then + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $while_break + (loop $while + (local.get 3) ;; j + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n-1 + (i32.eq) ;; j == x.n-1 + (if + (then + (br $while_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 --> addr where x.keys[j] is stored + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.mul) ;; j+1 *4 + (i32.add) ;; x + j+1 *4 --> addr where x.keys[j+1] is stored + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $while) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n -1 + (br $loop_break) + ) + ) + ) + ) + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) ;; continue + ) + ) + ) + (else ;; x is not leaf + (i32.const 0) ;;get the index of the appropriate child/key + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (i32.and) ;; i <= x.n - 1 && k > x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i + 1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.lt_s) ;; i < x.n + (if (result i32) + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (if + (then ;; key is present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.tee 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (call $btreeDelete) ;; (x.c[i], x.c[i].keys[x.c[i].n - 1]) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i].keys[x.c[i].n - 1] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i+1] + (local.tee 5) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (local.get 5) ;; x.c[i+1] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (call $btreeDelete) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 --> addr where x.c[i].keys[x.c[i].n] will be stored + (local.get 1) ;; k + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = k + (i32.const 0) ;; now merge x.c[i] with x.c[i+1] + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if ;; j == x.c[i+1].n + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n + j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[index] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x.c[i+1] + i*4 ;; addr of x.c[i+1].keys[j] + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[index] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n +1 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + 1 + x.c[i+1].n + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; now adjust the children + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.get 3) ;; j + (i32.add) ;; index = x.c[i].n+j + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x.c[i] + (t-1)*4 --> address where x.c[i].c[x.c[i].n+j] is stored + (i32.const 0) ;;get x.c[i+1].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x.c[i+1] + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (i32.const 1) + (i32.sub) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) + (i32.mul) ;; j+1*4 + (i32.add) ;; x + j+1*4 ;; addr of x.keys[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = i+1 + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 0) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n-1 + (local.get 5) ;; x.c[i] + (local.get 1) ;; k + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) + (else ;; key not present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) + (i32.const 1) + (i32.sub) ;; t/2 - 1 + (i32.eq) + (if + (then + (i32.const -1) + (local.set 4) ;; changed = -1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.le_s) ;; i+1 <= x.n + (if + (then + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; index = t/2 -1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[t/2-1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[t/2-1] = x.keys[i] + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;;x.c[i].n = x.c[i].n +1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i+1].keys[0] + (i32.load offset=8) ;; x.c[i+1].keys[0] + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + (local.get 5) + (i32.load) ;; x.c[i] is leaf? + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) ;;get x.c[i].c[t/2] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; index = t/2 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[t/2] + (i32.const 0) ;;get x.c[i+1].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[0] + (i32.store offset=8) ;; x.c[i].c[t/2] = x.c[i+1].c[0] + ) + ) + (local.get 5) ;; x.c[i] + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.store offset=4) ;; x.c[i].n = t/2 + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 --> address where x.c[i+1].keys[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j+1] + (i32.store offset=8) ;; x.c[i+1].keys[j] = x.c[i+1].keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i+1].c[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j+1] + (i32.store offset=8) ;; x.c[i+1].c[j] = x.c[i+1].c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr where x.c[i+1] is located + (i32.load offset=8) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i+1].n = x.c[i+1].n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i -1 >= 0 + (i32.and) ;; changed == -1 && i -1 >= 0 + (if + (then + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i-1].n >= t/2 + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.set 3) ;; j = x.c[i].n + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j-1] + (i32.load offset=8) ;; x.c[i].keys[j-1] + (i32.store offset=8) ;; x.c[i].keys[j] = x.c[i].keys[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (local.set 3) ;; j = x.c[i].n + 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i].c[j] is located + (i32.const 0) ;;get x.c[i].c[j-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) + (i32.load offset=8) ;; x.c[i].c[j-1] + (i32.store offset=8) ;; x.c[i].c[j] = x.c[i].c[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; ;; x.c[i].n = x.c[i].n + 1 + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[0] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i-1] + (i32.store offset=8) ;; x.c[i].keys[0] = x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n - 1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[i-1] = x.c[i-1].keys[x.c[i-1].n] + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; get x.c[i].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 ;; addr where x.c[i].c[0] is located + (i32.const 0) ;; get x.c[i-1].c[x.c[i-1].n + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.add) ;; index = x.c[i-1.n + 1] + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1].c[x.c[i-1].n + 1] + (i32.store offset=8) ;; x.c[i].c[0] = x.c[i-1].c[x.c[i-1].n + 1] + ) + ) + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (if ;; changed == -1? + (then + (local.get 2) ;; we have to merge x.c[i] with one sibling;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.le_s) + (if ;; i+1 <= x.n + (then + (local.get 5) ;; merge with right sibling;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = x.keys[i] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n+j] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n+j] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i+1].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[x.c[i].n+j] + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) ;; x.c[i].n + x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + x.c[i+1].n + 1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.set 3) ;; j = i+1 + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) ;; changed == -1 ? + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i - 1 >= 0? + (i32.and) ;; changed == -1 && i - 1 >= 0 + (if + (then + (i32.const 0) ;; merge with left sibling ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n] = x.keys[i-1] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n+j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[j] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n+j] = x.c[i].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i-1].c[x.c[i-1].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i-1].c[x.c[i-1].n+j] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i].c[j] + (i32.store offset=8) ;; x.c[i-1].c[x.c[i-1].n+j] = x.c[i].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.add) ;; x.c[i-1].n + x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n + x.c[i].n + 1 + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = i - 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + ) + ) + ) + ) + ) + ) + (local.get 4) + (i32.const -1) + (i32.eq) + (if ;; changed == -1? if yes, we merged with left sibling + (then + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (local.get 1) + (call $btreeDelete) + (drop) + ) + (else + (local.get 5) ;; x.c[i] + (local.get 1) + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) ;; end of if x is not leaf + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.load offset=4) + (i32.const 0) + (i32.eq) + (if ;; if root is empty + (then + (i32.const 0) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; root.c[0] + (i32.store offset=8) ;; root = root.c[0] + ) + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + ) + (func $main + (param $a i32) + (param $b i32) + (param $c i32) + (param $d i32) + (param $e i32) + (param $h i32) + (param $i i32) + (local i32) + (local.get $a) + (local.get $b) + (i32.gt_s) + (local.get $b) + (local.get $c) + (i32.gt_s) + (local.get $c) + (local.get $d) + (i32.gt_s) + (local.get $d) + (local.get $e) + (i32.gt_s) + (local.get $h) + (local.get $a) + (i32.ne) + (local.get $h) + (local.get $b) + (i32.ne) + (local.get $h) + (local.get $c) + (i32.ne) + (local.get $h) + (local.get $d) + (i32.ne) + (local.get $h) + (local.get $e) + (i32.ne) + (local.get $i) + (local.get $a) + (i32.ne) + (local.get $i) + (local.get $b) + (i32.ne) + (local.get $i) + (local.get $c) + (i32.ne) + (local.get $i) + (local.get $d) + (i32.ne) + (local.get $i) + (local.get $e) + (i32.ne) + (local.get $i) + (local.get $h) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.eqz) + (if (then (unreachable))) + (i32.const 4) ;;create a tree with degree 4 + (call $createBtree) + (local.set 7) + ;; insert variables + (local.get $a) + (call $btreeInsert) + (local.set 7) + (local.get $b) + (call $btreeInsert) + (local.set 7) + (local.get $c) + (call $btreeInsert) + (local.set 7) + (local.get $d) + (call $btreeInsert) + (local.set 7) + (local.get $e) + (call $btreeInsert) + (local.set 7) + (local.get $h) + (call $btreeInsert) + (local.set 7) + (local.get $i) + (call $btreeInsert) + (local.set 7) + ;; search for variables & check that they were inserted + (local.get 7) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 7) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 7) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 7) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 7) + (local.get $e) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 7) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 7) + (local.get $i) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + ;; delete & check that it was deleted + ;; a + (local.get 7) + (local.get $a) + (call $btreeDelete) + (local.set 7) + (local.get 7) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; b + (local.get 7) + (local.get $b) + (call $btreeDelete) + (local.set 7) + (local.get 7) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; c + (local.get 7) + (local.get $c) + (call $btreeDelete) + (local.set 7) + (local.get 7) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; d + (local.get 7) + (local.get $d) + (call $btreeDelete) + (local.set 7) + (local.get 7) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; e + (local.get 7) + (local.get $e) + (call $btreeDelete) + (local.set 7) + (local.get 7) + (local.get $e) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; h + (local.get 7) + (local.get $h) + (call $btreeDelete) + (local.set 7) + (local.get 7) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (drop)) + (export "main" (func $main))) diff --git a/benchmarks/wasm/btree/5o3u.wat b/benchmarks/wasm/btree/5o3u.wat new file mode 100644 index 00000000..2ec8d59f --- /dev/null +++ b/benchmarks/wasm/btree/5o3u.wat @@ -0,0 +1,2997 @@ +(module + (memory $0 2) + (func $createBtree (param i32) (result i32) ;; createBtree(t), where t: degree of the btree + (i32.const 0) + (local.get 0) + (i32.store) ;; store t at address 0 + (i32.const 0) + (i32.const 1) + (i32.store offset=4) ;; store the number of nodes in the btree = 1 + (i32.const 0) + (i32.const 65536) + (i32.store offset=8) ;; store the root addr + (i32.const 1) ;; Now create root + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then + (i32.const 65536) ;; 64KiB = 65536 bytes + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + (i32.const 65536) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + ) + ) + (i32.const 65536) ;; returns the address of the root + ) + ;; btreeSearch(x, k), where x: node address; k: key to search. Returns address of the key or -1 if key is not in tree. + (func $btreeSearch (param i32) (param i32) (result i32) (local i32) + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n -1 + (i32.le_s) ;; i <= x.children-1 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + (if + (then + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.le_s) ;; i <= x.children - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.eq) + ;; k == x.keys[i] + (if (result i32) + (then + (local.get 0) ;; save the address, which is x+8+i*4 + (i32.const 8) + (i32.add) ;; x+8 + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x+8+i*4 + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + ;; btreeSplitChild(x, i), where x: addr of a non full internal node; i: index such that x.children[i] is a full child of x + (func $btreeSplitChild (param i32) (param i32) (local i32) (local i32) + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then ;; get the number of nodes in the tree + (i32.const 0) + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address is 64KiB*(number of nodes+1) + (local.set 2) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the addr of the child in index i : y + (i32.load) ;; check if x.children[i] is leaf ;; load the first i32 in the child + (i32.const 1) + (i32.eq) ;; is y leaf? + (if + (then ;; yes + (local.get 2) ;; addr of new node z + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + ) + (else ;; no + (local.get 2) ;; addr of new node z + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + ) + ) + (local.get 2) ;; now store number of children, which will be t/2 -1 ;; addr of z + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.store offset=4) ;; store number of children = (t/2)-1 + (i32.const 0) ;; now to store the keys + (local.set 3) ;; counter = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.eq) ;; counter == (t/2)-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 2) ;; addr of z ;; we will store in the new node's address. z.keys[counter] + (i32.const 4) + (local.get 3) ;; counter + (i32.mul) ;; counter * 4 + (i32.add) ;; addr + counter * 4 -> where the key is goint to be located + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 4) ;; now get x.children[i].keys[counter+(t/2)] + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + (t/2) + (i32.mul) ;; counter + (t/2) *4 + (i32.add) ;; y + counter + (t/2) *4 + (i32.load offset=8) ;; y.keys[counter + (t/2)] + (i32.store offset=8) ;; z.keys[counter] = y.keys[counter + (t/2)] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) + (local.set 3) ;; set counter back to 0 + (i32.const 0) ;; check if x.children[i] is leaf + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load) ;; get first i32 in child --> isLeaf + (i32.const 1) + (i32.ne) ;; is leaf? + (if + (then ;; not leaf so we have to update the children + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.eq) ;; counter == (t/2) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; now get z.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; counter*4 + (i32.add) ;; (t-1)*4 + counter*4 + (local.get 2) ;; z + (i32.add) ;; z + (t-1)*4 + counter*4 --> address where addr of child is stored + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) ;; get x.children[i].children[counter+(t/2)] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + t/2 + (i32.const 4) + (i32.mul) ;; counter+t/2 * 4 + (i32.add) ;; (t-1)*4 + (counter+(t/2))*4 + (i32.add) ;; x.children[i] + (t-1)*4 + (counter + (t/2))*4 + (i32.load offset=8) ;; load the address of the child in index counter + t/2 + (i32.store offset=8) ;; z.children[counter] = x.children[i].children[counter] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.store offset=4) ;; number of keys of x.children[i] is now t/2 -1 + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (local.set 3) ;; counter = x.n + (block $loop_break + (loop $loop + (local.get 1) ;; i + (local.get 3) ;; counter + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; get x.children[counter + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (i32.add) ;; (t-1)*4 + (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter + 1)*4 -->address where addr of child is stored + (i32.const 0) ;; get x.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (i32.add) ;; (t-1)*4 + (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter)*4 + (i32.load offset=8) ;; load the addr of the child in index (counter) + (i32.store offset=8) ;; x.children[counter+1] = x.children[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 1) + (i32.add) ;; i + 1 + (i32.const 4) + (i32.mul) ;; (i + 1)*4 + (i32.add) ;; (t-1)*4 + (i + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i + 1)*4 --> addr where we will store the new addr of the child + (local.get 2) ;; addr of z + (i32.store offset=8) ;; x.children[i+1] = z + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (i32.const 1) + (i32.sub) + (local.set 3) ;; counter = x.n -1 + (block $loop_break + (loop $loop + (local.get 1) ;; i + (i32.const 1) + (i32.sub) ;; i-1 + (local.get 3) ;; counter + (i32.eq) ;; counter == i-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 3) ;; get x.keys[counter + 1] ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter + 1)*4 --> addr where we will store the new key + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter)*4 + (i32.load offset=8) ;; load the key in index (counter) + (i32.store offset=8) ;; x.keys[counter+1] = x.keys[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (local.get 1) ;; get x.keys[i] + (i32.const 4) + (i32.mul) ;; i*4 + (local.get 0) ;; x + (i32.add) ;; x +i*4 --> addr of the key + (i32.const 0) ;; get x.children[i].keys[t/2] + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.const 4) + (i32.mul) ;; (t/2)-1 *4 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; (i)*4 + (i32.add) ;; (t-1)*4 + (i)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i)*4 + (i32.load offset=8) ;; load the address of the child in index (i) + (i32.add) ;; y + (t/2)-1 *4 + (i32.load offset=8) ;; load the key in index (t/2)-1 from node x.children[i] + (i32.store offset=8) ;; store the new key in the addr + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) ;; x.n + 1 + (i32.store offset=4) ;; x.n = x.n + 1 + ) + ) + ) + ;; btreeInsertNonFull(x, k), where x: addr of a non full internal node; k: the key to insert + (func $btreeInsertNonFull (param i32) (param i32) (local i32) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n -1 + (local.set 2) ;; i = x.n -1 + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (i32.and) + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i *4 + (i32.add) ;; x + i *4 --> addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.keys[i+1] = x.keys[i] + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 1) ;; k + (i32.store offset=8) ;; x.keys[i+1] = k + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; x.n = x.n +1 + ) + (else ;; x is not leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i >= 0 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i -1 + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 2) ;;i = i+1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address of the child in index i = y + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load offset=4) ;; get y.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; y.n == t-1 --> node is full + (if + (then + (local.get 0) ;; x + (local.get 2) ;; i + (call $btreeSplitChild) + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k>x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i+1 + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (local.get 1) ;; k + (call $btreeInsertNonFull) + ) + ) + ) + (func $btreeInsert (param i32) (result i32) (local i32) (local i32) + (i32.const 0) + (i32.load offset=8) ;; root addr + (local.tee 2) ;; set r + (i32.load offset=4) ;; r.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; r.n == t-1 --> root is full + (if (result i32) + (then + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if (result i32) ;; if we can grow memory + (then + (i32.const 0) ;; get the number of nodes in the tree + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address of s is 64KiB*(number of nodes+1) + (local.set 1) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) + (local.get 1) ;; s + (i32.store offset=8) ;; root addr is now s + (local.get 1) ;; s + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + (local.get 1) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + (i32.const 0) ;; store children addresses + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 1) ;; x + (i32.add) ;; x + (t-1)*4 --> addre where addr of child number 0 is located + (local.get 2) + (i32.store offset=8) ;; s.children[0] = r + (local.get 1) ;; s + (i32.const 0) + (call $btreeSplitChild) + (local.get 1) ;; s + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 1) ;; return the address of the new root + ) + (else + (i32.const -1) ;; return -1 because we could not create a new node + ) + ) + ) + (else + (local.get 2) ;; r + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 2) ;; return the address of the root, which is the same + ) + ) + ) + ;; Returns the address of the new root (if applicable, otherwise returns the addres of the root) + (func $btreeDelete (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local i32) + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (local.get 2) ;; i + (i32.eq) ;; i == x.n + (if + (then + (br $loop_break) ;; break from loop + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + (if + (then + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $while_break + (loop $while + (local.get 3) ;; j + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n-1 + (i32.eq) ;; j == x.n-1 + (if + (then + (br $while_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 --> addr where x.keys[j] is stored + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.mul) ;; j+1 *4 + (i32.add) ;; x + j+1 *4 --> addr where x.keys[j+1] is stored + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $while) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n -1 + (br $loop_break) + ) + ) + ) + ) + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) ;; continue + ) + ) + ) + (else ;; x is not leaf + (i32.const 0) ;;get the index of the appropriate child/key + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (i32.and) ;; i <= x.n - 1 && k > x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i + 1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.lt_s) ;; i < x.n + (if (result i32) + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (if + (then ;; key is present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.tee 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (call $btreeDelete) ;; (x.c[i], x.c[i].keys[x.c[i].n - 1]) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i].keys[x.c[i].n - 1] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i+1] + (local.tee 5) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (local.get 5) ;; x.c[i+1] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (call $btreeDelete) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 --> addr where x.c[i].keys[x.c[i].n] will be stored + (local.get 1) ;; k + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = k + (i32.const 0) ;; now merge x.c[i] with x.c[i+1] + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if ;; j == x.c[i+1].n + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n + j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[index] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x.c[i+1] + i*4 ;; addr of x.c[i+1].keys[j] + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[index] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n +1 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + 1 + x.c[i+1].n + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; now adjust the children + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.get 3) ;; j + (i32.add) ;; index = x.c[i].n+j + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x.c[i] + (t-1)*4 --> address where x.c[i].c[x.c[i].n+j] is stored + (i32.const 0) ;;get x.c[i+1].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x.c[i+1] + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (i32.const 1) + (i32.sub) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) + (i32.mul) ;; j+1*4 + (i32.add) ;; x + j+1*4 ;; addr of x.keys[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = i+1 + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 0) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n-1 + (local.get 5) ;; x.c[i] + (local.get 1) ;; k + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) + (else ;; key not present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) + (i32.const 1) + (i32.sub) ;; t/2 - 1 + (i32.eq) + (if + (then + (i32.const -1) + (local.set 4) ;; changed = -1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.le_s) ;; i+1 <= x.n + (if + (then + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; index = t/2 -1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[t/2-1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[t/2-1] = x.keys[i] + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;;x.c[i].n = x.c[i].n +1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i+1].keys[0] + (i32.load offset=8) ;; x.c[i+1].keys[0] + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + (local.get 5) + (i32.load) ;; x.c[i] is leaf? + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) ;;get x.c[i].c[t/2] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; index = t/2 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[t/2] + (i32.const 0) ;;get x.c[i+1].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[0] + (i32.store offset=8) ;; x.c[i].c[t/2] = x.c[i+1].c[0] + ) + ) + (local.get 5) ;; x.c[i] + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.store offset=4) ;; x.c[i].n = t/2 + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 --> address where x.c[i+1].keys[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j+1] + (i32.store offset=8) ;; x.c[i+1].keys[j] = x.c[i+1].keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i+1].c[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j+1] + (i32.store offset=8) ;; x.c[i+1].c[j] = x.c[i+1].c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr where x.c[i+1] is located + (i32.load offset=8) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i+1].n = x.c[i+1].n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i -1 >= 0 + (i32.and) ;; changed == -1 && i -1 >= 0 + (if + (then + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i-1].n >= t/2 + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.set 3) ;; j = x.c[i].n + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j-1] + (i32.load offset=8) ;; x.c[i].keys[j-1] + (i32.store offset=8) ;; x.c[i].keys[j] = x.c[i].keys[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (local.set 3) ;; j = x.c[i].n + 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i].c[j] is located + (i32.const 0) ;;get x.c[i].c[j-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) + (i32.load offset=8) ;; x.c[i].c[j-1] + (i32.store offset=8) ;; x.c[i].c[j] = x.c[i].c[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; ;; x.c[i].n = x.c[i].n + 1 + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[0] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i-1] + (i32.store offset=8) ;; x.c[i].keys[0] = x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n - 1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[i-1] = x.c[i-1].keys[x.c[i-1].n] + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; get x.c[i].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 ;; addr where x.c[i].c[0] is located + (i32.const 0) ;; get x.c[i-1].c[x.c[i-1].n + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.add) ;; index = x.c[i-1.n + 1] + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1].c[x.c[i-1].n + 1] + (i32.store offset=8) ;; x.c[i].c[0] = x.c[i-1].c[x.c[i-1].n + 1] + ) + ) + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (if ;; changed == -1? + (then + (local.get 2) ;; we have to merge x.c[i] with one sibling;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.le_s) + (if ;; i+1 <= x.n + (then + (local.get 5) ;; merge with right sibling;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = x.keys[i] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n+j] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n+j] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i+1].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[x.c[i].n+j] + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) ;; x.c[i].n + x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + x.c[i+1].n + 1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.set 3) ;; j = i+1 + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) ;; changed == -1 ? + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i - 1 >= 0? + (i32.and) ;; changed == -1 && i - 1 >= 0 + (if + (then + (i32.const 0) ;; merge with left sibling ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n] = x.keys[i-1] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n+j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[j] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n+j] = x.c[i].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i-1].c[x.c[i-1].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i-1].c[x.c[i-1].n+j] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i].c[j] + (i32.store offset=8) ;; x.c[i-1].c[x.c[i-1].n+j] = x.c[i].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.add) ;; x.c[i-1].n + x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n + x.c[i].n + 1 + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = i - 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + ) + ) + ) + ) + ) + ) + (local.get 4) + (i32.const -1) + (i32.eq) + (if ;; changed == -1? if yes, we merged with left sibling + (then + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (local.get 1) + (call $btreeDelete) + (drop) + ) + (else + (local.get 5) ;; x.c[i] + (local.get 1) + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) ;; end of if x is not leaf + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.load offset=4) + (i32.const 0) + (i32.eq) + (if ;; if root is empty + (then + (i32.const 0) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; root.c[0] + (i32.store offset=8) ;; root = root.c[0] + ) + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + ) + (func $main + (param $a i32) + (param $b i32) + (param $c i32) + (param $d i32) + (param $e i32) + (param $h i32) + (param $i i32) + (param $j i32) + (local i32) + (local.get $a) + (local.get $b) + (i32.gt_s) + (local.get $b) + (local.get $c) + (i32.gt_s) + (local.get $c) + (local.get $d) + (i32.gt_s) + (local.get $d) + (local.get $e) + (i32.gt_s) + (local.get $h) + (local.get $a) + (i32.ne) + (local.get $h) + (local.get $b) + (i32.ne) + (local.get $h) + (local.get $c) + (i32.ne) + (local.get $h) + (local.get $d) + (i32.ne) + (local.get $h) + (local.get $e) + (i32.ne) + (local.get $i) + (local.get $a) + (i32.ne) + (local.get $i) + (local.get $b) + (i32.ne) + (local.get $i) + (local.get $c) + (i32.ne) + (local.get $i) + (local.get $d) + (i32.ne) + (local.get $i) + (local.get $e) + (i32.ne) + (local.get $i) + (local.get $h) + (i32.ne) + (local.get $j) + (local.get $a) + (i32.ne) + (local.get $j) + (local.get $b) + (i32.ne) + (local.get $j) + (local.get $c) + (i32.ne) + (local.get $j) + (local.get $d) + (i32.ne) + (local.get $j) + (local.get $e) + (i32.ne) + (local.get $j) + (local.get $h) + (i32.ne) + (local.get $j) + (local.get $i) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.eqz) + (if (then (unreachable))) + (i32.const 4) ;;create a tree with degree 4 + (call $createBtree) + (local.set 8) + ;; insert variables + (local.get $a) + (call $btreeInsert) + (local.set 8) + (local.get $b) + (call $btreeInsert) + (local.set 8) + (local.get $c) + (call $btreeInsert) + (local.set 8) + (local.get $d) + (call $btreeInsert) + (local.set 8) + (local.get $e) + (call $btreeInsert) + (local.set 8) + (local.get $h) + (call $btreeInsert) + (local.set 8) + (local.get $i) + (call $btreeInsert) + (local.set 8) + (local.get $j) + (call $btreeInsert) + (local.set 8) + ;; search for variables & check that they were inserted + (local.get 8) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 8) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 8) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 8) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 8) + (local.get $e) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 8) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 8) + (local.get $i) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 8) + (local.get $j) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + ;; delete & check that it was deleted + ;; a + (local.get 8) + (local.get $a) + (call $btreeDelete) + (local.set 8) + (local.get 8) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; b + (local.get 8) + (local.get $b) + (call $btreeDelete) + (local.set 8) + (local.get 8) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; c + (local.get 8) + (local.get $c) + (call $btreeDelete) + (local.set 8) + (local.get 8) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; d + (local.get 8) + (local.get $d) + (call $btreeDelete) + (local.set 8) + (local.get 8) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; e + (local.get 8) + (local.get $e) + (call $btreeDelete) + (local.set 8) + (local.get 8) + (local.get $e) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; h + (local.get 8) + (local.get $h) + (call $btreeDelete) + (local.set 8) + (local.get 8) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; i + (local.get 8) + (local.get $i) + (call $btreeDelete) + (local.set 8) + (local.get 8) + (local.get $i) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (drop)) + (export "main" (func $main))) diff --git a/benchmarks/wasm/btree/6o1u.wat b/benchmarks/wasm/btree/6o1u.wat new file mode 100644 index 00000000..352faf9d --- /dev/null +++ b/benchmarks/wasm/btree/6o1u.wat @@ -0,0 +1,2932 @@ +(module + (memory $0 1) + (func $createBtree (param i32) (result i32) ;; createBtree(t), where t: degree of the btree + (i32.const 0) + (local.get 0) + (i32.store) ;; store t at address 0 + (i32.const 0) + (i32.const 1) + (i32.store offset=4) ;; store the number of nodes in the btree = 1 + (i32.const 0) + (i32.const 65536) + (i32.store offset=8) ;; store the root addr + (i32.const 1) ;; Now create root + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then + (i32.const 65536) ;; 64KiB = 65536 bytes + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + (i32.const 65536) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + ) + ) + (i32.const 65536) ;; returns the address of the root + ) + ;; btreeSearch(x, k), where x: node address; k: key to search. Returns address of the key or -1 if key is not in tree. + (func $btreeSearch (param i32) (param i32) (result i32) (local i32) + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n -1 + (i32.le_s) ;; i <= x.children-1 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + (if + (then + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.le_s) ;; i <= x.children - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.eq) + ;; k == x.keys[i] + (if (result i32) + (then + (local.get 0) ;; save the address, which is x+8+i*4 + (i32.const 8) + (i32.add) ;; x+8 + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x+8+i*4 + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + ;; btreeSplitChild(x, i), where x: addr of a non full internal node; i: index such that x.children[i] is a full child of x + (func $btreeSplitChild (param i32) (param i32) (local i32) (local i32) + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then ;; get the number of nodes in the tree + (i32.const 0) + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address is 64KiB*(number of nodes+1) + (local.set 2) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the addr of the child in index i : y + (i32.load) ;; check if x.children[i] is leaf ;; load the first i32 in the child + (i32.const 1) + (i32.eq) ;; is y leaf? + (if + (then ;; yes + (local.get 2) ;; addr of new node z + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + ) + (else ;; no + (local.get 2) ;; addr of new node z + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + ) + ) + (local.get 2) ;; now store number of children, which will be t/2 -1 ;; addr of z + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.store offset=4) ;; store number of children = (t/2)-1 + (i32.const 0) ;; now to store the keys + (local.set 3) ;; counter = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.eq) ;; counter == (t/2)-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 2) ;; addr of z ;; we will store in the new node's address. z.keys[counter] + (i32.const 4) + (local.get 3) ;; counter + (i32.mul) ;; counter * 4 + (i32.add) ;; addr + counter * 4 -> where the key is goint to be located + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 4) ;; now get x.children[i].keys[counter+(t/2)] + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + (t/2) + (i32.mul) ;; counter + (t/2) *4 + (i32.add) ;; y + counter + (t/2) *4 + (i32.load offset=8) ;; y.keys[counter + (t/2)] + (i32.store offset=8) ;; z.keys[counter] = y.keys[counter + (t/2)] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) + (local.set 3) ;; set counter back to 0 + (i32.const 0) ;; check if x.children[i] is leaf + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load) ;; get first i32 in child --> isLeaf + (i32.const 1) + (i32.ne) ;; is leaf? + (if + (then ;; not leaf so we have to update the children + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.eq) ;; counter == (t/2) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; now get z.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; counter*4 + (i32.add) ;; (t-1)*4 + counter*4 + (local.get 2) ;; z + (i32.add) ;; z + (t-1)*4 + counter*4 --> address where addr of child is stored + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) ;; get x.children[i].children[counter+(t/2)] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + t/2 + (i32.const 4) + (i32.mul) ;; counter+t/2 * 4 + (i32.add) ;; (t-1)*4 + (counter+(t/2))*4 + (i32.add) ;; x.children[i] + (t-1)*4 + (counter + (t/2))*4 + (i32.load offset=8) ;; load the address of the child in index counter + t/2 + (i32.store offset=8) ;; z.children[counter] = x.children[i].children[counter] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.store offset=4) ;; number of keys of x.children[i] is now t/2 -1 + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (local.set 3) ;; counter = x.n + (block $loop_break + (loop $loop + (local.get 1) ;; i + (local.get 3) ;; counter + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; get x.children[counter + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (i32.add) ;; (t-1)*4 + (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter + 1)*4 -->address where addr of child is stored + (i32.const 0) ;; get x.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (i32.add) ;; (t-1)*4 + (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter)*4 + (i32.load offset=8) ;; load the addr of the child in index (counter) + (i32.store offset=8) ;; x.children[counter+1] = x.children[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 1) + (i32.add) ;; i + 1 + (i32.const 4) + (i32.mul) ;; (i + 1)*4 + (i32.add) ;; (t-1)*4 + (i + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i + 1)*4 --> addr where we will store the new addr of the child + (local.get 2) ;; addr of z + (i32.store offset=8) ;; x.children[i+1] = z + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (i32.const 1) + (i32.sub) + (local.set 3) ;; counter = x.n -1 + (block $loop_break + (loop $loop + (local.get 1) ;; i + (i32.const 1) + (i32.sub) ;; i-1 + (local.get 3) ;; counter + (i32.eq) ;; counter == i-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 3) ;; get x.keys[counter + 1] ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter + 1)*4 --> addr where we will store the new key + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter)*4 + (i32.load offset=8) ;; load the key in index (counter) + (i32.store offset=8) ;; x.keys[counter+1] = x.keys[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (local.get 1) ;; get x.keys[i] + (i32.const 4) + (i32.mul) ;; i*4 + (local.get 0) ;; x + (i32.add) ;; x +i*4 --> addr of the key + (i32.const 0) ;; get x.children[i].keys[t/2] + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.const 4) + (i32.mul) ;; (t/2)-1 *4 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; (i)*4 + (i32.add) ;; (t-1)*4 + (i)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i)*4 + (i32.load offset=8) ;; load the address of the child in index (i) + (i32.add) ;; y + (t/2)-1 *4 + (i32.load offset=8) ;; load the key in index (t/2)-1 from node x.children[i] + (i32.store offset=8) ;; store the new key in the addr + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) ;; x.n + 1 + (i32.store offset=4) ;; x.n = x.n + 1 + ) + ) + ) + ;; btreeInsertNonFull(x, k), where x: addr of a non full internal node; k: the key to insert + (func $btreeInsertNonFull (param i32) (param i32) (local i32) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n -1 + (local.set 2) ;; i = x.n -1 + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (i32.and) + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i *4 + (i32.add) ;; x + i *4 --> addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.keys[i+1] = x.keys[i] + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 1) ;; k + (i32.store offset=8) ;; x.keys[i+1] = k + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; x.n = x.n +1 + ) + (else ;; x is not leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i >= 0 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i -1 + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 2) ;;i = i+1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address of the child in index i = y + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load offset=4) ;; get y.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; y.n == t-1 --> node is full + (if + (then + (local.get 0) ;; x + (local.get 2) ;; i + (call $btreeSplitChild) + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k>x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i+1 + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (local.get 1) ;; k + (call $btreeInsertNonFull) + ) + ) + ) + (func $btreeInsert (param i32) (result i32) (local i32) (local i32) + (i32.const 0) + (i32.load offset=8) ;; root addr + (local.tee 2) ;; set r + (i32.load offset=4) ;; r.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; r.n == t-1 --> root is full + (if (result i32) + (then + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if (result i32) ;; if we can grow memory + (then + (i32.const 0) ;; get the number of nodes in the tree + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address of s is 64KiB*(number of nodes+1) + (local.set 1) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) + (local.get 1) ;; s + (i32.store offset=8) ;; root addr is now s + (local.get 1) ;; s + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + (local.get 1) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + (i32.const 0) ;; store children addresses + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 1) ;; x + (i32.add) ;; x + (t-1)*4 --> addre where addr of child number 0 is located + (local.get 2) + (i32.store offset=8) ;; s.children[0] = r + (local.get 1) ;; s + (i32.const 0) + (call $btreeSplitChild) + (local.get 1) ;; s + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 1) ;; return the address of the new root + ) + (else + (i32.const -1) ;; return -1 because we could not create a new node + ) + ) + ) + (else + (local.get 2) ;; r + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 2) ;; return the address of the root, which is the same + ) + ) + ) + ;; Returns the address of the new root (if applicable, otherwise returns the addres of the root) + (func $btreeDelete (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local i32) + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (local.get 2) ;; i + (i32.eq) ;; i == x.n + (if + (then + (br $loop_break) ;; break from loop + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + (if + (then + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $while_break + (loop $while + (local.get 3) ;; j + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n-1 + (i32.eq) ;; j == x.n-1 + (if + (then + (br $while_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 --> addr where x.keys[j] is stored + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.mul) ;; j+1 *4 + (i32.add) ;; x + j+1 *4 --> addr where x.keys[j+1] is stored + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $while) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n -1 + (br $loop_break) + ) + ) + ) + ) + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) ;; continue + ) + ) + ) + (else ;; x is not leaf + (i32.const 0) ;;get the index of the appropriate child/key + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (i32.and) ;; i <= x.n - 1 && k > x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i + 1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.lt_s) ;; i < x.n + (if (result i32) + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (if + (then ;; key is present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.tee 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (call $btreeDelete) ;; (x.c[i], x.c[i].keys[x.c[i].n - 1]) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i].keys[x.c[i].n - 1] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i+1] + (local.tee 5) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (local.get 5) ;; x.c[i+1] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (call $btreeDelete) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 --> addr where x.c[i].keys[x.c[i].n] will be stored + (local.get 1) ;; k + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = k + (i32.const 0) ;; now merge x.c[i] with x.c[i+1] + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if ;; j == x.c[i+1].n + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n + j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[index] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x.c[i+1] + i*4 ;; addr of x.c[i+1].keys[j] + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[index] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n +1 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + 1 + x.c[i+1].n + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; now adjust the children + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.get 3) ;; j + (i32.add) ;; index = x.c[i].n+j + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x.c[i] + (t-1)*4 --> address where x.c[i].c[x.c[i].n+j] is stored + (i32.const 0) ;;get x.c[i+1].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x.c[i+1] + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (i32.const 1) + (i32.sub) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) + (i32.mul) ;; j+1*4 + (i32.add) ;; x + j+1*4 ;; addr of x.keys[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = i+1 + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 0) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n-1 + (local.get 5) ;; x.c[i] + (local.get 1) ;; k + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) + (else ;; key not present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) + (i32.const 1) + (i32.sub) ;; t/2 - 1 + (i32.eq) + (if + (then + (i32.const -1) + (local.set 4) ;; changed = -1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.le_s) ;; i+1 <= x.n + (if + (then + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; index = t/2 -1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[t/2-1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[t/2-1] = x.keys[i] + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;;x.c[i].n = x.c[i].n +1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i+1].keys[0] + (i32.load offset=8) ;; x.c[i+1].keys[0] + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + (local.get 5) + (i32.load) ;; x.c[i] is leaf? + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) ;;get x.c[i].c[t/2] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; index = t/2 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[t/2] + (i32.const 0) ;;get x.c[i+1].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[0] + (i32.store offset=8) ;; x.c[i].c[t/2] = x.c[i+1].c[0] + ) + ) + (local.get 5) ;; x.c[i] + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.store offset=4) ;; x.c[i].n = t/2 + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 --> address where x.c[i+1].keys[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j+1] + (i32.store offset=8) ;; x.c[i+1].keys[j] = x.c[i+1].keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i+1].c[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j+1] + (i32.store offset=8) ;; x.c[i+1].c[j] = x.c[i+1].c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr where x.c[i+1] is located + (i32.load offset=8) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i+1].n = x.c[i+1].n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i -1 >= 0 + (i32.and) ;; changed == -1 && i -1 >= 0 + (if + (then + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i-1].n >= t/2 + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.set 3) ;; j = x.c[i].n + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j-1] + (i32.load offset=8) ;; x.c[i].keys[j-1] + (i32.store offset=8) ;; x.c[i].keys[j] = x.c[i].keys[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (local.set 3) ;; j = x.c[i].n + 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i].c[j] is located + (i32.const 0) ;;get x.c[i].c[j-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) + (i32.load offset=8) ;; x.c[i].c[j-1] + (i32.store offset=8) ;; x.c[i].c[j] = x.c[i].c[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; ;; x.c[i].n = x.c[i].n + 1 + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[0] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i-1] + (i32.store offset=8) ;; x.c[i].keys[0] = x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n - 1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[i-1] = x.c[i-1].keys[x.c[i-1].n] + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; get x.c[i].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 ;; addr where x.c[i].c[0] is located + (i32.const 0) ;; get x.c[i-1].c[x.c[i-1].n + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.add) ;; index = x.c[i-1.n + 1] + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1].c[x.c[i-1].n + 1] + (i32.store offset=8) ;; x.c[i].c[0] = x.c[i-1].c[x.c[i-1].n + 1] + ) + ) + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (if ;; changed == -1? + (then + (local.get 2) ;; we have to merge x.c[i] with one sibling;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.le_s) + (if ;; i+1 <= x.n + (then + (local.get 5) ;; merge with right sibling;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = x.keys[i] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n+j] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n+j] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i+1].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[x.c[i].n+j] + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) ;; x.c[i].n + x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + x.c[i+1].n + 1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.set 3) ;; j = i+1 + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) ;; changed == -1 ? + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i - 1 >= 0? + (i32.and) ;; changed == -1 && i - 1 >= 0 + (if + (then + (i32.const 0) ;; merge with left sibling ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n] = x.keys[i-1] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n+j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[j] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n+j] = x.c[i].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i-1].c[x.c[i-1].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i-1].c[x.c[i-1].n+j] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i].c[j] + (i32.store offset=8) ;; x.c[i-1].c[x.c[i-1].n+j] = x.c[i].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.add) ;; x.c[i-1].n + x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n + x.c[i].n + 1 + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = i - 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + ) + ) + ) + ) + ) + ) + (local.get 4) + (i32.const -1) + (i32.eq) + (if ;; changed == -1? if yes, we merged with left sibling + (then + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (local.get 1) + (call $btreeDelete) + (drop) + ) + (else + (local.get 5) ;; x.c[i] + (local.get 1) + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) ;; end of if x is not leaf + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.load offset=4) + (i32.const 0) + (i32.eq) + (if ;; if root is empty + (then + (i32.const 0) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; root.c[0] + (i32.store offset=8) ;; root = root.c[0] + ) + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + ) + (func $main + (param $a i32) + (param $b i32) + (param $c i32) + (param $d i32) + (param $e i32) + (param $f i32) + (param $h i32) + (local i32) + (local.get $a) + (local.get $b) + (i32.gt_s) + (local.get $b) + (local.get $c) + (i32.gt_s) + (local.get $c) + (local.get $d) + (i32.gt_s) + (local.get $d) + (local.get $e) + (i32.gt_s) + (local.get $e) + (local.get $f) + (i32.gt_s) + (local.get $h) + (local.get $a) + (i32.ne) + (local.get $h) + (local.get $b) + (i32.ne) + (local.get $h) + (local.get $c) + (i32.ne) + (local.get $h) + (local.get $d) + (i32.ne) + (local.get $h) + (local.get $e) + (i32.ne) + (local.get $h) + (local.get $f) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.eqz) + (if (then (unreachable))) + (i32.const 4) ;;create a tree with degree 4 + (call $createBtree) + (local.set 7) + ;; insert variables + (local.get $a) + (call $btreeInsert) + (local.set 7) + (local.get $b) + (call $btreeInsert) + (local.set 7) + (local.get $c) + (call $btreeInsert) + (local.set 7) + (local.get $d) + (call $btreeInsert) + (local.set 7) + (local.get $e) + (call $btreeInsert) + (local.set 7) + (local.get $f) + (call $btreeInsert) + (local.set 7) + (local.get $h) + (call $btreeInsert) + (local.set 7) + ;; search for variables & check that they were inserted + (local.get 7) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 7) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 7) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 7) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 7) + (local.get $e) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 7) + (local.get $f) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 7) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + ;; delete & check that it was deleted + ;; a + (local.get 7) + (local.get $a) + (call $btreeDelete) + (local.set 7) + (local.get 7) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; b + (local.get 7) + (local.get $b) + (call $btreeDelete) + (local.set 7) + (local.get 7) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; c + (local.get 7) + (local.get $c) + (call $btreeDelete) + (local.set 7) + (local.get 7) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; d + (local.get 7) + (local.get $d) + (call $btreeDelete) + (local.set 7) + (local.get 7) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; e + (local.get 7) + (local.get $e) + (call $btreeDelete) + (local.set 7) + (local.get 7) + (local.get $e) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; f + (local.get 7) + (local.get $f) + (call $btreeDelete) + (local.set 7) + (local.get 7) + (local.get $f) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (drop)) + (export "main" (func $main))) diff --git a/benchmarks/wasm/btree/6o2u.wat b/benchmarks/wasm/btree/6o2u.wat new file mode 100644 index 00000000..5e1c885e --- /dev/null +++ b/benchmarks/wasm/btree/6o2u.wat @@ -0,0 +1,2981 @@ +(module + (memory $0 1) + (func $createBtree (param i32) (result i32) ;; createBtree(t), where t: degree of the btree + (i32.const 0) + (local.get 0) + (i32.store) ;; store t at address 0 + (i32.const 0) + (i32.const 1) + (i32.store offset=4) ;; store the number of nodes in the btree = 1 + (i32.const 0) + (i32.const 65536) + (i32.store offset=8) ;; store the root addr + (i32.const 1) ;; Now create root + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then + (i32.const 65536) ;; 64KiB = 65536 bytes + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + (i32.const 65536) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + ) + ) + (i32.const 65536) ;; returns the address of the root + ) + ;; btreeSearch(x, k), where x: node address; k: key to search. Returns address of the key or -1 if key is not in tree. + (func $btreeSearch (param i32) (param i32) (result i32) (local i32) + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n -1 + (i32.le_s) ;; i <= x.children-1 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + (if + (then + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.le_s) ;; i <= x.children - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.eq) + ;; k == x.keys[i] + (if (result i32) + (then + (local.get 0) ;; save the address, which is x+8+i*4 + (i32.const 8) + (i32.add) ;; x+8 + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x+8+i*4 + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + ;; btreeSplitChild(x, i), where x: addr of a non full internal node; i: index such that x.children[i] is a full child of x + (func $btreeSplitChild (param i32) (param i32) (local i32) (local i32) + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then ;; get the number of nodes in the tree + (i32.const 0) + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address is 64KiB*(number of nodes+1) + (local.set 2) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the addr of the child in index i : y + (i32.load) ;; check if x.children[i] is leaf ;; load the first i32 in the child + (i32.const 1) + (i32.eq) ;; is y leaf? + (if + (then ;; yes + (local.get 2) ;; addr of new node z + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + ) + (else ;; no + (local.get 2) ;; addr of new node z + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + ) + ) + (local.get 2) ;; now store number of children, which will be t/2 -1 ;; addr of z + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.store offset=4) ;; store number of children = (t/2)-1 + (i32.const 0) ;; now to store the keys + (local.set 3) ;; counter = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.eq) ;; counter == (t/2)-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 2) ;; addr of z ;; we will store in the new node's address. z.keys[counter] + (i32.const 4) + (local.get 3) ;; counter + (i32.mul) ;; counter * 4 + (i32.add) ;; addr + counter * 4 -> where the key is goint to be located + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 4) ;; now get x.children[i].keys[counter+(t/2)] + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + (t/2) + (i32.mul) ;; counter + (t/2) *4 + (i32.add) ;; y + counter + (t/2) *4 + (i32.load offset=8) ;; y.keys[counter + (t/2)] + (i32.store offset=8) ;; z.keys[counter] = y.keys[counter + (t/2)] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) + (local.set 3) ;; set counter back to 0 + (i32.const 0) ;; check if x.children[i] is leaf + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load) ;; get first i32 in child --> isLeaf + (i32.const 1) + (i32.ne) ;; is leaf? + (if + (then ;; not leaf so we have to update the children + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.eq) ;; counter == (t/2) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; now get z.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; counter*4 + (i32.add) ;; (t-1)*4 + counter*4 + (local.get 2) ;; z + (i32.add) ;; z + (t-1)*4 + counter*4 --> address where addr of child is stored + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) ;; get x.children[i].children[counter+(t/2)] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + t/2 + (i32.const 4) + (i32.mul) ;; counter+t/2 * 4 + (i32.add) ;; (t-1)*4 + (counter+(t/2))*4 + (i32.add) ;; x.children[i] + (t-1)*4 + (counter + (t/2))*4 + (i32.load offset=8) ;; load the address of the child in index counter + t/2 + (i32.store offset=8) ;; z.children[counter] = x.children[i].children[counter] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.store offset=4) ;; number of keys of x.children[i] is now t/2 -1 + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (local.set 3) ;; counter = x.n + (block $loop_break + (loop $loop + (local.get 1) ;; i + (local.get 3) ;; counter + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; get x.children[counter + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (i32.add) ;; (t-1)*4 + (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter + 1)*4 -->address where addr of child is stored + (i32.const 0) ;; get x.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (i32.add) ;; (t-1)*4 + (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter)*4 + (i32.load offset=8) ;; load the addr of the child in index (counter) + (i32.store offset=8) ;; x.children[counter+1] = x.children[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 1) + (i32.add) ;; i + 1 + (i32.const 4) + (i32.mul) ;; (i + 1)*4 + (i32.add) ;; (t-1)*4 + (i + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i + 1)*4 --> addr where we will store the new addr of the child + (local.get 2) ;; addr of z + (i32.store offset=8) ;; x.children[i+1] = z + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (i32.const 1) + (i32.sub) + (local.set 3) ;; counter = x.n -1 + (block $loop_break + (loop $loop + (local.get 1) ;; i + (i32.const 1) + (i32.sub) ;; i-1 + (local.get 3) ;; counter + (i32.eq) ;; counter == i-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 3) ;; get x.keys[counter + 1] ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter + 1)*4 --> addr where we will store the new key + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter)*4 + (i32.load offset=8) ;; load the key in index (counter) + (i32.store offset=8) ;; x.keys[counter+1] = x.keys[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (local.get 1) ;; get x.keys[i] + (i32.const 4) + (i32.mul) ;; i*4 + (local.get 0) ;; x + (i32.add) ;; x +i*4 --> addr of the key + (i32.const 0) ;; get x.children[i].keys[t/2] + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.const 4) + (i32.mul) ;; (t/2)-1 *4 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; (i)*4 + (i32.add) ;; (t-1)*4 + (i)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i)*4 + (i32.load offset=8) ;; load the address of the child in index (i) + (i32.add) ;; y + (t/2)-1 *4 + (i32.load offset=8) ;; load the key in index (t/2)-1 from node x.children[i] + (i32.store offset=8) ;; store the new key in the addr + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) ;; x.n + 1 + (i32.store offset=4) ;; x.n = x.n + 1 + ) + ) + ) + ;; btreeInsertNonFull(x, k), where x: addr of a non full internal node; k: the key to insert + (func $btreeInsertNonFull (param i32) (param i32) (local i32) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n -1 + (local.set 2) ;; i = x.n -1 + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (i32.and) + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i *4 + (i32.add) ;; x + i *4 --> addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.keys[i+1] = x.keys[i] + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 1) ;; k + (i32.store offset=8) ;; x.keys[i+1] = k + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; x.n = x.n +1 + ) + (else ;; x is not leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i >= 0 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i -1 + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 2) ;;i = i+1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address of the child in index i = y + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load offset=4) ;; get y.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; y.n == t-1 --> node is full + (if + (then + (local.get 0) ;; x + (local.get 2) ;; i + (call $btreeSplitChild) + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k>x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i+1 + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (local.get 1) ;; k + (call $btreeInsertNonFull) + ) + ) + ) + (func $btreeInsert (param i32) (result i32) (local i32) (local i32) + (i32.const 0) + (i32.load offset=8) ;; root addr + (local.tee 2) ;; set r + (i32.load offset=4) ;; r.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; r.n == t-1 --> root is full + (if (result i32) + (then + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if (result i32) ;; if we can grow memory + (then + (i32.const 0) ;; get the number of nodes in the tree + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address of s is 64KiB*(number of nodes+1) + (local.set 1) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) + (local.get 1) ;; s + (i32.store offset=8) ;; root addr is now s + (local.get 1) ;; s + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + (local.get 1) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + (i32.const 0) ;; store children addresses + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 1) ;; x + (i32.add) ;; x + (t-1)*4 --> addre where addr of child number 0 is located + (local.get 2) + (i32.store offset=8) ;; s.children[0] = r + (local.get 1) ;; s + (i32.const 0) + (call $btreeSplitChild) + (local.get 1) ;; s + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 1) ;; return the address of the new root + ) + (else + (i32.const -1) ;; return -1 because we could not create a new node + ) + ) + ) + (else + (local.get 2) ;; r + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 2) ;; return the address of the root, which is the same + ) + ) + ) + ;; Returns the address of the new root (if applicable, otherwise returns the addres of the root) + (func $btreeDelete (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local i32) + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (local.get 2) ;; i + (i32.eq) ;; i == x.n + (if + (then + (br $loop_break) ;; break from loop + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + (if + (then + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $while_break + (loop $while + (local.get 3) ;; j + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n-1 + (i32.eq) ;; j == x.n-1 + (if + (then + (br $while_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 --> addr where x.keys[j] is stored + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.mul) ;; j+1 *4 + (i32.add) ;; x + j+1 *4 --> addr where x.keys[j+1] is stored + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $while) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n -1 + (br $loop_break) + ) + ) + ) + ) + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) ;; continue + ) + ) + ) + (else ;; x is not leaf + (i32.const 0) ;;get the index of the appropriate child/key + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (i32.and) ;; i <= x.n - 1 && k > x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i + 1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.lt_s) ;; i < x.n + (if (result i32) + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (if + (then ;; key is present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.tee 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (call $btreeDelete) ;; (x.c[i], x.c[i].keys[x.c[i].n - 1]) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i].keys[x.c[i].n - 1] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i+1] + (local.tee 5) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (local.get 5) ;; x.c[i+1] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (call $btreeDelete) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 --> addr where x.c[i].keys[x.c[i].n] will be stored + (local.get 1) ;; k + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = k + (i32.const 0) ;; now merge x.c[i] with x.c[i+1] + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if ;; j == x.c[i+1].n + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n + j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[index] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x.c[i+1] + i*4 ;; addr of x.c[i+1].keys[j] + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[index] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n +1 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + 1 + x.c[i+1].n + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; now adjust the children + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.get 3) ;; j + (i32.add) ;; index = x.c[i].n+j + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x.c[i] + (t-1)*4 --> address where x.c[i].c[x.c[i].n+j] is stored + (i32.const 0) ;;get x.c[i+1].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x.c[i+1] + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (i32.const 1) + (i32.sub) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) + (i32.mul) ;; j+1*4 + (i32.add) ;; x + j+1*4 ;; addr of x.keys[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = i+1 + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 0) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n-1 + (local.get 5) ;; x.c[i] + (local.get 1) ;; k + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) + (else ;; key not present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) + (i32.const 1) + (i32.sub) ;; t/2 - 1 + (i32.eq) + (if + (then + (i32.const -1) + (local.set 4) ;; changed = -1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.le_s) ;; i+1 <= x.n + (if + (then + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; index = t/2 -1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[t/2-1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[t/2-1] = x.keys[i] + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;;x.c[i].n = x.c[i].n +1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i+1].keys[0] + (i32.load offset=8) ;; x.c[i+1].keys[0] + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + (local.get 5) + (i32.load) ;; x.c[i] is leaf? + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) ;;get x.c[i].c[t/2] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; index = t/2 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[t/2] + (i32.const 0) ;;get x.c[i+1].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[0] + (i32.store offset=8) ;; x.c[i].c[t/2] = x.c[i+1].c[0] + ) + ) + (local.get 5) ;; x.c[i] + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.store offset=4) ;; x.c[i].n = t/2 + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 --> address where x.c[i+1].keys[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j+1] + (i32.store offset=8) ;; x.c[i+1].keys[j] = x.c[i+1].keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i+1].c[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j+1] + (i32.store offset=8) ;; x.c[i+1].c[j] = x.c[i+1].c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr where x.c[i+1] is located + (i32.load offset=8) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i+1].n = x.c[i+1].n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i -1 >= 0 + (i32.and) ;; changed == -1 && i -1 >= 0 + (if + (then + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i-1].n >= t/2 + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.set 3) ;; j = x.c[i].n + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j-1] + (i32.load offset=8) ;; x.c[i].keys[j-1] + (i32.store offset=8) ;; x.c[i].keys[j] = x.c[i].keys[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (local.set 3) ;; j = x.c[i].n + 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i].c[j] is located + (i32.const 0) ;;get x.c[i].c[j-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) + (i32.load offset=8) ;; x.c[i].c[j-1] + (i32.store offset=8) ;; x.c[i].c[j] = x.c[i].c[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; ;; x.c[i].n = x.c[i].n + 1 + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[0] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i-1] + (i32.store offset=8) ;; x.c[i].keys[0] = x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n - 1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[i-1] = x.c[i-1].keys[x.c[i-1].n] + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; get x.c[i].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 ;; addr where x.c[i].c[0] is located + (i32.const 0) ;; get x.c[i-1].c[x.c[i-1].n + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.add) ;; index = x.c[i-1.n + 1] + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1].c[x.c[i-1].n + 1] + (i32.store offset=8) ;; x.c[i].c[0] = x.c[i-1].c[x.c[i-1].n + 1] + ) + ) + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (if ;; changed == -1? + (then + (local.get 2) ;; we have to merge x.c[i] with one sibling;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.le_s) + (if ;; i+1 <= x.n + (then + (local.get 5) ;; merge with right sibling;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = x.keys[i] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n+j] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n+j] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i+1].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[x.c[i].n+j] + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) ;; x.c[i].n + x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + x.c[i+1].n + 1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.set 3) ;; j = i+1 + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) ;; changed == -1 ? + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i - 1 >= 0? + (i32.and) ;; changed == -1 && i - 1 >= 0 + (if + (then + (i32.const 0) ;; merge with left sibling ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n] = x.keys[i-1] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n+j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[j] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n+j] = x.c[i].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i-1].c[x.c[i-1].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i-1].c[x.c[i-1].n+j] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i].c[j] + (i32.store offset=8) ;; x.c[i-1].c[x.c[i-1].n+j] = x.c[i].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.add) ;; x.c[i-1].n + x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n + x.c[i].n + 1 + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = i - 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + ) + ) + ) + ) + ) + ) + (local.get 4) + (i32.const -1) + (i32.eq) + (if ;; changed == -1? if yes, we merged with left sibling + (then + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (local.get 1) + (call $btreeDelete) + (drop) + ) + (else + (local.get 5) ;; x.c[i] + (local.get 1) + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) ;; end of if x is not leaf + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.load offset=4) + (i32.const 0) + (i32.eq) + (if ;; if root is empty + (then + (i32.const 0) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; root.c[0] + (i32.store offset=8) ;; root = root.c[0] + ) + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + ) + (func $main + (param $a i32) + (param $b i32) + (param $c i32) + (param $d i32) + (param $e i32) + (param $f i32) + (param $h i32) + (param $i i32) + (local i32) + (local.get $a) + (local.get $b) + (i32.gt_s) + (local.get $b) + (local.get $c) + (i32.gt_s) + (local.get $c) + (local.get $d) + (i32.gt_s) + (local.get $d) + (local.get $e) + (i32.gt_s) + (local.get $e) + (local.get $f) + (i32.gt_s) + (local.get $h) + (local.get $a) + (i32.ne) + (local.get $h) + (local.get $b) + (i32.ne) + (local.get $h) + (local.get $c) + (i32.ne) + (local.get $h) + (local.get $d) + (i32.ne) + (local.get $h) + (local.get $e) + (i32.ne) + (local.get $h) + (local.get $f) + (i32.ne) + (local.get $i) + (local.get $a) + (i32.ne) + (local.get $i) + (local.get $b) + (i32.ne) + (local.get $i) + (local.get $c) + (i32.ne) + (local.get $i) + (local.get $d) + (i32.ne) + (local.get $i) + (local.get $e) + (i32.ne) + (local.get $i) + (local.get $f) + (i32.ne) + (local.get $i) + (local.get $h) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.eqz) + (if (then (unreachable))) + (i32.const 4) ;;create a tree with degree 4 + (call $createBtree) + (local.set 8) + ;; insert variables + (local.get $a) + (call $btreeInsert) + (local.set 8) + (local.get $b) + (call $btreeInsert) + (local.set 8) + (local.get $c) + (call $btreeInsert) + (local.set 8) + (local.get $d) + (call $btreeInsert) + (local.set 8) + (local.get $e) + (call $btreeInsert) + (local.set 8) + (local.get $f) + (call $btreeInsert) + (local.set 8) + (local.get $h) + (call $btreeInsert) + (local.set 8) + (local.get $i) + (call $btreeInsert) + (local.set 8) + ;; search for variables & check that they were inserted + (local.get 8) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 8) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 8) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 8) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 8) + (local.get $e) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 8) + (local.get $f) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 8) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 8) + (local.get $i) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + ;; delete & check that it was deleted + ;; a + (local.get 8) + (local.get $a) + (call $btreeDelete) + (local.set 8) + (local.get 8) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; b + (local.get 8) + (local.get $b) + (call $btreeDelete) + (local.set 8) + (local.get 8) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; c + (local.get 8) + (local.get $c) + (call $btreeDelete) + (local.set 8) + (local.get 8) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; d + (local.get 8) + (local.get $d) + (call $btreeDelete) + (local.set 8) + (local.get 8) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; e + (local.get 8) + (local.get $e) + (call $btreeDelete) + (local.set 8) + (local.get 8) + (local.get $e) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; f + (local.get 8) + (local.get $f) + (call $btreeDelete) + (local.set 8) + (local.get 8) + (local.get $f) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; h + (local.get 8) + (local.get $h) + (call $btreeDelete) + (local.set 8) + (local.get 8) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (drop)) + (export "main" (func $main))) diff --git a/benchmarks/wasm/btree/6o3u.wat b/benchmarks/wasm/btree/6o3u.wat new file mode 100644 index 00000000..2c0961a2 --- /dev/null +++ b/benchmarks/wasm/btree/6o3u.wat @@ -0,0 +1,3034 @@ +(module + (memory $0 2) + (func $createBtree (param i32) (result i32) ;; createBtree(t), where t: degree of the btree + (i32.const 0) + (local.get 0) + (i32.store) ;; store t at address 0 + (i32.const 0) + (i32.const 1) + (i32.store offset=4) ;; store the number of nodes in the btree = 1 + (i32.const 0) + (i32.const 65536) + (i32.store offset=8) ;; store the root addr + (i32.const 1) ;; Now create root + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then + (i32.const 65536) ;; 64KiB = 65536 bytes + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + (i32.const 65536) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + ) + ) + (i32.const 65536) ;; returns the address of the root + ) + ;; btreeSearch(x, k), where x: node address; k: key to search. Returns address of the key or -1 if key is not in tree. + (func $btreeSearch (param i32) (param i32) (result i32) (local i32) + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n -1 + (i32.le_s) ;; i <= x.children-1 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + (if + (then + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.le_s) ;; i <= x.children - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.eq) + ;; k == x.keys[i] + (if (result i32) + (then + (local.get 0) ;; save the address, which is x+8+i*4 + (i32.const 8) + (i32.add) ;; x+8 + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x+8+i*4 + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + ;; btreeSplitChild(x, i), where x: addr of a non full internal node; i: index such that x.children[i] is a full child of x + (func $btreeSplitChild (param i32) (param i32) (local i32) (local i32) + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then ;; get the number of nodes in the tree + (i32.const 0) + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address is 64KiB*(number of nodes+1) + (local.set 2) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the addr of the child in index i : y + (i32.load) ;; check if x.children[i] is leaf ;; load the first i32 in the child + (i32.const 1) + (i32.eq) ;; is y leaf? + (if + (then ;; yes + (local.get 2) ;; addr of new node z + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + ) + (else ;; no + (local.get 2) ;; addr of new node z + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + ) + ) + (local.get 2) ;; now store number of children, which will be t/2 -1 ;; addr of z + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.store offset=4) ;; store number of children = (t/2)-1 + (i32.const 0) ;; now to store the keys + (local.set 3) ;; counter = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.eq) ;; counter == (t/2)-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 2) ;; addr of z ;; we will store in the new node's address. z.keys[counter] + (i32.const 4) + (local.get 3) ;; counter + (i32.mul) ;; counter * 4 + (i32.add) ;; addr + counter * 4 -> where the key is goint to be located + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 4) ;; now get x.children[i].keys[counter+(t/2)] + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + (t/2) + (i32.mul) ;; counter + (t/2) *4 + (i32.add) ;; y + counter + (t/2) *4 + (i32.load offset=8) ;; y.keys[counter + (t/2)] + (i32.store offset=8) ;; z.keys[counter] = y.keys[counter + (t/2)] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) + (local.set 3) ;; set counter back to 0 + (i32.const 0) ;; check if x.children[i] is leaf + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load) ;; get first i32 in child --> isLeaf + (i32.const 1) + (i32.ne) ;; is leaf? + (if + (then ;; not leaf so we have to update the children + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.eq) ;; counter == (t/2) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; now get z.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; counter*4 + (i32.add) ;; (t-1)*4 + counter*4 + (local.get 2) ;; z + (i32.add) ;; z + (t-1)*4 + counter*4 --> address where addr of child is stored + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) ;; get x.children[i].children[counter+(t/2)] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + t/2 + (i32.const 4) + (i32.mul) ;; counter+t/2 * 4 + (i32.add) ;; (t-1)*4 + (counter+(t/2))*4 + (i32.add) ;; x.children[i] + (t-1)*4 + (counter + (t/2))*4 + (i32.load offset=8) ;; load the address of the child in index counter + t/2 + (i32.store offset=8) ;; z.children[counter] = x.children[i].children[counter] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.store offset=4) ;; number of keys of x.children[i] is now t/2 -1 + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (local.set 3) ;; counter = x.n + (block $loop_break + (loop $loop + (local.get 1) ;; i + (local.get 3) ;; counter + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; get x.children[counter + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (i32.add) ;; (t-1)*4 + (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter + 1)*4 -->address where addr of child is stored + (i32.const 0) ;; get x.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (i32.add) ;; (t-1)*4 + (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter)*4 + (i32.load offset=8) ;; load the addr of the child in index (counter) + (i32.store offset=8) ;; x.children[counter+1] = x.children[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 1) + (i32.add) ;; i + 1 + (i32.const 4) + (i32.mul) ;; (i + 1)*4 + (i32.add) ;; (t-1)*4 + (i + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i + 1)*4 --> addr where we will store the new addr of the child + (local.get 2) ;; addr of z + (i32.store offset=8) ;; x.children[i+1] = z + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (i32.const 1) + (i32.sub) + (local.set 3) ;; counter = x.n -1 + (block $loop_break + (loop $loop + (local.get 1) ;; i + (i32.const 1) + (i32.sub) ;; i-1 + (local.get 3) ;; counter + (i32.eq) ;; counter == i-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 3) ;; get x.keys[counter + 1] ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter + 1)*4 --> addr where we will store the new key + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter)*4 + (i32.load offset=8) ;; load the key in index (counter) + (i32.store offset=8) ;; x.keys[counter+1] = x.keys[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (local.get 1) ;; get x.keys[i] + (i32.const 4) + (i32.mul) ;; i*4 + (local.get 0) ;; x + (i32.add) ;; x +i*4 --> addr of the key + (i32.const 0) ;; get x.children[i].keys[t/2] + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.const 4) + (i32.mul) ;; (t/2)-1 *4 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; (i)*4 + (i32.add) ;; (t-1)*4 + (i)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i)*4 + (i32.load offset=8) ;; load the address of the child in index (i) + (i32.add) ;; y + (t/2)-1 *4 + (i32.load offset=8) ;; load the key in index (t/2)-1 from node x.children[i] + (i32.store offset=8) ;; store the new key in the addr + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) ;; x.n + 1 + (i32.store offset=4) ;; x.n = x.n + 1 + ) + ) + ) + ;; btreeInsertNonFull(x, k), where x: addr of a non full internal node; k: the key to insert + (func $btreeInsertNonFull (param i32) (param i32) (local i32) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n -1 + (local.set 2) ;; i = x.n -1 + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (i32.and) + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i *4 + (i32.add) ;; x + i *4 --> addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.keys[i+1] = x.keys[i] + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 1) ;; k + (i32.store offset=8) ;; x.keys[i+1] = k + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; x.n = x.n +1 + ) + (else ;; x is not leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i >= 0 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i -1 + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 2) ;;i = i+1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address of the child in index i = y + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load offset=4) ;; get y.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; y.n == t-1 --> node is full + (if + (then + (local.get 0) ;; x + (local.get 2) ;; i + (call $btreeSplitChild) + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k>x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i+1 + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (local.get 1) ;; k + (call $btreeInsertNonFull) + ) + ) + ) + (func $btreeInsert (param i32) (result i32) (local i32) (local i32) + (i32.const 0) + (i32.load offset=8) ;; root addr + (local.tee 2) ;; set r + (i32.load offset=4) ;; r.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; r.n == t-1 --> root is full + (if (result i32) + (then + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if (result i32) ;; if we can grow memory + (then + (i32.const 0) ;; get the number of nodes in the tree + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address of s is 64KiB*(number of nodes+1) + (local.set 1) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) + (local.get 1) ;; s + (i32.store offset=8) ;; root addr is now s + (local.get 1) ;; s + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + (local.get 1) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + (i32.const 0) ;; store children addresses + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 1) ;; x + (i32.add) ;; x + (t-1)*4 --> addre where addr of child number 0 is located + (local.get 2) + (i32.store offset=8) ;; s.children[0] = r + (local.get 1) ;; s + (i32.const 0) + (call $btreeSplitChild) + (local.get 1) ;; s + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 1) ;; return the address of the new root + ) + (else + (i32.const -1) ;; return -1 because we could not create a new node + ) + ) + ) + (else + (local.get 2) ;; r + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 2) ;; return the address of the root, which is the same + ) + ) + ) + ;; Returns the address of the new root (if applicable, otherwise returns the addres of the root) + (func $btreeDelete (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local i32) + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (local.get 2) ;; i + (i32.eq) ;; i == x.n + (if + (then + (br $loop_break) ;; break from loop + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + (if + (then + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $while_break + (loop $while + (local.get 3) ;; j + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n-1 + (i32.eq) ;; j == x.n-1 + (if + (then + (br $while_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 --> addr where x.keys[j] is stored + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.mul) ;; j+1 *4 + (i32.add) ;; x + j+1 *4 --> addr where x.keys[j+1] is stored + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $while) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n -1 + (br $loop_break) + ) + ) + ) + ) + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) ;; continue + ) + ) + ) + (else ;; x is not leaf + (i32.const 0) ;;get the index of the appropriate child/key + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (i32.and) ;; i <= x.n - 1 && k > x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i + 1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.lt_s) ;; i < x.n + (if (result i32) + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (if + (then ;; key is present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.tee 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (call $btreeDelete) ;; (x.c[i], x.c[i].keys[x.c[i].n - 1]) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i].keys[x.c[i].n - 1] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i+1] + (local.tee 5) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (local.get 5) ;; x.c[i+1] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (call $btreeDelete) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 --> addr where x.c[i].keys[x.c[i].n] will be stored + (local.get 1) ;; k + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = k + (i32.const 0) ;; now merge x.c[i] with x.c[i+1] + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if ;; j == x.c[i+1].n + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n + j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[index] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x.c[i+1] + i*4 ;; addr of x.c[i+1].keys[j] + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[index] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n +1 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + 1 + x.c[i+1].n + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; now adjust the children + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.get 3) ;; j + (i32.add) ;; index = x.c[i].n+j + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x.c[i] + (t-1)*4 --> address where x.c[i].c[x.c[i].n+j] is stored + (i32.const 0) ;;get x.c[i+1].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x.c[i+1] + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (i32.const 1) + (i32.sub) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) + (i32.mul) ;; j+1*4 + (i32.add) ;; x + j+1*4 ;; addr of x.keys[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = i+1 + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 0) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n-1 + (local.get 5) ;; x.c[i] + (local.get 1) ;; k + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) + (else ;; key not present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) + (i32.const 1) + (i32.sub) ;; t/2 - 1 + (i32.eq) + (if + (then + (i32.const -1) + (local.set 4) ;; changed = -1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.le_s) ;; i+1 <= x.n + (if + (then + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; index = t/2 -1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[t/2-1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[t/2-1] = x.keys[i] + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;;x.c[i].n = x.c[i].n +1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i+1].keys[0] + (i32.load offset=8) ;; x.c[i+1].keys[0] + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + (local.get 5) + (i32.load) ;; x.c[i] is leaf? + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) ;;get x.c[i].c[t/2] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; index = t/2 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[t/2] + (i32.const 0) ;;get x.c[i+1].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[0] + (i32.store offset=8) ;; x.c[i].c[t/2] = x.c[i+1].c[0] + ) + ) + (local.get 5) ;; x.c[i] + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.store offset=4) ;; x.c[i].n = t/2 + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 --> address where x.c[i+1].keys[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j+1] + (i32.store offset=8) ;; x.c[i+1].keys[j] = x.c[i+1].keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i+1].c[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j+1] + (i32.store offset=8) ;; x.c[i+1].c[j] = x.c[i+1].c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr where x.c[i+1] is located + (i32.load offset=8) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i+1].n = x.c[i+1].n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i -1 >= 0 + (i32.and) ;; changed == -1 && i -1 >= 0 + (if + (then + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i-1].n >= t/2 + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.set 3) ;; j = x.c[i].n + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j-1] + (i32.load offset=8) ;; x.c[i].keys[j-1] + (i32.store offset=8) ;; x.c[i].keys[j] = x.c[i].keys[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (local.set 3) ;; j = x.c[i].n + 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i].c[j] is located + (i32.const 0) ;;get x.c[i].c[j-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) + (i32.load offset=8) ;; x.c[i].c[j-1] + (i32.store offset=8) ;; x.c[i].c[j] = x.c[i].c[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; ;; x.c[i].n = x.c[i].n + 1 + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[0] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i-1] + (i32.store offset=8) ;; x.c[i].keys[0] = x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n - 1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[i-1] = x.c[i-1].keys[x.c[i-1].n] + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; get x.c[i].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 ;; addr where x.c[i].c[0] is located + (i32.const 0) ;; get x.c[i-1].c[x.c[i-1].n + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.add) ;; index = x.c[i-1.n + 1] + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1].c[x.c[i-1].n + 1] + (i32.store offset=8) ;; x.c[i].c[0] = x.c[i-1].c[x.c[i-1].n + 1] + ) + ) + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (if ;; changed == -1? + (then + (local.get 2) ;; we have to merge x.c[i] with one sibling;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.le_s) + (if ;; i+1 <= x.n + (then + (local.get 5) ;; merge with right sibling;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = x.keys[i] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n+j] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n+j] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i+1].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[x.c[i].n+j] + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) ;; x.c[i].n + x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + x.c[i+1].n + 1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.set 3) ;; j = i+1 + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) ;; changed == -1 ? + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i - 1 >= 0? + (i32.and) ;; changed == -1 && i - 1 >= 0 + (if + (then + (i32.const 0) ;; merge with left sibling ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n] = x.keys[i-1] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n+j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[j] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n+j] = x.c[i].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i-1].c[x.c[i-1].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i-1].c[x.c[i-1].n+j] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i].c[j] + (i32.store offset=8) ;; x.c[i-1].c[x.c[i-1].n+j] = x.c[i].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.add) ;; x.c[i-1].n + x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n + x.c[i].n + 1 + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = i - 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + ) + ) + ) + ) + ) + ) + (local.get 4) + (i32.const -1) + (i32.eq) + (if ;; changed == -1? if yes, we merged with left sibling + (then + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (local.get 1) + (call $btreeDelete) + (drop) + ) + (else + (local.get 5) ;; x.c[i] + (local.get 1) + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) ;; end of if x is not leaf + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.load offset=4) + (i32.const 0) + (i32.eq) + (if ;; if root is empty + (then + (i32.const 0) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; root.c[0] + (i32.store offset=8) ;; root = root.c[0] + ) + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + ) + (func $main + (param $a i32) + (param $b i32) + (param $c i32) + (param $d i32) + (param $e i32) + (param $f i32) + (param $h i32) + (param $i i32) + (param $j i32) + (local i32) + (local.get $a) + (local.get $b) + (i32.gt_s) + (local.get $b) + (local.get $c) + (i32.gt_s) + (local.get $c) + (local.get $d) + (i32.gt_s) + (local.get $d) + (local.get $e) + (i32.gt_s) + (local.get $e) + (local.get $f) + (i32.gt_s) + (local.get $h) + (local.get $a) + (i32.ne) + (local.get $h) + (local.get $b) + (i32.ne) + (local.get $h) + (local.get $c) + (i32.ne) + (local.get $h) + (local.get $d) + (i32.ne) + (local.get $h) + (local.get $e) + (i32.ne) + (local.get $h) + (local.get $f) + (i32.ne) + (local.get $i) + (local.get $a) + (i32.ne) + (local.get $i) + (local.get $b) + (i32.ne) + (local.get $i) + (local.get $c) + (i32.ne) + (local.get $i) + (local.get $d) + (i32.ne) + (local.get $i) + (local.get $e) + (i32.ne) + (local.get $i) + (local.get $f) + (i32.ne) + (local.get $i) + (local.get $h) + (i32.ne) + (local.get $j) + (local.get $a) + (i32.ne) + (local.get $j) + (local.get $b) + (i32.ne) + (local.get $j) + (local.get $c) + (i32.ne) + (local.get $j) + (local.get $d) + (i32.ne) + (local.get $j) + (local.get $e) + (i32.ne) + (local.get $j) + (local.get $f) + (i32.ne) + (local.get $j) + (local.get $h) + (i32.ne) + (local.get $j) + (local.get $i) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.eqz) + (if (then (unreachable))) + (i32.const 4) ;;create a tree with degree 4 + (call $createBtree) + (local.set 9) + ;; insert variables + (local.get $a) + (call $btreeInsert) + (local.set 9) + (local.get $b) + (call $btreeInsert) + (local.set 9) + (local.get $c) + (call $btreeInsert) + (local.set 9) + (local.get $d) + (call $btreeInsert) + (local.set 9) + (local.get $e) + (call $btreeInsert) + (local.set 9) + (local.get $f) + (call $btreeInsert) + (local.set 9) + (local.get $h) + (call $btreeInsert) + (local.set 9) + (local.get $i) + (call $btreeInsert) + (local.set 9) + (local.get $j) + (call $btreeInsert) + (local.set 9) + ;; search for variables & check that they were inserted + (local.get 9) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 9) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 9) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 9) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 9) + (local.get $e) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 9) + (local.get $f) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 9) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 9) + (local.get $i) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 9) + (local.get $j) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + ;; delete & check that it was deleted + ;; a + (local.get 9) + (local.get $a) + (call $btreeDelete) + (local.set 9) + (local.get 9) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; b + (local.get 9) + (local.get $b) + (call $btreeDelete) + (local.set 9) + (local.get 9) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; c + (local.get 9) + (local.get $c) + (call $btreeDelete) + (local.set 9) + (local.get 9) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; d + (local.get 9) + (local.get $d) + (call $btreeDelete) + (local.set 9) + (local.get 9) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; e + (local.get 9) + (local.get $e) + (call $btreeDelete) + (local.set 9) + (local.get 9) + (local.get $e) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; f + (local.get 9) + (local.get $f) + (call $btreeDelete) + (local.set 9) + (local.get 9) + (local.get $f) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; h + (local.get 9) + (local.get $h) + (call $btreeDelete) + (local.set 9) + (local.get 9) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; i + (local.get 9) + (local.get $i) + (call $btreeDelete) + (local.set 9) + (local.get 9) + (local.get $i) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (drop)) + (export "main" (func $main))) diff --git a/benchmarks/wasm/btree/7o1u.wat b/benchmarks/wasm/btree/7o1u.wat new file mode 100644 index 00000000..578f6da3 --- /dev/null +++ b/benchmarks/wasm/btree/7o1u.wat @@ -0,0 +1,2961 @@ +(module + (memory $0 2) + (func $createBtree (param i32) (result i32) ;; createBtree(t), where t: degree of the btree + (i32.const 0) + (local.get 0) + (i32.store) ;; store t at address 0 + (i32.const 0) + (i32.const 1) + (i32.store offset=4) ;; store the number of nodes in the btree = 1 + (i32.const 0) + (i32.const 65536) + (i32.store offset=8) ;; store the root addr + (i32.const 1) ;; Now create root + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then + (i32.const 65536) ;; 64KiB = 65536 bytes + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + (i32.const 65536) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + ) + ) + (i32.const 65536) ;; returns the address of the root + ) + ;; btreeSearch(x, k), where x: node address; k: key to search. Returns address of the key or -1 if key is not in tree. + (func $btreeSearch (param i32) (param i32) (result i32) (local i32) + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n -1 + (i32.le_s) ;; i <= x.children-1 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + (if + (then + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.le_s) ;; i <= x.children - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.eq) + ;; k == x.keys[i] + (if (result i32) + (then + (local.get 0) ;; save the address, which is x+8+i*4 + (i32.const 8) + (i32.add) ;; x+8 + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x+8+i*4 + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + ;; btreeSplitChild(x, i), where x: addr of a non full internal node; i: index such that x.children[i] is a full child of x + (func $btreeSplitChild (param i32) (param i32) (local i32) (local i32) + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then ;; get the number of nodes in the tree + (i32.const 0) + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address is 64KiB*(number of nodes+1) + (local.set 2) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the addr of the child in index i : y + (i32.load) ;; check if x.children[i] is leaf ;; load the first i32 in the child + (i32.const 1) + (i32.eq) ;; is y leaf? + (if + (then ;; yes + (local.get 2) ;; addr of new node z + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + ) + (else ;; no + (local.get 2) ;; addr of new node z + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + ) + ) + (local.get 2) ;; now store number of children, which will be t/2 -1 ;; addr of z + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.store offset=4) ;; store number of children = (t/2)-1 + (i32.const 0) ;; now to store the keys + (local.set 3) ;; counter = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.eq) ;; counter == (t/2)-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 2) ;; addr of z ;; we will store in the new node's address. z.keys[counter] + (i32.const 4) + (local.get 3) ;; counter + (i32.mul) ;; counter * 4 + (i32.add) ;; addr + counter * 4 -> where the key is goint to be located + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 4) ;; now get x.children[i].keys[counter+(t/2)] + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + (t/2) + (i32.mul) ;; counter + (t/2) *4 + (i32.add) ;; y + counter + (t/2) *4 + (i32.load offset=8) ;; y.keys[counter + (t/2)] + (i32.store offset=8) ;; z.keys[counter] = y.keys[counter + (t/2)] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) + (local.set 3) ;; set counter back to 0 + (i32.const 0) ;; check if x.children[i] is leaf + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load) ;; get first i32 in child --> isLeaf + (i32.const 1) + (i32.ne) ;; is leaf? + (if + (then ;; not leaf so we have to update the children + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.eq) ;; counter == (t/2) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; now get z.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; counter*4 + (i32.add) ;; (t-1)*4 + counter*4 + (local.get 2) ;; z + (i32.add) ;; z + (t-1)*4 + counter*4 --> address where addr of child is stored + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) ;; get x.children[i].children[counter+(t/2)] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + t/2 + (i32.const 4) + (i32.mul) ;; counter+t/2 * 4 + (i32.add) ;; (t-1)*4 + (counter+(t/2))*4 + (i32.add) ;; x.children[i] + (t-1)*4 + (counter + (t/2))*4 + (i32.load offset=8) ;; load the address of the child in index counter + t/2 + (i32.store offset=8) ;; z.children[counter] = x.children[i].children[counter] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.store offset=4) ;; number of keys of x.children[i] is now t/2 -1 + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (local.set 3) ;; counter = x.n + (block $loop_break + (loop $loop + (local.get 1) ;; i + (local.get 3) ;; counter + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; get x.children[counter + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (i32.add) ;; (t-1)*4 + (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter + 1)*4 -->address where addr of child is stored + (i32.const 0) ;; get x.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (i32.add) ;; (t-1)*4 + (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter)*4 + (i32.load offset=8) ;; load the addr of the child in index (counter) + (i32.store offset=8) ;; x.children[counter+1] = x.children[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 1) + (i32.add) ;; i + 1 + (i32.const 4) + (i32.mul) ;; (i + 1)*4 + (i32.add) ;; (t-1)*4 + (i + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i + 1)*4 --> addr where we will store the new addr of the child + (local.get 2) ;; addr of z + (i32.store offset=8) ;; x.children[i+1] = z + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (i32.const 1) + (i32.sub) + (local.set 3) ;; counter = x.n -1 + (block $loop_break + (loop $loop + (local.get 1) ;; i + (i32.const 1) + (i32.sub) ;; i-1 + (local.get 3) ;; counter + (i32.eq) ;; counter == i-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 3) ;; get x.keys[counter + 1] ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter + 1)*4 --> addr where we will store the new key + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter)*4 + (i32.load offset=8) ;; load the key in index (counter) + (i32.store offset=8) ;; x.keys[counter+1] = x.keys[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (local.get 1) ;; get x.keys[i] + (i32.const 4) + (i32.mul) ;; i*4 + (local.get 0) ;; x + (i32.add) ;; x +i*4 --> addr of the key + (i32.const 0) ;; get x.children[i].keys[t/2] + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.const 4) + (i32.mul) ;; (t/2)-1 *4 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; (i)*4 + (i32.add) ;; (t-1)*4 + (i)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i)*4 + (i32.load offset=8) ;; load the address of the child in index (i) + (i32.add) ;; y + (t/2)-1 *4 + (i32.load offset=8) ;; load the key in index (t/2)-1 from node x.children[i] + (i32.store offset=8) ;; store the new key in the addr + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) ;; x.n + 1 + (i32.store offset=4) ;; x.n = x.n + 1 + ) + ) + ) + ;; btreeInsertNonFull(x, k), where x: addr of a non full internal node; k: the key to insert + (func $btreeInsertNonFull (param i32) (param i32) (local i32) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n -1 + (local.set 2) ;; i = x.n -1 + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (i32.and) + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i *4 + (i32.add) ;; x + i *4 --> addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.keys[i+1] = x.keys[i] + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 1) ;; k + (i32.store offset=8) ;; x.keys[i+1] = k + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; x.n = x.n +1 + ) + (else ;; x is not leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i >= 0 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i -1 + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 2) ;;i = i+1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address of the child in index i = y + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load offset=4) ;; get y.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; y.n == t-1 --> node is full + (if + (then + (local.get 0) ;; x + (local.get 2) ;; i + (call $btreeSplitChild) + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k>x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i+1 + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (local.get 1) ;; k + (call $btreeInsertNonFull) + ) + ) + ) + (func $btreeInsert (param i32) (result i32) (local i32) (local i32) + (i32.const 0) + (i32.load offset=8) ;; root addr + (local.tee 2) ;; set r + (i32.load offset=4) ;; r.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; r.n == t-1 --> root is full + (if (result i32) + (then + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if (result i32) ;; if we can grow memory + (then + (i32.const 0) ;; get the number of nodes in the tree + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address of s is 64KiB*(number of nodes+1) + (local.set 1) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) + (local.get 1) ;; s + (i32.store offset=8) ;; root addr is now s + (local.get 1) ;; s + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + (local.get 1) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + (i32.const 0) ;; store children addresses + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 1) ;; x + (i32.add) ;; x + (t-1)*4 --> addre where addr of child number 0 is located + (local.get 2) + (i32.store offset=8) ;; s.children[0] = r + (local.get 1) ;; s + (i32.const 0) + (call $btreeSplitChild) + (local.get 1) ;; s + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 1) ;; return the address of the new root + ) + (else + (i32.const -1) ;; return -1 because we could not create a new node + ) + ) + ) + (else + (local.get 2) ;; r + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 2) ;; return the address of the root, which is the same + ) + ) + ) + ;; Returns the address of the new root (if applicable, otherwise returns the addres of the root) + (func $btreeDelete (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local i32) + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (local.get 2) ;; i + (i32.eq) ;; i == x.n + (if + (then + (br $loop_break) ;; break from loop + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + (if + (then + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $while_break + (loop $while + (local.get 3) ;; j + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n-1 + (i32.eq) ;; j == x.n-1 + (if + (then + (br $while_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 --> addr where x.keys[j] is stored + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.mul) ;; j+1 *4 + (i32.add) ;; x + j+1 *4 --> addr where x.keys[j+1] is stored + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $while) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n -1 + (br $loop_break) + ) + ) + ) + ) + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) ;; continue + ) + ) + ) + (else ;; x is not leaf + (i32.const 0) ;;get the index of the appropriate child/key + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (i32.and) ;; i <= x.n - 1 && k > x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i + 1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.lt_s) ;; i < x.n + (if (result i32) + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (if + (then ;; key is present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.tee 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (call $btreeDelete) ;; (x.c[i], x.c[i].keys[x.c[i].n - 1]) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i].keys[x.c[i].n - 1] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i+1] + (local.tee 5) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (local.get 5) ;; x.c[i+1] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (call $btreeDelete) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 --> addr where x.c[i].keys[x.c[i].n] will be stored + (local.get 1) ;; k + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = k + (i32.const 0) ;; now merge x.c[i] with x.c[i+1] + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if ;; j == x.c[i+1].n + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n + j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[index] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x.c[i+1] + i*4 ;; addr of x.c[i+1].keys[j] + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[index] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n +1 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + 1 + x.c[i+1].n + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; now adjust the children + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.get 3) ;; j + (i32.add) ;; index = x.c[i].n+j + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x.c[i] + (t-1)*4 --> address where x.c[i].c[x.c[i].n+j] is stored + (i32.const 0) ;;get x.c[i+1].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x.c[i+1] + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (i32.const 1) + (i32.sub) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) + (i32.mul) ;; j+1*4 + (i32.add) ;; x + j+1*4 ;; addr of x.keys[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = i+1 + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 0) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n-1 + (local.get 5) ;; x.c[i] + (local.get 1) ;; k + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) + (else ;; key not present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) + (i32.const 1) + (i32.sub) ;; t/2 - 1 + (i32.eq) + (if + (then + (i32.const -1) + (local.set 4) ;; changed = -1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.le_s) ;; i+1 <= x.n + (if + (then + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; index = t/2 -1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[t/2-1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[t/2-1] = x.keys[i] + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;;x.c[i].n = x.c[i].n +1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i+1].keys[0] + (i32.load offset=8) ;; x.c[i+1].keys[0] + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + (local.get 5) + (i32.load) ;; x.c[i] is leaf? + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) ;;get x.c[i].c[t/2] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; index = t/2 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[t/2] + (i32.const 0) ;;get x.c[i+1].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[0] + (i32.store offset=8) ;; x.c[i].c[t/2] = x.c[i+1].c[0] + ) + ) + (local.get 5) ;; x.c[i] + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.store offset=4) ;; x.c[i].n = t/2 + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 --> address where x.c[i+1].keys[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j+1] + (i32.store offset=8) ;; x.c[i+1].keys[j] = x.c[i+1].keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i+1].c[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j+1] + (i32.store offset=8) ;; x.c[i+1].c[j] = x.c[i+1].c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr where x.c[i+1] is located + (i32.load offset=8) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i+1].n = x.c[i+1].n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i -1 >= 0 + (i32.and) ;; changed == -1 && i -1 >= 0 + (if + (then + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i-1].n >= t/2 + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.set 3) ;; j = x.c[i].n + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j-1] + (i32.load offset=8) ;; x.c[i].keys[j-1] + (i32.store offset=8) ;; x.c[i].keys[j] = x.c[i].keys[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (local.set 3) ;; j = x.c[i].n + 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i].c[j] is located + (i32.const 0) ;;get x.c[i].c[j-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) + (i32.load offset=8) ;; x.c[i].c[j-1] + (i32.store offset=8) ;; x.c[i].c[j] = x.c[i].c[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; ;; x.c[i].n = x.c[i].n + 1 + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[0] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i-1] + (i32.store offset=8) ;; x.c[i].keys[0] = x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n - 1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[i-1] = x.c[i-1].keys[x.c[i-1].n] + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; get x.c[i].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 ;; addr where x.c[i].c[0] is located + (i32.const 0) ;; get x.c[i-1].c[x.c[i-1].n + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.add) ;; index = x.c[i-1.n + 1] + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1].c[x.c[i-1].n + 1] + (i32.store offset=8) ;; x.c[i].c[0] = x.c[i-1].c[x.c[i-1].n + 1] + ) + ) + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (if ;; changed == -1? + (then + (local.get 2) ;; we have to merge x.c[i] with one sibling;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.le_s) + (if ;; i+1 <= x.n + (then + (local.get 5) ;; merge with right sibling;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = x.keys[i] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n+j] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n+j] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i+1].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[x.c[i].n+j] + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) ;; x.c[i].n + x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + x.c[i+1].n + 1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.set 3) ;; j = i+1 + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) ;; changed == -1 ? + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i - 1 >= 0? + (i32.and) ;; changed == -1 && i - 1 >= 0 + (if + (then + (i32.const 0) ;; merge with left sibling ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n] = x.keys[i-1] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n+j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[j] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n+j] = x.c[i].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i-1].c[x.c[i-1].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i-1].c[x.c[i-1].n+j] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i].c[j] + (i32.store offset=8) ;; x.c[i-1].c[x.c[i-1].n+j] = x.c[i].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.add) ;; x.c[i-1].n + x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n + x.c[i].n + 1 + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = i - 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + ) + ) + ) + ) + ) + ) + (local.get 4) + (i32.const -1) + (i32.eq) + (if ;; changed == -1? if yes, we merged with left sibling + (then + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (local.get 1) + (call $btreeDelete) + (drop) + ) + (else + (local.get 5) ;; x.c[i] + (local.get 1) + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) ;; end of if x is not leaf + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.load offset=4) + (i32.const 0) + (i32.eq) + (if ;; if root is empty + (then + (i32.const 0) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; root.c[0] + (i32.store offset=8) ;; root = root.c[0] + ) + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + ) + (func $main + (param $a i32) + (param $b i32) + (param $c i32) + (param $d i32) + (param $e i32) + (param $f i32) + (param $g i32) + (param $h i32) + (local i32) + (local.get $a) + (local.get $b) + (i32.gt_s) + (local.get $b) + (local.get $c) + (i32.gt_s) + (local.get $c) + (local.get $d) + (i32.gt_s) + (local.get $d) + (local.get $e) + (i32.gt_s) + (local.get $e) + (local.get $f) + (i32.gt_s) + (local.get $f) + (local.get $g) + (i32.gt_s) + (local.get $h) + (local.get $a) + (i32.ne) + (local.get $h) + (local.get $b) + (i32.ne) + (local.get $h) + (local.get $c) + (i32.ne) + (local.get $h) + (local.get $d) + (i32.ne) + (local.get $h) + (local.get $e) + (i32.ne) + (local.get $h) + (local.get $f) + (i32.ne) + (local.get $h) + (local.get $g) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.eqz) + (if (then (unreachable))) + (i32.const 4) ;;create a tree with degree 4 + (call $createBtree) + (local.set 8) + ;; insert variables + (local.get $a) + (call $btreeInsert) + (local.set 8) + (local.get $b) + (call $btreeInsert) + (local.set 8) + (local.get $c) + (call $btreeInsert) + (local.set 8) + (local.get $d) + (call $btreeInsert) + (local.set 8) + (local.get $e) + (call $btreeInsert) + (local.set 8) + (local.get $f) + (call $btreeInsert) + (local.set 8) + (local.get $g) + (call $btreeInsert) + (local.set 8) + (local.get $h) + (call $btreeInsert) + (local.set 8) + ;; search for variables & check that they were inserted + (local.get 8) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 8) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 8) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 8) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 8) + (local.get $e) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 8) + (local.get $f) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 8) + (local.get $g) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 8) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + ;; delete & check that it was deleted + ;; a + (local.get 8) + (local.get $a) + (call $btreeDelete) + (local.set 8) + (local.get 8) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; b + (local.get 8) + (local.get $b) + (call $btreeDelete) + (local.set 8) + (local.get 8) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; c + (local.get 8) + (local.get $c) + (call $btreeDelete) + (local.set 8) + (local.get 8) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; d + (local.get 8) + (local.get $d) + (call $btreeDelete) + (local.set 8) + (local.get 8) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; e + (local.get 8) + (local.get $e) + (call $btreeDelete) + (local.set 8) + (local.get 8) + (local.get $e) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; f + (local.get 8) + (local.get $f) + (call $btreeDelete) + (local.set 8) + (local.get 8) + (local.get $f) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; g + (local.get 8) + (local.get $g) + (call $btreeDelete) + (local.set 8) + (local.get 8) + (local.get $g) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (drop)) + (export "main" (func $main))) diff --git a/benchmarks/wasm/btree/7o2u.wat b/benchmarks/wasm/btree/7o2u.wat new file mode 100644 index 00000000..e302844a --- /dev/null +++ b/benchmarks/wasm/btree/7o2u.wat @@ -0,0 +1,3014 @@ +(module + (memory $0 2) + (func $createBtree (param i32) (result i32) ;; createBtree(t), where t: degree of the btree + (i32.const 0) + (local.get 0) + (i32.store) ;; store t at address 0 + (i32.const 0) + (i32.const 1) + (i32.store offset=4) ;; store the number of nodes in the btree = 1 + (i32.const 0) + (i32.const 65536) + (i32.store offset=8) ;; store the root addr + (i32.const 1) ;; Now create root + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then + (i32.const 65536) ;; 64KiB = 65536 bytes + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + (i32.const 65536) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + ) + ) + (i32.const 65536) ;; returns the address of the root + ) + ;; btreeSearch(x, k), where x: node address; k: key to search. Returns address of the key or -1 if key is not in tree. + (func $btreeSearch (param i32) (param i32) (result i32) (local i32) + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n -1 + (i32.le_s) ;; i <= x.children-1 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + (if + (then + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.le_s) ;; i <= x.children - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.eq) + ;; k == x.keys[i] + (if (result i32) + (then + (local.get 0) ;; save the address, which is x+8+i*4 + (i32.const 8) + (i32.add) ;; x+8 + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x+8+i*4 + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + ;; btreeSplitChild(x, i), where x: addr of a non full internal node; i: index such that x.children[i] is a full child of x + (func $btreeSplitChild (param i32) (param i32) (local i32) (local i32) + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then ;; get the number of nodes in the tree + (i32.const 0) + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address is 64KiB*(number of nodes+1) + (local.set 2) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the addr of the child in index i : y + (i32.load) ;; check if x.children[i] is leaf ;; load the first i32 in the child + (i32.const 1) + (i32.eq) ;; is y leaf? + (if + (then ;; yes + (local.get 2) ;; addr of new node z + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + ) + (else ;; no + (local.get 2) ;; addr of new node z + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + ) + ) + (local.get 2) ;; now store number of children, which will be t/2 -1 ;; addr of z + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.store offset=4) ;; store number of children = (t/2)-1 + (i32.const 0) ;; now to store the keys + (local.set 3) ;; counter = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.eq) ;; counter == (t/2)-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 2) ;; addr of z ;; we will store in the new node's address. z.keys[counter] + (i32.const 4) + (local.get 3) ;; counter + (i32.mul) ;; counter * 4 + (i32.add) ;; addr + counter * 4 -> where the key is goint to be located + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 4) ;; now get x.children[i].keys[counter+(t/2)] + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + (t/2) + (i32.mul) ;; counter + (t/2) *4 + (i32.add) ;; y + counter + (t/2) *4 + (i32.load offset=8) ;; y.keys[counter + (t/2)] + (i32.store offset=8) ;; z.keys[counter] = y.keys[counter + (t/2)] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) + (local.set 3) ;; set counter back to 0 + (i32.const 0) ;; check if x.children[i] is leaf + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load) ;; get first i32 in child --> isLeaf + (i32.const 1) + (i32.ne) ;; is leaf? + (if + (then ;; not leaf so we have to update the children + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.eq) ;; counter == (t/2) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; now get z.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; counter*4 + (i32.add) ;; (t-1)*4 + counter*4 + (local.get 2) ;; z + (i32.add) ;; z + (t-1)*4 + counter*4 --> address where addr of child is stored + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) ;; get x.children[i].children[counter+(t/2)] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + t/2 + (i32.const 4) + (i32.mul) ;; counter+t/2 * 4 + (i32.add) ;; (t-1)*4 + (counter+(t/2))*4 + (i32.add) ;; x.children[i] + (t-1)*4 + (counter + (t/2))*4 + (i32.load offset=8) ;; load the address of the child in index counter + t/2 + (i32.store offset=8) ;; z.children[counter] = x.children[i].children[counter] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.store offset=4) ;; number of keys of x.children[i] is now t/2 -1 + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (local.set 3) ;; counter = x.n + (block $loop_break + (loop $loop + (local.get 1) ;; i + (local.get 3) ;; counter + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; get x.children[counter + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (i32.add) ;; (t-1)*4 + (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter + 1)*4 -->address where addr of child is stored + (i32.const 0) ;; get x.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (i32.add) ;; (t-1)*4 + (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter)*4 + (i32.load offset=8) ;; load the addr of the child in index (counter) + (i32.store offset=8) ;; x.children[counter+1] = x.children[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 1) + (i32.add) ;; i + 1 + (i32.const 4) + (i32.mul) ;; (i + 1)*4 + (i32.add) ;; (t-1)*4 + (i + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i + 1)*4 --> addr where we will store the new addr of the child + (local.get 2) ;; addr of z + (i32.store offset=8) ;; x.children[i+1] = z + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (i32.const 1) + (i32.sub) + (local.set 3) ;; counter = x.n -1 + (block $loop_break + (loop $loop + (local.get 1) ;; i + (i32.const 1) + (i32.sub) ;; i-1 + (local.get 3) ;; counter + (i32.eq) ;; counter == i-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 3) ;; get x.keys[counter + 1] ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter + 1)*4 --> addr where we will store the new key + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter)*4 + (i32.load offset=8) ;; load the key in index (counter) + (i32.store offset=8) ;; x.keys[counter+1] = x.keys[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (local.get 1) ;; get x.keys[i] + (i32.const 4) + (i32.mul) ;; i*4 + (local.get 0) ;; x + (i32.add) ;; x +i*4 --> addr of the key + (i32.const 0) ;; get x.children[i].keys[t/2] + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.const 4) + (i32.mul) ;; (t/2)-1 *4 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; (i)*4 + (i32.add) ;; (t-1)*4 + (i)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i)*4 + (i32.load offset=8) ;; load the address of the child in index (i) + (i32.add) ;; y + (t/2)-1 *4 + (i32.load offset=8) ;; load the key in index (t/2)-1 from node x.children[i] + (i32.store offset=8) ;; store the new key in the addr + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) ;; x.n + 1 + (i32.store offset=4) ;; x.n = x.n + 1 + ) + ) + ) + ;; btreeInsertNonFull(x, k), where x: addr of a non full internal node; k: the key to insert + (func $btreeInsertNonFull (param i32) (param i32) (local i32) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n -1 + (local.set 2) ;; i = x.n -1 + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (i32.and) + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i *4 + (i32.add) ;; x + i *4 --> addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.keys[i+1] = x.keys[i] + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 1) ;; k + (i32.store offset=8) ;; x.keys[i+1] = k + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; x.n = x.n +1 + ) + (else ;; x is not leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i >= 0 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i -1 + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 2) ;;i = i+1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address of the child in index i = y + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load offset=4) ;; get y.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; y.n == t-1 --> node is full + (if + (then + (local.get 0) ;; x + (local.get 2) ;; i + (call $btreeSplitChild) + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k>x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i+1 + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (local.get 1) ;; k + (call $btreeInsertNonFull) + ) + ) + ) + (func $btreeInsert (param i32) (result i32) (local i32) (local i32) + (i32.const 0) + (i32.load offset=8) ;; root addr + (local.tee 2) ;; set r + (i32.load offset=4) ;; r.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; r.n == t-1 --> root is full + (if (result i32) + (then + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if (result i32) ;; if we can grow memory + (then + (i32.const 0) ;; get the number of nodes in the tree + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address of s is 64KiB*(number of nodes+1) + (local.set 1) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) + (local.get 1) ;; s + (i32.store offset=8) ;; root addr is now s + (local.get 1) ;; s + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + (local.get 1) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + (i32.const 0) ;; store children addresses + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 1) ;; x + (i32.add) ;; x + (t-1)*4 --> addre where addr of child number 0 is located + (local.get 2) + (i32.store offset=8) ;; s.children[0] = r + (local.get 1) ;; s + (i32.const 0) + (call $btreeSplitChild) + (local.get 1) ;; s + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 1) ;; return the address of the new root + ) + (else + (i32.const -1) ;; return -1 because we could not create a new node + ) + ) + ) + (else + (local.get 2) ;; r + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 2) ;; return the address of the root, which is the same + ) + ) + ) + ;; Returns the address of the new root (if applicable, otherwise returns the addres of the root) + (func $btreeDelete (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local i32) + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (local.get 2) ;; i + (i32.eq) ;; i == x.n + (if + (then + (br $loop_break) ;; break from loop + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + (if + (then + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $while_break + (loop $while + (local.get 3) ;; j + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n-1 + (i32.eq) ;; j == x.n-1 + (if + (then + (br $while_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 --> addr where x.keys[j] is stored + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.mul) ;; j+1 *4 + (i32.add) ;; x + j+1 *4 --> addr where x.keys[j+1] is stored + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $while) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n -1 + (br $loop_break) + ) + ) + ) + ) + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) ;; continue + ) + ) + ) + (else ;; x is not leaf + (i32.const 0) ;;get the index of the appropriate child/key + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (i32.and) ;; i <= x.n - 1 && k > x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i + 1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.lt_s) ;; i < x.n + (if (result i32) + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (if + (then ;; key is present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.tee 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (call $btreeDelete) ;; (x.c[i], x.c[i].keys[x.c[i].n - 1]) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i].keys[x.c[i].n - 1] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i+1] + (local.tee 5) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (local.get 5) ;; x.c[i+1] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (call $btreeDelete) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 --> addr where x.c[i].keys[x.c[i].n] will be stored + (local.get 1) ;; k + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = k + (i32.const 0) ;; now merge x.c[i] with x.c[i+1] + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if ;; j == x.c[i+1].n + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n + j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[index] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x.c[i+1] + i*4 ;; addr of x.c[i+1].keys[j] + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[index] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n +1 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + 1 + x.c[i+1].n + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; now adjust the children + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.get 3) ;; j + (i32.add) ;; index = x.c[i].n+j + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x.c[i] + (t-1)*4 --> address where x.c[i].c[x.c[i].n+j] is stored + (i32.const 0) ;;get x.c[i+1].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x.c[i+1] + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (i32.const 1) + (i32.sub) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) + (i32.mul) ;; j+1*4 + (i32.add) ;; x + j+1*4 ;; addr of x.keys[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = i+1 + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 0) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n-1 + (local.get 5) ;; x.c[i] + (local.get 1) ;; k + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) + (else ;; key not present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) + (i32.const 1) + (i32.sub) ;; t/2 - 1 + (i32.eq) + (if + (then + (i32.const -1) + (local.set 4) ;; changed = -1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.le_s) ;; i+1 <= x.n + (if + (then + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; index = t/2 -1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[t/2-1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[t/2-1] = x.keys[i] + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;;x.c[i].n = x.c[i].n +1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i+1].keys[0] + (i32.load offset=8) ;; x.c[i+1].keys[0] + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + (local.get 5) + (i32.load) ;; x.c[i] is leaf? + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) ;;get x.c[i].c[t/2] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; index = t/2 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[t/2] + (i32.const 0) ;;get x.c[i+1].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[0] + (i32.store offset=8) ;; x.c[i].c[t/2] = x.c[i+1].c[0] + ) + ) + (local.get 5) ;; x.c[i] + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.store offset=4) ;; x.c[i].n = t/2 + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 --> address where x.c[i+1].keys[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j+1] + (i32.store offset=8) ;; x.c[i+1].keys[j] = x.c[i+1].keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i+1].c[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j+1] + (i32.store offset=8) ;; x.c[i+1].c[j] = x.c[i+1].c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr where x.c[i+1] is located + (i32.load offset=8) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i+1].n = x.c[i+1].n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i -1 >= 0 + (i32.and) ;; changed == -1 && i -1 >= 0 + (if + (then + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i-1].n >= t/2 + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.set 3) ;; j = x.c[i].n + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j-1] + (i32.load offset=8) ;; x.c[i].keys[j-1] + (i32.store offset=8) ;; x.c[i].keys[j] = x.c[i].keys[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (local.set 3) ;; j = x.c[i].n + 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i].c[j] is located + (i32.const 0) ;;get x.c[i].c[j-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) + (i32.load offset=8) ;; x.c[i].c[j-1] + (i32.store offset=8) ;; x.c[i].c[j] = x.c[i].c[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; ;; x.c[i].n = x.c[i].n + 1 + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[0] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i-1] + (i32.store offset=8) ;; x.c[i].keys[0] = x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n - 1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[i-1] = x.c[i-1].keys[x.c[i-1].n] + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; get x.c[i].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 ;; addr where x.c[i].c[0] is located + (i32.const 0) ;; get x.c[i-1].c[x.c[i-1].n + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.add) ;; index = x.c[i-1.n + 1] + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1].c[x.c[i-1].n + 1] + (i32.store offset=8) ;; x.c[i].c[0] = x.c[i-1].c[x.c[i-1].n + 1] + ) + ) + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (if ;; changed == -1? + (then + (local.get 2) ;; we have to merge x.c[i] with one sibling;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.le_s) + (if ;; i+1 <= x.n + (then + (local.get 5) ;; merge with right sibling;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = x.keys[i] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n+j] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n+j] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i+1].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[x.c[i].n+j] + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) ;; x.c[i].n + x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + x.c[i+1].n + 1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.set 3) ;; j = i+1 + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) ;; changed == -1 ? + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i - 1 >= 0? + (i32.and) ;; changed == -1 && i - 1 >= 0 + (if + (then + (i32.const 0) ;; merge with left sibling ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n] = x.keys[i-1] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n+j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[j] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n+j] = x.c[i].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i-1].c[x.c[i-1].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i-1].c[x.c[i-1].n+j] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i].c[j] + (i32.store offset=8) ;; x.c[i-1].c[x.c[i-1].n+j] = x.c[i].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.add) ;; x.c[i-1].n + x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n + x.c[i].n + 1 + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = i - 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + ) + ) + ) + ) + ) + ) + (local.get 4) + (i32.const -1) + (i32.eq) + (if ;; changed == -1? if yes, we merged with left sibling + (then + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (local.get 1) + (call $btreeDelete) + (drop) + ) + (else + (local.get 5) ;; x.c[i] + (local.get 1) + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) ;; end of if x is not leaf + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.load offset=4) + (i32.const 0) + (i32.eq) + (if ;; if root is empty + (then + (i32.const 0) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; root.c[0] + (i32.store offset=8) ;; root = root.c[0] + ) + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + ) + (func $main + (param $a i32) + (param $b i32) + (param $c i32) + (param $d i32) + (param $e i32) + (param $f i32) + (param $g i32) + (param $h i32) + (param $i i32) + (local i32) + (local.get $a) + (local.get $b) + (i32.gt_s) + (local.get $b) + (local.get $c) + (i32.gt_s) + (local.get $c) + (local.get $d) + (i32.gt_s) + (local.get $d) + (local.get $e) + (i32.gt_s) + (local.get $e) + (local.get $f) + (i32.gt_s) + (local.get $f) + (local.get $g) + (i32.gt_s) + (local.get $h) + (local.get $a) + (i32.ne) + (local.get $h) + (local.get $b) + (i32.ne) + (local.get $h) + (local.get $c) + (i32.ne) + (local.get $h) + (local.get $d) + (i32.ne) + (local.get $h) + (local.get $e) + (i32.ne) + (local.get $h) + (local.get $f) + (i32.ne) + (local.get $h) + (local.get $g) + (i32.ne) + (local.get $i) + (local.get $a) + (i32.ne) + (local.get $i) + (local.get $b) + (i32.ne) + (local.get $i) + (local.get $c) + (i32.ne) + (local.get $i) + (local.get $d) + (i32.ne) + (local.get $i) + (local.get $e) + (i32.ne) + (local.get $i) + (local.get $f) + (i32.ne) + (local.get $i) + (local.get $g) + (i32.ne) + (local.get $i) + (local.get $h) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.eqz) + (if (then (unreachable))) + (i32.const 4) ;;create a tree with degree 4 + (call $createBtree) + (local.set 9) + ;; insert variables + (local.get $a) + (call $btreeInsert) + (local.set 9) + (local.get $b) + (call $btreeInsert) + (local.set 9) + (local.get $c) + (call $btreeInsert) + (local.set 9) + (local.get $d) + (call $btreeInsert) + (local.set 9) + (local.get $e) + (call $btreeInsert) + (local.set 9) + (local.get $f) + (call $btreeInsert) + (local.set 9) + (local.get $g) + (call $btreeInsert) + (local.set 9) + (local.get $h) + (call $btreeInsert) + (local.set 9) + (local.get $i) + (call $btreeInsert) + (local.set 9) + ;; search for variables & check that they were inserted + (local.get 9) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 9) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 9) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 9) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 9) + (local.get $e) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 9) + (local.get $f) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 9) + (local.get $g) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 9) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 9) + (local.get $i) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + ;; delete & check that it was deleted + ;; a + (local.get 9) + (local.get $a) + (call $btreeDelete) + (local.set 9) + (local.get 9) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; b + (local.get 9) + (local.get $b) + (call $btreeDelete) + (local.set 9) + (local.get 9) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; c + (local.get 9) + (local.get $c) + (call $btreeDelete) + (local.set 9) + (local.get 9) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; d + (local.get 9) + (local.get $d) + (call $btreeDelete) + (local.set 9) + (local.get 9) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; e + (local.get 9) + (local.get $e) + (call $btreeDelete) + (local.set 9) + (local.get 9) + (local.get $e) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; f + (local.get 9) + (local.get $f) + (call $btreeDelete) + (local.set 9) + (local.get 9) + (local.get $f) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; g + (local.get 9) + (local.get $g) + (call $btreeDelete) + (local.set 9) + (local.get 9) + (local.get $g) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; h + (local.get 9) + (local.get $h) + (call $btreeDelete) + (local.set 9) + (local.get 9) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (drop)) + (export "main" (func $main))) diff --git a/benchmarks/wasm/btree/7o3u.wat b/benchmarks/wasm/btree/7o3u.wat new file mode 100644 index 00000000..2903fbfc --- /dev/null +++ b/benchmarks/wasm/btree/7o3u.wat @@ -0,0 +1,3071 @@ +(module + (memory $0 2) + (func $createBtree (param i32) (result i32) ;; createBtree(t), where t: degree of the btree + (i32.const 0) + (local.get 0) + (i32.store) ;; store t at address 0 + (i32.const 0) + (i32.const 1) + (i32.store offset=4) ;; store the number of nodes in the btree = 1 + (i32.const 0) + (i32.const 65536) + (i32.store offset=8) ;; store the root addr + (i32.const 1) ;; Now create root + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then + (i32.const 65536) ;; 64KiB = 65536 bytes + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + (i32.const 65536) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + ) + ) + (i32.const 65536) ;; returns the address of the root + ) + ;; btreeSearch(x, k), where x: node address; k: key to search. Returns address of the key or -1 if key is not in tree. + (func $btreeSearch (param i32) (param i32) (result i32) (local i32) + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n -1 + (i32.le_s) ;; i <= x.children-1 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + (if + (then + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.le_s) ;; i <= x.children - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.eq) + ;; k == x.keys[i] + (if (result i32) + (then + (local.get 0) ;; save the address, which is x+8+i*4 + (i32.const 8) + (i32.add) ;; x+8 + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x+8+i*4 + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + ;; btreeSplitChild(x, i), where x: addr of a non full internal node; i: index such that x.children[i] is a full child of x + (func $btreeSplitChild (param i32) (param i32) (local i32) (local i32) + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then ;; get the number of nodes in the tree + (i32.const 0) + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address is 64KiB*(number of nodes+1) + (local.set 2) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the addr of the child in index i : y + (i32.load) ;; check if x.children[i] is leaf ;; load the first i32 in the child + (i32.const 1) + (i32.eq) ;; is y leaf? + (if + (then ;; yes + (local.get 2) ;; addr of new node z + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + ) + (else ;; no + (local.get 2) ;; addr of new node z + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + ) + ) + (local.get 2) ;; now store number of children, which will be t/2 -1 ;; addr of z + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.store offset=4) ;; store number of children = (t/2)-1 + (i32.const 0) ;; now to store the keys + (local.set 3) ;; counter = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.eq) ;; counter == (t/2)-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 2) ;; addr of z ;; we will store in the new node's address. z.keys[counter] + (i32.const 4) + (local.get 3) ;; counter + (i32.mul) ;; counter * 4 + (i32.add) ;; addr + counter * 4 -> where the key is goint to be located + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 4) ;; now get x.children[i].keys[counter+(t/2)] + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + (t/2) + (i32.mul) ;; counter + (t/2) *4 + (i32.add) ;; y + counter + (t/2) *4 + (i32.load offset=8) ;; y.keys[counter + (t/2)] + (i32.store offset=8) ;; z.keys[counter] = y.keys[counter + (t/2)] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) + (local.set 3) ;; set counter back to 0 + (i32.const 0) ;; check if x.children[i] is leaf + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load) ;; get first i32 in child --> isLeaf + (i32.const 1) + (i32.ne) ;; is leaf? + (if + (then ;; not leaf so we have to update the children + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.eq) ;; counter == (t/2) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; now get z.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; counter*4 + (i32.add) ;; (t-1)*4 + counter*4 + (local.get 2) ;; z + (i32.add) ;; z + (t-1)*4 + counter*4 --> address where addr of child is stored + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) ;; get x.children[i].children[counter+(t/2)] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + t/2 + (i32.const 4) + (i32.mul) ;; counter+t/2 * 4 + (i32.add) ;; (t-1)*4 + (counter+(t/2))*4 + (i32.add) ;; x.children[i] + (t-1)*4 + (counter + (t/2))*4 + (i32.load offset=8) ;; load the address of the child in index counter + t/2 + (i32.store offset=8) ;; z.children[counter] = x.children[i].children[counter] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.store offset=4) ;; number of keys of x.children[i] is now t/2 -1 + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (local.set 3) ;; counter = x.n + (block $loop_break + (loop $loop + (local.get 1) ;; i + (local.get 3) ;; counter + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; get x.children[counter + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (i32.add) ;; (t-1)*4 + (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter + 1)*4 -->address where addr of child is stored + (i32.const 0) ;; get x.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (i32.add) ;; (t-1)*4 + (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter)*4 + (i32.load offset=8) ;; load the addr of the child in index (counter) + (i32.store offset=8) ;; x.children[counter+1] = x.children[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 1) + (i32.add) ;; i + 1 + (i32.const 4) + (i32.mul) ;; (i + 1)*4 + (i32.add) ;; (t-1)*4 + (i + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i + 1)*4 --> addr where we will store the new addr of the child + (local.get 2) ;; addr of z + (i32.store offset=8) ;; x.children[i+1] = z + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (i32.const 1) + (i32.sub) + (local.set 3) ;; counter = x.n -1 + (block $loop_break + (loop $loop + (local.get 1) ;; i + (i32.const 1) + (i32.sub) ;; i-1 + (local.get 3) ;; counter + (i32.eq) ;; counter == i-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 3) ;; get x.keys[counter + 1] ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter + 1)*4 --> addr where we will store the new key + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter)*4 + (i32.load offset=8) ;; load the key in index (counter) + (i32.store offset=8) ;; x.keys[counter+1] = x.keys[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (local.get 1) ;; get x.keys[i] + (i32.const 4) + (i32.mul) ;; i*4 + (local.get 0) ;; x + (i32.add) ;; x +i*4 --> addr of the key + (i32.const 0) ;; get x.children[i].keys[t/2] + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.const 4) + (i32.mul) ;; (t/2)-1 *4 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; (i)*4 + (i32.add) ;; (t-1)*4 + (i)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i)*4 + (i32.load offset=8) ;; load the address of the child in index (i) + (i32.add) ;; y + (t/2)-1 *4 + (i32.load offset=8) ;; load the key in index (t/2)-1 from node x.children[i] + (i32.store offset=8) ;; store the new key in the addr + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) ;; x.n + 1 + (i32.store offset=4) ;; x.n = x.n + 1 + ) + ) + ) + ;; btreeInsertNonFull(x, k), where x: addr of a non full internal node; k: the key to insert + (func $btreeInsertNonFull (param i32) (param i32) (local i32) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n -1 + (local.set 2) ;; i = x.n -1 + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (i32.and) + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i *4 + (i32.add) ;; x + i *4 --> addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.keys[i+1] = x.keys[i] + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 1) ;; k + (i32.store offset=8) ;; x.keys[i+1] = k + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; x.n = x.n +1 + ) + (else ;; x is not leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i >= 0 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i -1 + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 2) ;;i = i+1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address of the child in index i = y + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load offset=4) ;; get y.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; y.n == t-1 --> node is full + (if + (then + (local.get 0) ;; x + (local.get 2) ;; i + (call $btreeSplitChild) + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k>x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i+1 + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (local.get 1) ;; k + (call $btreeInsertNonFull) + ) + ) + ) + (func $btreeInsert (param i32) (result i32) (local i32) (local i32) + (i32.const 0) + (i32.load offset=8) ;; root addr + (local.tee 2) ;; set r + (i32.load offset=4) ;; r.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; r.n == t-1 --> root is full + (if (result i32) + (then + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if (result i32) ;; if we can grow memory + (then + (i32.const 0) ;; get the number of nodes in the tree + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address of s is 64KiB*(number of nodes+1) + (local.set 1) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) + (local.get 1) ;; s + (i32.store offset=8) ;; root addr is now s + (local.get 1) ;; s + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + (local.get 1) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + (i32.const 0) ;; store children addresses + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 1) ;; x + (i32.add) ;; x + (t-1)*4 --> addre where addr of child number 0 is located + (local.get 2) + (i32.store offset=8) ;; s.children[0] = r + (local.get 1) ;; s + (i32.const 0) + (call $btreeSplitChild) + (local.get 1) ;; s + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 1) ;; return the address of the new root + ) + (else + (i32.const -1) ;; return -1 because we could not create a new node + ) + ) + ) + (else + (local.get 2) ;; r + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 2) ;; return the address of the root, which is the same + ) + ) + ) + ;; Returns the address of the new root (if applicable, otherwise returns the addres of the root) + (func $btreeDelete (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local i32) + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (local.get 2) ;; i + (i32.eq) ;; i == x.n + (if + (then + (br $loop_break) ;; break from loop + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + (if + (then + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $while_break + (loop $while + (local.get 3) ;; j + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n-1 + (i32.eq) ;; j == x.n-1 + (if + (then + (br $while_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 --> addr where x.keys[j] is stored + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.mul) ;; j+1 *4 + (i32.add) ;; x + j+1 *4 --> addr where x.keys[j+1] is stored + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $while) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n -1 + (br $loop_break) + ) + ) + ) + ) + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) ;; continue + ) + ) + ) + (else ;; x is not leaf + (i32.const 0) ;;get the index of the appropriate child/key + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (i32.and) ;; i <= x.n - 1 && k > x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i + 1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.lt_s) ;; i < x.n + (if (result i32) + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (if + (then ;; key is present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.tee 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (call $btreeDelete) ;; (x.c[i], x.c[i].keys[x.c[i].n - 1]) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i].keys[x.c[i].n - 1] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i+1] + (local.tee 5) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (local.get 5) ;; x.c[i+1] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (call $btreeDelete) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 --> addr where x.c[i].keys[x.c[i].n] will be stored + (local.get 1) ;; k + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = k + (i32.const 0) ;; now merge x.c[i] with x.c[i+1] + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if ;; j == x.c[i+1].n + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n + j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[index] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x.c[i+1] + i*4 ;; addr of x.c[i+1].keys[j] + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[index] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n +1 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + 1 + x.c[i+1].n + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; now adjust the children + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.get 3) ;; j + (i32.add) ;; index = x.c[i].n+j + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x.c[i] + (t-1)*4 --> address where x.c[i].c[x.c[i].n+j] is stored + (i32.const 0) ;;get x.c[i+1].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x.c[i+1] + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (i32.const 1) + (i32.sub) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) + (i32.mul) ;; j+1*4 + (i32.add) ;; x + j+1*4 ;; addr of x.keys[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = i+1 + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 0) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n-1 + (local.get 5) ;; x.c[i] + (local.get 1) ;; k + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) + (else ;; key not present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) + (i32.const 1) + (i32.sub) ;; t/2 - 1 + (i32.eq) + (if + (then + (i32.const -1) + (local.set 4) ;; changed = -1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.le_s) ;; i+1 <= x.n + (if + (then + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; index = t/2 -1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[t/2-1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[t/2-1] = x.keys[i] + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;;x.c[i].n = x.c[i].n +1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i+1].keys[0] + (i32.load offset=8) ;; x.c[i+1].keys[0] + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + (local.get 5) + (i32.load) ;; x.c[i] is leaf? + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) ;;get x.c[i].c[t/2] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; index = t/2 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[t/2] + (i32.const 0) ;;get x.c[i+1].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[0] + (i32.store offset=8) ;; x.c[i].c[t/2] = x.c[i+1].c[0] + ) + ) + (local.get 5) ;; x.c[i] + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.store offset=4) ;; x.c[i].n = t/2 + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 --> address where x.c[i+1].keys[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j+1] + (i32.store offset=8) ;; x.c[i+1].keys[j] = x.c[i+1].keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i+1].c[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j+1] + (i32.store offset=8) ;; x.c[i+1].c[j] = x.c[i+1].c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr where x.c[i+1] is located + (i32.load offset=8) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i+1].n = x.c[i+1].n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i -1 >= 0 + (i32.and) ;; changed == -1 && i -1 >= 0 + (if + (then + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i-1].n >= t/2 + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.set 3) ;; j = x.c[i].n + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j-1] + (i32.load offset=8) ;; x.c[i].keys[j-1] + (i32.store offset=8) ;; x.c[i].keys[j] = x.c[i].keys[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (local.set 3) ;; j = x.c[i].n + 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i].c[j] is located + (i32.const 0) ;;get x.c[i].c[j-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) + (i32.load offset=8) ;; x.c[i].c[j-1] + (i32.store offset=8) ;; x.c[i].c[j] = x.c[i].c[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; ;; x.c[i].n = x.c[i].n + 1 + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[0] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i-1] + (i32.store offset=8) ;; x.c[i].keys[0] = x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n - 1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[i-1] = x.c[i-1].keys[x.c[i-1].n] + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; get x.c[i].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 ;; addr where x.c[i].c[0] is located + (i32.const 0) ;; get x.c[i-1].c[x.c[i-1].n + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.add) ;; index = x.c[i-1.n + 1] + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1].c[x.c[i-1].n + 1] + (i32.store offset=8) ;; x.c[i].c[0] = x.c[i-1].c[x.c[i-1].n + 1] + ) + ) + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (if ;; changed == -1? + (then + (local.get 2) ;; we have to merge x.c[i] with one sibling;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.le_s) + (if ;; i+1 <= x.n + (then + (local.get 5) ;; merge with right sibling;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = x.keys[i] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n+j] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n+j] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i+1].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[x.c[i].n+j] + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) ;; x.c[i].n + x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + x.c[i+1].n + 1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.set 3) ;; j = i+1 + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) ;; changed == -1 ? + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i - 1 >= 0? + (i32.and) ;; changed == -1 && i - 1 >= 0 + (if + (then + (i32.const 0) ;; merge with left sibling ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n] = x.keys[i-1] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n+j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[j] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n+j] = x.c[i].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i-1].c[x.c[i-1].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i-1].c[x.c[i-1].n+j] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i].c[j] + (i32.store offset=8) ;; x.c[i-1].c[x.c[i-1].n+j] = x.c[i].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.add) ;; x.c[i-1].n + x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n + x.c[i].n + 1 + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = i - 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + ) + ) + ) + ) + ) + ) + (local.get 4) + (i32.const -1) + (i32.eq) + (if ;; changed == -1? if yes, we merged with left sibling + (then + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (local.get 1) + (call $btreeDelete) + (drop) + ) + (else + (local.get 5) ;; x.c[i] + (local.get 1) + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) ;; end of if x is not leaf + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.load offset=4) + (i32.const 0) + (i32.eq) + (if ;; if root is empty + (then + (i32.const 0) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; root.c[0] + (i32.store offset=8) ;; root = root.c[0] + ) + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + ) + (func $main + (param $a i32) + (param $b i32) + (param $c i32) + (param $d i32) + (param $e i32) + (param $f i32) + (param $g i32) + (param $h i32) + (param $i i32) + (param $j i32) + (local i32) + (local.get $a) + (local.get $b) + (i32.gt_s) + (local.get $b) + (local.get $c) + (i32.gt_s) + (local.get $c) + (local.get $d) + (i32.gt_s) + (local.get $d) + (local.get $e) + (i32.gt_s) + (local.get $e) + (local.get $f) + (i32.gt_s) + (local.get $f) + (local.get $g) + (i32.gt_s) + (local.get $h) + (local.get $a) + (i32.ne) + (local.get $h) + (local.get $b) + (i32.ne) + (local.get $h) + (local.get $c) + (i32.ne) + (local.get $h) + (local.get $d) + (i32.ne) + (local.get $h) + (local.get $e) + (i32.ne) + (local.get $h) + (local.get $f) + (i32.ne) + (local.get $h) + (local.get $g) + (i32.ne) + (local.get $i) + (local.get $a) + (i32.ne) + (local.get $i) + (local.get $b) + (i32.ne) + (local.get $i) + (local.get $c) + (i32.ne) + (local.get $i) + (local.get $d) + (i32.ne) + (local.get $i) + (local.get $e) + (i32.ne) + (local.get $i) + (local.get $f) + (i32.ne) + (local.get $i) + (local.get $g) + (i32.ne) + (local.get $i) + (local.get $h) + (i32.ne) + (local.get $j) + (local.get $a) + (i32.ne) + (local.get $j) + (local.get $b) + (i32.ne) + (local.get $j) + (local.get $c) + (i32.ne) + (local.get $j) + (local.get $d) + (i32.ne) + (local.get $j) + (local.get $e) + (i32.ne) + (local.get $j) + (local.get $f) + (i32.ne) + (local.get $j) + (local.get $g) + (i32.ne) + (local.get $j) + (local.get $h) + (i32.ne) + (local.get $j) + (local.get $i) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.eqz) + (if (then (unreachable))) + (i32.const 4) ;;create a tree with degree 4 + (call $createBtree) + (local.set 10) + ;; insert variables + (local.get $a) + (call $btreeInsert) + (local.set 10) + (local.get $b) + (call $btreeInsert) + (local.set 10) + (local.get $c) + (call $btreeInsert) + (local.set 10) + (local.get $d) + (call $btreeInsert) + (local.set 10) + (local.get $e) + (call $btreeInsert) + (local.set 10) + (local.get $f) + (call $btreeInsert) + (local.set 10) + (local.get $g) + (call $btreeInsert) + (local.set 10) + (local.get $h) + (call $btreeInsert) + (local.set 10) + (local.get $i) + (call $btreeInsert) + (local.set 10) + (local.get $j) + (call $btreeInsert) + (local.set 10) + ;; search for variables & check that they were inserted + (local.get 10) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 10) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 10) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 10) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 10) + (local.get $e) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 10) + (local.get $f) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 10) + (local.get $g) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 10) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 10) + (local.get $i) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 10) + (local.get $j) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + ;; delete & check that it was deleted + ;; a + (local.get 10) + (local.get $a) + (call $btreeDelete) + (local.set 10) + (local.get 10) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; b + (local.get 10) + (local.get $b) + (call $btreeDelete) + (local.set 10) + (local.get 10) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; c + (local.get 10) + (local.get $c) + (call $btreeDelete) + (local.set 10) + (local.get 10) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; d + (local.get 10) + (local.get $d) + (call $btreeDelete) + (local.set 10) + (local.get 10) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; e + (local.get 10) + (local.get $e) + (call $btreeDelete) + (local.set 10) + (local.get 10) + (local.get $e) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; f + (local.get 10) + (local.get $f) + (call $btreeDelete) + (local.set 10) + (local.get 10) + (local.get $f) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; g + (local.get 10) + (local.get $g) + (call $btreeDelete) + (local.set 10) + (local.get 10) + (local.get $g) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; h + (local.get 10) + (local.get $h) + (call $btreeDelete) + (local.set 10) + (local.get 10) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; i + (local.get 10) + (local.get $i) + (call $btreeDelete) + (local.set 10) + (local.get 10) + (local.get $i) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (drop)) + (export "main" (func $main))) diff --git a/benchmarks/wasm/btree/8o1u.wat b/benchmarks/wasm/btree/8o1u.wat new file mode 100644 index 00000000..c23f550d --- /dev/null +++ b/benchmarks/wasm/btree/8o1u.wat @@ -0,0 +1,2990 @@ +(module + (memory $0 2) + (func $createBtree (param i32) (result i32) ;; createBtree(t), where t: degree of the btree + (i32.const 0) + (local.get 0) + (i32.store) ;; store t at address 0 + (i32.const 0) + (i32.const 1) + (i32.store offset=4) ;; store the number of nodes in the btree = 1 + (i32.const 0) + (i32.const 65536) + (i32.store offset=8) ;; store the root addr + (i32.const 1) ;; Now create root + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then + (i32.const 65536) ;; 64KiB = 65536 bytes + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + (i32.const 65536) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + ) + ) + (i32.const 65536) ;; returns the address of the root + ) + ;; btreeSearch(x, k), where x: node address; k: key to search. Returns address of the key or -1 if key is not in tree. + (func $btreeSearch (param i32) (param i32) (result i32) (local i32) + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n -1 + (i32.le_s) ;; i <= x.children-1 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + (if + (then + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.le_s) ;; i <= x.children - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.eq) + ;; k == x.keys[i] + (if (result i32) + (then + (local.get 0) ;; save the address, which is x+8+i*4 + (i32.const 8) + (i32.add) ;; x+8 + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x+8+i*4 + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + ;; btreeSplitChild(x, i), where x: addr of a non full internal node; i: index such that x.children[i] is a full child of x + (func $btreeSplitChild (param i32) (param i32) (local i32) (local i32) + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then ;; get the number of nodes in the tree + (i32.const 0) + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address is 64KiB*(number of nodes+1) + (local.set 2) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the addr of the child in index i : y + (i32.load) ;; check if x.children[i] is leaf ;; load the first i32 in the child + (i32.const 1) + (i32.eq) ;; is y leaf? + (if + (then ;; yes + (local.get 2) ;; addr of new node z + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + ) + (else ;; no + (local.get 2) ;; addr of new node z + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + ) + ) + (local.get 2) ;; now store number of children, which will be t/2 -1 ;; addr of z + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.store offset=4) ;; store number of children = (t/2)-1 + (i32.const 0) ;; now to store the keys + (local.set 3) ;; counter = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.eq) ;; counter == (t/2)-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 2) ;; addr of z ;; we will store in the new node's address. z.keys[counter] + (i32.const 4) + (local.get 3) ;; counter + (i32.mul) ;; counter * 4 + (i32.add) ;; addr + counter * 4 -> where the key is goint to be located + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 4) ;; now get x.children[i].keys[counter+(t/2)] + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + (t/2) + (i32.mul) ;; counter + (t/2) *4 + (i32.add) ;; y + counter + (t/2) *4 + (i32.load offset=8) ;; y.keys[counter + (t/2)] + (i32.store offset=8) ;; z.keys[counter] = y.keys[counter + (t/2)] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) + (local.set 3) ;; set counter back to 0 + (i32.const 0) ;; check if x.children[i] is leaf + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load) ;; get first i32 in child --> isLeaf + (i32.const 1) + (i32.ne) ;; is leaf? + (if + (then ;; not leaf so we have to update the children + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.eq) ;; counter == (t/2) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; now get z.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; counter*4 + (i32.add) ;; (t-1)*4 + counter*4 + (local.get 2) ;; z + (i32.add) ;; z + (t-1)*4 + counter*4 --> address where addr of child is stored + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) ;; get x.children[i].children[counter+(t/2)] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + t/2 + (i32.const 4) + (i32.mul) ;; counter+t/2 * 4 + (i32.add) ;; (t-1)*4 + (counter+(t/2))*4 + (i32.add) ;; x.children[i] + (t-1)*4 + (counter + (t/2))*4 + (i32.load offset=8) ;; load the address of the child in index counter + t/2 + (i32.store offset=8) ;; z.children[counter] = x.children[i].children[counter] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.store offset=4) ;; number of keys of x.children[i] is now t/2 -1 + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (local.set 3) ;; counter = x.n + (block $loop_break + (loop $loop + (local.get 1) ;; i + (local.get 3) ;; counter + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; get x.children[counter + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (i32.add) ;; (t-1)*4 + (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter + 1)*4 -->address where addr of child is stored + (i32.const 0) ;; get x.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (i32.add) ;; (t-1)*4 + (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter)*4 + (i32.load offset=8) ;; load the addr of the child in index (counter) + (i32.store offset=8) ;; x.children[counter+1] = x.children[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 1) + (i32.add) ;; i + 1 + (i32.const 4) + (i32.mul) ;; (i + 1)*4 + (i32.add) ;; (t-1)*4 + (i + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i + 1)*4 --> addr where we will store the new addr of the child + (local.get 2) ;; addr of z + (i32.store offset=8) ;; x.children[i+1] = z + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (i32.const 1) + (i32.sub) + (local.set 3) ;; counter = x.n -1 + (block $loop_break + (loop $loop + (local.get 1) ;; i + (i32.const 1) + (i32.sub) ;; i-1 + (local.get 3) ;; counter + (i32.eq) ;; counter == i-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 3) ;; get x.keys[counter + 1] ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter + 1)*4 --> addr where we will store the new key + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter)*4 + (i32.load offset=8) ;; load the key in index (counter) + (i32.store offset=8) ;; x.keys[counter+1] = x.keys[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (local.get 1) ;; get x.keys[i] + (i32.const 4) + (i32.mul) ;; i*4 + (local.get 0) ;; x + (i32.add) ;; x +i*4 --> addr of the key + (i32.const 0) ;; get x.children[i].keys[t/2] + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.const 4) + (i32.mul) ;; (t/2)-1 *4 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; (i)*4 + (i32.add) ;; (t-1)*4 + (i)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i)*4 + (i32.load offset=8) ;; load the address of the child in index (i) + (i32.add) ;; y + (t/2)-1 *4 + (i32.load offset=8) ;; load the key in index (t/2)-1 from node x.children[i] + (i32.store offset=8) ;; store the new key in the addr + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) ;; x.n + 1 + (i32.store offset=4) ;; x.n = x.n + 1 + ) + ) + ) + ;; btreeInsertNonFull(x, k), where x: addr of a non full internal node; k: the key to insert + (func $btreeInsertNonFull (param i32) (param i32) (local i32) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n -1 + (local.set 2) ;; i = x.n -1 + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (i32.and) + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i *4 + (i32.add) ;; x + i *4 --> addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.keys[i+1] = x.keys[i] + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 1) ;; k + (i32.store offset=8) ;; x.keys[i+1] = k + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; x.n = x.n +1 + ) + (else ;; x is not leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i >= 0 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i -1 + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 2) ;;i = i+1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address of the child in index i = y + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load offset=4) ;; get y.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; y.n == t-1 --> node is full + (if + (then + (local.get 0) ;; x + (local.get 2) ;; i + (call $btreeSplitChild) + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k>x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i+1 + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (local.get 1) ;; k + (call $btreeInsertNonFull) + ) + ) + ) + (func $btreeInsert (param i32) (result i32) (local i32) (local i32) + (i32.const 0) + (i32.load offset=8) ;; root addr + (local.tee 2) ;; set r + (i32.load offset=4) ;; r.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; r.n == t-1 --> root is full + (if (result i32) + (then + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if (result i32) ;; if we can grow memory + (then + (i32.const 0) ;; get the number of nodes in the tree + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address of s is 64KiB*(number of nodes+1) + (local.set 1) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) + (local.get 1) ;; s + (i32.store offset=8) ;; root addr is now s + (local.get 1) ;; s + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + (local.get 1) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + (i32.const 0) ;; store children addresses + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 1) ;; x + (i32.add) ;; x + (t-1)*4 --> addre where addr of child number 0 is located + (local.get 2) + (i32.store offset=8) ;; s.children[0] = r + (local.get 1) ;; s + (i32.const 0) + (call $btreeSplitChild) + (local.get 1) ;; s + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 1) ;; return the address of the new root + ) + (else + (i32.const -1) ;; return -1 because we could not create a new node + ) + ) + ) + (else + (local.get 2) ;; r + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 2) ;; return the address of the root, which is the same + ) + ) + ) + ;; Returns the address of the new root (if applicable, otherwise returns the addres of the root) + (func $btreeDelete (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local i32) + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (local.get 2) ;; i + (i32.eq) ;; i == x.n + (if + (then + (br $loop_break) ;; break from loop + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + (if + (then + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $while_break + (loop $while + (local.get 3) ;; j + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n-1 + (i32.eq) ;; j == x.n-1 + (if + (then + (br $while_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 --> addr where x.keys[j] is stored + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.mul) ;; j+1 *4 + (i32.add) ;; x + j+1 *4 --> addr where x.keys[j+1] is stored + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $while) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n -1 + (br $loop_break) + ) + ) + ) + ) + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) ;; continue + ) + ) + ) + (else ;; x is not leaf + (i32.const 0) ;;get the index of the appropriate child/key + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (i32.and) ;; i <= x.n - 1 && k > x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i + 1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.lt_s) ;; i < x.n + (if (result i32) + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (if + (then ;; key is present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.tee 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (call $btreeDelete) ;; (x.c[i], x.c[i].keys[x.c[i].n - 1]) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i].keys[x.c[i].n - 1] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i+1] + (local.tee 5) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (local.get 5) ;; x.c[i+1] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (call $btreeDelete) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 --> addr where x.c[i].keys[x.c[i].n] will be stored + (local.get 1) ;; k + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = k + (i32.const 0) ;; now merge x.c[i] with x.c[i+1] + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if ;; j == x.c[i+1].n + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n + j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[index] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x.c[i+1] + i*4 ;; addr of x.c[i+1].keys[j] + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[index] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n +1 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + 1 + x.c[i+1].n + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; now adjust the children + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.get 3) ;; j + (i32.add) ;; index = x.c[i].n+j + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x.c[i] + (t-1)*4 --> address where x.c[i].c[x.c[i].n+j] is stored + (i32.const 0) ;;get x.c[i+1].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x.c[i+1] + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (i32.const 1) + (i32.sub) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) + (i32.mul) ;; j+1*4 + (i32.add) ;; x + j+1*4 ;; addr of x.keys[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = i+1 + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 0) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n-1 + (local.get 5) ;; x.c[i] + (local.get 1) ;; k + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) + (else ;; key not present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) + (i32.const 1) + (i32.sub) ;; t/2 - 1 + (i32.eq) + (if + (then + (i32.const -1) + (local.set 4) ;; changed = -1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.le_s) ;; i+1 <= x.n + (if + (then + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; index = t/2 -1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[t/2-1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[t/2-1] = x.keys[i] + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;;x.c[i].n = x.c[i].n +1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i+1].keys[0] + (i32.load offset=8) ;; x.c[i+1].keys[0] + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + (local.get 5) + (i32.load) ;; x.c[i] is leaf? + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) ;;get x.c[i].c[t/2] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; index = t/2 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[t/2] + (i32.const 0) ;;get x.c[i+1].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[0] + (i32.store offset=8) ;; x.c[i].c[t/2] = x.c[i+1].c[0] + ) + ) + (local.get 5) ;; x.c[i] + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.store offset=4) ;; x.c[i].n = t/2 + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 --> address where x.c[i+1].keys[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j+1] + (i32.store offset=8) ;; x.c[i+1].keys[j] = x.c[i+1].keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i+1].c[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j+1] + (i32.store offset=8) ;; x.c[i+1].c[j] = x.c[i+1].c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr where x.c[i+1] is located + (i32.load offset=8) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i+1].n = x.c[i+1].n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i -1 >= 0 + (i32.and) ;; changed == -1 && i -1 >= 0 + (if + (then + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i-1].n >= t/2 + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.set 3) ;; j = x.c[i].n + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j-1] + (i32.load offset=8) ;; x.c[i].keys[j-1] + (i32.store offset=8) ;; x.c[i].keys[j] = x.c[i].keys[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (local.set 3) ;; j = x.c[i].n + 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i].c[j] is located + (i32.const 0) ;;get x.c[i].c[j-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) + (i32.load offset=8) ;; x.c[i].c[j-1] + (i32.store offset=8) ;; x.c[i].c[j] = x.c[i].c[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; ;; x.c[i].n = x.c[i].n + 1 + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[0] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i-1] + (i32.store offset=8) ;; x.c[i].keys[0] = x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n - 1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[i-1] = x.c[i-1].keys[x.c[i-1].n] + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; get x.c[i].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 ;; addr where x.c[i].c[0] is located + (i32.const 0) ;; get x.c[i-1].c[x.c[i-1].n + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.add) ;; index = x.c[i-1.n + 1] + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1].c[x.c[i-1].n + 1] + (i32.store offset=8) ;; x.c[i].c[0] = x.c[i-1].c[x.c[i-1].n + 1] + ) + ) + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (if ;; changed == -1? + (then + (local.get 2) ;; we have to merge x.c[i] with one sibling;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.le_s) + (if ;; i+1 <= x.n + (then + (local.get 5) ;; merge with right sibling;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = x.keys[i] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n+j] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n+j] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i+1].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[x.c[i].n+j] + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) ;; x.c[i].n + x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + x.c[i+1].n + 1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.set 3) ;; j = i+1 + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) ;; changed == -1 ? + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i - 1 >= 0? + (i32.and) ;; changed == -1 && i - 1 >= 0 + (if + (then + (i32.const 0) ;; merge with left sibling ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n] = x.keys[i-1] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n+j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[j] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n+j] = x.c[i].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i-1].c[x.c[i-1].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i-1].c[x.c[i-1].n+j] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i].c[j] + (i32.store offset=8) ;; x.c[i-1].c[x.c[i-1].n+j] = x.c[i].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.add) ;; x.c[i-1].n + x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n + x.c[i].n + 1 + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = i - 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + ) + ) + ) + ) + ) + ) + (local.get 4) + (i32.const -1) + (i32.eq) + (if ;; changed == -1? if yes, we merged with left sibling + (then + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (local.get 1) + (call $btreeDelete) + (drop) + ) + (else + (local.get 5) ;; x.c[i] + (local.get 1) + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) ;; end of if x is not leaf + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.load offset=4) + (i32.const 0) + (i32.eq) + (if ;; if root is empty + (then + (i32.const 0) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; root.c[0] + (i32.store offset=8) ;; root = root.c[0] + ) + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + ) + (func $main + (param $a i32) + (param $b i32) + (param $c i32) + (param $d i32) + (param $e i32) + (param $f i32) + (param $g i32) + (param $x i32) + (param $h i32) + (local i32) + (local.get $a) + (local.get $b) + (i32.gt_s) + (local.get $b) + (local.get $c) + (i32.gt_s) + (local.get $c) + (local.get $d) + (i32.gt_s) + (local.get $d) + (local.get $e) + (i32.gt_s) + (local.get $e) + (local.get $f) + (i32.gt_s) + (local.get $f) + (local.get $g) + (i32.gt_s) + (local.get $g) + (local.get $x) + (i32.gt_s) + (local.get $h) + (local.get $a) + (i32.ne) + (local.get $h) + (local.get $b) + (i32.ne) + (local.get $h) + (local.get $c) + (i32.ne) + (local.get $h) + (local.get $d) + (i32.ne) + (local.get $h) + (local.get $e) + (i32.ne) + (local.get $h) + (local.get $f) + (i32.ne) + (local.get $h) + (local.get $g) + (i32.ne) + (local.get $h) + (local.get $x) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.eqz) + (if (then (unreachable))) + (i32.const 4) ;;create a tree with degree 4 + (call $createBtree) + (local.set 9) + ;; insert variables + (local.get $a) + (call $btreeInsert) + (local.set 9) + (local.get $b) + (call $btreeInsert) + (local.set 9) + (local.get $c) + (call $btreeInsert) + (local.set 9) + (local.get $d) + (call $btreeInsert) + (local.set 9) + (local.get $e) + (call $btreeInsert) + (local.set 9) + (local.get $f) + (call $btreeInsert) + (local.set 9) + (local.get $g) + (call $btreeInsert) + (local.set 9) + (local.get $x) + (call $btreeInsert) + (local.set 9) + (local.get $h) + (call $btreeInsert) + (local.set 9) + ;; search for variables & check that they were inserted + (local.get 9) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 9) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 9) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 9) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 9) + (local.get $e) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 9) + (local.get $f) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 9) + (local.get $g) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 9) + (local.get $x) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 9) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + ;; delete & check that it was deleted + ;; a + (local.get 9) + (local.get $a) + (call $btreeDelete) + (local.set 9) + (local.get 9) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; b + (local.get 9) + (local.get $b) + (call $btreeDelete) + (local.set 9) + (local.get 9) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; c + (local.get 9) + (local.get $c) + (call $btreeDelete) + (local.set 9) + (local.get 9) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; d + (local.get 9) + (local.get $d) + (call $btreeDelete) + (local.set 9) + (local.get 9) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; e + (local.get 9) + (local.get $e) + (call $btreeDelete) + (local.set 9) + (local.get 9) + (local.get $e) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; f + (local.get 9) + (local.get $f) + (call $btreeDelete) + (local.set 9) + (local.get 9) + (local.get $f) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; g + (local.get 9) + (local.get $g) + (call $btreeDelete) + (local.set 9) + (local.get 9) + (local.get $g) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; x + (local.get 9) + (local.get $x) + (call $btreeDelete) + (local.set 9) + (local.get 9) + (local.get $x) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (drop)) + (export "main" (func $main))) diff --git a/benchmarks/wasm/btree/9o1u.wat b/benchmarks/wasm/btree/9o1u.wat new file mode 100644 index 00000000..4ae26f54 --- /dev/null +++ b/benchmarks/wasm/btree/9o1u.wat @@ -0,0 +1,3019 @@ +(module + (memory $0 1) + (func $createBtree (param i32) (result i32) ;; createBtree(t), where t: degree of the btree + (i32.const 0) + (local.get 0) + (i32.store) ;; store t at address 0 + (i32.const 0) + (i32.const 1) + (i32.store offset=4) ;; store the number of nodes in the btree = 1 + (i32.const 0) + (i32.const 65536) + (i32.store offset=8) ;; store the root addr + (i32.const 1) ;; Now create root + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then + (i32.const 65536) ;; 64KiB = 65536 bytes + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + (i32.const 65536) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + ) + ) + (i32.const 65536) ;; returns the address of the root + ) + ;; btreeSearch(x, k), where x: node address; k: key to search. Returns address of the key or -1 if key is not in tree. + (func $btreeSearch (param i32) (param i32) (result i32) (local i32) + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n -1 + (i32.le_s) ;; i <= x.children-1 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + (if + (then + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.le_s) ;; i <= x.children - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.eq) + ;; k == x.keys[i] + (if (result i32) + (then + (local.get 0) ;; save the address, which is x+8+i*4 + (i32.const 8) + (i32.add) ;; x+8 + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x+8+i*4 + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + ;; btreeSplitChild(x, i), where x: addr of a non full internal node; i: index such that x.children[i] is a full child of x + (func $btreeSplitChild (param i32) (param i32) (local i32) (local i32) + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then ;; get the number of nodes in the tree + (i32.const 0) + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address is 64KiB*(number of nodes+1) + (local.set 2) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the addr of the child in index i : y + (i32.load) ;; check if x.children[i] is leaf ;; load the first i32 in the child + (i32.const 1) + (i32.eq) ;; is y leaf? + (if + (then ;; yes + (local.get 2) ;; addr of new node z + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + ) + (else ;; no + (local.get 2) ;; addr of new node z + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + ) + ) + (local.get 2) ;; now store number of children, which will be t/2 -1 ;; addr of z + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.store offset=4) ;; store number of children = (t/2)-1 + (i32.const 0) ;; now to store the keys + (local.set 3) ;; counter = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.eq) ;; counter == (t/2)-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 2) ;; addr of z ;; we will store in the new node's address. z.keys[counter] + (i32.const 4) + (local.get 3) ;; counter + (i32.mul) ;; counter * 4 + (i32.add) ;; addr + counter * 4 -> where the key is goint to be located + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 4) ;; now get x.children[i].keys[counter+(t/2)] + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + (t/2) + (i32.mul) ;; counter + (t/2) *4 + (i32.add) ;; y + counter + (t/2) *4 + (i32.load offset=8) ;; y.keys[counter + (t/2)] + (i32.store offset=8) ;; z.keys[counter] = y.keys[counter + (t/2)] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) + (local.set 3) ;; set counter back to 0 + (i32.const 0) ;; check if x.children[i] is leaf + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load) ;; get first i32 in child --> isLeaf + (i32.const 1) + (i32.ne) ;; is leaf? + (if + (then ;; not leaf so we have to update the children + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.eq) ;; counter == (t/2) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; now get z.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; counter*4 + (i32.add) ;; (t-1)*4 + counter*4 + (local.get 2) ;; z + (i32.add) ;; z + (t-1)*4 + counter*4 --> address where addr of child is stored + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) ;; get x.children[i].children[counter+(t/2)] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + t/2 + (i32.const 4) + (i32.mul) ;; counter+t/2 * 4 + (i32.add) ;; (t-1)*4 + (counter+(t/2))*4 + (i32.add) ;; x.children[i] + (t-1)*4 + (counter + (t/2))*4 + (i32.load offset=8) ;; load the address of the child in index counter + t/2 + (i32.store offset=8) ;; z.children[counter] = x.children[i].children[counter] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.store offset=4) ;; number of keys of x.children[i] is now t/2 -1 + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (local.set 3) ;; counter = x.n + (block $loop_break + (loop $loop + (local.get 1) ;; i + (local.get 3) ;; counter + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; get x.children[counter + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (i32.add) ;; (t-1)*4 + (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter + 1)*4 -->address where addr of child is stored + (i32.const 0) ;; get x.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (i32.add) ;; (t-1)*4 + (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter)*4 + (i32.load offset=8) ;; load the addr of the child in index (counter) + (i32.store offset=8) ;; x.children[counter+1] = x.children[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 1) + (i32.add) ;; i + 1 + (i32.const 4) + (i32.mul) ;; (i + 1)*4 + (i32.add) ;; (t-1)*4 + (i + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i + 1)*4 --> addr where we will store the new addr of the child + (local.get 2) ;; addr of z + (i32.store offset=8) ;; x.children[i+1] = z + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (i32.const 1) + (i32.sub) + (local.set 3) ;; counter = x.n -1 + (block $loop_break + (loop $loop + (local.get 1) ;; i + (i32.const 1) + (i32.sub) ;; i-1 + (local.get 3) ;; counter + (i32.eq) ;; counter == i-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 3) ;; get x.keys[counter + 1] ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter + 1)*4 --> addr where we will store the new key + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter)*4 + (i32.load offset=8) ;; load the key in index (counter) + (i32.store offset=8) ;; x.keys[counter+1] = x.keys[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (local.get 1) ;; get x.keys[i] + (i32.const 4) + (i32.mul) ;; i*4 + (local.get 0) ;; x + (i32.add) ;; x +i*4 --> addr of the key + (i32.const 0) ;; get x.children[i].keys[t/2] + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.const 4) + (i32.mul) ;; (t/2)-1 *4 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; (i)*4 + (i32.add) ;; (t-1)*4 + (i)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i)*4 + (i32.load offset=8) ;; load the address of the child in index (i) + (i32.add) ;; y + (t/2)-1 *4 + (i32.load offset=8) ;; load the key in index (t/2)-1 from node x.children[i] + (i32.store offset=8) ;; store the new key in the addr + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) ;; x.n + 1 + (i32.store offset=4) ;; x.n = x.n + 1 + ) + ) + ) + ;; btreeInsertNonFull(x, k), where x: addr of a non full internal node; k: the key to insert + (func $btreeInsertNonFull (param i32) (param i32) (local i32) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n -1 + (local.set 2) ;; i = x.n -1 + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (i32.and) + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i *4 + (i32.add) ;; x + i *4 --> addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.keys[i+1] = x.keys[i] + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 1) ;; k + (i32.store offset=8) ;; x.keys[i+1] = k + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; x.n = x.n +1 + ) + (else ;; x is not leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i >= 0 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i -1 + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 2) ;;i = i+1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address of the child in index i = y + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load offset=4) ;; get y.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; y.n == t-1 --> node is full + (if + (then + (local.get 0) ;; x + (local.get 2) ;; i + (call $btreeSplitChild) + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k>x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i+1 + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (local.get 1) ;; k + (call $btreeInsertNonFull) + ) + ) + ) + (func $btreeInsert (param i32) (result i32) (local i32) (local i32) + (i32.const 0) + (i32.load offset=8) ;; root addr + (local.tee 2) ;; set r + (i32.load offset=4) ;; r.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; r.n == t-1 --> root is full + (if (result i32) + (then + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if (result i32) ;; if we can grow memory + (then + (i32.const 0) ;; get the number of nodes in the tree + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address of s is 64KiB*(number of nodes+1) + (local.set 1) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) + (local.get 1) ;; s + (i32.store offset=8) ;; root addr is now s + (local.get 1) ;; s + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + (local.get 1) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + (i32.const 0) ;; store children addresses + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 1) ;; x + (i32.add) ;; x + (t-1)*4 --> addre where addr of child number 0 is located + (local.get 2) + (i32.store offset=8) ;; s.children[0] = r + (local.get 1) ;; s + (i32.const 0) + (call $btreeSplitChild) + (local.get 1) ;; s + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 1) ;; return the address of the new root + ) + (else + (i32.const -1) ;; return -1 because we could not create a new node + ) + ) + ) + (else + (local.get 2) ;; r + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 2) ;; return the address of the root, which is the same + ) + ) + ) + ;; Returns the address of the new root (if applicable, otherwise returns the addres of the root) + (func $btreeDelete (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local i32) + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (local.get 2) ;; i + (i32.eq) ;; i == x.n + (if + (then + (br $loop_break) ;; break from loop + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + (if + (then + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $while_break + (loop $while + (local.get 3) ;; j + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n-1 + (i32.eq) ;; j == x.n-1 + (if + (then + (br $while_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 --> addr where x.keys[j] is stored + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.mul) ;; j+1 *4 + (i32.add) ;; x + j+1 *4 --> addr where x.keys[j+1] is stored + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $while) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n -1 + (br $loop_break) + ) + ) + ) + ) + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) ;; continue + ) + ) + ) + (else ;; x is not leaf + (i32.const 0) ;;get the index of the appropriate child/key + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (i32.and) ;; i <= x.n - 1 && k > x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i + 1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.lt_s) ;; i < x.n + (if (result i32) + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (if + (then ;; key is present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.tee 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (call $btreeDelete) ;; (x.c[i], x.c[i].keys[x.c[i].n - 1]) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i].keys[x.c[i].n - 1] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i+1] + (local.tee 5) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (local.get 5) ;; x.c[i+1] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (call $btreeDelete) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 --> addr where x.c[i].keys[x.c[i].n] will be stored + (local.get 1) ;; k + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = k + (i32.const 0) ;; now merge x.c[i] with x.c[i+1] + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if ;; j == x.c[i+1].n + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n + j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[index] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x.c[i+1] + i*4 ;; addr of x.c[i+1].keys[j] + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[index] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n +1 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + 1 + x.c[i+1].n + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; now adjust the children + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.get 3) ;; j + (i32.add) ;; index = x.c[i].n+j + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x.c[i] + (t-1)*4 --> address where x.c[i].c[x.c[i].n+j] is stored + (i32.const 0) ;;get x.c[i+1].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x.c[i+1] + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (i32.const 1) + (i32.sub) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) + (i32.mul) ;; j+1*4 + (i32.add) ;; x + j+1*4 ;; addr of x.keys[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = i+1 + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 0) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n-1 + (local.get 5) ;; x.c[i] + (local.get 1) ;; k + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) + (else ;; key not present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) + (i32.const 1) + (i32.sub) ;; t/2 - 1 + (i32.eq) + (if + (then + (i32.const -1) + (local.set 4) ;; changed = -1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.le_s) ;; i+1 <= x.n + (if + (then + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; index = t/2 -1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[t/2-1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[t/2-1] = x.keys[i] + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;;x.c[i].n = x.c[i].n +1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i+1].keys[0] + (i32.load offset=8) ;; x.c[i+1].keys[0] + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + (local.get 5) + (i32.load) ;; x.c[i] is leaf? + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) ;;get x.c[i].c[t/2] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; index = t/2 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[t/2] + (i32.const 0) ;;get x.c[i+1].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[0] + (i32.store offset=8) ;; x.c[i].c[t/2] = x.c[i+1].c[0] + ) + ) + (local.get 5) ;; x.c[i] + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.store offset=4) ;; x.c[i].n = t/2 + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 --> address where x.c[i+1].keys[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j+1] + (i32.store offset=8) ;; x.c[i+1].keys[j] = x.c[i+1].keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i+1].c[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j+1] + (i32.store offset=8) ;; x.c[i+1].c[j] = x.c[i+1].c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr where x.c[i+1] is located + (i32.load offset=8) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i+1].n = x.c[i+1].n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i -1 >= 0 + (i32.and) ;; changed == -1 && i -1 >= 0 + (if + (then + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i-1].n >= t/2 + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.set 3) ;; j = x.c[i].n + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j-1] + (i32.load offset=8) ;; x.c[i].keys[j-1] + (i32.store offset=8) ;; x.c[i].keys[j] = x.c[i].keys[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (local.set 3) ;; j = x.c[i].n + 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i].c[j] is located + (i32.const 0) ;;get x.c[i].c[j-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) + (i32.load offset=8) ;; x.c[i].c[j-1] + (i32.store offset=8) ;; x.c[i].c[j] = x.c[i].c[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; ;; x.c[i].n = x.c[i].n + 1 + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[0] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i-1] + (i32.store offset=8) ;; x.c[i].keys[0] = x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n - 1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[i-1] = x.c[i-1].keys[x.c[i-1].n] + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; get x.c[i].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 ;; addr where x.c[i].c[0] is located + (i32.const 0) ;; get x.c[i-1].c[x.c[i-1].n + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.add) ;; index = x.c[i-1.n + 1] + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1].c[x.c[i-1].n + 1] + (i32.store offset=8) ;; x.c[i].c[0] = x.c[i-1].c[x.c[i-1].n + 1] + ) + ) + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (if ;; changed == -1? + (then + (local.get 2) ;; we have to merge x.c[i] with one sibling;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.le_s) + (if ;; i+1 <= x.n + (then + (local.get 5) ;; merge with right sibling;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = x.keys[i] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n+j] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n+j] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i+1].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[x.c[i].n+j] + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) ;; x.c[i].n + x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + x.c[i+1].n + 1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.set 3) ;; j = i+1 + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) ;; changed == -1 ? + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i - 1 >= 0? + (i32.and) ;; changed == -1 && i - 1 >= 0 + (if + (then + (i32.const 0) ;; merge with left sibling ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n] = x.keys[i-1] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n+j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[j] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n+j] = x.c[i].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i-1].c[x.c[i-1].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i-1].c[x.c[i-1].n+j] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i].c[j] + (i32.store offset=8) ;; x.c[i-1].c[x.c[i-1].n+j] = x.c[i].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.add) ;; x.c[i-1].n + x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n + x.c[i].n + 1 + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = i - 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + ) + ) + ) + ) + ) + ) + (local.get 4) + (i32.const -1) + (i32.eq) + (if ;; changed == -1? if yes, we merged with left sibling + (then + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (local.get 1) + (call $btreeDelete) + (drop) + ) + (else + (local.get 5) ;; x.c[i] + (local.get 1) + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) ;; end of if x is not leaf + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.load offset=4) + (i32.const 0) + (i32.eq) + (if ;; if root is empty + (then + (i32.const 0) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; root.c[0] + (i32.store offset=8) ;; root = root.c[0] + ) + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + ) + (func $main + (param $a i32) + (param $b i32) + (param $c i32) + (param $d i32) + (param $e i32) + (param $f i32) + (param $g i32) + (param $x i32) + (param $y i32) + (param $h i32) + (local i32) + (local.get $a) + (local.get $b) + (i32.gt_s) + (local.get $b) + (local.get $c) + (i32.gt_s) + (local.get $c) + (local.get $d) + (i32.gt_s) + (local.get $d) + (local.get $e) + (i32.gt_s) + (local.get $e) + (local.get $f) + (i32.gt_s) + (local.get $f) + (local.get $g) + (i32.gt_s) + (local.get $g) + (local.get $x) + (i32.gt_s) + (local.get $x) + (local.get $y) + (i32.gt_s) + (local.get $h) + (local.get $a) + (i32.ne) + (local.get $h) + (local.get $b) + (i32.ne) + (local.get $h) + (local.get $c) + (i32.ne) + (local.get $h) + (local.get $d) + (i32.ne) + (local.get $h) + (local.get $e) + (i32.ne) + (local.get $h) + (local.get $f) + (i32.ne) + (local.get $h) + (local.get $g) + (i32.ne) + (local.get $h) + (local.get $x) + (i32.ne) + (local.get $h) + (local.get $y) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.eqz) + (if (then (unreachable))) + (i32.const 4) ;;create a tree with degree 4 + (call $createBtree) + (local.set 10) + ;; insert variables + (local.get $a) + (call $btreeInsert) + (local.set 10) + (local.get $b) + (call $btreeInsert) + (local.set 10) + (local.get $c) + (call $btreeInsert) + (local.set 10) + (local.get $d) + (call $btreeInsert) + (local.set 10) + (local.get $e) + (call $btreeInsert) + (local.set 10) + (local.get $f) + (call $btreeInsert) + (local.set 10) + (local.get $g) + (call $btreeInsert) + (local.set 10) + (local.get $x) + (call $btreeInsert) + (local.set 10) + (local.get $y) + (call $btreeInsert) + (local.set 10) + (local.get $h) + (call $btreeInsert) + (local.set 10) + ;; search for variables & check that they were inserted + (local.get 10) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 10) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 10) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 10) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 10) + (local.get $e) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 10) + (local.get $f) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 10) + (local.get $g) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 10) + (local.get $x) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 10) + (local.get $y) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 10) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + ;; delete & check that it was deleted + ;; a + (local.get 10) + (local.get $a) + (call $btreeDelete) + (local.set 10) + (local.get 10) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; b + (local.get 10) + (local.get $b) + (call $btreeDelete) + (local.set 10) + (local.get 10) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; c + (local.get 10) + (local.get $c) + (call $btreeDelete) + (local.set 10) + (local.get 10) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; d + (local.get 10) + (local.get $d) + (call $btreeDelete) + (local.set 10) + (local.get 10) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; e + (local.get 10) + (local.get $e) + (call $btreeDelete) + (local.set 10) + (local.get 10) + (local.get $e) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; f + (local.get 10) + (local.get $f) + (call $btreeDelete) + (local.set 10) + (local.get 10) + (local.get $f) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; g + (local.get 10) + (local.get $g) + (call $btreeDelete) + (local.set 10) + (local.get 10) + (local.get $g) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; x + (local.get 10) + (local.get $x) + (call $btreeDelete) + (local.set 10) + (local.get 10) + (local.get $x) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; y + (local.get 10) + (local.get $y) + (call $btreeDelete) + (local.set 10) + (local.get 10) + (local.get $y) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (drop)) + (export "main" (func $main))) diff --git a/benchmarks/wasm/btree/9o2u-unlabeled.wat b/benchmarks/wasm/btree/9o2u-unlabeled.wat new file mode 100644 index 00000000..18e42bfd --- /dev/null +++ b/benchmarks/wasm/btree/9o2u-unlabeled.wat @@ -0,0 +1,2896 @@ +(module + (type (;0;) (func (param i32) (result i32))) + (type (;1;) (func (param i32 i32) (result i32))) + (type (;2;) (func (param i32 i32))) + (type (;3;) (func (param i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32))) + (type (;4;) (func)) + (func (;0;) (type 0) (param i32) (result i32) + i32.const 0 + local.get 0 + i32.store + i32.const 0 + i32.const 1 + i32.store offset=4 + i32.const 0 + i32.const 65536 + i32.store offset=8 + i32.const 1 + memory.grow + i32.const -1 + i32.ne + if ;; label = @1 + i32.const 65536 + i32.const 1 + i32.store + i32.const 65536 + i32.const 0 + i32.store offset=4 + end + i32.const 65536) + (func (;1;) (type 1) (param i32 i32) (result i32) + (local i32) + i32.const 0 + local.set 2 + block ;; label = @1 + loop ;; label = @2 + local.get 2 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.le_s + if ;; label = @3 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.gt_s + if ;; label = @4 + i32.const 1 + local.get 2 + i32.add + local.set 2 + br 2 (;@2;) + else + br 3 (;@1;) + end + else + br 2 (;@1;) + end + end + end + local.get 2 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.le_s + if (result i32) ;; label = @1 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.eq + if (result i32) ;; label = @2 + local.get 0 + i32.const 8 + i32.add + i32.const 4 + local.get 2 + i32.mul + i32.add + else + local.get 0 + i32.load + i32.const 1 + i32.eq + if (result i32) ;; label = @3 + i32.const -1 + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.get 1 + call 1 + end + end + else + local.get 0 + i32.load + i32.const 1 + i32.eq + if (result i32) ;; label = @2 + i32.const -1 + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.get 1 + call 1 + end + end) + (func (;2;) (type 2) (param i32 i32) + (local i32 i32) + i32.const 1 + memory.grow + i32.const -1 + i32.ne + if ;; label = @1 + i32.const 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.const 65536 + i32.mul + local.set 2 + i32.const 0 + i32.const 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load + i32.const 1 + i32.eq + if ;; label = @2 + local.get 2 + i32.const 1 + i32.store + else + local.get 2 + i32.const 0 + i32.store + end + local.get 2 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.store offset=4 + i32.const 0 + local.set 3 + block ;; label = @2 + loop ;; label = @3 + local.get 3 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.eq + if ;; label = @4 + br 2 (;@2;) + else + local.get 2 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@3;) + end + end + end + i32.const 0 + local.set 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load + i32.const 1 + i32.ne + if ;; label = @2 + block ;; label = @3 + loop ;; label = @4 + local.get 3 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.eq + if ;; label = @5 + br 2 (;@3;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 2 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.add + i32.const 4 + i32.mul + i32.add + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@4;) + end + end + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.store offset=4 + local.get 0 + i32.load offset=4 + local.set 3 + block ;; label = @2 + loop ;; label = @3 + local.get 1 + local.get 3 + i32.eq + if ;; label = @4 + br 2 (;@2;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.sub + local.set 3 + br 1 (;@3;) + end + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + local.get 2 + i32.store offset=8 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + local.set 3 + block ;; label = @2 + loop ;; label = @3 + local.get 1 + i32.const 1 + i32.sub + local.get 3 + i32.eq + if ;; label = @4 + br 2 (;@2;) + else + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + local.get 0 + i32.add + local.get 3 + i32.const 4 + i32.mul + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.sub + local.set 3 + br 1 (;@3;) + end + end + end + local.get 1 + i32.const 4 + i32.mul + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 1 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + end) + (func (;3;) (type 2) (param i32 i32) + (local i32) + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + local.set 2 + local.get 0 + i32.load + i32.const 1 + i32.eq + if ;; label = @1 + block ;; label = @2 + loop ;; label = @3 + local.get 2 + i32.const 0 + i32.ge_s + if (result i32) ;; label = @4 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.lt_s + else + i32.const 0 + end + local.get 2 + i32.const 0 + i32.ge_s + i32.and + if ;; label = @4 + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.add + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 2 + i32.const 1 + i32.sub + local.set 2 + br 1 (;@3;) + else + br 2 (;@2;) + end + end + end + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.add + i32.mul + i32.add + local.get 1 + i32.store offset=8 + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + else + block ;; label = @2 + loop ;; label = @3 + local.get 2 + i32.const 0 + i32.ge_s + if ;; label = @4 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.lt_s + if ;; label = @5 + local.get 2 + i32.const 1 + i32.sub + local.set 2 + br 2 (;@3;) + else + br 3 (;@2;) + end + else + br 2 (;@2;) + end + end + end + local.get 2 + i32.const 1 + i32.add + local.set 2 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.eq + if ;; label = @2 + local.get 0 + local.get 2 + call 2 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.gt_s + if ;; label = @3 + local.get 2 + i32.const 1 + i32.add + local.set 2 + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.get 1 + call 3 + end) + (func (;4;) (type 0) (param i32) (result i32) + (local i32 i32) + i32.const 0 + i32.load offset=8 + local.tee 2 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.eq + if (result i32) ;; label = @1 + i32.const 1 + memory.grow + i32.const -1 + i32.ne + if (result i32) ;; label = @2 + i32.const 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.const 65536 + i32.mul + local.set 1 + i32.const 0 + i32.const 0 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + i32.const 0 + local.get 1 + i32.store offset=8 + local.get 1 + i32.const 0 + i32.store + local.get 1 + i32.const 0 + i32.store offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.const 4 + i32.mul + i32.add + local.get 1 + i32.add + local.get 2 + i32.store offset=8 + local.get 1 + i32.const 0 + call 2 + local.get 1 + local.get 0 + call 3 + local.get 1 + else + i32.const -1 + end + else + local.get 2 + local.get 0 + call 3 + local.get 2 + end) + (func (;5;) (type 1) (param i32 i32) (result i32) + (local i32 i32 i32 i32) + local.get 0 + i32.load + i32.const 1 + i32.eq + if ;; label = @1 + i32.const 0 + local.set 2 + block ;; label = @2 + loop ;; label = @3 + local.get 0 + i32.load offset=4 + local.get 2 + i32.eq + if ;; label = @4 + br 2 (;@2;) + else + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + local.get 1 + i32.eq + if ;; label = @5 + local.get 2 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 0 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + br 3 (;@2;) + end + end + i32.const 1 + local.get 2 + i32.add + local.set 2 + br 0 (;@3;) + end + end + else + i32.const 0 + local.set 2 + block ;; label = @2 + loop ;; label = @3 + local.get 2 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.le_s + if (result i32) ;; label = @4 + local.get 1 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.gt_s + else + i32.const 0 + end + local.get 2 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.le_s + i32.and + if ;; label = @4 + local.get 2 + i32.const 1 + i32.add + local.set 2 + br 1 (;@3;) + else + br 2 (;@2;) + end + end + end + local.get 2 + local.get 0 + i32.load offset=4 + i32.lt_s + if (result i32) ;; label = @2 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + local.get 1 + i32.eq + else + i32.const 0 + end + if ;; label = @2 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.tee 5 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.ge_s + if ;; label = @3 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + local.get 5 + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + call 5 + drop + i32.store offset=8 + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.tee 5 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.ge_s + if ;; label = @4 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + local.get 5 + i32.const 4 + i32.const 0 + i32.mul + i32.add + i32.load offset=8 + local.get 5 + local.get 5 + i32.const 4 + i32.const 0 + i32.mul + i32.add + i32.load offset=8 + call 5 + drop + i32.store offset=8 + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.set 5 + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + i32.mul + i32.add + local.get 1 + i32.store offset=8 + i32.const 0 + local.set 3 + block ;; label = @5 + loop ;; label = @6 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.eq + if ;; label = @7 + br 2 (;@5;) + else + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@6;) + end + end + end + local.get 5 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.add + i32.store offset=4 + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @5 + i32.const 0 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.add + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 5 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + end + local.get 2 + local.set 3 + block ;; label = @5 + loop ;; label = @6 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + local.get 3 + i32.eq + if ;; label = @7 + br 2 (;@5;) + else + local.get 0 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@6;) + end + end + end + local.get 2 + i32.const 1 + i32.add + local.set 3 + block ;; label = @5 + loop ;; label = @6 + local.get 0 + i32.load offset=4 + local.get 3 + i32.eq + if ;; label = @7 + br 2 (;@5;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@6;) + end + end + end + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + local.get 5 + local.get 1 + call 5 + drop + end + end + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.set 5 + local.get 5 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.eq + if ;; label = @3 + i32.const -1 + local.set 4 + local.get 2 + i32.const 1 + i32.add + local.get 0 + i32.load offset=4 + i32.le_s + if ;; label = @4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.ge_s + if ;; label = @5 + local.get 5 + i32.const 4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 1 + i32.sub + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 5 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + i32.const 0 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + end + local.get 5 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.store offset=4 + i32.const 0 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.sub + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + local.set 3 + block ;; label = @7 + loop ;; label = @8 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.eq + if ;; label = @9 + br 2 (;@7;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@8;) + end + end + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + i32.const 0 + local.set 4 + end + end + local.get 4 + i32.const -1 + i32.eq + local.get 2 + i32.const 1 + i32.sub + i32.const 0 + i32.ge_s + i32.and + if ;; label = @4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 2 + i32.div_s + i32.ge_s + if ;; label = @5 + local.get 5 + i32.load offset=4 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + i32.const 0 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 5 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 5 + i32.const 4 + local.get 3 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.sub + local.set 3 + br 1 (;@7;) + end + end + end + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + local.set 3 + block ;; label = @7 + loop ;; label = @8 + local.get 3 + i32.const 0 + i32.eq + if ;; label = @9 + br 2 (;@7;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.sub + local.set 3 + br 1 (;@8;) + end + end + end + end + local.get 5 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + i32.store offset=4 + local.get 5 + i32.const 4 + i32.const 0 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + end + i32.const 0 + local.set 4 + end + end + local.get 4 + i32.const -1 + i32.eq + if ;; label = @4 + local.get 2 + i32.const 1 + i32.add + local.get 0 + i32.load offset=4 + i32.le_s + if ;; label = @5 + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + i32.const 0 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 5 + i32.const 4 + local.get 5 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + local.set 3 + block ;; label = @7 + loop ;; label = @8 + local.get 3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.const 1 + i32.add + i32.eq + if ;; label = @9 + br 2 (;@7;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 5 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@8;) + end + end + end + end + local.get 5 + local.get 5 + i32.load offset=4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.add + i32.const 1 + i32.add + i32.store offset=4 + local.get 2 + i32.const 1 + i32.add + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 2 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 0 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + i32.const 0 + local.set 4 + end + local.get 4 + i32.const -1 + i32.eq + local.get 2 + i32.const 1 + i32.sub + i32.const 0 + i32.ge_s + i32.and + if ;; label = @5 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 2 + i32.const 1 + i32.sub + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + i32.const 0 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 5 + i32.load offset=4 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 4 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.mul + i32.add + local.get 5 + i32.const 4 + local.get 3 + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 5 + i32.load + i32.const 1 + i32.ne + if ;; label = @6 + i32.const 0 + local.set 3 + block ;; label = @7 + loop ;; label = @8 + local.get 3 + local.get 5 + i32.load offset=4 + i32.const 1 + i32.add + i32.eq + if ;; label = @9 + br 2 (;@7;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + local.get 3 + i32.add + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 5 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@8;) + end + end + end + end + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.load offset=4 + local.get 5 + i32.load offset=4 + i32.add + i32.const 1 + i32.add + i32.store offset=4 + local.get 2 + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 3 + i32.const 1 + i32.add + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 2 + i32.const 1 + i32.sub + local.set 3 + block ;; label = @6 + loop ;; label = @7 + local.get 3 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.eq + if ;; label = @8 + br 2 (;@6;) + else + local.get 0 + i32.const 4 + local.get 3 + i32.mul + i32.add + local.get 0 + i32.const 4 + local.get 3 + i32.const 1 + i32.add + i32.mul + i32.add + i32.load offset=8 + i32.store offset=8 + local.get 3 + i32.const 1 + i32.add + local.set 3 + br 1 (;@7;) + end + end + end + local.get 0 + local.get 0 + i32.load offset=4 + i32.const 1 + i32.sub + i32.store offset=4 + end + end + end + local.get 4 + i32.const -1 + i32.eq + if ;; label = @3 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + local.get 2 + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.add + local.get 0 + i32.add + i32.load offset=8 + local.get 1 + call 5 + drop + else + local.get 5 + local.get 1 + call 5 + drop + end + end + end + i32.const 0 + i32.load offset=8 + i32.load offset=4 + i32.const 0 + i32.eq + if ;; label = @1 + i32.const 0 + i32.const 0 + i32.load + i32.const 1 + i32.sub + i32.const 4 + i32.mul + i32.const 0 + i32.const 4 + i32.mul + i32.add + i32.const 0 + i32.load offset=8 + i32.add + i32.load offset=8 + i32.store offset=8 + end + i32.const 0 + i32.load offset=8) + (func (;6;) (type 3) (param i32 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32) + (local i32) + local.get 0 + local.get 1 + i32.gt_s + local.get 1 + local.get 2 + i32.gt_s + local.get 2 + local.get 3 + i32.gt_s + local.get 3 + local.get 4 + i32.gt_s + local.get 4 + local.get 5 + i32.gt_s + local.get 5 + local.get 6 + i32.gt_s + local.get 6 + local.get 7 + i32.gt_s + local.get 7 + local.get 8 + i32.gt_s + local.get 9 + local.get 0 + i32.ne + local.get 9 + local.get 1 + i32.ne + local.get 9 + local.get 2 + i32.ne + local.get 9 + local.get 3 + i32.ne + local.get 9 + local.get 4 + i32.ne + local.get 9 + local.get 5 + i32.ne + local.get 9 + local.get 6 + i32.ne + local.get 9 + local.get 7 + i32.ne + local.get 9 + local.get 8 + i32.ne + local.get 10 + local.get 0 + i32.ne + local.get 10 + local.get 1 + i32.ne + local.get 10 + local.get 2 + i32.ne + local.get 10 + local.get 3 + i32.ne + local.get 10 + local.get 4 + i32.ne + local.get 10 + local.get 5 + i32.ne + local.get 10 + local.get 6 + i32.ne + local.get 10 + local.get 7 + i32.ne + local.get 10 + local.get 8 + i32.ne + local.get 10 + local.get 9 + i32.ne + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.eqz + if ;; label = @1 + unreachable + end + i32.const 4 + call 0 + local.set 11 + local.get 0 + call 4 + local.set 11 + local.get 1 + call 4 + local.set 11 + local.get 2 + call 4 + local.set 11 + local.get 3 + call 4 + local.set 11 + local.get 4 + call 4 + local.set 11 + local.get 5 + call 4 + local.set 11 + local.get 6 + call 4 + local.set 11 + local.get 7 + call 4 + local.set 11 + local.get 8 + call 4 + local.set 11 + local.get 9 + call 4 + local.set 11 + local.get 10 + call 4 + local.set 11 + local.get 11 + local.get 0 + call 1 + i32.const -1 + i32.ne + local.get 11 + local.get 1 + call 1 + i32.const -1 + i32.ne + local.get 11 + local.get 2 + call 1 + i32.const -1 + i32.ne + local.get 11 + local.get 3 + call 1 + i32.const -1 + i32.ne + local.get 11 + local.get 4 + call 1 + i32.const -1 + i32.ne + local.get 11 + local.get 5 + call 1 + i32.const -1 + i32.ne + local.get 11 + local.get 6 + call 1 + i32.const -1 + i32.ne + local.get 11 + local.get 7 + call 1 + i32.const -1 + i32.ne + local.get 11 + local.get 8 + call 1 + i32.const -1 + i32.ne + local.get 11 + local.get 9 + call 1 + i32.const -1 + i32.ne + local.get 11 + local.get 10 + call 1 + i32.const -1 + i32.ne + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + local.get 11 + local.get 0 + call 5 + local.set 11 + local.get 11 + local.get 0 + call 1 + i32.const -1 + i32.eq + local.get 11 + local.get 1 + call 5 + local.set 11 + local.get 11 + local.get 1 + call 1 + i32.const -1 + i32.eq + local.get 11 + local.get 2 + call 5 + local.set 11 + local.get 11 + local.get 2 + call 1 + i32.const -1 + i32.eq + local.get 11 + local.get 3 + call 5 + local.set 11 + local.get 11 + local.get 3 + call 1 + i32.const -1 + i32.eq + local.get 11 + local.get 4 + call 5 + local.set 11 + local.get 11 + local.get 4 + call 1 + i32.const -1 + i32.eq + local.get 11 + local.get 5 + call 5 + local.set 11 + local.get 11 + local.get 5 + call 1 + i32.const -1 + i32.eq + local.get 11 + local.get 6 + call 5 + local.set 11 + local.get 11 + local.get 6 + call 1 + i32.const -1 + i32.eq + local.get 11 + local.get 7 + call 5 + local.set 11 + local.get 11 + local.get 7 + call 1 + i32.const -1 + i32.eq + local.get 11 + local.get 8 + call 5 + local.set 11 + local.get 11 + local.get 8 + call 1 + i32.const -1 + i32.eq + local.get 11 + local.get 9 + call 5 + local.set 11 + local.get 11 + local.get 9 + call 1 + i32.const -1 + i32.eq + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.and + i32.eqz + if ;; label = @1 + unreachable + end) + (func (;7;) (type 4) + i32.const 11 + i32.const 10 + i32.const 9 + i32.const 8 + i32.const 7 + i32.const 6 + i32.const 5 + i32.const 4 + i32.const 3 + i32.const -100 + i32.const 1 + call 6) + (memory (;0;) 1) + (export "main" (func 7)) + (start 7)) diff --git a/benchmarks/wasm/btree/9o2u.wat b/benchmarks/wasm/btree/9o2u.wat new file mode 100644 index 00000000..c2af90bc --- /dev/null +++ b/benchmarks/wasm/btree/9o2u.wat @@ -0,0 +1,3099 @@ +(module + (memory $0 1) + (func $createBtree (param i32) (result i32) ;; createBtree(t), where t: degree of the btree + (i32.const 0) + (local.get 0) + (i32.store) ;; store t at address 0 + (i32.const 0) + (i32.const 1) + (i32.store offset=4) ;; store the number of nodes in the btree = 1 + (i32.const 0) + (i32.const 65536) + (i32.store offset=8) ;; store the root addr + (i32.const 1) ;; Now create root + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then + (i32.const 65536) ;; 64KiB = 65536 bytes + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + (i32.const 65536) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + ) + ) + (i32.const 65536) ;; returns the address of the root + ) + ;; btreeSearch(x, k), where x: node address; k: key to search. Returns address of the key or -1 if key is not in tree. + (func $btreeSearch (param i32) (param i32) (result i32) (local i32) + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n -1 + (i32.le_s) ;; i <= x.children-1 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + (if + (then + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.le_s) ;; i <= x.children - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.eq) + ;; k == x.keys[i] + (if (result i32) + (then + (local.get 0) ;; save the address, which is x+8+i*4 + (i32.const 8) + (i32.add) ;; x+8 + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x+8+i*4 + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + ;; btreeSplitChild(x, i), where x: addr of a non full internal node; i: index such that x.children[i] is a full child of x + (func $btreeSplitChild (param i32) (param i32) (local i32) (local i32) + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then ;; get the number of nodes in the tree + (i32.const 0) + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address is 64KiB*(number of nodes+1) + (local.set 2) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the addr of the child in index i : y + (i32.load) ;; check if x.children[i] is leaf ;; load the first i32 in the child + (i32.const 1) + (i32.eq) ;; is y leaf? + (if + (then ;; yes + (local.get 2) ;; addr of new node z + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + ) + (else ;; no + (local.get 2) ;; addr of new node z + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + ) + ) + (local.get 2) ;; now store number of children, which will be t/2 -1 ;; addr of z + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.store offset=4) ;; store number of children = (t/2)-1 + (i32.const 0) ;; now to store the keys + (local.set 3) ;; counter = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.eq) ;; counter == (t/2)-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 2) ;; addr of z ;; we will store in the new node's address. z.keys[counter] + (i32.const 4) + (local.get 3) ;; counter + (i32.mul) ;; counter * 4 + (i32.add) ;; addr + counter * 4 -> where the key is goint to be located + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 4) ;; now get x.children[i].keys[counter+(t/2)] + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + (t/2) + (i32.mul) ;; counter + (t/2) *4 + (i32.add) ;; y + counter + (t/2) *4 + (i32.load offset=8) ;; y.keys[counter + (t/2)] + (i32.store offset=8) ;; z.keys[counter] = y.keys[counter + (t/2)] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) + (local.set 3) ;; set counter back to 0 + (i32.const 0) ;; check if x.children[i] is leaf + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load) ;; get first i32 in child --> isLeaf + (i32.const 1) + (i32.ne) ;; is leaf? + (if + (then ;; not leaf so we have to update the children + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.eq) ;; counter == (t/2) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; now get z.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; counter*4 + (i32.add) ;; (t-1)*4 + counter*4 + (local.get 2) ;; z + (i32.add) ;; z + (t-1)*4 + counter*4 --> address where addr of child is stored + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) ;; get x.children[i].children[counter+(t/2)] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + t/2 + (i32.const 4) + (i32.mul) ;; counter+t/2 * 4 + (i32.add) ;; (t-1)*4 + (counter+(t/2))*4 + (i32.add) ;; x.children[i] + (t-1)*4 + (counter + (t/2))*4 + (i32.load offset=8) ;; load the address of the child in index counter + t/2 + (i32.store offset=8) ;; z.children[counter] = x.children[i].children[counter] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.store offset=4) ;; number of keys of x.children[i] is now t/2 -1 + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (local.set 3) ;; counter = x.n + (block $loop_break + (loop $loop + (local.get 1) ;; i + (local.get 3) ;; counter + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; get x.children[counter + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (i32.add) ;; (t-1)*4 + (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter + 1)*4 -->address where addr of child is stored + (i32.const 0) ;; get x.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (i32.add) ;; (t-1)*4 + (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter)*4 + (i32.load offset=8) ;; load the addr of the child in index (counter) + (i32.store offset=8) ;; x.children[counter+1] = x.children[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 1) + (i32.add) ;; i + 1 + (i32.const 4) + (i32.mul) ;; (i + 1)*4 + (i32.add) ;; (t-1)*4 + (i + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i + 1)*4 --> addr where we will store the new addr of the child + (local.get 2) ;; addr of z + (i32.store offset=8) ;; x.children[i+1] = z + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (i32.const 1) + (i32.sub) + (local.set 3) ;; counter = x.n -1 + (block $loop_break + (loop $loop + (local.get 1) ;; i + (i32.const 1) + (i32.sub) ;; i-1 + (local.get 3) ;; counter + (i32.eq) ;; counter == i-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 3) ;; get x.keys[counter + 1] ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter + 1)*4 --> addr where we will store the new key + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter)*4 + (i32.load offset=8) ;; load the key in index (counter) + (i32.store offset=8) ;; x.keys[counter+1] = x.keys[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (local.get 1) ;; get x.keys[i] + (i32.const 4) + (i32.mul) ;; i*4 + (local.get 0) ;; x + (i32.add) ;; x +i*4 --> addr of the key + (i32.const 0) ;; get x.children[i].keys[t/2] + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.const 4) + (i32.mul) ;; (t/2)-1 *4 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; (i)*4 + (i32.add) ;; (t-1)*4 + (i)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i)*4 + (i32.load offset=8) ;; load the address of the child in index (i) + (i32.add) ;; y + (t/2)-1 *4 + (i32.load offset=8) ;; load the key in index (t/2)-1 from node x.children[i] + (i32.store offset=8) ;; store the new key in the addr + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) ;; x.n + 1 + (i32.store offset=4) ;; x.n = x.n + 1 + ) + ) + ) + ;; btreeInsertNonFull(x, k), where x: addr of a non full internal node; k: the key to insert + (func $btreeInsertNonFull (param i32) (param i32) (local i32) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n -1 + (local.set 2) ;; i = x.n -1 + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (i32.and) + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i *4 + (i32.add) ;; x + i *4 --> addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.keys[i+1] = x.keys[i] + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 1) ;; k + (i32.store offset=8) ;; x.keys[i+1] = k + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; x.n = x.n +1 + ) + (else ;; x is not leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i >= 0 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i -1 + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 2) ;;i = i+1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address of the child in index i = y + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load offset=4) ;; get y.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; y.n == t-1 --> node is full + (if + (then + (local.get 0) ;; x + (local.get 2) ;; i + (call $btreeSplitChild) + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k>x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i+1 + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (local.get 1) ;; k + (call $btreeInsertNonFull) + ) + ) + ) + (func $btreeInsert (param i32) (result i32) (local i32) (local i32) + (i32.const 0) + (i32.load offset=8) ;; root addr + (local.tee 2) ;; set r + (i32.load offset=4) ;; r.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; r.n == t-1 --> root is full + (if (result i32) + (then + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if (result i32) ;; if we can grow memory + (then + (i32.const 0) ;; get the number of nodes in the tree + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address of s is 64KiB*(number of nodes+1) + (local.set 1) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) + (local.get 1) ;; s + (i32.store offset=8) ;; root addr is now s + (local.get 1) ;; s + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + (local.get 1) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + (i32.const 0) ;; store children addresses + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 1) ;; x + (i32.add) ;; x + (t-1)*4 --> addre where addr of child number 0 is located + (local.get 2) + (i32.store offset=8) ;; s.children[0] = r + (local.get 1) ;; s + (i32.const 0) + (call $btreeSplitChild) + (local.get 1) ;; s + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 1) ;; return the address of the new root + ) + (else + (i32.const -1) ;; return -1 because we could not create a new node + ) + ) + ) + (else + (local.get 2) ;; r + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 2) ;; return the address of the root, which is the same + ) + ) + ) + ;; Returns the address of the new root (if applicable, otherwise returns the addres of the root) + (func $btreeDelete (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local i32) + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (local.get 2) ;; i + (i32.eq) ;; i == x.n + (if + (then + (br $loop_break) ;; break from loop + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + (if + (then + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $while_break + (loop $while + (local.get 3) ;; j + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n-1 + (i32.eq) ;; j == x.n-1 + (if + (then + (br $while_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 --> addr where x.keys[j] is stored + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.mul) ;; j+1 *4 + (i32.add) ;; x + j+1 *4 --> addr where x.keys[j+1] is stored + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $while) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n -1 + (br $loop_break) + ) + ) + ) + ) + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) ;; continue + ) + ) + ) + (else ;; x is not leaf + (i32.const 0) ;;get the index of the appropriate child/key + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (i32.and) ;; i <= x.n - 1 && k > x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i + 1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.lt_s) ;; i < x.n + (if (result i32) + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (if + (then ;; key is present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.tee 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (call $btreeDelete) ;; (x.c[i], x.c[i].keys[x.c[i].n - 1]) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i].keys[x.c[i].n - 1] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i+1] + (local.tee 5) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (local.get 5) ;; x.c[i+1] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (call $btreeDelete) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 --> addr where x.c[i].keys[x.c[i].n] will be stored + (local.get 1) ;; k + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = k + (i32.const 0) ;; now merge x.c[i] with x.c[i+1] + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if ;; j == x.c[i+1].n + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n + j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[index] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x.c[i+1] + i*4 ;; addr of x.c[i+1].keys[j] + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[index] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n +1 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + 1 + x.c[i+1].n + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; now adjust the children + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.get 3) ;; j + (i32.add) ;; index = x.c[i].n+j + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x.c[i] + (t-1)*4 --> address where x.c[i].c[x.c[i].n+j] is stored + (i32.const 0) ;;get x.c[i+1].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x.c[i+1] + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (i32.const 1) + (i32.sub) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) + (i32.mul) ;; j+1*4 + (i32.add) ;; x + j+1*4 ;; addr of x.keys[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = i+1 + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 0) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n-1 + (local.get 5) ;; x.c[i] + (local.get 1) ;; k + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) + (else ;; key not present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) + (i32.const 1) + (i32.sub) ;; t/2 - 1 + (i32.eq) + (if + (then + (i32.const -1) + (local.set 4) ;; changed = -1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.le_s) ;; i+1 <= x.n + (if + (then + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; index = t/2 -1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[t/2-1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[t/2-1] = x.keys[i] + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;;x.c[i].n = x.c[i].n +1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i+1].keys[0] + (i32.load offset=8) ;; x.c[i+1].keys[0] + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + (local.get 5) + (i32.load) ;; x.c[i] is leaf? + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) ;;get x.c[i].c[t/2] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; index = t/2 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[t/2] + (i32.const 0) ;;get x.c[i+1].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[0] + (i32.store offset=8) ;; x.c[i].c[t/2] = x.c[i+1].c[0] + ) + ) + (local.get 5) ;; x.c[i] + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.store offset=4) ;; x.c[i].n = t/2 + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 --> address where x.c[i+1].keys[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j+1] + (i32.store offset=8) ;; x.c[i+1].keys[j] = x.c[i+1].keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i+1].c[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j+1] + (i32.store offset=8) ;; x.c[i+1].c[j] = x.c[i+1].c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr where x.c[i+1] is located + (i32.load offset=8) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i+1].n = x.c[i+1].n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i -1 >= 0 + (i32.and) ;; changed == -1 && i -1 >= 0 + (if + (then + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i-1].n >= t/2 + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.set 3) ;; j = x.c[i].n + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j-1] + (i32.load offset=8) ;; x.c[i].keys[j-1] + (i32.store offset=8) ;; x.c[i].keys[j] = x.c[i].keys[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (local.set 3) ;; j = x.c[i].n + 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i].c[j] is located + (i32.const 0) ;;get x.c[i].c[j-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) + (i32.load offset=8) ;; x.c[i].c[j-1] + (i32.store offset=8) ;; x.c[i].c[j] = x.c[i].c[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; ;; x.c[i].n = x.c[i].n + 1 + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[0] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i-1] + (i32.store offset=8) ;; x.c[i].keys[0] = x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n - 1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[i-1] = x.c[i-1].keys[x.c[i-1].n] + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; get x.c[i].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 ;; addr where x.c[i].c[0] is located + (i32.const 0) ;; get x.c[i-1].c[x.c[i-1].n + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.add) ;; index = x.c[i-1.n + 1] + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1].c[x.c[i-1].n + 1] + (i32.store offset=8) ;; x.c[i].c[0] = x.c[i-1].c[x.c[i-1].n + 1] + ) + ) + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (if ;; changed == -1? + (then + (local.get 2) ;; we have to merge x.c[i] with one sibling;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.le_s) + (if ;; i+1 <= x.n + (then + (local.get 5) ;; merge with right sibling;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = x.keys[i] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n+j] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n+j] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i+1].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[x.c[i].n+j] + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) ;; x.c[i].n + x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + x.c[i+1].n + 1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.set 3) ;; j = i+1 + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) ;; changed == -1 ? + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i - 1 >= 0? + (i32.and) ;; changed == -1 && i - 1 >= 0 + (if + (then + (i32.const 0) ;; merge with left sibling ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n] = x.keys[i-1] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n+j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[j] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n+j] = x.c[i].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i-1].c[x.c[i-1].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i-1].c[x.c[i-1].n+j] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i].c[j] + (i32.store offset=8) ;; x.c[i-1].c[x.c[i-1].n+j] = x.c[i].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.add) ;; x.c[i-1].n + x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n + x.c[i].n + 1 + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = i - 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + ) + ) + ) + ) + ) + ) + (local.get 4) + (i32.const -1) + (i32.eq) + (if ;; changed == -1? if yes, we merged with left sibling + (then + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (local.get 1) + (call $btreeDelete) + (drop) + ) + (else + (local.get 5) ;; x.c[i] + (local.get 1) + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) ;; end of if x is not leaf + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.load offset=4) + (i32.const 0) + (i32.eq) + (if ;; if root is empty + (then + (i32.const 0) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; root.c[0] + (i32.store offset=8) ;; root = root.c[0] + ) + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + ) + (func $main + (param $a i32) + (param $b i32) + (param $c i32) + (param $d i32) + (param $e i32) + (param $f i32) + (param $g i32) + (param $x i32) + (param $y i32) + (param $h i32) + (param $i i32) + (local i32) + (local.get $a) + (local.get $b) + (i32.gt_s) + (local.get $b) + (local.get $c) + (i32.gt_s) + (local.get $c) + (local.get $d) + (i32.gt_s) + (local.get $d) + (local.get $e) + (i32.gt_s) + (local.get $e) + (local.get $f) + (i32.gt_s) + (local.get $f) + (local.get $g) + (i32.gt_s) + (local.get $g) + (local.get $x) + (i32.gt_s) + (local.get $x) + (local.get $y) + (i32.gt_s) + (local.get $h) + (local.get $a) + (i32.ne) + (local.get $h) + (local.get $b) + (i32.ne) + (local.get $h) + (local.get $c) + (i32.ne) + (local.get $h) + (local.get $d) + (i32.ne) + (local.get $h) + (local.get $e) + (i32.ne) + (local.get $h) + (local.get $f) + (i32.ne) + (local.get $h) + (local.get $g) + (i32.ne) + (local.get $h) + (local.get $x) + (i32.ne) + (local.get $h) + (local.get $y) + (i32.ne) + (local.get $i) + (local.get $a) + (i32.ne) + (local.get $i) + (local.get $b) + (i32.ne) + (local.get $i) + (local.get $c) + (i32.ne) + (local.get $i) + (local.get $d) + (i32.ne) + (local.get $i) + (local.get $e) + (i32.ne) + (local.get $i) + (local.get $f) + (i32.ne) + (local.get $i) + (local.get $g) + (i32.ne) + (local.get $i) + (local.get $x) + (i32.ne) + (local.get $i) + (local.get $y) + (i32.ne) + (local.get $i) + (local.get $h) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.eqz) + (if (then (unreachable))) + (i32.const 4) ;;create a tree with degree 4 + (call $createBtree) + (local.set 11) + ;; insert variables + (local.get $a) + (call $btreeInsert) + (local.set 11) + (local.get $b) + (call $btreeInsert) + (local.set 11) + (local.get $c) + (call $btreeInsert) + (local.set 11) + (local.get $d) + (call $btreeInsert) + (local.set 11) + (local.get $e) + (call $btreeInsert) + (local.set 11) + (local.get $f) + (call $btreeInsert) + (local.set 11) + (local.get $g) + (call $btreeInsert) + (local.set 11) + (local.get $x) + (call $btreeInsert) + (local.set 11) + (local.get $y) + (call $btreeInsert) + (local.set 11) + (local.get $h) + (call $btreeInsert) + (local.set 11) + (local.get $i) + (call $btreeInsert) + (local.set 11) + ;; search for variables & check that they were inserted + (local.get 11) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 11) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 11) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 11) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 11) + (local.get $e) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 11) + (local.get $f) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 11) + (local.get $g) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 11) + (local.get $x) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 11) + (local.get $y) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 11) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 11) + (local.get $i) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + ;; delete & check that it was deleted + ;; a + (local.get 11) + (local.get $a) + (call $btreeDelete) + (local.set 11) + (local.get 11) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; b + (local.get 11) + (local.get $b) + (call $btreeDelete) + (local.set 11) + (local.get 11) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; c + (local.get 11) + (local.get $c) + (call $btreeDelete) + (local.set 11) + (local.get 11) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; d + (local.get 11) + (local.get $d) + (call $btreeDelete) + (local.set 11) + (local.get 11) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; e + (local.get 11) + (local.get $e) + (call $btreeDelete) + (local.set 11) + (local.get 11) + (local.get $e) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; f + (local.get 11) + (local.get $f) + (call $btreeDelete) + (local.set 11) + (local.get 11) + (local.get $f) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; g + (local.get 11) + (local.get $g) + (call $btreeDelete) + (local.set 11) + (local.get 11) + (local.get $g) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; x + (local.get 11) + (local.get $x) + (call $btreeDelete) + (local.set 11) + (local.get 11) + (local.get $x) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; y + (local.get 11) + (local.get $y) + (call $btreeDelete) + (local.set 11) + (local.get 11) + (local.get $y) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; h + (local.get 11) + (local.get $h) + (call $btreeDelete) + (local.set 11) + (local.get 11) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + ;; last functional check + (i32.eqz) + (if (then (unreachable))) + ) + (export "main" (func $real_main)) + (func $real_main + i32.const 11 + i32.const 10 + i32.const 9 + i32.const 8 + i32.const 7 + i32.const 6 + i32.const 5 + i32.const 4 + i32.const 3 + i32.const -100 + i32.const 1 + call $main + ) + (start $real_main) +) diff --git a/benchmarks/wasm/btree/unlabel-all.sh b/benchmarks/wasm/btree/unlabel-all.sh new file mode 100755 index 00000000..45eead2f --- /dev/null +++ b/benchmarks/wasm/btree/unlabel-all.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +# get all files with ending u.wat +for f in *u.wat; do + # use wasm2wat to get rid of wat labels and the nested form + x=${f%.*} + wat2wasm "$f" -o "$x.wasm" + wasm2wat "$x.wasm" -o "$x-unlabeled.wat" + rm "$x.wasm" +done diff --git a/benchmarks/wasm/btree/unlabel.sh b/benchmarks/wasm/btree/unlabel.sh new file mode 100755 index 00000000..07564363 --- /dev/null +++ b/benchmarks/wasm/btree/unlabel.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +# use wasm2wat to get rid of wat labels and the nested form +a="$1" +x=${a%.*} +wat2wasm "$1" -o "$x.wasm" +wasm2wat "$x.wasm" -o "$x-unlabeled.wat" +rm "$x.wasm" \ No newline at end of file diff --git a/benchmarks/wasm/div_s.wat b/benchmarks/wasm/div_s.wat new file mode 100644 index 00000000..8ecf3b96 --- /dev/null +++ b/benchmarks/wasm/div_s.wat @@ -0,0 +1,17 @@ +(module + (type (;0;) (func (result i32))) + (type (;1;) (func)) + (func (;0;) (type 0) (result i32) + i32.const 12 + i32.const -5 + + i32.div_s ;; divide one number by the other + ) + (func (;1;) (type 1) + call 0 + ;; drop + ) + (start 1) + (memory (;0;) 2) + (export "main" (func 1)) +) diff --git a/benchmarks/wasm/load.wat b/benchmarks/wasm/load.wat index 916328aa..6a81fddd 100644 --- a/benchmarks/wasm/load.wat +++ b/benchmarks/wasm/load.wat @@ -10,7 +10,6 @@ ) (func (;1;) (type 1) call 0 - ;; should be 65536 ;; drop ) (start 1) diff --git a/src/main/scala/wasm/MiniWasm.scala b/src/main/scala/wasm/MiniWasm.scala index b2789e2e..ff797560 100644 --- a/src/main/scala/wasm/MiniWasm.scala +++ b/src/main/scala/wasm/MiniWasm.scala @@ -52,6 +52,12 @@ object Primtives { case (I64V(v1), I64V(v2)) => I64V(v1 & v2) case _ => throw new Exception("Invalid types") } + case DivS(_) => + (lhs, rhs) match { + case (I32V(v1), I32V(v2)) => I32V(v1 / v2) + case (I64V(v1), I64V(v2)) => I64V(v1 / v2) + case _ => throw new Exception("Invalid types") + } case _ => ??? } def evalUnaryOp(op: UnaryOp, value: Value) = op match { diff --git a/src/test/scala/genwasym/TestEval.scala b/src/test/scala/genwasym/TestEval.scala index f2d66b15..1cdb535c 100644 --- a/src/test/scala/genwasym/TestEval.scala +++ b/src/test/scala/genwasym/TestEval.scala @@ -42,8 +42,19 @@ class TestEval extends FunSuite { test("even-odd") { testFile("./benchmarks/wasm/even_odd.wat", None, Some(1)) } test("return") { testFile("./benchmarks/wasm/return.wat", None, None) } test("load") { testFile("./benchmarks/wasm/load.wat", None, Some(1)) } - test("btree") { testFile("./benchmarks/wasm/btree/2o1u-unlabeled.wat") } test("fib") { testFile("./benchmarks/wasm/fib.wat", None, Some(144)) } + test("div_s") { testFile("./benchmarks/wasm/div_s.wat", None, Some(-2)) } + + // for btree, the `main` function already did the checks + test("2o1u") { testFile("./benchmarks/wasm/btree/2o1u-unlabeled.wat") } + test("2o2u") { testFile("./benchmarks/wasm/btree/2o2u-unlabeled.wat") } + test("2o3u") { testFile("./benchmarks/wasm/btree/2o3u-unlabeled.wat") } + + test("3o1u") { testFile("./benchmarks/wasm/btree/3o1u-unlabeled.wat") } + test("3o2u") { testFile("./benchmarks/wasm/btree/3o2u-unlabeled.wat") } + test("3o3u") { testFile("./benchmarks/wasm/btree/3o3u-unlabeled.wat") } + + test("9o2u") { testFile("./benchmarks/wasm/btree/9o2u-unlabeled.wat") } // TODO: add wasm spec tests? How to utilize wast files? } From 099b95c8793b2674a04173b5fd0cd108241706f4 Mon Sep 17 00:00:00 2001 From: ahuoguo Date: Mon, 7 Oct 2024 22:48:13 -0400 Subject: [PATCH 2/3] forgor 8o2u --- benchmarks/wasm/btree/8o2u.wat | 3047 ++++++++++++++++++++++++++++++++ 1 file changed, 3047 insertions(+) create mode 100644 benchmarks/wasm/btree/8o2u.wat diff --git a/benchmarks/wasm/btree/8o2u.wat b/benchmarks/wasm/btree/8o2u.wat new file mode 100644 index 00000000..3bdec048 --- /dev/null +++ b/benchmarks/wasm/btree/8o2u.wat @@ -0,0 +1,3047 @@ +(module + (memory $0 2) + (func $createBtree (param i32) (result i32) ;; createBtree(t), where t: degree of the btree + (i32.const 0) + (local.get 0) + (i32.store) ;; store t at address 0 + (i32.const 0) + (i32.const 1) + (i32.store offset=4) ;; store the number of nodes in the btree = 1 + (i32.const 0) + (i32.const 65536) + (i32.store offset=8) ;; store the root addr + (i32.const 1) ;; Now create root + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then + (i32.const 65536) ;; 64KiB = 65536 bytes + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + (i32.const 65536) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + ) + ) + (i32.const 65536) ;; returns the address of the root + ) + ;; btreeSearch(x, k), where x: node address; k: key to search. Returns address of the key or -1 if key is not in tree. + (func $btreeSearch (param i32) (param i32) (result i32) (local i32) + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n -1 + (i32.le_s) ;; i <= x.children-1 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + (if + (then + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.children + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.le_s) ;; i <= x.children - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.eq) + ;; k == x.keys[i] + (if (result i32) + (then + (local.get 0) ;; save the address, which is x+8+i*4 + (i32.const 8) + (i32.add) ;; x+8 + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x+8+i*4 + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + (else + (local.get 0) ;; x + (i32.load) ;; x is leaf? + (i32.const 1) ;; if == 1, then yes + (i32.eq) + (if (result i32) + (then + (i32.const -1) + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i + (local.get 1) ;; k + (call $btreeSearch) ;; btreeSearch(x.children[i], k) + ) + ) + ) + ) + ) + ;; btreeSplitChild(x, i), where x: addr of a non full internal node; i: index such that x.children[i] is a full child of x + (func $btreeSplitChild (param i32) (param i32) (local i32) (local i32) + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if ;; if we can grow memory + (then ;; get the number of nodes in the tree + (i32.const 0) + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address is 64KiB*(number of nodes+1) + (local.set 2) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the addr of the child in index i : y + (i32.load) ;; check if x.children[i] is leaf ;; load the first i32 in the child + (i32.const 1) + (i32.eq) ;; is y leaf? + (if + (then ;; yes + (local.get 2) ;; addr of new node z + (i32.const 1) + (i32.store) ;; store 1(TRUE) regarding if node is leaf + ) + (else ;; no + (local.get 2) ;; addr of new node z + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + ) + ) + (local.get 2) ;; now store number of children, which will be t/2 -1 ;; addr of z + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.store offset=4) ;; store number of children = (t/2)-1 + (i32.const 0) ;; now to store the keys + (local.set 3) ;; counter = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; (t/2)-1 + (i32.eq) ;; counter == (t/2)-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 2) ;; addr of z ;; we will store in the new node's address. z.keys[counter] + (i32.const 4) + (local.get 3) ;; counter + (i32.mul) ;; counter * 4 + (i32.add) ;; addr + counter * 4 -> where the key is goint to be located + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 4) ;; now get x.children[i].keys[counter+(t/2)] + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + (t/2) + (i32.mul) ;; counter + (t/2) *4 + (i32.add) ;; y + counter + (t/2) *4 + (i32.load offset=8) ;; y.keys[counter + (t/2)] + (i32.store offset=8) ;; z.keys[counter] = y.keys[counter + (t/2)] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) + (local.set 3) ;; set counter back to 0 + (i32.const 0) ;; check if x.children[i] is leaf + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + i*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load) ;; get first i32 in child --> isLeaf + (i32.const 1) + (i32.ne) ;; is leaf? + (if + (then ;; not leaf so we have to update the children + (block $loop_break + (loop $loop + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.eq) ;; counter == (t/2) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; now get z.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; counter*4 + (i32.add) ;; (t-1)*4 + counter*4 + (local.get 2) ;; z + (i32.add) ;; z + (t-1)*4 + counter*4 --> address where addr of child is stored + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) ;; get x.children[i].children[counter+(t/2)] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.add) ;; counter + t/2 + (i32.const 4) + (i32.mul) ;; counter+t/2 * 4 + (i32.add) ;; (t-1)*4 + (counter+(t/2))*4 + (i32.add) ;; x.children[i] + (t-1)*4 + (counter + (t/2))*4 + (i32.load offset=8) ;; load the address of the child in index counter + t/2 + (i32.store offset=8) ;; z.children[counter] = x.children[i].children[counter] + (local.get 3) ;; increment counter + (i32.const 1) + (i32.add) + (local.set 3) + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.store offset=4) ;; number of keys of x.children[i] is now t/2 -1 + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (local.set 3) ;; counter = x.n + (block $loop_break + (loop $loop + (local.get 1) ;; i + (local.get 3) ;; counter + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; get x.children[counter + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (i32.add) ;; (t-1)*4 + (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter + 1)*4 -->address where addr of child is stored + (i32.const 0) ;; get x.children[counter] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (i32.add) ;; (t-1)*4 + (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (counter)*4 + (i32.load offset=8) ;; load the addr of the child in index (counter) + (i32.store offset=8) ;; x.children[counter+1] = x.children[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 1) + (i32.add) ;; i + 1 + (i32.const 4) + (i32.mul) ;; (i + 1)*4 + (i32.add) ;; (t-1)*4 + (i + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i + 1)*4 --> addr where we will store the new addr of the child + (local.get 2) ;; addr of z + (i32.store offset=8) ;; x.children[i+1] = z + (local.get 0) ;; x + (i32.load offset=4) ;; get x.n + (i32.const 1) + (i32.sub) + (local.set 3) ;; counter = x.n -1 + (block $loop_break + (loop $loop + (local.get 1) ;; i + (i32.const 1) + (i32.sub) ;; i-1 + (local.get 3) ;; counter + (i32.eq) ;; counter == i-1 + (if + (then + (br $loop_break) + ) + (else + (local.get 3) ;; get x.keys[counter + 1] ;; counter + (i32.const 1) + (i32.add) ;; counter + 1 + (i32.const 4) + (i32.mul) ;; (counter + 1)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter + 1)*4 --> addr where we will store the new key + (local.get 3) ;; counter + (i32.const 4) + (i32.mul) ;; (counter)*4 + (local.get 0) ;; x + (i32.add) ;; x +(counter)*4 + (i32.load offset=8) ;; load the key in index (counter) + (i32.store offset=8) ;; x.keys[counter+1] = x.keys[counter] + (local.get 3) ;; decrement counter + (i32.const 1) + (i32.sub) + (local.set 3) + (br $loop) + ) + ) + ) + ) + (local.get 1) ;; get x.keys[i] + (i32.const 4) + (i32.mul) ;; i*4 + (local.get 0) ;; x + (i32.add) ;; x +i*4 --> addr of the key + (i32.const 0) ;; get x.children[i].keys[t/2] + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; t/2 -1 + (i32.const 4) + (i32.mul) ;; (t/2)-1 *4 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 1) ;; i + (i32.const 4) + (i32.mul) ;; (i)*4 + (i32.add) ;; (t-1)*4 + (i)*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i)*4 + (i32.load offset=8) ;; load the address of the child in index (i) + (i32.add) ;; y + (t/2)-1 *4 + (i32.load offset=8) ;; load the key in index (t/2)-1 from node x.children[i] + (i32.store offset=8) ;; store the new key in the addr + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) ;; x.n + 1 + (i32.store offset=4) ;; x.n = x.n + 1 + ) + ) + ) + ;; btreeInsertNonFull(x, k), where x: addr of a non full internal node; k: the key to insert + (func $btreeInsertNonFull (param i32) (param i32) (local i32) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n -1 + (local.set 2) ;; i = x.n -1 + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + ;; x+4*i + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i <= 0? + (i32.and) + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i *4 + (i32.add) ;; x + i *4 --> addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.keys[i+1] = x.keys[i] + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.mul) ;; i+1 *4 + (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] + (local.get 1) ;; k + (i32.store offset=8) ;; x.keys[i+1] = k + (local.get 0) + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; x.n = x.n +1 + ) + (else ;; x is not leaf + (block $loop_break + (loop $loop + (local.get 2) ;; i + (i32.const 0) + (i32.ge_s) ;; i >= 0 + (if + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.lt_s) ;; k < x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i -1 + (local.set 2) ;; i = i-1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 2) ;;i = i+1 + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address of the child in index i = y + (i32.load offset=8) ;; load the address of the child in index i = y + (i32.load offset=4) ;; get y.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; y.n == t-1 --> node is full + (if + (then + (local.get 0) ;; x + (local.get 2) ;; i + (call $btreeSplitChild) + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k>x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i+1 + ) + ) + ) + ) + (i32.const 0) ;; get x.children[i] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; load the address of the child in index i = y + (local.get 1) ;; k + (call $btreeInsertNonFull) + ) + ) + ) + (func $btreeInsert (param i32) (result i32) (local i32) (local i32) + (i32.const 0) + (i32.load offset=8) ;; root addr + (local.tee 2) ;; set r + (i32.load offset=4) ;; r.n + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.eq) ;; r.n == t-1 --> root is full + (if (result i32) + (then + (i32.const 1) ;; create a new node + (memory.grow) ;; create new page + (i32.const -1) + (i32.ne) + (if (result i32) ;; if we can grow memory + (then + (i32.const 0) ;; get the number of nodes in the tree + (i32.load offset=4) ;; number of nodes in the tree + (i32.const 1) + (i32.add) ;; number of nodes + 1 + (i32.const 65536) + (i32.mul) ;; address of s is 64KiB*(number of nodes+1) + (local.set 1) + (i32.const 0) ;; change number of nodes + (i32.const 0) + (i32.load offset=4) + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; set number of nodes to + 1 + (i32.const 0) + (local.get 1) ;; s + (i32.store offset=8) ;; root addr is now s + (local.get 1) ;; s + (i32.const 0) + (i32.store) ;; store 0(FALSE) regarding if node is leaf + (local.get 1) ;; now store number of keys + (i32.const 0) + (i32.store offset=4) ;; store number of keys = 0 + (i32.const 0) ;; store children addresses + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 1) ;; x + (i32.add) ;; x + (t-1)*4 --> addre where addr of child number 0 is located + (local.get 2) + (i32.store offset=8) ;; s.children[0] = r + (local.get 1) ;; s + (i32.const 0) + (call $btreeSplitChild) + (local.get 1) ;; s + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 1) ;; return the address of the new root + ) + (else + (i32.const -1) ;; return -1 because we could not create a new node + ) + ) + ) + (else + (local.get 2) ;; r + (local.get 0) ;; k + (call $btreeInsertNonFull) + (local.get 2) ;; return the address of the root, which is the same + ) + ) + ) + ;; Returns the address of the new root (if applicable, otherwise returns the addres of the root) + (func $btreeDelete (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local i32) + (local.get 0) ;; x + (i32.load) ;; get first i32 --> isLeaf + (i32.const 1) + (i32.eq) ;; is leaf? + (if + (then ;; x is leaf + (i32.const 0) + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (local.get 2) ;; i + (i32.eq) ;; i == x.n + (if + (then + (br $loop_break) ;; break from loop + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + (if + (then + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $while_break + (loop $while + (local.get 3) ;; j + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n-1 + (i32.eq) ;; j == x.n-1 + (if + (then + (br $while_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 --> addr where x.keys[j] is stored + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.mul) ;; j+1 *4 + (i32.add) ;; x + j+1 *4 --> addr where x.keys[j+1] is stored + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $while) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n -1 + (br $loop_break) + ) + ) + ) + ) + (i32.const 1) + (local.get 2) + (i32.add) + (local.set 2) ;; i = i+1 + (br $loop) ;; continue + ) + ) + ) + (else ;; x is not leaf + (i32.const 0) ;;get the index of the appropriate child/key + (local.set 2) ;; i = 0 + (block $loop_break + (loop $loop + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (if (result i32) + (then + (local.get 1) ;; k + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (i32.gt_s) ;; k > x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (local.get 2) ;; i + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.le_s) ;; i <= x.n - 1 + (i32.and) ;; i <= x.n - 1 && k > x.keys[i] + (if + (then + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (local.set 2) ;; i = i + 1 + (br $loop) + ) + (else + (br $loop_break) + ) + ) + ) + ) + (local.get 2) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.lt_s) ;; i < x.n + (if (result i32) + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[i] + (local.get 1) ;; k + (i32.eq) ;; k == x.keys[i] + ) + (else + (i32.const 0) + ) + ) + (if + (then ;; key is present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.tee 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.sub) ;; index = x.c[i].n - 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] + (call $btreeDelete) ;; (x.c[i], x.c[i].keys[x.c[i].n - 1]) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i].keys[x.c[i].n - 1] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i+1] + (local.tee 5) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (local.get 5) ;; x.c[i+1] + (local.get 5) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[0] + (call $btreeDelete) + (drop) + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + ) + (else + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i *4 + (i32.add) ;; x + i*4 --> addr where x.c[i].keys[x.c[i].n] will be stored + (local.get 1) ;; k + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = k + (i32.const 0) ;; now merge x.c[i] with x.c[i+1] + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if ;; j == x.c[i+1].n + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n + j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[index] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x.c[i+1] + i*4 ;; addr of x.c[i+1].keys[j] + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[index] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n +1 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + 1 + x.c[i+1].n + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; now adjust the children + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.get 3) ;; j + (i32.add) ;; index = x.c[i].n+j + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x.c[i] + (t-1)*4 --> address where x.c[i].c[x.c[i].n+j] is stored + (i32.const 0) ;;get x.c[i+1].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x.c[i+1] + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (i32.const 1) + (i32.sub) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; j*4 + (i32.add) ;; x + j*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.add) + (i32.mul) ;; j+1*4 + (i32.add) ;; x + j+1*4 ;; addr of x.keys[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 2) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = i+1 + (block $loop_break + (loop $loop + (local.get 0) + (i32.load offset=4) + (local.get 3) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.add) ;; j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 0) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.n = x.n-1 + (local.get 5) ;; x.c[i] + (local.get 1) ;; k + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) + (else ;; key not present in node x + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 4) + (i32.mul) ;; i*4 + (i32.add) ;; (t-1)*4 + i*4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located + (i32.load offset=8) ;; x.c[i] + (local.set 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) + (i32.const 1) + (i32.sub) ;; t/2 - 1 + (i32.eq) + (if + (then + (i32.const -1) + (local.set 4) ;; changed = -1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.le_s) ;; i+1 <= x.n + (if + (then + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i+1].n >= t/2 + (if + (then + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.const 1) + (i32.sub) ;; index = t/2 -1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[t/2-1] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[t/2-1] = x.keys[i] + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;;x.c[i].n = x.c[i].n +1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i+1].keys[0] + (i32.load offset=8) ;; x.c[i+1].keys[0] + (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] + (local.get 5) + (i32.load) ;; x.c[i] is leaf? + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) ;;get x.c[i].c[t/2] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; index = t/2 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[t/2] + (i32.const 0) ;;get x.c[i+1].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[0] + (i32.store offset=8) ;; x.c[i].c[t/2] = x.c[i+1].c[0] + ) + ) + (local.get 5) ;; x.c[i] + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.store offset=4) ;; x.c[i].n = t/2 + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 --> address where x.c[i+1].keys[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j+1] + (i32.store offset=8) ;; x.c[i+1].keys[j] = x.c[i+1].keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if ;; if x.c[i] is not leaf, we have to adjust children + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i+1].c[j] is located + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j+1 + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j+1] + (i32.store offset=8) ;; x.c[i+1].c[j] = x.c[i+1].c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j+1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr where x.c[i+1] is located + (i32.load offset=8) + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i+1].n = x.c[i+1].n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i -1 >= 0 + (i32.and) ;; changed == -1 && i -1 >= 0 + (if + (then + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 0) + (i32.load) ;; t + (i32.const 2) + (i32.div_s) ;; t/2 + (i32.ge_s) ;; x.c[i-1].n >= t/2 + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (local.set 3) ;; j = x.c[i].n + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j-1] + (i32.load offset=8) ;; x.c[i].keys[j-1] + (i32.store offset=8) ;; x.c[i].keys[j] = x.c[i].keys[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (local.set 3) ;; j = x.c[i].n + 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> address where x.c[i].c[j] is located + (i32.const 0) ;;get x.c[i].c[j-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; j + (i32.const 1) + (i32.sub) ;; j-1 + (i32.const 4) + (i32.mul) ;; j *4 + (i32.add) ;; (t-1)*4 + j *4 + (local.get 5) ;; x.c[i] + (i32.add) + (i32.load offset=8) ;; x.c[i].c[j-1] + (i32.store offset=8) ;; x.c[i].c[j] = x.c[i].c[j-1] + (local.get 3) + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = j-1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) + (i32.store offset=4) ;; ;; x.c[i].n = x.c[i].n + 1 + (local.get 5) ;; x.c[i] + (i32.const 4) + (i32.const 0) ;; index = 0 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[0] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i-1] + (i32.store offset=8) ;; x.c[i].keys[0] = x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.sub) + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n - 1 + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (i32.load offset=8) + (i32.store offset=8) ;; x.keys[i-1] = x.c[i-1].keys[x.c[i-1].n] + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) ;; get x.c[i].c[0] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 ;; addr where x.c[i].c[0] is located + (i32.const 0) ;; get x.c[i-1].c[x.c[i-1].n + 1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (i32.const 1) + (i32.add) ;; index = x.c[i-1.n + 1] + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1].c[x.c[i-1].n + 1] + (i32.store offset=8) ;; x.c[i].c[0] = x.c[i-1].c[x.c[i-1].n + 1] + ) + ) + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) + (if ;; changed == -1? + (then + (local.get 2) ;; we have to merge x.c[i] with one sibling;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.le_s) + (if ;; i+1 <= x.n + (then + (local.get 5) ;; merge with right sibling;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) ;; index = x.c[i].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = x.keys[i] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n+j] + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i+1].keys[j] + (i32.store offset=8) ;; x.c[i].keys[x.c[i].n+j] = x.c[i+1].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i+1].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 5) + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[x.c[i].n+j] + (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1].c[j] + (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (local.get 5) ;; x.c[i] + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 0) ;;get x.c[i+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.add) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i+1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i+1] + (i32.load offset=4) ;; x.c[i+1].n + (i32.add) ;; x.c[i].n + x.c[i+1].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i].n = x.c[i].n + x.c[i+1].n + 1 + (local.get 2) ;; i + (i32.const 1) + (i32.add) ;; i+1 + (local.set 3) ;; j = i+1 + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + (i32.const 0) + (local.set 4) ;; changed = 0 + ) + ) + (local.get 4) ;; changed + (i32.const -1) + (i32.eq) ;; changed == -1 ? + (local.get 2) ;; i + (i32.const 1) + (i32.sub) ;; i - 1 + (i32.const 0) + (i32.ge_s) ;; i - 1 >= 0? + (i32.and) ;; changed == -1 && i - 1 >= 0 + (if + (then + (i32.const 0) ;; merge with left sibling ;;get x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; index = x.c[i-1].n + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] + (local.get 0) ;; x + (i32.const 4) + (local.get 2) ;; index = i + (i32.const 1) + (i32.sub) ;; index = i-1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] + (i32.load offset=8) ;; x.keys[i] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n] = x.keys[i-1] + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;; x.c[i-1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 4) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n+j] + (local.get 5) ;; x.c[i] + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.c[i].keys[j] + (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n+j] = x.c[i].keys[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 5) + (i32.load) + (i32.const 1) + (i32.ne) + (if + (then + (i32.const 0) + (local.set 3) ;; j = 0 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 5) ;; x.c[i] + (i32.load offset=4) ;; x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + 1 + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[i-1].c[x.c[i-1].n+j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) + (local.get 3) + (i32.add) + (i32.const 1) + (i32.add) ;; index = x.c[i-1].n+j+1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.add) ;; x + (t-1)*4 --> addr of x.c[i-1].c[x.c[i-1].n+j] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 5) ;; x.c[i] + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i].c[j] + (i32.store offset=8) ;; x.c[i-1].c[x.c[i-1].n+j] = x.c[i].c[j] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + ) + ) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (i32.load offset=4) ;; x.c[i-1].n + (local.get 5) + (i32.load offset=4) ;; x.c[i].n + (i32.add) ;; x.c[i-1].n + x.c[i].n + (i32.const 1) + (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 + (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n + x.c[i].n + 1 + (local.get 2) ;; i + (local.set 3) ;; j = i + (block $loop_break ;; re-organize x + (loop $loop + (local.get 3) + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (i32.const 0) ;;get x.c[j] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) ;; index = j + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.const 0) ;;get x.c[j+1] + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.const 4) + (i32.mul) ;; i-1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] + (i32.load offset=8) ;; x.c[j+1] + (i32.store offset=8) ;; x.c[j] = x.c[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (local.set 3) ;; j = i - 1 + (block $loop_break + (loop $loop + (local.get 3) ;; j + (local.get 0) + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) + (i32.eq) + (if + (then + (br $loop_break) + ) + (else + (local.get 0) ;; x + (i32.const 4) + (local.get 3) ;; index = j + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 ;; addr of x.keys[j] + (local.get 0) ;; x + (i32.const 4) + (local.get 3) + (i32.const 1) + (i32.add) ;; index = j + 1 + (i32.mul) ;; i*4 + (i32.add) ;; x + i*4 + (i32.load offset=8) ;; x.keys[j+1] + (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] + (local.get 3) + (i32.const 1) + (i32.add) + (local.set 3) ;; j = j + 1 + (br $loop) + ) + ) + ) + ) + (local.get 0) ;; x + (local.get 0) ;; x + (i32.load offset=4) ;; x.n + (i32.const 1) + (i32.sub) ;; x.n - 1 + (i32.store offset=4) ;; x.n = x.n - 1 + ) + ) + ) + ) + ) + ) + (local.get 4) + (i32.const -1) + (i32.eq) + (if ;; changed == -1? if yes, we merged with left sibling + (then + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (local.get 2) ;; i + (i32.const 1) + (i32.sub) + (i32.const 4) + (i32.mul) ;; i+1 *4 + (i32.add) ;; (t-1)*4 + i-1 *4 + (local.get 0) ;; x + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; x.c[i-1] + (local.get 1) + (call $btreeDelete) + (drop) + ) + (else + (local.get 5) ;; x.c[i] + (local.get 1) + (call $btreeDelete) + (drop) + ) + ) + ) + ) + ) ;; end of if x is not leaf + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.load offset=4) + (i32.const 0) + (i32.eq) + (if ;; if root is empty + (then + (i32.const 0) + (i32.const 0) + (i32.load) ;; t + (i32.const 1) + (i32.sub) ;; t-1 + (i32.const 4) + (i32.mul) ;; (t-1)*4 + (i32.const 0) ;; index = 0 + (i32.const 4) + (i32.mul) ;; i *4 + (i32.add) ;; (t-1)*4 + i *4 + (i32.const 0) + (i32.load offset=8) ;; root addr + (i32.add) ;; x + (t-1)*4 + (i32.load offset=8) ;; root.c[0] + (i32.store offset=8) ;; root = root.c[0] + ) + ) + (i32.const 0) + (i32.load offset=8) ;; root addr + ) + (func $main + (param $a i32) + (param $b i32) + (param $c i32) + (param $d i32) + (param $e i32) + (param $f i32) + (param $g i32) + (param $x i32) + (param $h i32) + (param $i i32) + (local i32) + (local.get $a) + (local.get $b) + (i32.gt_s) + (local.get $b) + (local.get $c) + (i32.gt_s) + (local.get $c) + (local.get $d) + (i32.gt_s) + (local.get $d) + (local.get $e) + (i32.gt_s) + (local.get $e) + (local.get $f) + (i32.gt_s) + (local.get $f) + (local.get $g) + (i32.gt_s) + (local.get $g) + (local.get $x) + (i32.gt_s) + (local.get $h) + (local.get $a) + (i32.ne) + (local.get $h) + (local.get $b) + (i32.ne) + (local.get $h) + (local.get $c) + (i32.ne) + (local.get $h) + (local.get $d) + (i32.ne) + (local.get $h) + (local.get $e) + (i32.ne) + (local.get $h) + (local.get $f) + (i32.ne) + (local.get $h) + (local.get $g) + (i32.ne) + (local.get $h) + (local.get $x) + (i32.ne) + (local.get $i) + (local.get $a) + (i32.ne) + (local.get $i) + (local.get $b) + (i32.ne) + (local.get $i) + (local.get $c) + (i32.ne) + (local.get $i) + (local.get $d) + (i32.ne) + (local.get $i) + (local.get $e) + (i32.ne) + (local.get $i) + (local.get $f) + (i32.ne) + (local.get $i) + (local.get $g) + (i32.ne) + (local.get $i) + (local.get $x) + (i32.ne) + (local.get $i) + (local.get $h) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.eqz) + (if (then (unreachable))) + (i32.const 4) ;;create a tree with degree 4 + (call $createBtree) + (local.set 10) + ;; insert variables + (local.get $a) + (call $btreeInsert) + (local.set 10) + (local.get $b) + (call $btreeInsert) + (local.set 10) + (local.get $c) + (call $btreeInsert) + (local.set 10) + (local.get $d) + (call $btreeInsert) + (local.set 10) + (local.get $e) + (call $btreeInsert) + (local.set 10) + (local.get $f) + (call $btreeInsert) + (local.set 10) + (local.get $g) + (call $btreeInsert) + (local.set 10) + (local.get $x) + (call $btreeInsert) + (local.set 10) + (local.get $h) + (call $btreeInsert) + (local.set 10) + (local.get $i) + (call $btreeInsert) + (local.set 10) + ;; search for variables & check that they were inserted + (local.get 10) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 10) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 10) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 10) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 10) + (local.get $e) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 10) + (local.get $f) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 10) + (local.get $g) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 10) + (local.get $x) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 10) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (local.get 10) + (local.get $i) + (call $btreeSearch) + (i32.const -1) + (i32.ne) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + ;; delete & check that it was deleted + ;; a + (local.get 10) + (local.get $a) + (call $btreeDelete) + (local.set 10) + (local.get 10) + (local.get $a) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; b + (local.get 10) + (local.get $b) + (call $btreeDelete) + (local.set 10) + (local.get 10) + (local.get $b) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; c + (local.get 10) + (local.get $c) + (call $btreeDelete) + (local.set 10) + (local.get 10) + (local.get $c) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; d + (local.get 10) + (local.get $d) + (call $btreeDelete) + (local.set 10) + (local.get 10) + (local.get $d) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; e + (local.get 10) + (local.get $e) + (call $btreeDelete) + (local.set 10) + (local.get 10) + (local.get $e) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; f + (local.get 10) + (local.get $f) + (call $btreeDelete) + (local.set 10) + (local.get 10) + (local.get $f) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; g + (local.get 10) + (local.get $g) + (call $btreeDelete) + (local.set 10) + (local.get 10) + (local.get $g) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; x + (local.get 10) + (local.get $x) + (call $btreeDelete) + (local.set 10) + (local.get 10) + (local.get $x) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + ;; h + (local.get 10) + (local.get $h) + (call $btreeDelete) + (local.set 10) + (local.get 10) + (local.get $h) + (call $btreeSearch) + (i32.const -1) + (i32.eq) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (i32.and) + (drop)) + (export "main" (func $main))) From fde10787768e8591b0a0f57a09f771b9334a7f6e Mon Sep 17 00:00:00 2001 From: ahuoguo Date: Tue, 8 Oct 2024 11:54:17 -0400 Subject: [PATCH 3/3] rm extra files --- benchmarks/wasm/btree/2o1u-fd_write.wat | 2877 ----------------------- benchmarks/wasm/btree/2o1u-orig.wat | 2819 ---------------------- 2 files changed, 5696 deletions(-) delete mode 100644 benchmarks/wasm/btree/2o1u-fd_write.wat delete mode 100644 benchmarks/wasm/btree/2o1u-orig.wat diff --git a/benchmarks/wasm/btree/2o1u-fd_write.wat b/benchmarks/wasm/btree/2o1u-fd_write.wat deleted file mode 100644 index e0fe0d79..00000000 --- a/benchmarks/wasm/btree/2o1u-fd_write.wat +++ /dev/null @@ -1,2877 +0,0 @@ -(module - (import "wasi_snapshot_preview1" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32))) - (memory $0 2) - (export "memory" (memory 0)) - (data (i32.const 1024) "hello world\n") - (func $createBtree (param i32) (result i32) ;; createBtree(t), where t: degree of the btree - ;; (i32.store (i32.const 0) (i32.const 1024)) ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string - ;; (i32.store (i32.const 4) (i32.const 12)) ;; iov.iov_len - The length of the 'hello world\n' string - ;; (call $fd_write - ;; (i32.const 1) ;; file_descriptor - 1 for stdout - ;; (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0 - ;; (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one. - ;; (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written - ;; ) - ;; drop - (i32.const 0) - (local.get 0) - (i32.store) ;; store t at address 0 - (i32.const 0) - (i32.const 1) - (i32.store offset=4) ;; store the number of nodes in the btree = 1 - (i32.const 0) - (i32.const 65536) - (i32.store offset=8) ;; store the root addr - (i32.const 1) ;; Now create root - (memory.grow) ;; create new page - (i32.const -1) - (i32.ne) - (if ;; if we can grow memory - (then - (i32.const 65536) ;; 64KiB = 65536 bytes - (i32.const 1) - (i32.store) ;; store 1(TRUE) regarding if node is leaf - (i32.const 65536) ;; now store number of keys - (i32.const 0) - (i32.store offset=4) ;; store number of keys = 0 - ) - ) - (i32.const 65536) ;; returns the address of the root - ) - ;; btreeSearch(x, k), where x: node address; k: key to search. Returns address of the key or -1 if key is not in tree. - (func $btreeSearch (param i32) (param i32) (result i32) (local i32) - ;; (i32.store (i32.const 0) (i32.const 8)) ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string - ;; (i32.store (i32.const 4) (i32.const 12)) ;; iov.iov_len - The length of the 'hello world\n' string - ;; (call $fd_write - ;; (i32.const 1) ;; file_descriptor - 1 for stdout - ;; (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0 - ;; (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one. - ;; (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written - ;; ) - ;; drop - (i32.const 0) - (local.set 2) ;; i = 0 - (block $loop_break - (loop $loop - (local.get 2) ;; i - (local.get 0) ;; x - (i32.load offset=4) ;; x.children - (i32.const 1) - (i32.sub) ;; x.n -1 - (i32.le_s) ;; i <= x.children-1 - (if - (then - (local.get 1) ;; k - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.keys[i] - (i32.gt_s) ;; k > x.keys[i] - (if - (then - (i32.const 1) - (local.get 2) - (i32.add) - (local.set 2) ;; i = i+1 - (br $loop) - ) - (else - (br $loop_break) - ) - ) - ) - (else - (br $loop_break) - ) - ) - ) - ) - (local.get 2) ;; i - (local.get 0) ;; x - (i32.load offset=4) ;; x.children - (i32.const 1) - (i32.sub) ;; x.n - 1 - (i32.le_s) ;; i <= x.children - 1 - (if (result i32) - (then - (local.get 1) ;; k - (local.get 0) ;; x - ;; x+4*i - (i32.const 4) - (local.get 2) ;; i - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.keys[i] - (i32.eq) - ;; k == x.keys[i] - (if (result i32) - (then - (local.get 0) ;; save the address, which is x+8+i*4 - (i32.const 8) - (i32.add) ;; x+8 - (i32.const 4) - (local.get 2) ;; i - (i32.mul) ;; i*4 - (i32.add) ;; x+8+i*4 - ) - (else - (local.get 0) ;; x - (i32.load) ;; x is leaf? - (i32.const 1) ;; if == 1, then yes - (i32.eq) - (if (result i32) - (then - (i32.const -1) - ) - (else - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 4) - (i32.mul) ;; i*4 - (i32.add) ;; (t-1)*4 + i*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 + i*4 - (i32.load offset=8) ;; load the address of the child in index i - (local.get 1) ;; k - (call $btreeSearch) ;; btreeSearch(x.children[i], k) - ) - ) - ) - ) - ) - (else - (local.get 0) ;; x - (i32.load) ;; x is leaf? - (i32.const 1) ;; if == 1, then yes - (i32.eq) - (if (result i32) - (then - (i32.const -1) - ) - (else - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 4) - (i32.mul) ;; i*4 - (i32.add) ;; (t-1)*4 + i*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 + i*4 - (i32.load offset=8) ;; load the address of the child in index i - (local.get 1) ;; k - (call $btreeSearch) ;; btreeSearch(x.children[i], k) - ) - ) - ) - ) - ) - ;; btreeSplitChild(x, i), where x: addr of a non full internal node; i: index such that x.children[i] is a full child of x - (func $btreeSplitChild (param i32) (param i32) (local i32) (local i32) - ;; (i32.store (i32.const 0) (i32.const 8)) ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string - ;; (i32.store (i32.const 4) (i32.const 12)) ;; iov.iov_len - The length of the 'hello world\n' string - ;; (call $fd_write - ;; (i32.const 1) ;; file_descriptor - 1 for stdout - ;; (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0 - ;; (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one. - ;; (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written - ;; ) - ;; drop - (i32.const 1) ;; create a new node - (memory.grow) ;; create new page - (i32.const -1) - (i32.ne) - (if ;; if we can grow memory - (then ;; get the number of nodes in the tree - (i32.const 0) - (i32.load offset=4) ;; number of nodes in the tree - (i32.const 1) - (i32.add) ;; number of nodes + 1 - (i32.const 65536) - (i32.mul) ;; address is 64KiB*(number of nodes+1) - (local.set 2) - (i32.const 0) ;; change number of nodes - (i32.const 0) - (i32.load offset=4) - (i32.const 1) - (i32.add) - (i32.store offset=4) ;; set number of nodes to + 1 - (i32.const 0) ;; get x.children[i] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 1) ;; i - (i32.const 4) - (i32.mul) ;; i*4 - (i32.add) ;; (t-1)*4 + i*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 + i*4 - (i32.load offset=8) ;; load the addr of the child in index i : y - (i32.load) ;; check if x.children[i] is leaf ;; load the first i32 in the child - (i32.const 1) - (i32.eq) ;; is y leaf? - (if - (then ;; yes - (local.get 2) ;; addr of new node z - (i32.const 1) - (i32.store) ;; store 1(TRUE) regarding if node is leaf - ) - (else ;; no - (local.get 2) ;; addr of new node z - (i32.const 0) - (i32.store) ;; store 0(FALSE) regarding if node is leaf - ) - ) - (local.get 2) ;; now store number of children, which will be t/2 -1 ;; addr of z - (i32.const 0) - (i32.load) ;; t - (i32.const 2) - (i32.div_s) ;; t/2 - (i32.const 1) - (i32.sub) ;; (t/2)-1 - (i32.store offset=4) ;; store number of children = (t/2)-1 - (i32.const 0) ;; now to store the keys - (local.set 3) ;; counter = 0 - (block $loop_break - (loop $loop - (local.get 3) ;; counter - (i32.const 0) - (i32.load) ;; t - (i32.const 2) - (i32.div_s) ;; t/2 - (i32.const 1) - (i32.sub) ;; (t/2)-1 - (i32.eq) ;; counter == (t/2)-1 - (if - (then - (br $loop_break) - ) - (else - (local.get 2) ;; addr of z ;; we will store in the new node's address. z.keys[counter] - (i32.const 4) - (local.get 3) ;; counter - (i32.mul) ;; counter * 4 - (i32.add) ;; addr + counter * 4 -> where the key is goint to be located - (i32.const 0) ;; get x.children[i] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 1) ;; i - (i32.const 4) - (i32.mul) ;; i*4 - (i32.add) ;; (t-1)*4 + i*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 + i*4 - (i32.load offset=8) ;; load the address of the child in index i = y - (i32.const 4) ;; now get x.children[i].keys[counter+(t/2)] - (local.get 3) ;; counter - (i32.const 0) - (i32.load) ;; t - (i32.const 2) - (i32.div_s) ;; t/2 - (i32.add) ;; counter + (t/2) - (i32.mul) ;; counter + (t/2) *4 - (i32.add) ;; y + counter + (t/2) *4 - (i32.load offset=8) ;; y.keys[counter + (t/2)] - (i32.store offset=8) ;; z.keys[counter] = y.keys[counter + (t/2)] - (local.get 3) ;; increment counter - (i32.const 1) - (i32.add) - (local.set 3) - (br $loop) - ) - ) - ) - ) - (i32.const 0) - (local.set 3) ;; set counter back to 0 - (i32.const 0) ;; check if x.children[i] is leaf - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 1) ;; i - (i32.const 4) - (i32.mul) ;; i*4 - (i32.add) ;; (t-1)*4 + i*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 + i*4 - (i32.load offset=8) ;; load the address of the child in index i = y - (i32.load) ;; get first i32 in child --> isLeaf - (i32.const 1) - (i32.ne) ;; is leaf? - (if - (then ;; not leaf so we have to update the children - (block $loop_break - (loop $loop - (local.get 3) ;; counter - (i32.const 0) - (i32.load) ;; t - (i32.const 2) - (i32.div_s) ;; t/2 - (i32.eq) ;; counter == (t/2) - (if - (then - (br $loop_break) - ) - (else - (i32.const 0) ;; now get z.children[counter] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) ;; counter - (i32.const 4) - (i32.mul) ;; counter*4 - (i32.add) ;; (t-1)*4 + counter*4 - (local.get 2) ;; z - (i32.add) ;; z + (t-1)*4 + counter*4 --> address where addr of child is stored - (i32.const 0) ;; get x.children[i] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 1) ;; i - (i32.const 4) - (i32.mul) ;; i*4 - (i32.add) ;; (t-1)*4 + i*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; load the address of the child in index i = y - (i32.const 0) ;; get x.children[i].children[counter+(t/2)] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) ;; counter - (i32.const 0) - (i32.load) ;; t - (i32.const 2) - (i32.div_s) ;; t/2 - (i32.add) ;; counter + t/2 - (i32.const 4) - (i32.mul) ;; counter+t/2 * 4 - (i32.add) ;; (t-1)*4 + (counter+(t/2))*4 - (i32.add) ;; x.children[i] + (t-1)*4 + (counter + (t/2))*4 - (i32.load offset=8) ;; load the address of the child in index counter + t/2 - (i32.store offset=8) ;; z.children[counter] = x.children[i].children[counter] - (local.get 3) ;; increment counter - (i32.const 1) - (i32.add) - (local.set 3) - (br $loop) - ) - ) - ) - ) - ) - ) - (i32.const 0) ;; get x.children[i] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 1) ;; i - (i32.const 4) - (i32.mul) ;; i*4 - (i32.add) ;; (t-1)*4 + i*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; load the address of the child in index i = y - (i32.const 0) - (i32.load) ;; t - (i32.const 2) - (i32.div_s) ;; t/2 - (i32.const 1) - (i32.sub) ;; t/2 -1 - (i32.store offset=4) ;; number of keys of x.children[i] is now t/2 -1 - (local.get 0) ;; x - (i32.load offset=4) ;; get x.n - (local.set 3) ;; counter = x.n - (block $loop_break - (loop $loop - (local.get 1) ;; i - (local.get 3) ;; counter - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (i32.const 0) ;; get x.children[counter + 1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) ;; counter - (i32.const 1) - (i32.add) ;; counter + 1 - (i32.const 4) - (i32.mul) ;; (counter + 1)*4 - (i32.add) ;; (t-1)*4 + (counter + 1)*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 + (counter + 1)*4 -->address where addr of child is stored - (i32.const 0) ;; get x.children[counter] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) ;; counter - (i32.const 4) - (i32.mul) ;; (counter)*4 - (i32.add) ;; (t-1)*4 + (counter)*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 + (counter)*4 - (i32.load offset=8) ;; load the addr of the child in index (counter) - (i32.store offset=8) ;; x.children[counter+1] = x.children[counter] - (local.get 3) ;; decrement counter - (i32.const 1) - (i32.sub) - (local.set 3) - (br $loop) - ) - ) - ) - ) - (i32.const 0) ;; get x.children[i + 1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 1) ;; i - (i32.const 1) - (i32.add) ;; i + 1 - (i32.const 4) - (i32.mul) ;; (i + 1)*4 - (i32.add) ;; (t-1)*4 + (i + 1)*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 + (i + 1)*4 --> addr where we will store the new addr of the child - (local.get 2) ;; addr of z - (i32.store offset=8) ;; x.children[i+1] = z - (local.get 0) ;; x - (i32.load offset=4) ;; get x.n - (i32.const 1) - (i32.sub) - (local.set 3) ;; counter = x.n -1 - (block $loop_break - (loop $loop - (local.get 1) ;; i - (i32.const 1) - (i32.sub) ;; i-1 - (local.get 3) ;; counter - (i32.eq) ;; counter == i-1 - (if - (then - (br $loop_break) - ) - (else - (local.get 3) ;; get x.keys[counter + 1] ;; counter - (i32.const 1) - (i32.add) ;; counter + 1 - (i32.const 4) - (i32.mul) ;; (counter + 1)*4 - (local.get 0) ;; x - (i32.add) ;; x +(counter + 1)*4 --> addr where we will store the new key - (local.get 3) ;; counter - (i32.const 4) - (i32.mul) ;; (counter)*4 - (local.get 0) ;; x - (i32.add) ;; x +(counter)*4 - (i32.load offset=8) ;; load the key in index (counter) - (i32.store offset=8) ;; x.keys[counter+1] = x.keys[counter] - (local.get 3) ;; decrement counter - (i32.const 1) - (i32.sub) - (local.set 3) - (br $loop) - ) - ) - ) - ) - (local.get 1) ;; get x.keys[i] - (i32.const 4) - (i32.mul) ;; i*4 - (local.get 0) ;; x - (i32.add) ;; x +i*4 --> addr of the key - (i32.const 0) ;; get x.children[i].keys[t/2] - (i32.load) ;; t - (i32.const 2) - (i32.div_s) ;; t/2 - (i32.const 1) - (i32.sub) ;; t/2 -1 - (i32.const 4) - (i32.mul) ;; (t/2)-1 *4 - (i32.const 0) ;; get x.children[i] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 1) ;; i - (i32.const 4) - (i32.mul) ;; (i)*4 - (i32.add) ;; (t-1)*4 + (i)*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 + (i)*4 - (i32.load offset=8) ;; load the address of the child in index (i) - (i32.add) ;; y + (t/2)-1 *4 - (i32.load offset=8) ;; load the key in index (t/2)-1 from node x.children[i] - (i32.store offset=8) ;; store the new key in the addr - (local.get 0) - (local.get 0) ;; x - (i32.load offset=4) ;; x.n - (i32.const 1) - (i32.add) ;; x.n + 1 - (i32.store offset=4) ;; x.n = x.n + 1 - ) - ) - ) - ;; btreeInsertNonFull(x, k), where x: addr of a non full internal node; k: the key to insert - (func $btreeInsertNonFull (param i32) (param i32) (local i32) - (local.get 0) ;; x - (i32.load offset=4) ;; x.n - (i32.const 1) - (i32.sub) ;; x.n -1 - (local.set 2) ;; i = x.n -1 - (local.get 0) ;; x - (i32.load) ;; get first i32 --> isLeaf - (i32.const 1) - (i32.eq) ;; is leaf? - ;; (i32.store (i32.const 0) (i32.const 1024)) ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string - ;; (i32.store (i32.const 4) (i32.const 12)) ;; iov.iov_len - The length of the 'hello world\n' string - ;; (call $fd_write - ;; (i32.const 1) ;; file_descriptor - 1 for stdout - ;; (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0 - ;; (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one. - ;; (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written - ;; ) - ;; drop - (if - (then ;; x is leaf - (block $loop_break - (loop $loop - (local.get 2) ;; i - (i32.const 0) - (i32.ge_s) ;; i <= 0? - (if (result i32) - (then - (local.get 1) ;; k - (local.get 0) ;; x - ;; x+4*i - (i32.const 4) - (local.get 2) ;; i - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.keys[i] - (i32.lt_s) ;; k < x.keys[i] - ) - (else - (i32.const 0) - ) - ) - (local.get 2) ;; i - (i32.const 0) - (i32.ge_s) ;; i <= 0? - (i32.and) - (if - (then - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.const 1) - (i32.add) ;; i+1 - (i32.mul) ;; i+1 *4 - (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.mul) ;; i *4 - (i32.add) ;; x + i *4 --> addr of x.keys[i] - (i32.load offset=8) ;; x.keys[i] - (i32.store offset=8) ;; x.keys[i+1] = x.keys[i] - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (local.set 2) ;; i = i-1 - (br $loop) - ) - (else - (br $loop_break) - ) - ) - ) - ) - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.const 1) - (i32.add) ;; i+1 - (i32.mul) ;; i+1 *4 - (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] - (local.get 1) ;; k - (i32.store offset=8) ;; x.keys[i+1] = k - (local.get 0) - (local.get 0) ;; x - (i32.load offset=4) ;; x.n - (i32.const 1) - (i32.add) - (i32.store offset=4) ;; x.n = x.n +1 - ) - (else ;; x is not leaf - (block $loop_break - (loop $loop - (local.get 2) ;; i - (i32.const 0) - (i32.ge_s) ;; i >= 0 - (if - (then - (local.get 1) ;; k - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.keys[i] - (i32.lt_s) ;; k < x.keys[i] - (if - (then - (local.get 2) ;; i - (i32.const 1) - (i32.sub) ;; i -1 - (local.set 2) ;; i = i-1 - (br $loop) - ) - (else - (br $loop_break) - ) - ) - ) - (else - (br $loop_break) - ) - ) - ) - ) - (local.get 2) - (i32.const 1) - (i32.add) - (local.set 2) ;;i = i+1 - (i32.const 0) ;; get x.children[i] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 4) - (i32.mul) ;; i*4 - (i32.add) ;; (t-1)*4 + i*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 --> address of the child in index i = y - (i32.load offset=8) ;; load the address of the child in index i = y - (i32.load offset=4) ;; get y.n - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.eq) ;; y.n == t-1 --> node is full - (if - (then - (local.get 0) ;; x - (local.get 2) ;; i - (call $btreeSplitChild) - (local.get 1) ;; k - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.keys[i] - (i32.gt_s) ;; k>x.keys[i] - (if - (then - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (local.set 2) ;; i = i+1 - ) - ) - ) - ) - (i32.const 0) ;; get x.children[i] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 4) - (i32.mul) ;; i*4 - (i32.add) ;; (t-1)*4 + i*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; load the address of the child in index i = y - (local.get 1) ;; k - (call $btreeInsertNonFull) - ) - ) - ) - (func $btreeInsert (param i32) (result i32) (local i32) (local i32) - ;; (i32.store (i32.const 0) (i32.const 1024)) ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string - ;; (i32.store (i32.const 4) (i32.const 12)) ;; iov.iov_len - The length of the 'hello world\n' string - ;; (call $fd_write - ;; (i32.const 1) ;; file_descriptor - 1 for stdout - ;; (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0 - ;; (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one. - ;; (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written - ;; ) - ;; drop - (i32.const 0) - (i32.load offset=8) ;; root addr - (local.tee 2) ;; set r - (i32.load offset=4) ;; r.n - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.eq) ;; r.n == t-1 --> root is full - (if (result i32) - (then - (i32.const 1) ;; create a new node - (memory.grow) ;; create new page - (i32.const -1) - (i32.ne) - (if (result i32) ;; if we can grow memory - (then - (i32.const 0) ;; get the number of nodes in the tree - (i32.load offset=4) ;; number of nodes in the tree - (i32.const 1) - (i32.add) ;; number of nodes + 1 - (i32.const 65536) - (i32.mul) ;; address of s is 64KiB*(number of nodes+1) - (local.set 1) - (i32.const 0) ;; change number of nodes - (i32.const 0) - (i32.load offset=4) - (i32.const 1) - (i32.add) - (i32.store offset=4) ;; set number of nodes to + 1 - (i32.const 0) - (local.get 1) ;; s - (i32.store offset=8) ;; root addr is now s - (local.get 1) ;; s - (i32.const 0) - (i32.store) ;; store 0(FALSE) regarding if node is leaf - (local.get 1) ;; now store number of keys - (i32.const 0) - (i32.store offset=4) ;; store number of keys = 0 - (i32.const 0) ;; store children addresses - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (i32.const 0) ;; index = 0 - (i32.const 4) - (i32.mul) ;; i*4 - (i32.add) ;; (t-1)*4 + i*4 - (local.get 1) ;; x - (i32.add) ;; x + (t-1)*4 --> addre where addr of child number 0 is located - (local.get 2) - (i32.store offset=8) ;; s.children[0] = r - (local.get 1) ;; s - (i32.const 0) - (call $btreeSplitChild) - (local.get 1) ;; s - (local.get 0) ;; k - (call $btreeInsertNonFull) - (local.get 1) ;; return the address of the new root - ) - (else - (i32.const -1) ;; return -1 because we could not create a new node - ) - ) - ) - (else - (local.get 2) ;; r - (local.get 0) ;; k - (call $btreeInsertNonFull) - (local.get 2) ;; return the address of the root, which is the same - ) - ) - ) - ;; Returns the address of the new root (if applicable, otherwise returns the addres of the root) - (func $btreeDelete (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local i32) - ;; (i32.store (i32.const 0) (i32.const 8)) ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string - ;; (i32.store (i32.const 4) (i32.const 12)) ;; iov.iov_len - The length of the 'hello world\n' string - ;; (call $fd_write - ;; (i32.const 1) ;; file_descriptor - 1 for stdout - ;; (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0 - ;; (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one. - ;; (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written - ;; ) - ;; drop - (local.get 0) ;; x - (i32.load) ;; get first i32 --> isLeaf - (i32.const 1) - (i32.eq) ;; is leaf? - (if - (then ;; x is leaf - (i32.const 0) - (local.set 2) ;; i = 0 - (block $loop_break - (loop $loop - (local.get 0) ;; x - (i32.load offset=4) ;; x.n - (local.get 2) ;; i - (i32.eq) ;; i == x.n - (if - (then - (br $loop_break) ;; break from loop - ) - (else - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.keys[i] - (local.get 1) ;; k - (i32.eq) ;; k == x.keys[i] - (if - (then - (local.get 2) ;; i - (local.set 3) ;; j = i - (block $while_break - (loop $while - (local.get 3) ;; j - (local.get 0) ;; x - (i32.load offset=4) ;; x.n - (i32.const 1) - (i32.sub) ;; x.n-1 - (i32.eq) ;; j == x.n-1 - (if - (then - (br $while_break) - ) - (else - (local.get 0) ;; x - (i32.const 4) - (local.get 3) ;; j - (i32.mul) ;; j*4 - (i32.add) ;; x + j*4 --> addr where x.keys[j] is stored - (local.get 0) ;; x - (i32.const 4) - (local.get 3) ;; j - (i32.const 1) - (i32.add) ;; j+1 - (i32.mul) ;; j+1 *4 - (i32.add) ;; x + j+1 *4 --> addr where x.keys[j+1] is stored - (i32.load offset=8) ;; x.keys[j+1] - (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j+1 - (br $while) - ) - ) - ) - ) - (local.get 0) ;; x - (local.get 0) ;; x - (i32.load offset=4) ;; x.n - (i32.const 1) - (i32.sub) - (i32.store offset=4) ;; x.n = x.n -1 - (br $loop_break) - ) - ) - ) - ) - (i32.const 1) - (local.get 2) - (i32.add) - (local.set 2) ;; i = i+1 - (br $loop) ;; continue - ) - ) - ) - (else ;; x is not leaf - (i32.const 0) ;;get the index of the appropriate child/key - (local.set 2) ;; i = 0 - (block $loop_break - (loop $loop - (local.get 2) ;; i - (local.get 0) ;; x - (i32.load offset=4) ;; x.n - (i32.const 1) - (i32.sub) - (i32.le_s) ;; i <= x.n - 1 - (if (result i32) - (then - (local.get 1) ;; k - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.keys[i] - (i32.gt_s) ;; k > x.keys[i] - ) - (else - (i32.const 0) - ) - ) - (local.get 2) ;; i - (local.get 0) ;; x - (i32.load offset=4) ;; x.n - (i32.const 1) - (i32.sub) - (i32.le_s) ;; i <= x.n - 1 - (i32.and) ;; i <= x.n - 1 && k > x.keys[i] - (if - (then - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (local.set 2) ;; i = i + 1 - (br $loop) - ) - (else - (br $loop_break) - ) - ) - ) - ) - (local.get 2) - (local.get 0) - (i32.load offset=4) ;; x.n - (i32.lt_s) ;; i < x.n - (if (result i32) - (then - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.keys[i] - (local.get 1) ;; k - (i32.eq) ;; k == x.keys[i] - ) - (else - (i32.const 0) - ) - ) - (if - (then ;; key is present in node x - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 4) - (i32.mul) ;; i*4 - (i32.add) ;; (t-1)*4 + i*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located - (i32.load offset=8) ;; x.c[i] - (local.tee 5) - (i32.load offset=4) ;; x.c[i].n - (i32.const 0) - (i32.load) ;; t - (i32.const 2) - (i32.div_s) ;; t/2 - (i32.ge_s) ;; x.c[i].n >= t/2 - (if - (then - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.keys[i] - (local.get 5) ;; x.c[i] - (i32.const 4) - (local.get 5) - (i32.load offset=4) ;; x.c[i].n - (i32.const 1) - (i32.sub) ;; index = x.c[i].n - 1 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] - (local.get 5) ;; x.c[i] - (local.get 5) ;; x.c[i] - (i32.const 4) - (local.get 5) - (i32.load offset=4) ;; x.c[i].n - (i32.const 1) - (i32.sub) ;; index = x.c[i].n - 1 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] - (call $btreeDelete) ;; (x.c[i], x.c[i].keys[x.c[i].n - 1]) - (drop) - (i32.store offset=8) ;; x.keys[i] = x.c[i].keys[x.c[i].n - 1] - ) - (else - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) ;; i+1 - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located - (i32.load offset=8) ;; x.c[i+1] - (local.tee 5) - (i32.load offset=4) ;; x.c[i+1].n - (i32.const 0) - (i32.load) ;; t - (i32.const 2) - (i32.div_s) ;; t/2 - (i32.ge_s) ;; x.c[i+1].n >= t/2 - (if - (then - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.keys[i] - (local.get 5) ;; x.c[i+1] - (i32.const 4) - (i32.const 0) ;; index = 0 - (i32.mul) ;; i *4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.c[i+1].keys[0] - (local.get 5) ;; x.c[i+1] - (local.get 5) ;; x.c[i+1] - (i32.const 4) - (i32.const 0) ;; index = 0 - (i32.mul) ;; i *4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.c[i+1].keys[0] - (call $btreeDelete) - (drop) - (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] - ) - (else - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 4) - (i32.mul) ;; i*4 - (i32.add) ;; (t-1)*4 + i*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located - (i32.load offset=8) ;; x.c[i] - (local.set 5) - (local.get 5) ;; x.c[i] - (i32.const 4) - (local.get 5) - (i32.load offset=4) ;; index = x.c[i].n - (i32.mul) ;; i *4 - (i32.add) ;; x + i*4 --> addr where x.c[i].keys[x.c[i].n] will be stored - (local.get 1) ;; k - (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = k - (i32.const 0) ;; now merge x.c[i] with x.c[i+1] - (local.set 3) ;; j = 0 - (block $loop_break - (loop $loop - (local.get 3) ;; j - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) ;; i+1 - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.load offset=4) ;; x.c[i+1].n - (i32.eq) - (if ;; j == x.c[i+1].n - (then - (br $loop_break) - ) - (else - (local.get 5) ;; x.c[i] - (i32.const 4) - (local.get 5) - (i32.load offset=4) - (local.get 3) - (i32.add) - (i32.const 1) - (i32.add) ;; index = x.c[i].n + j + 1 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[index] - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) ;; i+1 - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.const 4) - (local.get 3) ;; index = j - (i32.mul) ;; i*4 - (i32.add) ;; x.c[i+1] + i*4 ;; addr of x.c[i+1].keys[j] - (i32.load offset=8) ;; x.c[i+1].keys[j] - (i32.store offset=8) ;; x.c[i].keys[index] = x.c[i+1].keys[j] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j+1 - (br $loop) - ) - ) - ) - ) - (local.get 5) - (local.get 5) - (i32.load offset=4) ;; x.c[i].n - (i32.const 1) - (i32.add) ;; x.c[i].n +1 - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) ;; i+1 - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.load offset=4) ;; x.c[i+1].n - (i32.add) - (i32.store offset=4) ;; x.c[i].n = x.c[i].n + 1 + x.c[i+1].n - (local.get 5) - (i32.load) - (i32.const 1) - (i32.ne) - (if - (then - (i32.const 0) ;; now adjust the children - (local.set 3) ;; j = 0 - (block $loop_break - (loop $loop - (local.get 3) ;; j - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) ;; i+1 - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.load offset=4) ;; x.c[i+1].n - (i32.const 1) - (i32.add) - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 5) - (i32.load offset=4) ;; x.c[i].n - (local.get 3) ;; j - (i32.add) ;; index = x.c[i].n+j - (i32.const 1) - (i32.add) ;; i+1 - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 5) ;; x.c[i] - (i32.add) ;; x.c[i] + (t-1)*4 --> address where x.c[i].c[x.c[i].n+j] is stored - (i32.const 0) ;;get x.c[i+1].c[j] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) ;; j - (i32.const 4) - (i32.mul) ;; j *4 - (i32.add) ;; (t-1)*4 + j *4 - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) ;; i+1 - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.add) ;; x.c[i+1] + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1].c[j] - (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j+1 - (br $loop) - ) - ) - ) - ) - ) - ) - (local.get 2) ;; i - (local.set 3) ;; j = i - (block $loop_break - (loop $loop - (local.get 0) - (i32.load offset=4) - (i32.const 1) - (i32.sub) - (local.get 3) - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (local.get 0) ;; x - (i32.const 4) - (local.get 3) ;; j - (i32.mul) ;; j*4 - (i32.add) ;; x + j*4 ;; addr of x.keys[j] - (local.get 0) ;; x - (i32.const 4) - (local.get 3) ;; j - (i32.const 1) - (i32.add) - (i32.mul) ;; j+1*4 - (i32.add) ;; x + j+1*4 ;; addr of x.keys[j+1] - (i32.load offset=8) - (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j+1 - (br $loop) - ) - ) - ) - ) - (local.get 2) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = i+1 - (block $loop_break - (loop $loop - (local.get 0) - (i32.load offset=4) - (local.get 3) - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (i32.const 0) ;;get x.c[j] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) ;; j - (i32.const 4) - (i32.mul) ;; j *4 - (i32.add) ;; (t-1)*4 + j+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] - (i32.const 0) ;;get x.c[j+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) ;; j - (i32.const 1) - (i32.add) ;; j+1 - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] - (i32.load offset=8) - (i32.store offset=8) ;; x.c[j] = x.c[j+1] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j+1 - (br $loop) - ) - ) - ) - ) - (local.get 0) - (local.get 0) - (i32.load offset=4) ;; x.n - (i32.const 1) - (i32.sub) - (i32.store offset=4) ;; x.n = x.n-1 - (local.get 5) ;; x.c[i] - (local.get 1) ;; k - (call $btreeDelete) - (drop) - ) - ) - ) - ) - ) - (else ;; key not present in node x - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 4) - (i32.mul) ;; i*4 - (i32.add) ;; (t-1)*4 + i*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located - (i32.load offset=8) ;; x.c[i] - (local.set 5) - (local.get 5) - (i32.load offset=4) ;; x.c[i].n - (i32.const 0) - (i32.load) ;; t - (i32.const 2) - (i32.div_s) - (i32.const 1) - (i32.sub) ;; t/2 - 1 - (i32.eq) - (if - (then - (i32.const -1) - (local.set 4) ;; changed = -1 - (local.get 2) ;; i - (i32.const 1) - (i32.add) ;; i+1 - (local.get 0) - (i32.load offset=4) ;; x.n - (i32.le_s) ;; i+1 <= x.n - (if - (then - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) - (i32.load offset=4) ;; x.c[i+1].n - (i32.const 0) - (i32.load) ;; t - (i32.const 2) - (i32.div_s) ;; t/2 - (i32.ge_s) ;; x.c[i+1].n >= t/2 - (if - (then - (local.get 5) ;; x.c[i] - (i32.const 4) - (i32.const 0) - (i32.load) ;; t - (i32.const 2) - (i32.div_s) ;; t/2 - (i32.const 1) - (i32.sub) ;; index = t/2 -1 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[t/2-1] - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.keys[i] - (i32.load offset=8) ;; x.keys[i] - (i32.store offset=8) ;; x.c[i].keys[t/2-1] = x.keys[i] - (local.get 5) - (local.get 5) - (i32.load offset=4) ;; x.c[i].n - (i32.const 1) - (i32.add) - (i32.store offset=4) ;;x.c[i].n = x.c[i].n +1 - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.keys[i] - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.const 4) - (i32.const 0) ;; index = 0 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.c[i+1].keys[0] - (i32.load offset=8) ;; x.c[i+1].keys[0] - (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] - (local.get 5) - (i32.load) ;; x.c[i] is leaf? - (i32.const 1) - (i32.ne) - (if ;; if x.c[i] is not leaf, we have to adjust children - (then - (i32.const 0) ;;get x.c[i].c[t/2] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (i32.const 0) - (i32.load) ;; t - (i32.const 2) - (i32.div_s) ;; index = t/2 - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 5) ;; x.c[i] - (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[t/2] - (i32.const 0) ;;get x.c[i+1].c[0] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (i32.const 0) ;; index = 0 - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1].c[0] - (i32.store offset=8) ;; x.c[i].c[t/2] = x.c[i+1].c[0] - ) - ) - (local.get 5) ;; x.c[i] - (i32.const 0) - (i32.load) ;; t - (i32.const 2) - (i32.div_s) ;; t/2 - (i32.store offset=4) ;; x.c[i].n = t/2 - (i32.const 0) - (local.set 3) ;; j = 0 - (block $loop_break - (loop $loop - (local.get 3) ;; j - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.load offset=4) ;; x.c[i+1].n - (i32.const 1) - (i32.sub) - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.const 4) - (local.get 3) ;; index = j - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 --> address where x.c[i+1].keys[j] is located - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.const 4) - (local.get 3) - (i32.const 1) - (i32.add) ;; index = j+1 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.c[i+1].keys[j+1] - (i32.store offset=8) ;; x.c[i+1].keys[j] = x.c[i+1].keys[j+1] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j+1 - (br $loop) - ) - ) - ) - ) - (local.get 5) - (i32.load) - (i32.const 1) - (i32.ne) - (if ;; if x.c[i] is not leaf, we have to adjust children - (then - (i32.const 0) - (local.set 3) ;; j = 0 - (block $loop_break - (loop $loop - (local.get 3) ;; j - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.load offset=4) ;; x.c[i+1].n - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) ;; index = j - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.add) ;; x + (t-1)*4 --> address where x.c[i+1].c[j] is located - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) - (i32.const 1) - (i32.add) ;; index = j+1 - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1].c[j+1] - (i32.store offset=8) ;; x.c[i+1].c[j] = x.c[i+1].c[j+1] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j+1 - (br $loop) - ) - ) - ) - ) - ) - ) - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 --> addr where x.c[i+1] is located - (i32.load offset=8) - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.load offset=4) ;; x.c[i+1].n - (i32.const 1) - (i32.sub) - (i32.store offset=4) ;; x.c[i+1].n = x.c[i+1].n - 1 - (i32.const 0) - (local.set 4) ;; changed = 0 - ) - ) - ) - ) - (local.get 4) ;; changed - (i32.const -1) - (i32.eq) - (local.get 2) ;; i - (i32.const 1) - (i32.sub) ;; i - 1 - (i32.const 0) - (i32.ge_s) ;; i -1 >= 0 - (i32.and) ;; changed == -1 && i -1 >= 0 - (if - (then - (i32.const 0) ;;get x.c[i-1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) - (i32.load offset=4) ;; x.c[i-1].n - (i32.const 0) - (i32.load) ;; t - (i32.const 2) - (i32.div_s) ;; t/2 - (i32.ge_s) ;; x.c[i-1].n >= t/2 - (if - (then - (local.get 5) - (i32.load offset=4) ;; x.c[i].n - (local.set 3) ;; j = x.c[i].n - (block $loop_break - (loop $loop - (local.get 3) ;; j - (i32.const 0) - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (local.get 5) ;; x.c[i] - (i32.const 4) - (local.get 3) ;; j - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j] - (local.get 5) ;; x.c[i] - (i32.const 4) - (local.get 3) ;; j - (i32.const 1) - (i32.sub) ;; j-1 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j-1] - (i32.load offset=8) ;; x.c[i].keys[j-1] - (i32.store offset=8) ;; x.c[i].keys[j] = x.c[i].keys[j-1] - (local.get 3) - (i32.const 1) - (i32.sub) - (local.set 3) ;; j = j-1 - (br $loop) - ) - ) - ) - ) - (local.get 5) - (i32.load) - (i32.const 1) - (i32.ne) - (if - (then - (local.get 5) - (i32.load offset=4) ;; x.c[i].n - (i32.const 1) - (i32.add) - (local.set 3) ;; j = x.c[i].n + 1 - (block $loop_break - (loop $loop - (local.get 3) ;; j - (i32.const 0) - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (i32.const 0) ;;get x.c[i].c[j] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) ;; j - (i32.const 4) - (i32.mul) ;; j *4 - (i32.add) ;; (t-1)*4 + j *4 - (local.get 5) ;; x.c[i] - (i32.add) ;; x + (t-1)*4 --> address where x.c[i].c[j] is located - (i32.const 0) ;;get x.c[i].c[j-1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) ;; j - (i32.const 1) - (i32.sub) ;; j-1 - (i32.const 4) - (i32.mul) ;; j *4 - (i32.add) ;; (t-1)*4 + j *4 - (local.get 5) ;; x.c[i] - (i32.add) - (i32.load offset=8) ;; x.c[i].c[j-1] - (i32.store offset=8) ;; x.c[i].c[j] = x.c[i].c[j-1] - (local.get 3) - (i32.const 1) - (i32.sub) - (local.set 3) ;; j = j-1 - (br $loop) - ) - ) - ) - ) - ) - ) - (local.get 5) - (local.get 5) - (i32.load offset=4) ;; x.c[i].n - (i32.const 1) - (i32.add) - (i32.store offset=4) ;; ;; x.c[i].n = x.c[i].n + 1 - (local.get 5) ;; x.c[i] - (i32.const 4) - (i32.const 0) ;; index = 0 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[0] - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.const 1) - (i32.sub) ;; index = i-1 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] - (i32.load offset=8) ;; x.keys[i-1] - (i32.store offset=8) ;; x.c[i].keys[0] = x.keys[i-1] - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) - (i32.load offset=4) ;; x.c[i-1].n - (i32.const 1) - (i32.sub) - (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n - 1 - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.const 1) - (i32.sub) ;; index = i-1 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i-1] - (i32.const 4) - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i-1] - (i32.load offset=4) ;; index = x.c[i-1].n - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] - (i32.load offset=8) - (i32.store offset=8) ;; x.keys[i-1] = x.c[i-1].keys[x.c[i-1].n] - (local.get 5) - (i32.load) - (i32.const 1) - (i32.ne) - (if - (then - (i32.const 0) ;; get x.c[i].c[0] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (i32.const 0) ;; index = 0 - (i32.const 4) - (i32.mul) ;; i *4 - (i32.add) ;; (t-1)*4 + i *4 - (local.get 5) ;; x.c[i] - (i32.add) ;; x + (t-1)*4 ;; addr where x.c[i].c[0] is located - (i32.const 0) ;; get x.c[i-1].c[x.c[i-1].n + 1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i-1] - (i32.load offset=4) ;; x.c[i-1].n - (i32.const 1) - (i32.add) ;; index = x.c[i-1.n + 1] - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (i32.const 0) ;;get x.c[i-1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i-1] - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i-1].c[x.c[i-1].n + 1] - (i32.store offset=8) ;; x.c[i].c[0] = x.c[i-1].c[x.c[i-1].n + 1] - ) - ) - (i32.const 0) - (local.set 4) ;; changed = 0 - ) - ) - ) - ) - (local.get 4) ;; changed - (i32.const -1) - (i32.eq) - (if ;; changed == -1? - (then - (local.get 2) ;; we have to merge x.c[i] with one sibling;; i - (i32.const 1) - (i32.add) ;; i+1 - (local.get 0) ;; x - (i32.load offset=4) ;; x.n - (i32.le_s) - (if ;; i+1 <= x.n - (then - (local.get 5) ;; merge with right sibling;; x.c[i] - (i32.const 4) - (local.get 5) - (i32.load offset=4) ;; index = x.c[i].n - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n] - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; index = i - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.keys[i] - (i32.load offset=8) ;; x.keys[i] - (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = x.keys[i] - (i32.const 0) - (local.set 3) ;; j = 0 - (block $loop_break - (loop $loop - (local.get 3) ;; j - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.load offset=4) ;; x.c[i+1].n - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (local.get 5) ;; x.c[i] - (i32.const 4) - (local.get 5) - (i32.load offset=4) - (local.get 3) - (i32.add) - (i32.const 1) - (i32.add) ;; index = x.c[i].n+j+1 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n+j] - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.const 4) - (local.get 3) ;; index = j - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.c[i+1].keys[j] - (i32.store offset=8) ;; x.c[i].keys[x.c[i].n+j] = x.c[i+1].keys[j] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j + 1 - (br $loop) - ) - ) - ) - ) - (local.get 5) - (i32.load) - (i32.const 1) - (i32.ne) - (if - (then - (i32.const 0) - (local.set 3) ;; j = 0 - (block $loop_break - (loop $loop - (local.get 3) ;; j - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.load offset=4) ;; x.c[i+1].n - (i32.const 1) - (i32.add) ;; x.c[i+1].n + 1 - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 5) - (i32.load offset=4) - (local.get 3) - (i32.add) - (i32.const 1) - (i32.add) ;; index = x.c[i].n+j+1 - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 5) ;; x.c[i] - (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[x.c[i].n+j] - (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) ;; index = j - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1].c[j] - (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j + 1 - (br $loop) - ) - ) - ) - ) - ) - ) - (local.get 5) ;; x.c[i] - (local.get 5) ;; x.c[i] - (i32.load offset=4) ;; x.c[i].n - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.load offset=4) ;; x.c[i+1].n - (i32.add) ;; x.c[i].n + x.c[i+1].n - (i32.const 1) - (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 - (i32.store offset=4) ;; x.c[i].n = x.c[i].n + x.c[i+1].n + 1 - (local.get 2) ;; i - (i32.const 1) - (i32.add) ;; i+1 - (local.set 3) ;; j = i+1 - (block $loop_break ;; re-organize x - (loop $loop - (local.get 3) - (local.get 0) - (i32.load offset=4) ;; x.n - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (i32.const 0) ;;get x.c[j] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) ;; index = j - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] - (i32.const 0) ;;get x.c[j+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) - (i32.const 1) - (i32.add) ;; index = j + 1 - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] - (i32.load offset=8) ;; x.c[j+1] - (i32.store offset=8) ;; x.c[j] = x.c[j+1] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j + 1 - (br $loop) - ) - ) - ) - ) - (local.get 2) ;; i - (local.set 3) ;; j = i - (block $loop_break - (loop $loop - (local.get 3) ;; j - (local.get 0) - (i32.load offset=4) ;; x.n - (i32.const 1) - (i32.sub) - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (local.get 0) ;; x - (i32.const 4) - (local.get 3) ;; index = j - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.keys[j] - (local.get 0) ;; x - (i32.const 4) - (local.get 3) - (i32.const 1) - (i32.add) ;; index = j + 1 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.keys[j+1] - (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j + 1 - (br $loop) - ) - ) - ) - ) - (local.get 0) ;; x - (local.get 0) ;; x - (i32.load offset=4) ;; x.n - (i32.const 1) - (i32.sub) ;; x.n - 1 - (i32.store offset=4) ;; x.n = x.n - 1 - (i32.const 0) - (local.set 4) ;; changed = 0 - ) - ) - (local.get 4) ;; changed - (i32.const -1) - (i32.eq) ;; changed == -1 ? - (local.get 2) ;; i - (i32.const 1) - (i32.sub) ;; i - 1 - (i32.const 0) - (i32.ge_s) ;; i - 1 >= 0? - (i32.and) ;; changed == -1 && i - 1 >= 0 - (if - (then - (i32.const 0) ;; merge with left sibling ;;get x.c[i-1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i-1] - (i32.const 4) - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i-1] - (i32.load offset=4) ;; index = x.c[i-1].n - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; index = i - (i32.const 1) - (i32.sub) ;; index = i-1 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] - (i32.load offset=8) ;; x.keys[i] - (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n] = x.keys[i-1] - (i32.const 0) - (local.set 3) ;; j = 0 - (block $loop_break - (loop $loop - (local.get 3) ;; j - (local.get 5) ;; x.c[i] - (i32.load offset=4) ;; x.c[i].n - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (i32.const 0) ;; x.c[i-1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i-1] - (i32.const 4) - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i-1] - (i32.load offset=4) ;; x.c[i-1].n - (local.get 3) - (i32.add) - (i32.const 1) - (i32.add) ;; index = x.c[i-1].n+j+1 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n+j] - (local.get 5) ;; x.c[i] - (i32.const 4) - (local.get 3) ;; index = j - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.c[i].keys[j] - (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n+j] = x.c[i].keys[j] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j + 1 - (br $loop) - ) - ) - ) - ) - (local.get 5) - (i32.load) - (i32.const 1) - (i32.ne) - (if - (then - (i32.const 0) - (local.set 3) ;; j = 0 - (block $loop_break - (loop $loop - (local.get 3) ;; j - (local.get 5) ;; x.c[i] - (i32.load offset=4) ;; x.c[i].n - (i32.const 1) - (i32.add) ;; x.c[i].n + 1 - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (i32.const 0) ;;get x.c[i-1].c[x.c[i-1].n+j] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i-1] - (i32.load offset=4) - (local.get 3) - (i32.add) - (i32.const 1) - (i32.add) ;; index = x.c[i-1].n+j+1 - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i-1] - (i32.add) ;; x + (t-1)*4 --> addr of x.c[i-1].c[x.c[i-1].n+j] - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) ;; index = j - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 5) ;; x.c[i] - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i].c[j] - (i32.store offset=8) ;; x.c[i-1].c[x.c[i-1].n+j] = x.c[i].c[j] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j + 1 - (br $loop) - ) - ) - ) - ) - ) - ) - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i-1] - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i-1] - (i32.load offset=4) ;; x.c[i-1].n - (local.get 5) - (i32.load offset=4) ;; x.c[i].n - (i32.add) ;; x.c[i-1].n + x.c[i].n - (i32.const 1) - (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 - (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n + x.c[i].n + 1 - (local.get 2) ;; i - (local.set 3) ;; j = i - (block $loop_break ;; re-organize x - (loop $loop - (local.get 3) - (local.get 0) - (i32.load offset=4) ;; x.n - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (i32.const 0) ;;get x.c[j] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) ;; index = j - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] - (i32.const 0) ;;get x.c[j+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) - (i32.const 1) - (i32.add) ;; index = j + 1 - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] - (i32.load offset=8) ;; x.c[j+1] - (i32.store offset=8) ;; x.c[j] = x.c[j+1] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j + 1 - (br $loop) - ) - ) - ) - ) - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (local.set 3) ;; j = i - 1 - (block $loop_break - (loop $loop - (local.get 3) ;; j - (local.get 0) - (i32.load offset=4) ;; x.n - (i32.const 1) - (i32.sub) - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (local.get 0) ;; x - (i32.const 4) - (local.get 3) ;; index = j - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.keys[j] - (local.get 0) ;; x - (i32.const 4) - (local.get 3) - (i32.const 1) - (i32.add) ;; index = j + 1 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.keys[j+1] - (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j + 1 - (br $loop) - ) - ) - ) - ) - (local.get 0) ;; x - (local.get 0) ;; x - (i32.load offset=4) ;; x.n - (i32.const 1) - (i32.sub) ;; x.n - 1 - (i32.store offset=4) ;; x.n = x.n - 1 - ) - ) - ) - ) - ) - ) - (local.get 4) - (i32.const -1) - (i32.eq) - (if ;; changed == -1? if yes, we merged with left sibling - (then - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i-1] - (local.get 1) - (call $btreeDelete) - (drop) - ) - (else - (local.get 5) ;; x.c[i] - (local.get 1) - (call $btreeDelete) - (drop) - ) - ) - ) - ) - ) ;; end of if x is not leaf - ) - (i32.const 0) - (i32.load offset=8) ;; root addr - (i32.load offset=4) - (i32.const 0) - (i32.eq) - (if ;; if root is empty - (then - (i32.const 0) - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (i32.const 0) ;; index = 0 - (i32.const 4) - (i32.mul) ;; i *4 - (i32.add) ;; (t-1)*4 + i *4 - (i32.const 0) - (i32.load offset=8) ;; root addr - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; root.c[0] - (i32.store offset=8) ;; root = root.c[0] - ) - ) - (i32.const 0) - (i32.load offset=8) ;; root addr - ) - (func $main (param $a i32) (param $b i32) (param $h i32) - (local $btree i32) - (local.get $a) - (local.get $b) - (i32.gt_s) ;; a > b - (local.get $a) - (local.get $h) - (i32.ne) ;; a != h - (local.get $b) - (local.get $h) - (i32.ne) ;; b != h - (i32.and) - (i32.and) - (i32.eqz) - (if (then (unreachable))) - (i32.const 4) ;;create a tree with degree 4 - (call $createBtree) - (local.set $btree) - ;; insert variables - (local.get $a) - (call $btreeInsert) - (local.set $btree) - (local.get $b) - (call $btreeInsert) - (local.set $btree) - (local.get $h) - (call $btreeInsert) - (local.set $btree) - ;; search for variables & check that they were inserted - (local.get $btree) - (local.get $a) - (call $btreeSearch) - (i32.const -1) - (i32.ne) - (local.get $btree) - (local.get $b) - (call $btreeSearch) - (i32.const -1) - (i32.ne) - (local.get $btree) - (local.get $h) - (call $btreeSearch) - (i32.const -1) - (i32.ne) - (i32.and) - (i32.and) - ;; delete & check that it was deleted - ;; a - (local.get $btree) - (local.get $a) - (call $btreeDelete) - (local.tee $btree) - (local.get $a) - (call $btreeSearch) - (i32.const -1) - (i32.eq) - ;; b - (local.get $btree) - (local.get $b) - (call $btreeDelete) - (local.tee $btree) - (local.get $b) - (call $btreeSearch) - (i32.const -1) - (i32.eq) - (i32.and) - (i32.and) - (drop) - ) - (export "main" (func $main)) - (func $real_main - i32.const 3 - i32.const 2 - i32.const 1 - call $main - ) - (start $real_main) - ) diff --git a/benchmarks/wasm/btree/2o1u-orig.wat b/benchmarks/wasm/btree/2o1u-orig.wat deleted file mode 100644 index a8536b0b..00000000 --- a/benchmarks/wasm/btree/2o1u-orig.wat +++ /dev/null @@ -1,2819 +0,0 @@ -(module - (memory $0 2) - (func $createBtree (param i32) (result i32) ;; createBtree(t), where t: degree of the btree - (i32.const 0) - (local.get 0) - (i32.store) ;; store t at address 0 - (i32.const 0) - (i32.const 1) - (i32.store offset=4) ;; store the number of nodes in the btree = 1 - (i32.const 0) - (i32.const 65536) - (i32.store offset=8) ;; store the root addr - (i32.const 1) ;; Now create root - (memory.grow) ;; create new page - (i32.const -1) - (i32.ne) - (if ;; if we can grow memory - (then - (i32.const 65536) ;; 64KiB = 65536 bytes - (i32.const 1) - (i32.store) ;; store 1(TRUE) regarding if node is leaf - (i32.const 65536) ;; now store number of keys - (i32.const 0) - (i32.store offset=4) ;; store number of keys = 0 - ) - ) - (i32.const 65536) ;; returns the address of the root - ) - ;; btreeSearch(x, k), where x: node address; k: key to search. Returns address of the key or -1 if key is not in tree. - (func $btreeSearch (param i32) (param i32) (result i32) (local i32) - (i32.const 0) - (local.set 2) ;; i = 0 - (block $loop_break - (loop $loop - (local.get 2) ;; i - (local.get 0) ;; x - (i32.load offset=4) ;; x.children - (i32.const 1) - (i32.sub) ;; x.n -1 - (i32.le_s) ;; i <= x.children-1 - (if - (then - (local.get 1) ;; k - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.keys[i] - (i32.gt_s) ;; k > x.keys[i] - (if - (then - (i32.const 1) - (local.get 2) - (i32.add) - (local.set 2) ;; i = i+1 - (br $loop) - ) - (else - (br $loop_break) - ) - ) - ) - (else - (br $loop_break) - ) - ) - ) - ) - (local.get 2) ;; i - (local.get 0) ;; x - (i32.load offset=4) ;; x.children - (i32.const 1) - (i32.sub) ;; x.n - 1 - (i32.le_s) ;; i <= x.children - 1 - (if (result i32) - (then - (local.get 1) ;; k - (local.get 0) ;; x - ;; x+4*i - (i32.const 4) - (local.get 2) ;; i - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.keys[i] - (i32.eq) - ;; k == x.keys[i] - (if (result i32) - (then - (local.get 0) ;; save the address, which is x+8+i*4 - (i32.const 8) - (i32.add) ;; x+8 - (i32.const 4) - (local.get 2) ;; i - (i32.mul) ;; i*4 - (i32.add) ;; x+8+i*4 - ) - (else - (local.get 0) ;; x - (i32.load) ;; x is leaf? - (i32.const 1) ;; if == 1, then yes - (i32.eq) - (if (result i32) - (then - (i32.const -1) - ) - (else - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 4) - (i32.mul) ;; i*4 - (i32.add) ;; (t-1)*4 + i*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 + i*4 - (i32.load offset=8) ;; load the address of the child in index i - (local.get 1) ;; k - (call $btreeSearch) ;; btreeSearch(x.children[i], k) - ) - ) - ) - ) - ) - (else - (local.get 0) ;; x - (i32.load) ;; x is leaf? - (i32.const 1) ;; if == 1, then yes - (i32.eq) - (if (result i32) - (then - (i32.const -1) - ) - (else - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 4) - (i32.mul) ;; i*4 - (i32.add) ;; (t-1)*4 + i*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 + i*4 - (i32.load offset=8) ;; load the address of the child in index i - (local.get 1) ;; k - (call $btreeSearch) ;; btreeSearch(x.children[i], k) - ) - ) - ) - ) - ) - ;; btreeSplitChild(x, i), where x: addr of a non full internal node; i: index such that x.children[i] is a full child of x - (func $btreeSplitChild (param i32) (param i32) (local i32) (local i32) - (i32.const 1) ;; create a new node - (memory.grow) ;; create new page - (i32.const -1) - (i32.ne) - (if ;; if we can grow memory - (then ;; get the number of nodes in the tree - (i32.const 0) - (i32.load offset=4) ;; number of nodes in the tree - (i32.const 1) - (i32.add) ;; number of nodes + 1 - (i32.const 65536) - (i32.mul) ;; address is 64KiB*(number of nodes+1) - (local.set 2) - (i32.const 0) ;; change number of nodes - (i32.const 0) - (i32.load offset=4) - (i32.const 1) - (i32.add) - (i32.store offset=4) ;; set number of nodes to + 1 - (i32.const 0) ;; get x.children[i] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 1) ;; i - (i32.const 4) - (i32.mul) ;; i*4 - (i32.add) ;; (t-1)*4 + i*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 + i*4 - (i32.load offset=8) ;; load the addr of the child in index i : y - (i32.load) ;; check if x.children[i] is leaf ;; load the first i32 in the child - (i32.const 1) - (i32.eq) ;; is y leaf? - (if - (then ;; yes - (local.get 2) ;; addr of new node z - (i32.const 1) - (i32.store) ;; store 1(TRUE) regarding if node is leaf - ) - (else ;; no - (local.get 2) ;; addr of new node z - (i32.const 0) - (i32.store) ;; store 0(FALSE) regarding if node is leaf - ) - ) - (local.get 2) ;; now store number of children, which will be t/2 -1 ;; addr of z - (i32.const 0) - (i32.load) ;; t - (i32.const 2) - (i32.div_s) ;; t/2 - (i32.const 1) - (i32.sub) ;; (t/2)-1 - (i32.store offset=4) ;; store number of children = (t/2)-1 - (i32.const 0) ;; now to store the keys - (local.set 3) ;; counter = 0 - (block $loop_break - (loop $loop - (local.get 3) ;; counter - (i32.const 0) - (i32.load) ;; t - (i32.const 2) - (i32.div_s) ;; t/2 - (i32.const 1) - (i32.sub) ;; (t/2)-1 - (i32.eq) ;; counter == (t/2)-1 - (if - (then - (br $loop_break) - ) - (else - (local.get 2) ;; addr of z ;; we will store in the new node's address. z.keys[counter] - (i32.const 4) - (local.get 3) ;; counter - (i32.mul) ;; counter * 4 - (i32.add) ;; addr + counter * 4 -> where the key is goint to be located - (i32.const 0) ;; get x.children[i] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 1) ;; i - (i32.const 4) - (i32.mul) ;; i*4 - (i32.add) ;; (t-1)*4 + i*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 + i*4 - (i32.load offset=8) ;; load the address of the child in index i = y - (i32.const 4) ;; now get x.children[i].keys[counter+(t/2)] - (local.get 3) ;; counter - (i32.const 0) - (i32.load) ;; t - (i32.const 2) - (i32.div_s) ;; t/2 - (i32.add) ;; counter + (t/2) - (i32.mul) ;; counter + (t/2) *4 - (i32.add) ;; y + counter + (t/2) *4 - (i32.load offset=8) ;; y.keys[counter + (t/2)] - (i32.store offset=8) ;; z.keys[counter] = y.keys[counter + (t/2)] - (local.get 3) ;; increment counter - (i32.const 1) - (i32.add) - (local.set 3) - (br $loop) - ) - ) - ) - ) - (i32.const 0) - (local.set 3) ;; set counter back to 0 - (i32.const 0) ;; check if x.children[i] is leaf - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 1) ;; i - (i32.const 4) - (i32.mul) ;; i*4 - (i32.add) ;; (t-1)*4 + i*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 + i*4 - (i32.load offset=8) ;; load the address of the child in index i = y - (i32.load) ;; get first i32 in child --> isLeaf - (i32.const 1) - (i32.ne) ;; is leaf? - (if - (then ;; not leaf so we have to update the children - (block $loop_break - (loop $loop - (local.get 3) ;; counter - (i32.const 0) - (i32.load) ;; t - (i32.const 2) - (i32.div_s) ;; t/2 - (i32.eq) ;; counter == (t/2) - (if - (then - (br $loop_break) - ) - (else - (i32.const 0) ;; now get z.children[counter] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) ;; counter - (i32.const 4) - (i32.mul) ;; counter*4 - (i32.add) ;; (t-1)*4 + counter*4 - (local.get 2) ;; z - (i32.add) ;; z + (t-1)*4 + counter*4 --> address where addr of child is stored - (i32.const 0) ;; get x.children[i] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 1) ;; i - (i32.const 4) - (i32.mul) ;; i*4 - (i32.add) ;; (t-1)*4 + i*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; load the address of the child in index i = y - (i32.const 0) ;; get x.children[i].children[counter+(t/2)] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) ;; counter - (i32.const 0) - (i32.load) ;; t - (i32.const 2) - (i32.div_s) ;; t/2 - (i32.add) ;; counter + t/2 - (i32.const 4) - (i32.mul) ;; counter+t/2 * 4 - (i32.add) ;; (t-1)*4 + (counter+(t/2))*4 - (i32.add) ;; x.children[i] + (t-1)*4 + (counter + (t/2))*4 - (i32.load offset=8) ;; load the address of the child in index counter + t/2 - (i32.store offset=8) ;; z.children[counter] = x.children[i].children[counter] - (local.get 3) ;; increment counter - (i32.const 1) - (i32.add) - (local.set 3) - (br $loop) - ) - ) - ) - ) - ) - ) - (i32.const 0) ;; get x.children[i] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 1) ;; i - (i32.const 4) - (i32.mul) ;; i*4 - (i32.add) ;; (t-1)*4 + i*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; load the address of the child in index i = y - (i32.const 0) - (i32.load) ;; t - (i32.const 2) - (i32.div_s) ;; t/2 - (i32.const 1) - (i32.sub) ;; t/2 -1 - (i32.store offset=4) ;; number of keys of x.children[i] is now t/2 -1 - (local.get 0) ;; x - (i32.load offset=4) ;; get x.n - (local.set 3) ;; counter = x.n - (block $loop_break - (loop $loop - (local.get 1) ;; i - (local.get 3) ;; counter - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (i32.const 0) ;; get x.children[counter + 1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) ;; counter - (i32.const 1) - (i32.add) ;; counter + 1 - (i32.const 4) - (i32.mul) ;; (counter + 1)*4 - (i32.add) ;; (t-1)*4 + (counter + 1)*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 + (counter + 1)*4 -->address where addr of child is stored - (i32.const 0) ;; get x.children[counter] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) ;; counter - (i32.const 4) - (i32.mul) ;; (counter)*4 - (i32.add) ;; (t-1)*4 + (counter)*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 + (counter)*4 - (i32.load offset=8) ;; load the addr of the child in index (counter) - (i32.store offset=8) ;; x.children[counter+1] = x.children[counter] - (local.get 3) ;; decrement counter - (i32.const 1) - (i32.sub) - (local.set 3) - (br $loop) - ) - ) - ) - ) - (i32.const 0) ;; get x.children[i + 1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 1) ;; i - (i32.const 1) - (i32.add) ;; i + 1 - (i32.const 4) - (i32.mul) ;; (i + 1)*4 - (i32.add) ;; (t-1)*4 + (i + 1)*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 + (i + 1)*4 --> addr where we will store the new addr of the child - (local.get 2) ;; addr of z - (i32.store offset=8) ;; x.children[i+1] = z - (local.get 0) ;; x - (i32.load offset=4) ;; get x.n - (i32.const 1) - (i32.sub) - (local.set 3) ;; counter = x.n -1 - (block $loop_break - (loop $loop - (local.get 1) ;; i - (i32.const 1) - (i32.sub) ;; i-1 - (local.get 3) ;; counter - (i32.eq) ;; counter == i-1 - (if - (then - (br $loop_break) - ) - (else - (local.get 3) ;; get x.keys[counter + 1] ;; counter - (i32.const 1) - (i32.add) ;; counter + 1 - (i32.const 4) - (i32.mul) ;; (counter + 1)*4 - (local.get 0) ;; x - (i32.add) ;; x +(counter + 1)*4 --> addr where we will store the new key - (local.get 3) ;; counter - (i32.const 4) - (i32.mul) ;; (counter)*4 - (local.get 0) ;; x - (i32.add) ;; x +(counter)*4 - (i32.load offset=8) ;; load the key in index (counter) - (i32.store offset=8) ;; x.keys[counter+1] = x.keys[counter] - (local.get 3) ;; decrement counter - (i32.const 1) - (i32.sub) - (local.set 3) - (br $loop) - ) - ) - ) - ) - (local.get 1) ;; get x.keys[i] - (i32.const 4) - (i32.mul) ;; i*4 - (local.get 0) ;; x - (i32.add) ;; x +i*4 --> addr of the key - (i32.const 0) ;; get x.children[i].keys[t/2] - (i32.load) ;; t - (i32.const 2) - (i32.div_s) ;; t/2 - (i32.const 1) - (i32.sub) ;; t/2 -1 - (i32.const 4) - (i32.mul) ;; (t/2)-1 *4 - (i32.const 0) ;; get x.children[i] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 1) ;; i - (i32.const 4) - (i32.mul) ;; (i)*4 - (i32.add) ;; (t-1)*4 + (i)*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 + (i)*4 - (i32.load offset=8) ;; load the address of the child in index (i) - (i32.add) ;; y + (t/2)-1 *4 - (i32.load offset=8) ;; load the key in index (t/2)-1 from node x.children[i] - (i32.store offset=8) ;; store the new key in the addr - (local.get 0) - (local.get 0) ;; x - (i32.load offset=4) ;; x.n - (i32.const 1) - (i32.add) ;; x.n + 1 - (i32.store offset=4) ;; x.n = x.n + 1 - ) - ) - ) - ;; btreeInsertNonFull(x, k), where x: addr of a non full internal node; k: the key to insert - (func $btreeInsertNonFull (param i32) (param i32) (local i32) - (local.get 0) ;; x - (i32.load offset=4) ;; x.n - (i32.const 1) - (i32.sub) ;; x.n -1 - (local.set 2) ;; i = x.n -1 - (local.get 0) ;; x - (i32.load) ;; get first i32 --> isLeaf - (i32.const 1) - (i32.eq) ;; is leaf? - (if - (then ;; x is leaf - (block $loop_break - (loop $loop - (local.get 2) ;; i - (i32.const 0) - (i32.ge_s) ;; i <= 0? - (if (result i32) - (then - (local.get 1) ;; k - (local.get 0) ;; x - ;; x+4*i - (i32.const 4) - (local.get 2) ;; i - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.keys[i] - (i32.lt_s) ;; k < x.keys[i] - ) - (else - (i32.const 0) - ) - ) - (local.get 2) ;; i - (i32.const 0) - (i32.ge_s) ;; i <= 0? - (i32.and) - (if - (then - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.const 1) - (i32.add) ;; i+1 - (i32.mul) ;; i+1 *4 - (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.mul) ;; i *4 - (i32.add) ;; x + i *4 --> addr of x.keys[i] - (i32.load offset=8) ;; x.keys[i] - (i32.store offset=8) ;; x.keys[i+1] = x.keys[i] - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (local.set 2) ;; i = i-1 - (br $loop) - ) - (else - (br $loop_break) - ) - ) - ) - ) - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.const 1) - (i32.add) ;; i+1 - (i32.mul) ;; i+1 *4 - (i32.add) ;; x + i+1 *4 --> addr of x.keys[i+1] - (local.get 1) ;; k - (i32.store offset=8) ;; x.keys[i+1] = k - (local.get 0) - (local.get 0) ;; x - (i32.load offset=4) ;; x.n - (i32.const 1) - (i32.add) - (i32.store offset=4) ;; x.n = x.n +1 - ) - (else ;; x is not leaf - (block $loop_break - (loop $loop - (local.get 2) ;; i - (i32.const 0) - (i32.ge_s) ;; i >= 0 - (if - (then - (local.get 1) ;; k - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.keys[i] - (i32.lt_s) ;; k < x.keys[i] - (if - (then - (local.get 2) ;; i - (i32.const 1) - (i32.sub) ;; i -1 - (local.set 2) ;; i = i-1 - (br $loop) - ) - (else - (br $loop_break) - ) - ) - ) - (else - (br $loop_break) - ) - ) - ) - ) - (local.get 2) - (i32.const 1) - (i32.add) - (local.set 2) ;;i = i+1 - (i32.const 0) ;; get x.children[i] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 4) - (i32.mul) ;; i*4 - (i32.add) ;; (t-1)*4 + i*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 --> address of the child in index i = y - (i32.load offset=8) ;; load the address of the child in index i = y - (i32.load offset=4) ;; get y.n - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.eq) ;; y.n == t-1 --> node is full - (if - (then - (local.get 0) ;; x - (local.get 2) ;; i - (call $btreeSplitChild) - (local.get 1) ;; k - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.keys[i] - (i32.gt_s) ;; k>x.keys[i] - (if - (then - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (local.set 2) ;; i = i+1 - ) - ) - ) - ) - (i32.const 0) ;; get x.children[i] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 4) - (i32.mul) ;; i*4 - (i32.add) ;; (t-1)*4 + i*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; load the address of the child in index i = y - (local.get 1) ;; k - (call $btreeInsertNonFull) - ) - ) - ) - (func $btreeInsert (param i32) (result i32) (local i32) (local i32) - (i32.const 0) - (i32.load offset=8) ;; root addr - (local.tee 2) ;; set r - (i32.load offset=4) ;; r.n - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.eq) ;; r.n == t-1 --> root is full - (if (result i32) - (then - (i32.const 1) ;; create a new node - (memory.grow) ;; create new page - (i32.const -1) - (i32.ne) - (if (result i32) ;; if we can grow memory - (then - (i32.const 0) ;; get the number of nodes in the tree - (i32.load offset=4) ;; number of nodes in the tree - (i32.const 1) - (i32.add) ;; number of nodes + 1 - (i32.const 65536) - (i32.mul) ;; address of s is 64KiB*(number of nodes+1) - (local.set 1) - (i32.const 0) ;; change number of nodes - (i32.const 0) - (i32.load offset=4) - (i32.const 1) - (i32.add) - (i32.store offset=4) ;; set number of nodes to + 1 - (i32.const 0) - (local.get 1) ;; s - (i32.store offset=8) ;; root addr is now s - (local.get 1) ;; s - (i32.const 0) - (i32.store) ;; store 0(FALSE) regarding if node is leaf - (local.get 1) ;; now store number of keys - (i32.const 0) - (i32.store offset=4) ;; store number of keys = 0 - (i32.const 0) ;; store children addresses - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (i32.const 0) ;; index = 0 - (i32.const 4) - (i32.mul) ;; i*4 - (i32.add) ;; (t-1)*4 + i*4 - (local.get 1) ;; x - (i32.add) ;; x + (t-1)*4 --> addre where addr of child number 0 is located - (local.get 2) - (i32.store offset=8) ;; s.children[0] = r - (local.get 1) ;; s - (i32.const 0) - (call $btreeSplitChild) - (local.get 1) ;; s - (local.get 0) ;; k - (call $btreeInsertNonFull) - (local.get 1) ;; return the address of the new root - ) - (else - (i32.const -1) ;; return -1 because we could not create a new node - ) - ) - ) - (else - (local.get 2) ;; r - (local.get 0) ;; k - (call $btreeInsertNonFull) - (local.get 2) ;; return the address of the root, which is the same - ) - ) - ) - ;; Returns the address of the new root (if applicable, otherwise returns the addres of the root) - (func $btreeDelete (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local i32) - (local.get 0) ;; x - (i32.load) ;; get first i32 --> isLeaf - (i32.const 1) - (i32.eq) ;; is leaf? - (if - (then ;; x is leaf - (i32.const 0) - (local.set 2) ;; i = 0 - (block $loop_break - (loop $loop - (local.get 0) ;; x - (i32.load offset=4) ;; x.n - (local.get 2) ;; i - (i32.eq) ;; i == x.n - (if - (then - (br $loop_break) ;; break from loop - ) - (else - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.keys[i] - (local.get 1) ;; k - (i32.eq) ;; k == x.keys[i] - (if - (then - (local.get 2) ;; i - (local.set 3) ;; j = i - (block $while_break - (loop $while - (local.get 3) ;; j - (local.get 0) ;; x - (i32.load offset=4) ;; x.n - (i32.const 1) - (i32.sub) ;; x.n-1 - (i32.eq) ;; j == x.n-1 - (if - (then - (br $while_break) - ) - (else - (local.get 0) ;; x - (i32.const 4) - (local.get 3) ;; j - (i32.mul) ;; j*4 - (i32.add) ;; x + j*4 --> addr where x.keys[j] is stored - (local.get 0) ;; x - (i32.const 4) - (local.get 3) ;; j - (i32.const 1) - (i32.add) ;; j+1 - (i32.mul) ;; j+1 *4 - (i32.add) ;; x + j+1 *4 --> addr where x.keys[j+1] is stored - (i32.load offset=8) ;; x.keys[j+1] - (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j+1 - (br $while) - ) - ) - ) - ) - (local.get 0) ;; x - (local.get 0) ;; x - (i32.load offset=4) ;; x.n - (i32.const 1) - (i32.sub) - (i32.store offset=4) ;; x.n = x.n -1 - (br $loop_break) - ) - ) - ) - ) - (i32.const 1) - (local.get 2) - (i32.add) - (local.set 2) ;; i = i+1 - (br $loop) ;; continue - ) - ) - ) - (else ;; x is not leaf - (i32.const 0) ;;get the index of the appropriate child/key - (local.set 2) ;; i = 0 - (block $loop_break - (loop $loop - (local.get 2) ;; i - (local.get 0) ;; x - (i32.load offset=4) ;; x.n - (i32.const 1) - (i32.sub) - (i32.le_s) ;; i <= x.n - 1 - (if (result i32) - (then - (local.get 1) ;; k - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.keys[i] - (i32.gt_s) ;; k > x.keys[i] - ) - (else - (i32.const 0) - ) - ) - (local.get 2) ;; i - (local.get 0) ;; x - (i32.load offset=4) ;; x.n - (i32.const 1) - (i32.sub) - (i32.le_s) ;; i <= x.n - 1 - (i32.and) ;; i <= x.n - 1 && k > x.keys[i] - (if - (then - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (local.set 2) ;; i = i + 1 - (br $loop) - ) - (else - (br $loop_break) - ) - ) - ) - ) - (local.get 2) - (local.get 0) - (i32.load offset=4) ;; x.n - (i32.lt_s) ;; i < x.n - (if (result i32) - (then - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.keys[i] - (local.get 1) ;; k - (i32.eq) ;; k == x.keys[i] - ) - (else - (i32.const 0) - ) - ) - (if - (then ;; key is present in node x - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 4) - (i32.mul) ;; i*4 - (i32.add) ;; (t-1)*4 + i*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located - (i32.load offset=8) ;; x.c[i] - (local.tee 5) - (i32.load offset=4) ;; x.c[i].n - (i32.const 0) - (i32.load) ;; t - (i32.const 2) - (i32.div_s) ;; t/2 - (i32.ge_s) ;; x.c[i].n >= t/2 - (if - (then - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.keys[i] - (local.get 5) ;; x.c[i] - (i32.const 4) - (local.get 5) - (i32.load offset=4) ;; x.c[i].n - (i32.const 1) - (i32.sub) ;; index = x.c[i].n - 1 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] - (local.get 5) ;; x.c[i] - (local.get 5) ;; x.c[i] - (i32.const 4) - (local.get 5) - (i32.load offset=4) ;; x.c[i].n - (i32.const 1) - (i32.sub) ;; index = x.c[i].n - 1 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.c[i].keys[x.c[i].n - 1] - (call $btreeDelete) ;; (x.c[i], x.c[i].keys[x.c[i].n - 1]) - (drop) - (i32.store offset=8) ;; x.keys[i] = x.c[i].keys[x.c[i].n - 1] - ) - (else - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) ;; i+1 - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located - (i32.load offset=8) ;; x.c[i+1] - (local.tee 5) - (i32.load offset=4) ;; x.c[i+1].n - (i32.const 0) - (i32.load) ;; t - (i32.const 2) - (i32.div_s) ;; t/2 - (i32.ge_s) ;; x.c[i+1].n >= t/2 - (if - (then - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.keys[i] - (local.get 5) ;; x.c[i+1] - (i32.const 4) - (i32.const 0) ;; index = 0 - (i32.mul) ;; i *4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.c[i+1].keys[0] - (local.get 5) ;; x.c[i+1] - (local.get 5) ;; x.c[i+1] - (i32.const 4) - (i32.const 0) ;; index = 0 - (i32.mul) ;; i *4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.c[i+1].keys[0] - (call $btreeDelete) - (drop) - (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] - ) - (else - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 4) - (i32.mul) ;; i*4 - (i32.add) ;; (t-1)*4 + i*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located - (i32.load offset=8) ;; x.c[i] - (local.set 5) - (local.get 5) ;; x.c[i] - (i32.const 4) - (local.get 5) - (i32.load offset=4) ;; index = x.c[i].n - (i32.mul) ;; i *4 - (i32.add) ;; x + i*4 --> addr where x.c[i].keys[x.c[i].n] will be stored - (local.get 1) ;; k - (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = k - (i32.const 0) ;; now merge x.c[i] with x.c[i+1] - (local.set 3) ;; j = 0 - (block $loop_break - (loop $loop - (local.get 3) ;; j - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) ;; i+1 - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.load offset=4) ;; x.c[i+1].n - (i32.eq) - (if ;; j == x.c[i+1].n - (then - (br $loop_break) - ) - (else - (local.get 5) ;; x.c[i] - (i32.const 4) - (local.get 5) - (i32.load offset=4) - (local.get 3) - (i32.add) - (i32.const 1) - (i32.add) ;; index = x.c[i].n + j + 1 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[index] - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) ;; i+1 - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.const 4) - (local.get 3) ;; index = j - (i32.mul) ;; i*4 - (i32.add) ;; x.c[i+1] + i*4 ;; addr of x.c[i+1].keys[j] - (i32.load offset=8) ;; x.c[i+1].keys[j] - (i32.store offset=8) ;; x.c[i].keys[index] = x.c[i+1].keys[j] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j+1 - (br $loop) - ) - ) - ) - ) - (local.get 5) - (local.get 5) - (i32.load offset=4) ;; x.c[i].n - (i32.const 1) - (i32.add) ;; x.c[i].n +1 - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) ;; i+1 - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.load offset=4) ;; x.c[i+1].n - (i32.add) - (i32.store offset=4) ;; x.c[i].n = x.c[i].n + 1 + x.c[i+1].n - (local.get 5) - (i32.load) - (i32.const 1) - (i32.ne) - (if - (then - (i32.const 0) ;; now adjust the children - (local.set 3) ;; j = 0 - (block $loop_break - (loop $loop - (local.get 3) ;; j - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) ;; i+1 - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.load offset=4) ;; x.c[i+1].n - (i32.const 1) - (i32.add) - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 5) - (i32.load offset=4) ;; x.c[i].n - (local.get 3) ;; j - (i32.add) ;; index = x.c[i].n+j - (i32.const 1) - (i32.add) ;; i+1 - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 5) ;; x.c[i] - (i32.add) ;; x.c[i] + (t-1)*4 --> address where x.c[i].c[x.c[i].n+j] is stored - (i32.const 0) ;;get x.c[i+1].c[j] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) ;; j - (i32.const 4) - (i32.mul) ;; j *4 - (i32.add) ;; (t-1)*4 + j *4 - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) ;; i+1 - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.add) ;; x.c[i+1] + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1].c[j] - (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j+1 - (br $loop) - ) - ) - ) - ) - ) - ) - (local.get 2) ;; i - (local.set 3) ;; j = i - (block $loop_break - (loop $loop - (local.get 0) - (i32.load offset=4) - (i32.const 1) - (i32.sub) - (local.get 3) - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (local.get 0) ;; x - (i32.const 4) - (local.get 3) ;; j - (i32.mul) ;; j*4 - (i32.add) ;; x + j*4 ;; addr of x.keys[j] - (local.get 0) ;; x - (i32.const 4) - (local.get 3) ;; j - (i32.const 1) - (i32.add) - (i32.mul) ;; j+1*4 - (i32.add) ;; x + j+1*4 ;; addr of x.keys[j+1] - (i32.load offset=8) - (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j+1 - (br $loop) - ) - ) - ) - ) - (local.get 2) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = i+1 - (block $loop_break - (loop $loop - (local.get 0) - (i32.load offset=4) - (local.get 3) - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (i32.const 0) ;;get x.c[j] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) ;; j - (i32.const 4) - (i32.mul) ;; j *4 - (i32.add) ;; (t-1)*4 + j+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] - (i32.const 0) ;;get x.c[j+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) ;; j - (i32.const 1) - (i32.add) ;; j+1 - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] - (i32.load offset=8) - (i32.store offset=8) ;; x.c[j] = x.c[j+1] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j+1 - (br $loop) - ) - ) - ) - ) - (local.get 0) - (local.get 0) - (i32.load offset=4) ;; x.n - (i32.const 1) - (i32.sub) - (i32.store offset=4) ;; x.n = x.n-1 - (local.get 5) ;; x.c[i] - (local.get 1) ;; k - (call $btreeDelete) - (drop) - ) - ) - ) - ) - ) - (else ;; key not present in node x - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 4) - (i32.mul) ;; i*4 - (i32.add) ;; (t-1)*4 + i*4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 --> address where addr of child in index i is located - (i32.load offset=8) ;; x.c[i] - (local.set 5) - (local.get 5) - (i32.load offset=4) ;; x.c[i].n - (i32.const 0) - (i32.load) ;; t - (i32.const 2) - (i32.div_s) - (i32.const 1) - (i32.sub) ;; t/2 - 1 - (i32.eq) - (if - (then - (i32.const -1) - (local.set 4) ;; changed = -1 - (local.get 2) ;; i - (i32.const 1) - (i32.add) ;; i+1 - (local.get 0) - (i32.load offset=4) ;; x.n - (i32.le_s) ;; i+1 <= x.n - (if - (then - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) - (i32.load offset=4) ;; x.c[i+1].n - (i32.const 0) - (i32.load) ;; t - (i32.const 2) - (i32.div_s) ;; t/2 - (i32.ge_s) ;; x.c[i+1].n >= t/2 - (if - (then - (local.get 5) ;; x.c[i] - (i32.const 4) - (i32.const 0) - (i32.load) ;; t - (i32.const 2) - (i32.div_s) ;; t/2 - (i32.const 1) - (i32.sub) ;; index = t/2 -1 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[t/2-1] - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.keys[i] - (i32.load offset=8) ;; x.keys[i] - (i32.store offset=8) ;; x.c[i].keys[t/2-1] = x.keys[i] - (local.get 5) - (local.get 5) - (i32.load offset=4) ;; x.c[i].n - (i32.const 1) - (i32.add) - (i32.store offset=4) ;;x.c[i].n = x.c[i].n +1 - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.keys[i] - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.const 4) - (i32.const 0) ;; index = 0 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.c[i+1].keys[0] - (i32.load offset=8) ;; x.c[i+1].keys[0] - (i32.store offset=8) ;; x.keys[i] = x.c[i+1].keys[0] - (local.get 5) - (i32.load) ;; x.c[i] is leaf? - (i32.const 1) - (i32.ne) - (if ;; if x.c[i] is not leaf, we have to adjust children - (then - (i32.const 0) ;;get x.c[i].c[t/2] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (i32.const 0) - (i32.load) ;; t - (i32.const 2) - (i32.div_s) ;; index = t/2 - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 5) ;; x.c[i] - (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[t/2] - (i32.const 0) ;;get x.c[i+1].c[0] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (i32.const 0) ;; index = 0 - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1].c[0] - (i32.store offset=8) ;; x.c[i].c[t/2] = x.c[i+1].c[0] - ) - ) - (local.get 5) ;; x.c[i] - (i32.const 0) - (i32.load) ;; t - (i32.const 2) - (i32.div_s) ;; t/2 - (i32.store offset=4) ;; x.c[i].n = t/2 - (i32.const 0) - (local.set 3) ;; j = 0 - (block $loop_break - (loop $loop - (local.get 3) ;; j - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.load offset=4) ;; x.c[i+1].n - (i32.const 1) - (i32.sub) - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.const 4) - (local.get 3) ;; index = j - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 --> address where x.c[i+1].keys[j] is located - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.const 4) - (local.get 3) - (i32.const 1) - (i32.add) ;; index = j+1 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.c[i+1].keys[j+1] - (i32.store offset=8) ;; x.c[i+1].keys[j] = x.c[i+1].keys[j+1] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j+1 - (br $loop) - ) - ) - ) - ) - (local.get 5) - (i32.load) - (i32.const 1) - (i32.ne) - (if ;; if x.c[i] is not leaf, we have to adjust children - (then - (i32.const 0) - (local.set 3) ;; j = 0 - (block $loop_break - (loop $loop - (local.get 3) ;; j - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.load offset=4) ;; x.c[i+1].n - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) ;; index = j - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.add) ;; x + (t-1)*4 --> address where x.c[i+1].c[j] is located - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) - (i32.const 1) - (i32.add) ;; index = j+1 - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1].c[j+1] - (i32.store offset=8) ;; x.c[i+1].c[j] = x.c[i+1].c[j+1] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j+1 - (br $loop) - ) - ) - ) - ) - ) - ) - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 --> addr where x.c[i+1] is located - (i32.load offset=8) - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.load offset=4) ;; x.c[i+1].n - (i32.const 1) - (i32.sub) - (i32.store offset=4) ;; x.c[i+1].n = x.c[i+1].n - 1 - (i32.const 0) - (local.set 4) ;; changed = 0 - ) - ) - ) - ) - (local.get 4) ;; changed - (i32.const -1) - (i32.eq) - (local.get 2) ;; i - (i32.const 1) - (i32.sub) ;; i - 1 - (i32.const 0) - (i32.ge_s) ;; i -1 >= 0 - (i32.and) ;; changed == -1 && i -1 >= 0 - (if - (then - (i32.const 0) ;;get x.c[i-1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) - (i32.load offset=4) ;; x.c[i-1].n - (i32.const 0) - (i32.load) ;; t - (i32.const 2) - (i32.div_s) ;; t/2 - (i32.ge_s) ;; x.c[i-1].n >= t/2 - (if - (then - (local.get 5) - (i32.load offset=4) ;; x.c[i].n - (local.set 3) ;; j = x.c[i].n - (block $loop_break - (loop $loop - (local.get 3) ;; j - (i32.const 0) - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (local.get 5) ;; x.c[i] - (i32.const 4) - (local.get 3) ;; j - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j] - (local.get 5) ;; x.c[i] - (i32.const 4) - (local.get 3) ;; j - (i32.const 1) - (i32.sub) ;; j-1 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[j-1] - (i32.load offset=8) ;; x.c[i].keys[j-1] - (i32.store offset=8) ;; x.c[i].keys[j] = x.c[i].keys[j-1] - (local.get 3) - (i32.const 1) - (i32.sub) - (local.set 3) ;; j = j-1 - (br $loop) - ) - ) - ) - ) - (local.get 5) - (i32.load) - (i32.const 1) - (i32.ne) - (if - (then - (local.get 5) - (i32.load offset=4) ;; x.c[i].n - (i32.const 1) - (i32.add) - (local.set 3) ;; j = x.c[i].n + 1 - (block $loop_break - (loop $loop - (local.get 3) ;; j - (i32.const 0) - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (i32.const 0) ;;get x.c[i].c[j] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) ;; j - (i32.const 4) - (i32.mul) ;; j *4 - (i32.add) ;; (t-1)*4 + j *4 - (local.get 5) ;; x.c[i] - (i32.add) ;; x + (t-1)*4 --> address where x.c[i].c[j] is located - (i32.const 0) ;;get x.c[i].c[j-1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) ;; j - (i32.const 1) - (i32.sub) ;; j-1 - (i32.const 4) - (i32.mul) ;; j *4 - (i32.add) ;; (t-1)*4 + j *4 - (local.get 5) ;; x.c[i] - (i32.add) - (i32.load offset=8) ;; x.c[i].c[j-1] - (i32.store offset=8) ;; x.c[i].c[j] = x.c[i].c[j-1] - (local.get 3) - (i32.const 1) - (i32.sub) - (local.set 3) ;; j = j-1 - (br $loop) - ) - ) - ) - ) - ) - ) - (local.get 5) - (local.get 5) - (i32.load offset=4) ;; x.c[i].n - (i32.const 1) - (i32.add) - (i32.store offset=4) ;; ;; x.c[i].n = x.c[i].n + 1 - (local.get 5) ;; x.c[i] - (i32.const 4) - (i32.const 0) ;; index = 0 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[0] - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.const 1) - (i32.sub) ;; index = i-1 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] - (i32.load offset=8) ;; x.keys[i-1] - (i32.store offset=8) ;; x.c[i].keys[0] = x.keys[i-1] - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) - (i32.load offset=4) ;; x.c[i-1].n - (i32.const 1) - (i32.sub) - (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n - 1 - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; i - (i32.const 1) - (i32.sub) ;; index = i-1 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i-1] - (i32.const 4) - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i-1] - (i32.load offset=4) ;; index = x.c[i-1].n - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] - (i32.load offset=8) - (i32.store offset=8) ;; x.keys[i-1] = x.c[i-1].keys[x.c[i-1].n] - (local.get 5) - (i32.load) - (i32.const 1) - (i32.ne) - (if - (then - (i32.const 0) ;; get x.c[i].c[0] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (i32.const 0) ;; index = 0 - (i32.const 4) - (i32.mul) ;; i *4 - (i32.add) ;; (t-1)*4 + i *4 - (local.get 5) ;; x.c[i] - (i32.add) ;; x + (t-1)*4 ;; addr where x.c[i].c[0] is located - (i32.const 0) ;; get x.c[i-1].c[x.c[i-1].n + 1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i-1] - (i32.load offset=4) ;; x.c[i-1].n - (i32.const 1) - (i32.add) ;; index = x.c[i-1.n + 1] - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (i32.const 0) ;;get x.c[i-1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i-1] - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i-1].c[x.c[i-1].n + 1] - (i32.store offset=8) ;; x.c[i].c[0] = x.c[i-1].c[x.c[i-1].n + 1] - ) - ) - (i32.const 0) - (local.set 4) ;; changed = 0 - ) - ) - ) - ) - (local.get 4) ;; changed - (i32.const -1) - (i32.eq) - (if ;; changed == -1? - (then - (local.get 2) ;; we have to merge x.c[i] with one sibling;; i - (i32.const 1) - (i32.add) ;; i+1 - (local.get 0) ;; x - (i32.load offset=4) ;; x.n - (i32.le_s) - (if ;; i+1 <= x.n - (then - (local.get 5) ;; merge with right sibling;; x.c[i] - (i32.const 4) - (local.get 5) - (i32.load offset=4) ;; index = x.c[i].n - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n] - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; index = i - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.keys[i] - (i32.load offset=8) ;; x.keys[i] - (i32.store offset=8) ;; x.c[i].keys[x.c[i].n] = x.keys[i] - (i32.const 0) - (local.set 3) ;; j = 0 - (block $loop_break - (loop $loop - (local.get 3) ;; j - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.load offset=4) ;; x.c[i+1].n - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (local.get 5) ;; x.c[i] - (i32.const 4) - (local.get 5) - (i32.load offset=4) - (local.get 3) - (i32.add) - (i32.const 1) - (i32.add) ;; index = x.c[i].n+j+1 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.c[i].keys[x.c[i].n+j] - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.const 4) - (local.get 3) ;; index = j - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.c[i+1].keys[j] - (i32.store offset=8) ;; x.c[i].keys[x.c[i].n+j] = x.c[i+1].keys[j] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j + 1 - (br $loop) - ) - ) - ) - ) - (local.get 5) - (i32.load) - (i32.const 1) - (i32.ne) - (if - (then - (i32.const 0) - (local.set 3) ;; j = 0 - (block $loop_break - (loop $loop - (local.get 3) ;; j - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.load offset=4) ;; x.c[i+1].n - (i32.const 1) - (i32.add) ;; x.c[i+1].n + 1 - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 5) - (i32.load offset=4) - (local.get 3) - (i32.add) - (i32.const 1) - (i32.add) ;; index = x.c[i].n+j+1 - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 5) ;; x.c[i] - (i32.add) ;; x + (t-1)*4 --> addr of x.c[i].c[x.c[i].n+j] - (i32.const 0) ;;get x.c[i].c[x.c[i].n+j] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) ;; index = j - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1].c[j] - (i32.store offset=8) ;; x.c[i].c[x.c[i].n+j] = x.c[i+1].c[j] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j + 1 - (br $loop) - ) - ) - ) - ) - ) - ) - (local.get 5) ;; x.c[i] - (local.get 5) ;; x.c[i] - (i32.load offset=4) ;; x.c[i].n - (i32.const 0) ;;get x.c[i+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.add) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i+1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i+1] - (i32.load offset=4) ;; x.c[i+1].n - (i32.add) ;; x.c[i].n + x.c[i+1].n - (i32.const 1) - (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 - (i32.store offset=4) ;; x.c[i].n = x.c[i].n + x.c[i+1].n + 1 - (local.get 2) ;; i - (i32.const 1) - (i32.add) ;; i+1 - (local.set 3) ;; j = i+1 - (block $loop_break ;; re-organize x - (loop $loop - (local.get 3) - (local.get 0) - (i32.load offset=4) ;; x.n - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (i32.const 0) ;;get x.c[j] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) ;; index = j - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] - (i32.const 0) ;;get x.c[j+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) - (i32.const 1) - (i32.add) ;; index = j + 1 - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 --> addr of x.c[j+1] - (i32.load offset=8) ;; x.c[j+1] - (i32.store offset=8) ;; x.c[j] = x.c[j+1] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j + 1 - (br $loop) - ) - ) - ) - ) - (local.get 2) ;; i - (local.set 3) ;; j = i - (block $loop_break - (loop $loop - (local.get 3) ;; j - (local.get 0) - (i32.load offset=4) ;; x.n - (i32.const 1) - (i32.sub) - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (local.get 0) ;; x - (i32.const 4) - (local.get 3) ;; index = j - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.keys[j] - (local.get 0) ;; x - (i32.const 4) - (local.get 3) - (i32.const 1) - (i32.add) ;; index = j + 1 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.keys[j+1] - (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j + 1 - (br $loop) - ) - ) - ) - ) - (local.get 0) ;; x - (local.get 0) ;; x - (i32.load offset=4) ;; x.n - (i32.const 1) - (i32.sub) ;; x.n - 1 - (i32.store offset=4) ;; x.n = x.n - 1 - (i32.const 0) - (local.set 4) ;; changed = 0 - ) - ) - (local.get 4) ;; changed - (i32.const -1) - (i32.eq) ;; changed == -1 ? - (local.get 2) ;; i - (i32.const 1) - (i32.sub) ;; i - 1 - (i32.const 0) - (i32.ge_s) ;; i - 1 >= 0? - (i32.and) ;; changed == -1 && i - 1 >= 0 - (if - (then - (i32.const 0) ;; merge with left sibling ;;get x.c[i-1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i-1] - (i32.const 4) - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i-1] - (i32.load offset=4) ;; index = x.c[i-1].n - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n] - (local.get 0) ;; x - (i32.const 4) - (local.get 2) ;; index = i - (i32.const 1) - (i32.sub) ;; index = i-1 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.keys[i-1] - (i32.load offset=8) ;; x.keys[i] - (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n] = x.keys[i-1] - (i32.const 0) - (local.set 3) ;; j = 0 - (block $loop_break - (loop $loop - (local.get 3) ;; j - (local.get 5) ;; x.c[i] - (i32.load offset=4) ;; x.c[i].n - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (i32.const 0) ;; x.c[i-1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i-1] - (i32.const 4) - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i-1] - (i32.load offset=4) ;; x.c[i-1].n - (local.get 3) - (i32.add) - (i32.const 1) - (i32.add) ;; index = x.c[i-1].n+j+1 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.c[i-1].keys[x.c[i-1].n+j] - (local.get 5) ;; x.c[i] - (i32.const 4) - (local.get 3) ;; index = j - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.c[i].keys[j] - (i32.store offset=8) ;; x.c[i-1].keys[x.c[i-1].n+j] = x.c[i].keys[j] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j + 1 - (br $loop) - ) - ) - ) - ) - (local.get 5) - (i32.load) - (i32.const 1) - (i32.ne) - (if - (then - (i32.const 0) - (local.set 3) ;; j = 0 - (block $loop_break - (loop $loop - (local.get 3) ;; j - (local.get 5) ;; x.c[i] - (i32.load offset=4) ;; x.c[i].n - (i32.const 1) - (i32.add) ;; x.c[i].n + 1 - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (i32.const 0) ;;get x.c[i-1].c[x.c[i-1].n+j] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i-1] - (i32.load offset=4) - (local.get 3) - (i32.add) - (i32.const 1) - (i32.add) ;; index = x.c[i-1].n+j+1 - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i-1] - (i32.add) ;; x + (t-1)*4 --> addr of x.c[i-1].c[x.c[i-1].n+j] - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) ;; index = j - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 5) ;; x.c[i] - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i].c[j] - (i32.store offset=8) ;; x.c[i-1].c[x.c[i-1].n+j] = x.c[i].c[j] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j + 1 - (br $loop) - ) - ) - ) - ) - ) - ) - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i-1] - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i-1] - (i32.load offset=4) ;; x.c[i-1].n - (local.get 5) - (i32.load offset=4) ;; x.c[i].n - (i32.add) ;; x.c[i-1].n + x.c[i].n - (i32.const 1) - (i32.add) ;; x.c[i].n + x.c[i+1].n + 1 - (i32.store offset=4) ;; x.c[i-1].n = x.c[i-1].n + x.c[i].n + 1 - (local.get 2) ;; i - (local.set 3) ;; j = i - (block $loop_break ;; re-organize x - (loop $loop - (local.get 3) - (local.get 0) - (i32.load offset=4) ;; x.n - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (i32.const 0) ;;get x.c[j] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) ;; index = j - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] - (i32.const 0) ;;get x.c[j+1] - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 3) - (i32.const 1) - (i32.add) ;; index = j + 1 - (i32.const 4) - (i32.mul) ;; i-1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 --> addr of x.c[j] - (i32.load offset=8) ;; x.c[j+1] - (i32.store offset=8) ;; x.c[j] = x.c[j+1] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j + 1 - (br $loop) - ) - ) - ) - ) - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (local.set 3) ;; j = i - 1 - (block $loop_break - (loop $loop - (local.get 3) ;; j - (local.get 0) - (i32.load offset=4) ;; x.n - (i32.const 1) - (i32.sub) - (i32.eq) - (if - (then - (br $loop_break) - ) - (else - (local.get 0) ;; x - (i32.const 4) - (local.get 3) ;; index = j - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 ;; addr of x.keys[j] - (local.get 0) ;; x - (i32.const 4) - (local.get 3) - (i32.const 1) - (i32.add) ;; index = j + 1 - (i32.mul) ;; i*4 - (i32.add) ;; x + i*4 - (i32.load offset=8) ;; x.keys[j+1] - (i32.store offset=8) ;; x.keys[j] = x.keys[j+1] - (local.get 3) - (i32.const 1) - (i32.add) - (local.set 3) ;; j = j + 1 - (br $loop) - ) - ) - ) - ) - (local.get 0) ;; x - (local.get 0) ;; x - (i32.load offset=4) ;; x.n - (i32.const 1) - (i32.sub) ;; x.n - 1 - (i32.store offset=4) ;; x.n = x.n - 1 - ) - ) - ) - ) - ) - ) - (local.get 4) - (i32.const -1) - (i32.eq) - (if ;; changed == -1? if yes, we merged with left sibling - (then - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (local.get 2) ;; i - (i32.const 1) - (i32.sub) - (i32.const 4) - (i32.mul) ;; i+1 *4 - (i32.add) ;; (t-1)*4 + i-1 *4 - (local.get 0) ;; x - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; x.c[i-1] - (local.get 1) - (call $btreeDelete) - (drop) - ) - (else - (local.get 5) ;; x.c[i] - (local.get 1) - (call $btreeDelete) - (drop) - ) - ) - ) - ) - ) ;; end of if x is not leaf - ) - (i32.const 0) - (i32.load offset=8) ;; root addr - (i32.load offset=4) - (i32.const 0) - (i32.eq) - (if ;; if root is empty - (then - (i32.const 0) - (i32.const 0) - (i32.load) ;; t - (i32.const 1) - (i32.sub) ;; t-1 - (i32.const 4) - (i32.mul) ;; (t-1)*4 - (i32.const 0) ;; index = 0 - (i32.const 4) - (i32.mul) ;; i *4 - (i32.add) ;; (t-1)*4 + i *4 - (i32.const 0) - (i32.load offset=8) ;; root addr - (i32.add) ;; x + (t-1)*4 - (i32.load offset=8) ;; root.c[0] - (i32.store offset=8) ;; root = root.c[0] - ) - ) - (i32.const 0) - (i32.load offset=8) ;; root addr - ) - (func $main (param $a i32) (param $b i32) (param $h i32) - (local $btree i32) - (local.get $a) - (local.get $b) - (i32.gt_s) - (local.get $a) - (local.get $h) - (i32.ne) - (local.get $b) - (local.get $h) - (i32.ne) - (i32.and) - (i32.and) - (i32.eqz) - (if (then (unreachable))) - (i32.const 4) ;;create a tree with degree 4 - (call $createBtree) - (local.set $btree) - ;; insert variables - (local.get $a) - (call $btreeInsert) - (local.set $btree) - (local.get $b) - (call $btreeInsert) - (local.set $btree) - (local.get $h) - (call $btreeInsert) - (local.set $btree) - ;; search for variables & check that they were inserted - (local.get $btree) - (local.get $a) - (call $btreeSearch) - (i32.const -1) - (i32.ne) - (local.get $btree) - (local.get $b) - (call $btreeSearch) - (i32.const -1) - (i32.ne) - (local.get $btree) - (local.get $h) - (call $btreeSearch) - (i32.const -1) - (i32.ne) - (i32.and) - (i32.and) - ;; delete & check that it was deleted - ;; a - (local.get $btree) - (local.get $a) - (call $btreeDelete) - (local.tee $btree) - (local.get $a) - (call $btreeSearch) - (i32.const -1) - (i32.eq) - ;; b - (local.get $btree) - (local.get $b) - (call $btreeDelete) - (local.tee $btree) - (local.get $b) - (call $btreeSearch) - (i32.const -1) - (i32.eq) - (i32.and) - (i32.and) - (drop) - ) - (export "main" (func $real_main)) - (func $real_main - i32.const 3 - i32.const 2 - i32.const 1 - call $main - ) - )