#### Test using many variables

section data
align
format:
string "many_locals %d: %d\n\x00"

align
format1:
string "many_args: %d\n\x00"

align
format2:
string "many_args_tail: %d\n\x00"

section functions
import printf
export main

many_locals:
function x
    let a x
    let b add x 1
    let c add x 2
    let d add x 3
    let e add x 4
    let f add x 5
    let g add x 6
    let h add x 7
    let i add x 8
    let j add x 9
    let k add x 10
    let l add x 11
    let m add x 12
    let n add x 13
    let o add x 14
    let p add x 15
    let q add x 16
    let r add x 17
    let s add x 18
    let t add x 19

    let sum add a b
    set sum add sum c
    set sum add sum d
    set sum add sum e
    set sum add sum f
    set sum add sum g
    set sum add sum h
    set sum add sum i
    set sum add sum j
    set sum add sum k
    set sum add sum l
    set sum add sum m
    set sum add sum n
    set sum add sum o
    set sum add sum p
    set sum add sum q
    set sum add sum r
    set sum add sum s
    set sum add sum t

    return sum
end function

many_args:
function count a b c d e f g h i j k l m n o p q r s t

    let result 0
    ifle count 0
        set result add result a
        set result add result b
        set result add result c
        set result add result d
        set result add result e
        set result add result f
        set result add result g
        set result add result h
        set result add result i
        set result add result j
        set result add result k
        set result add result l
        set result add result m
        set result add result n
        set result add result o
        set result add result p
        set result add result q
        set result add result r
        set result add result s
        set result add result t
        return result
    end if

    let newcount sub count 1
    let newa add a 1
    let newb add b 1
    let newc add c 1
    let newd add d 1
    let newe add e 1
    let newf add f 1
    let newg add g 1
    let newh add h 1
    let newi add i 1
    let newj add j 1
    let newk add k 1
    let newl add l 1
    let newm add m 1
    let newn add n 1
    let newo add o 1
    let newp add p 1
    let newq add q 1
    let newr add r 1
    let news add s 1
    let newt add t 1

    return call many_args newcount newa newb newc newd newe newf newg newh \
                newi newj newk newl newm newn newo newp newq newr news newt
end function

many_args_tail:
function count a b c d e f g h i j k l m n o p q r s t

    let result 0
    ifle count 0
        set result add result a
        set result add result b
        set result add result c
        set result add result d
        set result add result e
        set result add result f
        set result add result g
        set result add result h
        set result add result i
        set result add result j
        set result add result k
        set result add result l
        set result add result m
        set result add result n
        set result add result o
        set result add result p
        set result add result q
        set result add result r
        set result add result s
        set result add result t
        return result
    end if

    let newcount sub count 1
    let newa add a 1
    let newb add b 1
    let newc add c 1
    let newd add d 1
    let newe add e 1
    let newf add f 1
    let newg add g 1
    let newh add h 1
    let newi add i 1
    let newj add j 1
    let newk add k 1
    let newl add l 1
    let newm add m 1
    let newn add n 1
    let newo add o 1
    let newp add p 1
    let newq add q 1
    let newr add r 1
    let news add s 1
    let newt add t 1

    tail-call many_args_tail newcount newa newb newc newd newe newf newg newh \
              newi newj newk newl newm newn newo newp newq newr news newt
end function

main:
function argc argv
    let x call many_locals 1
    call printf format 1 x

    set x call many_locals -20
    call printf format -20 x

    set x call many_args 3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
    call printf format1 x

    set x call many_args_tail 1000000 1 2 3 4 5 6 7 8 9 10 11 \
                              12 13 14 15 16 17 18 19 20
    call printf format2 x

    return 0
end function
