// File: /workspace/github_c_makefile_star2_O1/wahern[P]cqueues/src/thread.c
// Function: ct_start
static int ct_start(lua_State *L) {
	struct cthread **ud, *ct;
	sigset_t mask, omask;
	int top, error;

	top = lua_gettop(L);

	ud = lua_newuserdata(L, sizeof *ud);
	*ud = NULL;

	luaL_getmetatable(L, CQS_THREAD);
	lua_setmetatable(L, -2);

	if (!(ct = *ud = ct_create(&error)))
		goto error;

	luaL_checktype(L, 1, LUA_TFUNCTION);

	if (!(ct->tmp.arg = calloc(sizeof *ct->tmp.arg, top)))
		goto syerr;

	for (int index = 1; index <= top; index++) {
		struct cthread_arg *arg = &ct->tmp.arg[ct->tmp.argc];

		switch (lua_type(L, index)) {
		case LUA_TNIL:
			arg->type = LUA_TNIL;
			break;
		case LUA_TNUMBER:
#if LUA_VERSION_NUM >= 503
			if (lua_isinteger(L, index)) {
				arg->v.integer = lua_tointeger(L, index);
				arg->isinteger = 1;
				arg->type = LUA_TNUMBER;
				break;
			}
#endif
			arg->v.number = lua_tonumber(L, index);
			arg->type = LUA_TNUMBER;
			break;
		case LUA_TBOOLEAN:
			arg->v.boolean = lua_toboolean(L, index);
			arg->type = LUA_TBOOLEAN;
			break;
		case LUA_TLIGHTUSERDATA:
			arg->v.pointer = lua_touserdata(L, index);
			arg->type = LUA_TLIGHTUSERDATA;
			break;
		case LUA_TFUNCTION:
			if ((error = ct_setfarg(L, ct, arg, index)))
				goto error;
			break;
		default:
			/* FALL THROUGH (maybe has __tostring metamethod) */
		case LUA_TSTRING:
			arg->v.string.iov_base = (char *)luaL_checklstring(L, index, &arg->v.string.iov_len);
			arg->type = LUA_TSTRING;
			break;
		}

		ct->tmp.argc++;
	}

	/* we may have added more stack objects above the thread object */
	luaL_checkstack(L, 2, "too many arguments");
	lua_pushvalue(L, top + 1);

	if (0 != cqs_socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, ct->tmp.fd, O_NONBLOCK|O_CLOEXEC))
		goto syerr;

	if ((error = cqs_socket_fdopen(L, ct->tmp.fd[0], NULL)))
		goto error;

	ct->tmp.fd[0] = -1;

	sigfillset(&mask);
	sigemptyset(&omask);
	if ((error = pthread_sigmask(SIG_SETMASK, &mask, &omask)))
		goto error;

	pthread_mutex_lock(&ct->mutex);

	if (!(error = pthread_create(&ct->id, &ct->attr, &ct_enter, ct)))
		pthread_cond_wait(&ct->cond, &ct->mutex);

	pthread_mutex_unlock(&ct->mutex);

	pthread_sigmask(SIG_SETMASK, &omask, NULL);

	if (error)
		goto error;

	return 2;
syerr:
	error = errno;
error:
	lua_settop(L, 0);

	lua_pushnil(L);
	lua_pushnil(L);
	lua_pushinteger(L, error);

	return 3;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20b8, %rsp           # imm = 0x20B8
movq %rdi, %rbx
callq 0xf920
movl %eax, %ebp
movl $0x8, %esi
movq %rbx, %rdi
callq 0xfb00
movq %rax, %r15
movq $0x0, (%rax)
leaq 0x18a7b(%rip), %rdx     # 0x35fe4
movq %rbx, %rdi
movl $0xfff0b9d8, %esi       # imm = 0xFFF0B9D8
callq 0xf1e0
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x10910
movl $0x1, %edi
movl $0x1d0, %esi            # imm = 0x1D0
callq 0xf8a0
testq %rax, %rax
je 0x1d5f0
movq %rax, %r14
movl %ebp, 0x8(%rsp)
movl $0x1, (%rax)
movq $-0x1, %rax
movq %rax, 0x1a8(%r14)
movq %rax, 0x1c4(%r14)
leaq 0x20(%r14), %r13
xorl %ebp, %ebp
movq %r13, %rdi
xorl %esi, %esi
callq 0x103f0
movl %eax, %r12d
testl %eax, %eax
jne 0x1d67e
leaq 0x48(%r14), %rdi
xorl %esi, %esi
callq 0xff80
testl %eax, %eax
je 0x1d5fd
movl %eax, %r12d
movl $0x1, %ebp
jmp 0x1d67e
callq 0xf0a0
movl (%rax), %r12d
jmp 0x1d6bf
leaq 0x78(%r14), %rdi
callq 0xff90
testl %eax, %eax
je 0x1d614
movl %eax, %r12d
movl $0x2, %ebp
jmp 0x1d67e
movl $0x0, 0x178(%r14)
leaq 0x98(%rsp), %rdi
callq 0xf0c0
movl %eax, %r12d
testl %eax, %eax
jne 0x1d670
leaq 0x98(%rsp), %rdi
movl $0x1, %esi
callq 0xfae0
movl %eax, %r12d
testl %eax, %eax
jne 0x1d663
leaq 0x180(%r14), %rdi
leaq 0x98(%rsp), %rsi
callq 0x103f0
movl %eax, %r12d
leaq 0x98(%rsp), %rdi
callq 0x103e0
movl $0x3, %ebp
testl %r12d, %r12d
je 0x1dce1
movl %ebp, %eax
leaq 0x18915(%rip), %rcx     # 0x35f9c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x78(%r14), %rdi
callq 0x106c0
leaq 0x48(%r14), %rdi
callq 0x109b0
movq %r13, %rdi
callq 0xfef0
movl 0x8(%rsp), %ebp
testl %r12d, %r12d
je 0x1db9f
movq %r14, %rdi
callq 0xff30
xorl %r14d, %r14d
movq %r14, %rdi
callq 0x1ce34
xorl %r14d, %r14d
movq %r14, (%r15)
testq %r14, %r14
je 0x1dc1d
movq %rbx, %rdi
movl $0x1, %esi
movl $0x6, %edx
callq 0xf6c0
movslq %ebp, %rsi
movl $0x18, %edi
callq 0xf8a0
movq %rax, 0x1b8(%r14)
testq %rax, %rax
je 0x1dc15
movl $0x4, %eax
testl %ebp, %ebp
jle 0x1dbc2
leaq 0x1b0(%r14), %rax
movq %rax, 0x8(%rsp)
movl $0x1, %r15d
movl 0x1c0(%r14), %eax
leaq (%rax,%rax,2), %r13
shlq $0x3, %r13
addq 0x1b8(%r14), %r13
movq %rbx, %rdi
movl %r15d, %esi
callq 0xf260
cmpl $0x6, %eax
ja 0x1d77b
movl %eax, %ecx
leaq 0x18856(%rip), %rdx     # 0x35fac
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movq %rbx, %rdi
movl %r15d, %esi
callq 0xf540
testl %eax, %eax
setne 0x8(%r13)
movl $0x1, %eax
jmp 0x1d896
leaq 0x10(%r13), %rdx
movq %rbx, %rdi
movl %r15d, %esi
callq 0xf620
movq %rax, 0x8(%r13)
movl $0x4, %eax
jmp 0x1d896
movq %rbx, %rdi
movl %r15d, %esi
callq 0x10350
movq %rax, 0x8(%r13)
movl $0x2, %eax
jmp 0x1d896
movq %rbx, %rdi
movl %r15d, %esi
callq 0xf9f0
movq %rbx, %rdi
movl %r15d, %esi
xorl %edx, %edx
testl %eax, %eax
je 0x1d886
callq 0xfd20
movq %rax, 0x8(%r13)
orb $0x2, 0x4(%r13)
jmp 0x1d891
movq %rbx, %rdi
movl %r15d, %esi
callq 0xf9c0
movq %rbx, %rdi
leaq 0x18847(%rip), %rsi     # 0x3603b
leaq 0x18(%rsp), %rdx
callq 0x10040
movq %rbx, %rdi
movl %r15d, %esi
callq 0x109a0
testq %rax, %rax
je 0x1d8c2
cmpb $0x0, 0x4c(%rsp)
jne 0x1d940
movq %rax, 0x10(%rsp)
movq %rax, %rdi
leaq 0x98(%rsp), %rsi
callq 0xf5c0
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testl %eax, %eax
je 0x1d961
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0xa0(%rsp), %rcx
testq %rax, %rax
je 0x1d957
movq 0x8(%rax), %rdi
movl $0x28, %esi
cmpq %rdi, %rcx
jl 0x1d86e
movl $0x30, %esi
jle 0x1d877
movq (%rax,%rsi), %rax
movb $0x1, %sil
jmp 0x1d87c
xorl %esi, %esi
movq %rax, %rdx
testb %sil, %sil
jne 0x1d850
jmp 0x1d959
callq 0x10750
movsd %xmm0, 0x8(%r13)
movl $0x3, %eax
movl %eax, (%r13)
incl 0x1c0(%r14)
xorl %eax, %eax
movb $0x1, %cl
testb %cl, %cl
je 0x1dbc2
leal 0x1(%r15), %eax
cmpl %ebp, %r15d
movl %eax, %r15d
jne 0x1d727
jmp 0x1dbbd
cmpb $0x1, 0x4c(%rsp)
ja 0x1d940
movq %rbx, %rdi
movl $0x2, %esi
leaq 0x17887(%rip), %rdx     # 0x3515f
callq 0xfca0
movq %rbx, %rdi
callq 0x10a40
movq %rax, %r12
movq %rax, %rdi
leaq 0x98(%rsp), %rsi
callq 0x10630
movq %rbx, %rdi
movl %r15d, %esi
callq 0xf9c0
movq %rbx, %rdi
leaq 0x941(%rip), %rsi       # 0x1e24e
leaq 0x98(%rsp), %rdx
xorl %ecx, %ecx
callq 0xf570
leaq 0x98(%rsp), %rdi
callq 0x10710
leaq 0x10(%r13), %rdx
movq %r12, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xf620
movq %rax, 0x8(%r13)
jmp 0x1d98a
movq %rbx, %rdi
movl %r15d, %esi
leaq 0x186f1(%rip), %rdx     # 0x3603e
callq 0xf3f0
movl %eax, %r12d
jmp 0x1d995
xorl %edx, %edx
testq %rdx, %rdx
je 0x1d9aa
xorl %r12d, %r12d
testl %r12d, %r12d
je 0x1d97c
cmpl $-0x1, %r12d
jne 0x1d995
callq 0xfc50
movq %rbx, %rdi
movl %r15d, %esi
movq %rax, %rdx
jmp 0x1d94d
movq 0x10(%rsp), %rax
movq %rax, 0x8(%r13)
orb $0x1, 0x4(%r13)
movl $0x6, (%r13)
xorl %r12d, %r12d
testl %r12d, %r12d
je 0x1d89a
movl $0x2, %eax
xorl %ecx, %ecx
jmp 0x1d8a5
movq 0x98(%rsp), %rdi
movl $0x2, %esi
callq 0x10220
testq %rax, %rax
je 0x1d961
movq %rax, %r12
movl $0x1, %edi
movl $0x48, %esi
callq 0xf8a0
testq %rax, %rax
je 0x1da34
movups 0x98(%rsp), %xmm0
movups 0xa8(%rsp), %xmm1
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
movq %r12, 0x20(%rax)
movq 0x8(%rsp), %r10
movq (%r10), %rdi
testq %rdi, %rdi
je 0x1da49
movq 0x8(%rax), %rsi
movq %r10, %rdx
movq %rdi, %rcx
movq 0x8(%rdi), %rdi
cmpq %rdi, %rsi
je 0x1d95e
leaq 0x28(%rcx), %r8
leaq 0x30(%rcx), %r9
cmpq %rdi, %rsi
cmovgq %r9, %rdx
cmovlq %r8, %rdx
movq (%rdx), %rdi
testq %rdi, %rdi
jne 0x1da07
jmp 0x1da4e
movq %r12, %rdi
callq 0xfa20
callq 0xf0a0
movl (%rax), %r12d
jmp 0x1d961
movq %r10, %rdx
xorl %ecx, %ecx
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%rax)
movb $0x1, 0x40(%rax)
movq %rcx, 0x38(%rax)
movq %rax, (%rdx)
testq %rcx, %rcx
je 0x1db93
movq 0x28(%rcx), %rax
testq %rax, %rax
je 0x1da78
cmpb $0x0, 0x40(%rax)
jne 0x1da8f
movq 0x30(%rcx), %rax
testq %rax, %rax
je 0x1db93
cmpb $0x1, 0x40(%rax)
jne 0x1db93
movq %r10, %rax
cmpq (%r10), %rcx
je 0x1daab
movq 0x38(%rcx), %rax
cmpq 0x28(%rax), %rcx
je 0x1daa7
addq $0x30, %rax
jmp 0x1daab
addq $0x28, %rax
movq 0x38(%rcx), %rcx
movq (%rax), %rdx
movq 0x30(%rdx), %rsi
testq %rsi, %rsi
je 0x1db00
cmpb $0x1, 0x40(%rsi)
jne 0x1db00
movq 0x28(%rdx), %rdi
testq %rdi, %rdi
je 0x1dad0
cmpb $0x0, 0x40(%rdi)
jne 0x1db00
movq 0x28(%rsi), %rdi
movq %rdi, 0x30(%rdx)
testq %rdi, %rdi
je 0x1dae1
movq %rdx, 0x38(%rdi)
movq %rdx, 0x28(%rsi)
movb 0x40(%rdx), %dil
movb %dil, 0x40(%rsi)
movb $0x1, 0x40(%rdx)
movq 0x38(%rdx), %rdi
movq %rdi, 0x38(%rsi)
movq %rsi, 0x38(%rdx)
movq %rsi, (%rax)
movq (%rax), %rdx
movq 0x28(%rdx), %rsi
testq %rsi, %rsi
je 0x1db51
cmpb $0x1, 0x40(%rsi)
jne 0x1db51
movq 0x28(%rsi), %rdi
testq %rdi, %rdi
je 0x1db51
cmpb $0x1, 0x40(%rdi)
jne 0x1db51
movq 0x30(%rsi), %rdi
movq %rdi, 0x28(%rdx)
testq %rdi, %rdi
je 0x1db32
movq %rdx, 0x38(%rdi)
movq %rdx, 0x30(%rsi)
movb 0x40(%rdx), %dil
movb %dil, 0x40(%rsi)
movb $0x1, 0x40(%rdx)
movq 0x38(%rdx), %rdi
movq %rdi, 0x38(%rsi)
movq %rsi, 0x38(%rdx)
movq %rsi, (%rax)
movq (%rax), %rax
movq 0x28(%rax), %rdx
testq %rdx, %rdx
je 0x1da60
cmpb $0x1, 0x40(%rdx)
jne 0x1da60
movq 0x30(%rax), %rsi
testq %rsi, %rsi
je 0x1da60
cmpb $0x1, 0x40(%rsi)
jne 0x1da60
xorb $0x1, 0x40(%rax)
xorb $0x1, 0x40(%rdx)
xorb $0x1, 0x40(%rsi)
jmp 0x1da60
movq (%r10), %rax
movb $0x0, 0x40(%rax)
jmp 0x1d95e
leaq 0x78(%r14), %rdi
movl $0x1, %esi
callq 0x100d0
testl %eax, %eax
je 0x1dc5d
movl %eax, %r12d
jmp 0x1d6c2
movl $0x4, %eax
cmpl $0x2, %eax
je 0x1dc1d
cmpl $0x4, %eax
jne 0x1dc48
leaq 0x1758c(%rip), %rdx     # 0x3515f
movq %rbx, %rdi
movl $0x2, %esi
callq 0xfca0
incl %ebp
movq %rbx, %rdi
movl %ebp, %esi
callq 0xf9c0
leaq 0x1c4(%r14), %r15
movl $0x1, %edi
movl $0x80801, %esi          # imm = 0x80801
xorl %edx, %edx
movq %r15, %rcx
callq 0xfb60
testl %eax, %eax
je 0x1dc7f
callq 0xf0a0
cmpl $0x0, (%rax)
je 0x1dc7f
callq 0xf0a0
movl (%rax), %r12d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x10640
movq %rbx, %rdi
callq 0xf980
movq %rbx, %rdi
callq 0xf980
movslq %r12d, %rsi
movq %rbx, %rdi
callq 0xf910
movl $0x3, %r15d
movl %r15d, %eax
addq $0x20b8, %rsp           # imm = 0x20B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
addq $0x1a8, %rdi            # imm = 0x1A8
movl $0x80800, %esi          # imm = 0x80800
callq 0xf880
testl %eax, %eax
je 0x1dcd0
callq 0xf0a0
movl (%rax), %r12d
jmp 0x1dcd3
movl (%r15), %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x10170
testl %eax, %eax
jne 0x1dcc8
movl $0xffffffff, (%r15)     # imm = 0xFFFFFFFF
leaq 0x98(%rsp), %r15
movq %r15, %rdi
callq 0xf410
leaq 0x18(%rsp), %r12
movq %r12, %rdi
callq 0x10010
movl $0x2, %edi
movq %r15, %rsi
movq %r12, %rdx
callq 0xf350
testl %eax, %eax
je 0x1dce9
movl %eax, %r12d
jmp 0x1dc1d
xorl %r12d, %r12d
testl %r12d, %r12d
jne 0x1d6c2
jmp 0x1d6cd
xorl %r12d, %r12d
jmp 0x1d6aa
leaq 0x20(%r14), %r15
movq %r15, %rdi
callq 0x103b0
leaq 0x18(%r14), %rdi
leaq 0x78(%r14), %rsi
leaq 0x131(%rip), %rdx       # 0x1de35
movq %r14, %rcx
callq 0x10880
testl %eax, %eax
je 0x1dd31
movl %eax, %r12d
movq %r15, %rdi
callq 0xf9b0
leaq 0x18(%rsp), %rsi
movl $0x2, %edi
xorl %edx, %edx
callq 0xf350
jmp 0x1dc1d
addq $0x48, %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x104a0
movq %r15, %rdi
callq 0xf9b0
movl $0x2, %r15d
leaq 0x18(%rsp), %rsi
movl $0x2, %edi
xorl %edx, %edx
callq 0xf350
jmp 0x1dc48