#This work is licensed under the
#Creative Commons Attribution-Share Alike 3.0 United States License.
#To view a copy of this license, visit
#http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to
#Creative Commons,
#171 Second Street, Suite 300,
#San Francisco, California, 94105, USA.

INPUT unsigned_;
OUTPUT "a long value written to the stream";
VAR unsigned_ "unsigned value";
VAR written "written value";
VAR LSB "LSB";
FUNC write_unsigned "write unsigned" "shorten:write_unsigned";
if unsigned_ == 0 {
    write_unsigned(2, 0);
    write_unsigned(0, 0);
} else {
    LSB <- floor(log 2 {unsigned_}) + 1;
    write_unsigned(2, LSB);
    write_unsigned(LSB, unsigned_);
}
return written;
