// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0xf9b0
movl %eax, -0x124(%rbp)
movq -0x10(%rbp), %rdi
movl $0x8, %esi
callq 0xfbe0
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq $0x0, (%rax)
movq -0x10(%rbp), %rdi
movl $0xfff0b9d8, %esi       # imm = 0xFFF0B9D8
leaq 0x24de9(%rip), %rdx     # 0x49194
callq 0xf200
movq -0x10(%rbp), %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x10b10
leaq -0x128(%rbp), %rdi
callq 0x24870
movq -0x18(%rbp), %rcx
movq %rax, (%rcx)
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
jne 0x243e0
jmp 0x24775
movq -0x10(%rbp), %rdi
movl $0x1, %esi
movl $0x6, %edx
callq 0xf740
movslq -0x124(%rbp), %rsi
movl $0x18, %edi
callq 0xf930
movq -0x20(%rbp), %rcx
movq %rax, 0x1b8(%rcx)
cmpq $0x0, %rax
jne 0x2441a
jmp 0x24768
movl $0x1, -0x12c(%rbp)
movl -0x12c(%rbp), %eax
cmpl -0x124(%rbp), %eax
jg 0x24619
movq -0x20(%rbp), %rcx
movq 0x1b8(%rcx), %rax
movl 0x1c0(%rcx), %ecx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %rax
movq %rax, -0x138(%rbp)
movq -0x10(%rbp), %rdi
movl -0x12c(%rbp), %esi
callq 0xf280
movl %eax, %ecx
movq %rcx, -0x140(%rbp)
subl $0x6, %eax
ja 0x245b7
movq -0x140(%rbp), %rax
leaq 0x24cc3(%rip), %rcx     # 0x49148
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x138(%rbp), %rax
movl $0x0, (%rax)
jmp 0x245f2
movq -0x10(%rbp), %rdi
movl -0x12c(%rbp), %esi
callq 0xfaa0
cmpl $0x0, %eax
je 0x244fa
movq -0x10(%rbp), %rdi
movl -0x12c(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0xfe30
movq %rax, %rcx
movq -0x138(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x138(%rbp), %rax
movb 0x4(%rax), %cl
andb $-0x3, %cl
orb $0x2, %cl
movb %cl, 0x4(%rax)
movq -0x138(%rbp), %rax
movl $0x3, (%rax)
jmp 0x245f2
movq -0x10(%rbp), %rdi
movl -0x12c(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x10910
movq -0x138(%rbp), %rax
movsd %xmm0, 0x8(%rax)
movq -0x138(%rbp), %rax
movl $0x3, (%rax)
jmp 0x245f2
movq -0x10(%rbp), %rdi
movl -0x12c(%rbp), %esi
callq 0xf590
cmpl $0x0, %eax
setne %cl
movq -0x138(%rbp), %rax
andb $0x1, %cl
movb %cl, 0x8(%rax)
movq -0x138(%rbp), %rax
movl $0x1, (%rax)
jmp 0x245f2
movq -0x10(%rbp), %rdi
movl -0x12c(%rbp), %esi
callq 0x104b0
movq %rax, %rcx
movq -0x138(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x138(%rbp), %rax
movl $0x2, (%rax)
jmp 0x245f2
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x138(%rbp), %rdx
movl -0x12c(%rbp), %ecx
callq 0x24940
movl %eax, -0x128(%rbp)
cmpl $0x0, %eax
je 0x245b5
jmp 0x24775
jmp 0x245f2
jmp 0x245b9
movq -0x10(%rbp), %rdi
movl -0x12c(%rbp), %esi
movq -0x138(%rbp), %rdx
addq $0x8, %rdx
addq $0x8, %rdx
callq 0xf6a0
movq %rax, %rcx
movq -0x138(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x138(%rbp), %rax
movl $0x4, (%rax)
movq -0x20(%rbp), %rax
movl 0x1c0(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x1c0(%rax)
movl -0x12c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x12c(%rbp)
jmp 0x24424
movq -0x10(%rbp), %rdi
movl $0x2, %esi
leaq 0x23baa(%rip), %rdx     # 0x481d3
callq 0xfdb0
movq -0x10(%rbp), %rdi
movl -0x124(%rbp), %esi
addl $0x1, %esi
callq 0xfa50
movq -0x20(%rbp), %rcx
addq $0x1b8, %rcx            # imm = 0x1B8
addq $0xc, %rcx
movl $0x1, %esi
xorl %edx, %edx
movl $0x80800, %r8d          # imm = 0x80800
movl %esi, %edi
callq 0x24b10
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x24670
jmp 0x24768
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movl 0x1c4(%rax), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x102c0
movl %eax, -0x128(%rbp)
cmpl $0x0, %eax
je 0x24697
jmp 0x24775
movq -0x20(%rbp), %rax
movl $0xffffffff, 0x1c4(%rax) # imm = 0xFFFFFFFF
leaq -0xa0(%rbp), %rdi
callq 0xf460
leaq -0x120(%rbp), %rdi
callq 0x10150
movl $0x2, %edi
leaq -0xa0(%rbp), %rsi
leaq -0x120(%rbp), %rdx
callq 0xf390
movl %eax, -0x128(%rbp)
cmpl $0x0, %eax
je 0x246e5
jmp 0x24775
movq -0x20(%rbp), %rdi
addq $0x20, %rdi
callq 0x10510
movq -0x20(%rbp), %rdi
addq $0x18, %rdi
movq -0x20(%rbp), %rsi
addq $0x78, %rsi
movq -0x20(%rbp), %rcx
leaq 0x483(%rip), %rdx       # 0x24b90
callq 0x10a70
movl %eax, -0x128(%rbp)
cmpl $0x0, %eax
jne 0x24732
movq -0x20(%rbp), %rdi
addq $0x48, %rdi
movq -0x20(%rbp), %rsi
addq $0x20, %rsi
callq 0x10620
movq -0x20(%rbp), %rdi
addq $0x20, %rdi
callq 0xfa40
movl $0x2, %edi
leaq -0x120(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0xf390
cmpl $0x0, -0x128(%rbp)
je 0x2475f
jmp 0x24775
movl $0x2, -0x4(%rbp)
jmp 0x247a9
callq 0xf0a0
movl (%rax), %eax
movl %eax, -0x128(%rbp)
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x107f0
movq -0x10(%rbp), %rdi
callq 0xfa10
movq -0x10(%rbp), %rdi
callq 0xfa10
movq -0x10(%rbp), %rdi
movslq -0x128(%rbp), %rsi
callq 0xf9a0
movl $0x3, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
nopw %cs:(%rax,%rax)