1.text
2.global __clone
3.type __clone, %function
4__clone:
5	# int clone(
6	#    fn,      a = r2
7	#    stack,   b = r3
8	#    flags,   c = r4
9	#    arg,     d = r5
10	#    ptid,    e = r6
11	#    tls,     f = *(r15+160)
12	#    ctid)    g = *(r15+168)
13	#
14	# pseudo C code:
15	# tid = syscall(SYS_clone,b,c,e,g,f);
16	# if (!tid) syscall(SYS_exit, a(d));
17	# return tid;
18
19	# create initial stack frame for new thread
20	nill %r3, 0xfff8
21	aghi %r3, -160
22	lghi %r0, 0
23	stg  %r0, 0(%r3)
24
25	# save fn and arg to child stack
26	stg  %r2,  8(%r3)
27	stg  %r5, 16(%r3)
28
29	# shuffle args into correct registers and call SYS_clone
30	lgr  %r2, %r3
31	lgr  %r3, %r4
32	lgr  %r4, %r6
33	lg   %r5, 168(%r15)
34	lg   %r6, 160(%r15)
35	svc  120
36
37	# if error or if we're the parent, return
38	ltgr %r2, %r2
39	bnzr %r14
40
41	# we're the child. call fn(arg)
42	lg   %r1,  8(%r15)
43	lg   %r2, 16(%r15)
44	basr %r14, %r1
45
46	# call SYS_exit. exit code is already in r2 from fn return value
47	svc  1
48