// File: /workspace/github_c_makefile_star2_O2/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 $0x20d8, %rsp           # imm = 0x20D8
movq %rdi, %rbx
callq 0xf970
movl %eax, %ebp
pushq $0x8
popq %rsi
movq %rbx, %rdi
callq 0xfb70
movq %rax, %r13
andq $0x0, (%rax)
leaq 0x1588c(%rip), %rdx     # 0x30fac
movq %rbx, %rdi
movl $0xfff0b9d8, %esi       # imm = 0xFFF0B9D8
callq 0xf1f0
pushq $-0x2
popq %rsi
movq %rbx, %rdi
callq 0x109f0
pushq $0x1
popq %rdi
movl $0x1d0, %esi            # imm = 0x1D0
callq 0xf8f0
testq %rax, %rax
je 0x1b7d3
movq %rax, %r14
movl $0x1, (%rax)
orq $-0x1, 0x1a8(%rax)
orq $-0x1, 0x1c4(%rax)
leaq 0x20(%rax), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x104a0
movl %eax, %r12d
testl %eax, %eax
jne 0x1b837
movl %ebp, 0x10(%rsp)
leaq 0x48(%r14), %rbp
movq %rbp, %rdi
xorl %esi, %esi
callq 0x10010
movl %eax, %r12d
testl %eax, %eax
jne 0x1b82f
movq %rbp, 0x20(%rsp)
leaq 0x78(%r14), %rbp
movq %rbp, %rdi
callq 0x10020
movl %eax, %r12d
testl %eax, %eax
jne 0x1b825
andl $0x0, 0x178(%r14)
leaq 0xb8(%rsp), %rdi
callq 0xf0c0
testl %eax, %eax
je 0x1b7dd
movl %eax, %r12d
jmp 0x1b81d
callq 0xf0a0
movl (%rax), %r12d
jmp 0x1b83f
leaq 0xb8(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0xfb50
movl %eax, %r12d
testl %eax, %eax
jne 0x1b80b
leaq 0x180(%r14), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x104a0
movl %eax, %r12d
leaq 0xb8(%rsp), %rdi
callq 0x10490
testl %r12d, %r12d
je 0x1b88b
movq %rbp, %rdi
callq 0x10770
movq 0x20(%rsp), %rdi
callq 0x10a90
movq %r15, %rdi
callq 0xff80
movq %r14, %rdi
callq 0xffc0
xorl %r14d, %r14d
movq %r14, %rdi
callq 0x1b22b
andq $0x0, (%r13)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x106f0
movq %rbx, %rdi
callq 0xf9d0
movq %rbx, %rdi
callq 0xf9d0
movslq %r12d, %rsi
movq %rbx, %rdi
callq 0xf960
pushq $0x3
popq %rbp
movl %ebp, %eax
addq $0x20d8, %rsp           # imm = 0x20D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rsi
movq %rbp, %rdi
callq 0x10160
testl %eax, %eax
je 0x1b89f
movl %eax, %r12d
jmp 0x1b842
leaq 0x1a8(%r14), %rdi
movl $0x80800, %esi          # imm = 0x80800
callq 0xf8d0
testl %eax, %eax
je 0x1b8c1
callq 0xf0a0
movl (%rax), %r12d
testl %r12d, %r12d
jne 0x1b842
movq %r14, (%r13)
pushq $0x1
popq %rsi
pushq $0x6
popq %rdx
movq %rbx, %rdi
movl %esi, %r13d
callq 0xf700
movl 0x10(%rsp), %r12d
movslq %r12d, %rsi
pushq $0x18
popq %rdi
callq 0xf8f0
movq %rax, 0x1b8(%r14)
testq %rax, %rax
je 0x1bd26
leaq 0x1c4(%r14), %rax
movq %rax, 0x28(%rsp)
leaq 0x1b0(%r14), %rax
movq %rax, 0x8(%rsp)
xorl %eax, %eax
testl %r12d, %r12d
cmovgl %r12d, %eax
incl %eax
movq %rax, 0x30(%rsp)
movl %r13d, %r12d
cmpl 0x30(%rsp), %r12d
je 0x1bc8c
movl 0x1c0(%r14), %eax
imulq $0x18, %rax, %r13
addq 0x1b8(%r14), %r13
movq %rbx, %rdi
movl %r12d, %esi
callq 0xf270
cmpl $0x6, %eax
ja 0x1b96a
movl %eax, %eax
leaq 0x1561d(%rip), %rcx     # 0x30f74
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
andl $0x0, (%r13)
jmp 0x1ba9c
leaq 0x10(%r13), %rdx
movq %rbx, %rdi
movl %r12d, %esi
callq 0xf660
movq %rax, 0x8(%r13)
movl $0x4, (%r13)
jmp 0x1ba9c
movq %rbx, %rdi
movl %r12d, %esi
callq 0x103f0
movq %rax, 0x8(%r13)
movl $0x2, (%r13)
jmp 0x1ba9c
movq %rbx, %rdi
movl %r12d, %esi
callq 0xf560
testl %eax, %eax
setne 0x8(%r13)
movl $0x1, (%r13)
jmp 0x1ba9c
movq %rbx, %rdi
movl %r12d, %esi
callq 0xfa40
movq %rbx, %rdi
movl %r12d, %esi
xorl %edx, %edx
testl %eax, %eax
je 0x1ba89
callq 0xfdb0
movq %rax, 0x8(%r13)
orb $0x2, 0x4(%r13)
jmp 0x1ba94
movq %rbx, %rdi
movl %r12d, %esi
callq 0xfa10
movq %rbx, %rdi
leaq 0x155fb(%rip), %rsi     # 0x31003
leaq 0x38(%rsp), %rdx
callq 0x100d0
movq %rbx, %rdi
movl %r12d, %esi
callq 0x10a80
testq %rax, %rax
movl %r12d, 0x4(%rsp)
je 0x1bab0
cmpb $0x0, 0x6c(%rsp)
jne 0x1bab7
movq %rax, 0x18(%rsp)
movq %rax, %rdi
leaq 0xb8(%rsp), %rsi
callq 0xf600
testl %eax, %eax
je 0x1bc78
movq 0xc0(%rsp), %rax
movq 0x8(%rsp), %rcx
pushq $0x28
popq %rsi
pushq $0x30
popq %rdi
movq (%rcx), %rcx
testq %rcx, %rcx
je 0x1bb51
movq %rsi, %rdx
cmpq 0x8(%rcx), %rax
jl 0x1ba84
movq %rdi, %rdx
jle 0x1bc59
addq %rdx, %rcx
jmp 0x1ba66
callq 0x10810
movsd %xmm0, 0x8(%r13)
movl $0x3, (%r13)
movl %r12d, %eax
incl 0x1c0(%r14)
incl %eax
movl %eax, %r12d
jmp 0x1b921
cmpb $0x1, 0x6c(%rsp)
jbe 0x1bad9
movq %rbx, %rdi
movl %r12d, %esi
leaq 0x15542(%rip), %rdx     # 0x31006
callq 0xf410
movl %eax, %r12d
testl %eax, %eax
movl 0x4(%rsp), %eax
je 0x1ba9f
jmp 0x1b84f
movq %rbx, %rdi
pushq $0x2
popq %rsi
leaq 0x14679(%rip), %rdx     # 0x3015f
callq 0xfd30
movq %rbx, %rdi
callq 0x10b20
movq %rax, 0x18(%rsp)
movq %rax, %rdi
leaq 0xb8(%rsp), %rsi
callq 0x106e0
movq %rbx, %rdi
movl %r12d, %esi
callq 0xfa10
movq %rbx, %rdi
leaq 0x728(%rip), %rsi       # 0x1c245
leaq 0xb8(%rsp), %r12
movq %r12, %rdx
xorl %ecx, %ecx
callq 0xf590
movq %r12, %rdi
callq 0x107d0
leaq 0x10(%r13), %rdx
movq 0x18(%rsp), %rdi
pushq $-0x1
popq %rsi
callq 0xf660
movq %rax, 0x8(%r13)
jmp 0x1bc67
movq 0xb8(%rsp), %rdi
pushq $0x2
popq %rsi
callq 0x102b0
testq %rax, %rax
je 0x1bc78
movq %rax, %r12
pushq $0x1
popq %rdi
pushq $0x48
popq %rsi
callq 0xf8f0
testq %rax, %rax
je 0x1bc3a
movups 0xb8(%rsp), %xmm0
movups 0xc8(%rsp), %xmm1
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
movq %r12, 0x20(%rax)
movq 0x8(%rsp), %rdx
xorl %ecx, %ecx
pushq $0x28
popq %rdi
pushq $0x30
popq %r8
movq (%rdx), %rsi
testq %rsi, %rsi
je 0x1bbd0
movq 0x8(%rsi), %rcx
movq %rdi, %rdx
cmpq %rcx, 0x8(%rax)
jl 0x1bbc8
movq %r8, %rdx
jle 0x1bc59
addq %rsi, %rdx
movq %rsi, %rcx
jmp 0x1bbaa
movb $0x1, 0x40(%rax)
movq %rcx, 0x38(%rax)
movq %rax, (%rdx)
testq %rcx, %rcx
je 0x1bc2c
movq 0x28(%rcx), %rax
testq %rax, %rax
je 0x1bbef
cmpb $0x0, 0x40(%rax)
jne 0x1bbfe
movq 0x30(%rcx), %rax
testq %rax, %rax
je 0x1bc2c
cmpb $0x1, 0x40(%rax)
jne 0x1bc2c
movq 0x38(%rcx), %r12
movq 0x8(%rsp), %rax
movq %rax, %rdi
cmpq (%rax), %rcx
je 0x1bc22
cmpq 0x28(%r12), %rcx
je 0x1bc1d
leaq 0x30(%r12), %rdi
jmp 0x1bc22
leaq 0x28(%r12), %rdi
callq 0x1b625
movq %r12, %rcx
jmp 0x1bbdb
movq 0x8(%rsp), %rax
movq (%rax), %rax
movb $0x0, 0x40(%rax)
jmp 0x1bc59
movq %r12, %rdi
callq 0xfa90
callq 0xf0a0
movl (%rax), %r12d
cmpl $-0x1, %r12d
je 0x1bc78
testl %r12d, %r12d
jne 0x1b84f
movq 0x18(%rsp), %rax
movq %rax, 0x8(%r13)
orb $0x1, 0x4(%r13)
movl $0x6, (%r13)
movl 0x4(%rsp), %eax
jmp 0x1ba9f
callq 0xfcd0
movq %rbx, %rdi
movl 0x4(%rsp), %esi
movq %rax, %rdx
jmp 0x1bac4
leaq 0x144cc(%rip), %rdx     # 0x3015f
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0xfd30
movl 0x10(%rsp), %esi
incl %esi
movq %rbx, %rdi
callq 0xfa10
pushq $0x1
popq %rdi
movl $0x80801, %esi          # imm = 0x80801
xorl %edx, %edx
movq 0x28(%rsp), %r12
movq %r12, %rcx
callq 0xfbd0
testl %eax, %eax
je 0x1bcd1
callq 0xf0a0
cmpl $0x0, (%rax)
jne 0x1bd2b
movl (%r12), %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x10200
testl %eax, %eax
jne 0x1bd1e
movq %rbp, 0x10(%rsp)
orl $-0x1, (%r12)
leaq 0xb8(%rsp), %r12
movq %r12, %rdi
callq 0xf430
leaq 0x38(%rsp), %r13
movq %r13, %rdi
callq 0x100a0
pushq $0x2
popq %rbp
movl %ebp, %edi
movq %r12, %rsi
movq %r13, %rdx
callq 0xf360
testl %eax, %eax
je 0x1bd33
movl %eax, %r12d
jmp 0x1b84f
callq 0xf0a0
movl (%rax), %r12d
jmp 0x1b84f
movq %r15, %rdi
callq 0x10450
movq %r14, %rdi
addq $0x18, %rdi
leaq 0x11e(%rip), %rdx       # 0x1be67
movq 0x10(%rsp), %rsi
movq %r14, %rcx
callq 0x10960
testl %eax, %eax
je 0x1bd79
movl %eax, %r12d
movq %r15, %rdi
callq 0xfa00
pushq $0x2
popq %rdi
leaq 0x38(%rsp), %rsi
xorl %edx, %edx
callq 0xf360
jmp 0x1b84f
movq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x10550
movq %r15, %rdi
callq 0xfa00
leaq 0x38(%rsp), %rsi
movl %ebp, %edi
xorl %edx, %edx
callq 0xf360
jmp 0x1b877