# skip amd64_win (pthread and tls not implemented)
thread data $i = align 4 {w 42}
data $fmti = align 1 {b "i%d==%d\n", b 0}

thread data $x = {w 1, w 2, w 3, w 4}
data $fmtx = align 1 {b "*(x+%d)==%d\n", b 0}

export
function w $main() {
@start
	%pthr =l alloc8 8
	%rval =l alloc8 8
	call $pthread_create(l %pthr, l 0, l $thread, l 0)
	%t =l load %pthr
	call $pthread_join(l %t, l %rval)
	%i0 =w loadw thread $i
	call $printf(l $fmti, ..., w 0, w %i0)
	%i1 =w load %rval
	call $printf(l $fmti, ..., w 1, w %i1)

	%a0 =l call $xaddr()
	%x0 =w load %a0
	call $printf(l $fmtx, ..., w 0, w %x0)
	%a4 =l call $xaddroff4()
	%x4 =w load %a4
	call $printf(l $fmtx, ..., w 4, w %x4)
	%a8 =l call $xaddroff(l 8)
	%x8 =w load %a8
	call $printf(l $fmtx, ..., w 8, w %x8)
	%xc =l call $xvalcnt(l 3)
	call $printf(l $fmtx, ..., w 12, w %xc)
	ret 0
}

function l $thread(l %arg) {
@start
	%i3 =l add thread $i, 3
	storeb 24, %i3
	%ret =l loadsw thread $i
	ret %ret
}

function l $xaddr() {
@start
	ret thread $x
}

function l $xaddroff4() {
@start
	%a =l add 4, thread $x
	ret %a
}

function l $xaddroff(l %off) {
@start
	%a =l add thread $x, %off
	ret %a
}

function w $xvalcnt(l %cnt) {
@start
	%o =l mul 4, %cnt
	%a =l add thread $x, %o
	%x =w load %a
	ret %x
}

# >>> output
# i0==42
# i1==402653226
# *(x+0)==1
# *(x+4)==2
# *(x+8)==3
# *(x+12)==4
# <<<
