// File: UTSASRG[P]Guarder/./xthread.hh
// Function: xthread::thread_create(unsigned long*, pthread_attr_t const*, void* (*)(void*), void*)
int thread_create(pthread_t * tid, const pthread_attr_t * attr, threadFunction * fn, void * arg) {
		acquireGlobalLock();
		int tindex = allocThreadIndex();
		releaseGlobalLock();

		// Acquire the thread structure.
		thread_t* children = getThread(tindex);	
		children->startArg = arg;
		children->startRoutine = fn;
		children->index = tindex;

		#ifdef CUSTOMIZED_STACK
		pthread_attr_t iattr;
		if(attr == NULL) {
			pthread_attr_init(&iattr);
		} else {
			memcpy(&iattr, attr, sizeof(pthread_attr_t));
		}
		pthread_attr_setstack(&iattr, (void*)(globalStackAddr + (intptr_t)tindex * STACK_SIZE + GUARD_PAGE_SIZE), STACK_SIZE - 2 * GUARD_PAGE_SIZE);
		/* Guard pages have already been setted before main()
		if(0 != mprotect((void*)(childrenStackStart + STACK_SIZE - GUARD_PAGE_SIZE), GUARD_PAGE_SIZE, PROT_NONE)
				|| 0 != mprotect((void*)childrenStackStart, GUARD_PAGE_SIZE, PROT_NONE)) {
			fprintf(stderr, "Failed to set guard page for thread#%d\n", tindex);
			abort();
		}
		*/

		int result = Real::pthread_create(tid, &iattr, xthread::startThread, (void *)children);
		#else
		int result = Real::pthread_create(tid, attr, xthread::startThread, (void *)children);
		#endif

		if(result) {
			FATAL("thread_create failure");
		}
		
		// Setting up this in the main thread so that
		// pthread_join can always find its pthread_t. 
		// Otherwise, it can fail because the child have set this value.
		children->pthreadt = *tid;
		acquireGlobalLock();
		_xmap.insertIfAbsent((void*)*tid, sizeof(void*), tindex);
		releaseGlobalLock();
		return result;
	}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x50(%rbp)
callq 0x51c0
movq -0x50(%rbp), %rdi
callq 0x52a0
movq -0x50(%rbp), %rdi
movl %eax, -0x2c(%rbp)
callq 0x5380
movq -0x50(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x5780
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x20(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x18(%rax)
movl -0x2c(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x10(%rax)
movq 0x8ea9(%rip), %rax      # 0x10f00
movq (%rax), %rax
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x38(%rbp), %rcx
movq 0x8f43(%rip), %rdx      # 0x10fb0
callq *%rax
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
je 0x80de
callq 0x52d0
movq %rax, -0x60(%rbp)
callq 0x5400
movq -0x60(%rbp), %rdi
movq %rax, %rcx
movl $0x1000, %esi           # imm = 0x1000
leaq 0x5860(%rip), %rdx      # 0xd8f9
leaq 0x4b53(%rip), %r8       # 0xcbf3
movl $0xcb, %r9d
movb $0x0, %al
callq 0x57e0
callq 0x52d0
movq %rax, -0x58(%rbp)
callq 0x52d0
movq %rax, %rdi
callq 0x51e0
movq -0x58(%rbp), %rsi
movq %rax, %rdx
movl $0x2, %edi
callq 0x5160
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x55b0
movq -0x50(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x8(%rax)
callq 0x51c0
movq -0x50(%rbp), %rdi
addq $0x8, %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x48(%rbp)
movl -0x2c(%rbp), %ecx
leaq -0x48(%rbp), %rsi
movl $0x8, %edx
callq 0x5150
movq -0x50(%rbp), %rdi
callq 0x5380
movl -0x3c(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)