// File: /workspace/github_c_makefile_star2_O3/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 $0x20c8, %rsp           # imm = 0x20C8
movq %rdi, %rbx
callq 0xf920
movl %eax, %ebp
movl $0x8, %esi
movq %rbx, %rdi
callq 0xfb00
movq %rax, %r13
movq $0x0, (%rax)
leaq 0x18b5c(%rip), %rdx     # 0x35fac
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 0x1d513
movq %rax, %r14
movl $0x1, (%rax)
movq $-0x1, %rax
movq %rax, 0x1a8(%r14)
movq %rax, 0x1c4(%r14)
leaq 0x20(%r14), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x103f0
movl %eax, %r12d
testl %eax, %eax
jne 0x1d588
movl %ebp, 0x4(%rsp)
leaq 0x48(%r14), %rbp
movq %rbp, %rdi
xorl %esi, %esi
callq 0xff80
movl %eax, %r12d
testl %eax, %eax
jne 0x1d580
movq %rbp, 0x20(%rsp)
leaq 0x78(%r14), %rbp
movq %rbp, %rdi
callq 0xff90
movl %eax, %r12d
testl %eax, %eax
jne 0x1d576
movl $0x0, 0x178(%r14)
leaq 0xa8(%rsp), %rdi
callq 0xf0c0
testl %eax, %eax
je 0x1d51d
movl %eax, %r12d
jmp 0x1d56e
callq 0xf0a0
movl (%rax), %r12d
jmp 0x1d590
leaq 0xa8(%rsp), %rdi
movl $0x1, %esi
callq 0xfae0
testl %eax, %eax
je 0x1d545
movl %eax, %r12d
leaq 0xa8(%rsp), %rdi
callq 0x103e0
jmp 0x1d56e
leaq 0x180(%r14), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x103f0
movl %eax, %r12d
leaq 0xa8(%rsp), %rdi
callq 0x103e0
testl %r12d, %r12d
je 0x1d5e1
movq %rbp, %rdi
callq 0x106c0
movq 0x20(%rsp), %rdi
callq 0x109b0
movq %r15, %rdi
callq 0xfef0
movq %r14, %rdi
callq 0xff30
xorl %r14d, %r14d
movq %r14, %rdi
callq 0x1ccc9
movq $0x0, (%r13)
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, %ebx
movl %ebx, %eax
addq $0x20c8, %rsp           # imm = 0x20C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbp, %rdi
movl $0x1, %esi
callq 0x100d0
testl %eax, %eax
je 0x1d5f7
movl %eax, %r12d
jmp 0x1d593
leaq 0x1a8(%r14), %rdi
movl $0x80800, %esi          # imm = 0x80800
callq 0xf880
testl %eax, %eax
je 0x1d61d
callq 0xf0a0
movl (%rax), %r12d
testl %r12d, %r12d
jne 0x1d593
movq %r14, (%r13)
movq %rbx, %rdi
movl $0x1, %esi
movl $0x6, %edx
callq 0xf6c0
movl 0x4(%rsp), %r12d
movslq %r12d, %rsi
movl $0x18, %edi
callq 0xf8a0
movq %rax, 0x1b8(%r14)
testq %rax, %rax
je 0x1db9d
testl %r12d, %r12d
jle 0x1dafa
leaq 0x1b0(%r14), %rax
movq %rax, 0x8(%rsp)
movl 0x1c0(%r14), %eax
movl $0x1, %r12d
movl %eax, %eax
leaq (%rax,%rax,2), %r13
shlq $0x3, %r13
addq 0x1b8(%r14), %r13
movq %rbx, %rdi
movl %r12d, %esi
callq 0xf260
cmpl $0x6, %eax
ja 0x1d6b7
movl %eax, %eax
leaq 0x188d3(%rip), %rcx     # 0x35f74
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x0, (%r13)
jmp 0x1d9c6
leaq 0x10(%r13), %rdx
movq %rbx, %rdi
movl %r12d, %esi
callq 0xf620
movq %rax, 0x8(%r13)
movl $0x4, (%r13)
jmp 0x1d9c6
movq %rbx, %rdi
movl %r12d, %esi
callq 0x10350
movq %rax, 0x8(%r13)
movl $0x2, (%r13)
jmp 0x1d9c6
movq %rbx, %rdi
movl %r12d, %esi
callq 0xf540
testl %eax, %eax
setne 0x8(%r13)
movl $0x1, (%r13)
jmp 0x1d9c6
movq %rbx, %rdi
movl %r12d, %esi
callq 0xf9f0
movq %rbx, %rdi
movl %r12d, %esi
xorl %edx, %edx
testl %eax, %eax
je 0x1d9b3
callq 0xfd20
movq %rax, 0x8(%r13)
orb $0x2, 0x4(%r13)
jmp 0x1d9be
movq %rbx, %rdi
movl %r12d, %esi
callq 0xf9c0
movq %rbx, %rdi
leaq 0x188ae(%rip), %rsi     # 0x36003
leaq 0x28(%rsp), %rdx
callq 0x10040
movq %rbx, %rdi
movl %r12d, %esi
callq 0x109a0
testq %rax, %rax
movq %r12, 0x18(%rsp)
je 0x1d9ee
cmpb $0x0, 0x5c(%rsp)
jne 0x1d9f5
movq %rax, 0x10(%rsp)
movq %rax, %rdi
leaq 0xa8(%rsp), %rsi
callq 0xf5c0
testl %eax, %eax
je 0x1daa0
movq 0x8(%rsp), %rax
movq (%rax), %rax
testq %rax, %rax
je 0x1d7d4
movq 0xb0(%rsp), %rcx
movl $0x28, %edx
cmpq %rcx, 0x8(%rax)
jg 0x1d7cb
movl $0x30, %edx
jge 0x1dada
movq (%rax,%rdx), %rax
testq %rax, %rax
jne 0x1d7b5
movq 0xa8(%rsp), %rdi
movl $0x2, %esi
callq 0x10220
testq %rax, %rax
je 0x1daa0
movq %rax, %r12
movl $0x1, %edi
movl $0x48, %esi
callq 0xf8a0
testq %rax, %rax
je 0x1da80
movups 0xa8(%rsp), %xmm0
movups 0xb8(%rsp), %xmm1
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
movq %r12, 0x20(%rax)
movq 0x8(%rsp), %rcx
movq (%rcx), %rdi
testq %rdi, %rdi
je 0x1daca
movq 0x8(%rax), %rdx
movq %rdi, %rcx
movl $0x28, %esi
cmpq 0x8(%rdi), %rdx
jl 0x1d853
movl $0x30, %esi
jle 0x1dada
movq (%rcx,%rsi), %rdi
testq %rdi, %rdi
jne 0x1d83a
movb $0x1, 0x40(%rax)
movq %rcx, 0x38(%rax)
movq %rax, (%rcx,%rsi)
movq %rcx, %rax
movq 0x28(%rcx), %rcx
testq %rcx, %rcx
je 0x1d87a
cmpb $0x0, 0x40(%rcx)
jne 0x1d891
movq 0x30(%rax), %rcx
testq %rcx, %rcx
je 0x1d9a6
cmpb $0x1, 0x40(%rcx)
jne 0x1d9a6
movq 0x8(%rsp), %rdx
movq (%rdx), %rsi
movq 0x38(%rax), %rcx
cmpq %rsi, %rax
je 0x1d8bd
leaq 0x28(%rcx), %rsi
leaq 0x30(%rcx), %rdx
xorl %edi, %edi
cmpq 0x28(%rcx), %rax
setne %dil
cmoveq %rsi, %rdx
movq 0x28(%rcx,%rdi,8), %rsi
movq 0x30(%rsi), %rax
testq %rax, %rax
je 0x1d8db
cmpb $0x1, 0x40(%rax)
jne 0x1d8db
movq 0x28(%rsi), %rdi
testq %rdi, %rdi
je 0x1d8e0
cmpb $0x0, 0x40(%rdi)
je 0x1d8e0
movq %rsi, %rax
jmp 0x1d910
movq 0x28(%rax), %rdi
movq %rdi, 0x30(%rsi)
testq %rdi, %rdi
je 0x1d8f1
movq %rsi, 0x38(%rdi)
movq %rsi, 0x28(%rax)
movb 0x40(%rsi), %dil
movb %dil, 0x40(%rax)
movb $0x1, 0x40(%rsi)
movq 0x38(%rsi), %rdi
movq %rdi, 0x38(%rax)
movq %rax, 0x38(%rsi)
movq %rax, (%rdx)
movq 0x28(%rax), %rsi
testq %rsi, %rsi
je 0x1d99d
movb 0x40(%rsi), %dil
cmpb $0x1, %dil
jne 0x1d979
movq 0x28(%rsi), %r8
testq %r8, %r8
je 0x1d979
cmpb $0x1, 0x40(%r8)
jne 0x1d979
movq 0x30(%rsi), %rdi
movq %rdi, 0x28(%rax)
testq %rdi, %rdi
je 0x1d948
movq %rax, 0x38(%rdi)
movq %rax, 0x30(%rsi)
movb 0x40(%rax), %dil
movb %dil, 0x40(%rsi)
movb $0x1, 0x40(%rax)
movq 0x38(%rax), %rdi
movq %rdi, 0x38(%rsi)
movq %rsi, 0x38(%rax)
movq %rsi, (%rdx)
movq 0x28(%rsi), %rdx
testq %rdx, %rdx
je 0x1d99d
movb 0x40(%rdx), %dil
movq %rsi, %rax
jmp 0x1d97c
movq %rsi, %rdx
testb $0x1, %dil
je 0x1d99d
movq 0x30(%rax), %rsi
testq %rsi, %rsi
je 0x1d99d
cmpb $0x1, 0x40(%rsi)
jne 0x1d99d
xorb $0x1, 0x40(%rax)
xorb $0x1, 0x40(%rdx)
xorb $0x1, 0x40(%rsi)
testq %rcx, %rcx
jne 0x1d868
movq 0x8(%rsp), %rax
movq (%rax), %rax
jmp 0x1dad6
callq 0x10750
movsd %xmm0, 0x8(%r13)
movl $0x3, (%r13)
movq %r12, %rdx
movl 0x1c0(%r14), %eax
incl %eax
movl %eax, 0x1c0(%r14)
leal 0x1(%rdx), %ecx
cmpl 0x4(%rsp), %edx
movl %ecx, %r12d
jne 0x1d677
jmp 0x1dafa
cmpb $0x1, 0x5c(%rsp)
jbe 0x1da07
movq %rbx, %rdi
movl %r12d, %esi
leaq 0x18604(%rip), %rdx     # 0x36006
jmp 0x1dab0
movq %rbx, %rdi
movl $0x2, %esi
leaq 0x17749(%rip), %rdx     # 0x3515f
callq 0xfca0
movq %rbx, %rdi
callq 0x10a40
movq %rax, 0x10(%rsp)
movq %rax, %rdi
leaq 0xa8(%rsp), %rsi
callq 0x10630
movq %rbx, %rdi
movl %r12d, %esi
callq 0xf9c0
movq %rbx, %rdi
leaq 0x6bd(%rip), %rsi       # 0x1e10a
leaq 0xa8(%rsp), %r12
movq %r12, %rdx
xorl %ecx, %ecx
callq 0xf570
movq %r12, %rdi
callq 0x10710
leaq 0x10(%r13), %rdx
movq 0x10(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xf620
movq %rax, 0x8(%r13)
jmp 0x1dae8
movq %r12, %rdi
callq 0xfa20
callq 0xf0a0
movl (%rax), %r12d
cmpl $-0x1, %r12d
je 0x1daa0
testl %r12d, %r12d
je 0x1dada
jmp 0x1d5a3
callq 0xfc50
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
movq %rax, %rdx
callq 0xf3f0
movl %eax, %r12d
testl %eax, %eax
movq 0x18(%rsp), %rdx
je 0x1d9c9
jmp 0x1d5a3
movb $0x1, 0x40(%rax)
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
movb $0x0, 0x40(%rax)
movq 0x10(%rsp), %rax
movq %rax, 0x8(%r13)
orb $0x1, 0x4(%r13)
movl $0x6, (%r13)
movq 0x18(%rsp), %rdx
jmp 0x1d9c9
leaq 0x1c4(%r14), %r13
movl 0x4(%rsp), %r12d
incl %r12d
leaq 0x1764f(%rip), %rdx     # 0x3515f
movq %rbx, %rdi
movl $0x2, %esi
callq 0xfca0
movq %rbx, %rdi
movl %r12d, %esi
callq 0xf9c0
movl $0x1, %edi
movl $0x80801, %esi          # imm = 0x80801
xorl %edx, %edx
movq %r13, %rcx
callq 0xfb60
testl %eax, %eax
je 0x1db4a
callq 0xf0a0
cmpl $0x0, (%rax)
jne 0x1dba2
movl (%r13), %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x10170
testl %eax, %eax
jne 0x1db95
movl $0xffffffff, (%r13)     # imm = 0xFFFFFFFF
leaq 0xa8(%rsp), %r12
movq %r12, %rdi
callq 0xf410
leaq 0x28(%rsp), %r13
movq %r13, %rdi
callq 0x10010
movl $0x2, %edi
movq %r12, %rsi
movq %r13, %rdx
callq 0xf350
testl %eax, %eax
je 0x1dbaa
movl %eax, %r12d
jmp 0x1d5a3
callq 0xf0a0
movl (%rax), %r12d
jmp 0x1d5a3
movq %r15, %rdi
callq 0x103b0
movq %r14, %rdi
addq $0x18, %rdi
leaq 0x131(%rip), %rdx       # 0x1dcf1
movq %rbp, %rsi
movq %r14, %rcx
callq 0x10880
testl %eax, %eax
je 0x1dbf0
movl %eax, %r12d
movq %r15, %rdi
callq 0xf9b0
leaq 0x28(%rsp), %rsi
movl $0x2, %edi
xorl %edx, %edx
callq 0xf350
jmp 0x1d5a3
movq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x104a0
movq %r15, %rdi
callq 0xf9b0
movl $0x2, %ebx
leaq 0x28(%rsp), %rsi
movl $0x2, %edi
xorl %edx, %edx
callq 0xf350
jmp 0x1d5cd