#!/bin/bash

# first imp2026

cd dhrystone
echo "%externalintegerfnspec cpu time" > dhry_1+clock.imp
cat dhry_1.imp >> dhry_1+clock.imp
imp77 -c dhry_1+clock.imp
imp77 -c dhry_2.imp
imp77 -c clock.imp
gcc -no-pie -o dry-77 dhry_1+clock.o dhry_2.o clock.o  /home/gtoal/src/imp2026/release/lib/libimp77.a -lm -T /home/gtoal/src/imp2026/release/bin/ld.i77.script
cd ..


./i2c --stdout -O -c --no-check dhrystone/dhry_1.imp > dhrystone/dhry_1.c
./i2c --stdout -O -c --no-check dhrystone/dhry_2.imp > dhrystone/dhry_2.c

# manualy fix one error in translation:
##ecce  dhrystone/dhry_2.c -command "f/_U((int *)/s/_U((int)/p;%c" 2> /dev/null
sed -i 's/_U((int \*)/_U((int)/' dhrystone/dhry_2.c

# gcc12 -DVALGRIND_AVAILABLE -O -g -ftrivial-auto-var-init=pattern -fmerge-constants -fno-strict-aliasing -Wall -Wno-unused-but-set-variable -Wno-unused-variable -Wno-unused-label -Wno-unused-function -Wno-maybe-uninitialized -Wno-uninitialized -frecord-gcc-switches -fsanitize=undefined -fsanitize=float-divide-by-zero -fsanitize-undefined-trap-on-error -fsanitize=float-cast-overflow -fno-sanitize-recover=all -fstack-protector -Wno-frame-address -fno-omit-frame-pointer -Wno-return-type -Wno-comment -ftrapv -ggdb3 -gdwarf-4 -I. -I.. -o dry dhrystone/dhry_1.c dhrystone/dhry_2.c perms.c -lm

gcc12 -O2 -I. -I.. -o dry-opt dhrystone/dhry_1.c dhrystone/dhry_2.c perms.c -lm

echo "Dhrystone benchmark using i2c:"
./dry-opt | tail -4

pushd dhrystone/ORIG
echo "Dhrystone benchmark in C:"
./make.sh 2> /dev/null | tail -4
popd

echo "Dhrystone benchmark using imp2026:"
./dhrystone/dry-77
