// File: /workspace/github_c_makefile_star2_O0/deepika9294[P]Threading-Library/many-one/dthread.c
// Function: dthread_create
int dthread_create(dthread_t *thread, void *(*start_routine) (void *), void *args) {

    struct dthread *t;
    t = (dthread *) malloc(sizeof(dthread));

    t->tid = tid_count++;
	t->args = args;
    t->status = READY;
	t->start_routine = start_routine;
    t->signal = -1;
    t->state = JOINABLE;

    // allocate the memory for stack with mmap

    t->stack = mmap(NULL,THREAD_STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,-1, 0);

    if(t->stack == MAP_FAILED) {
        start_timer(&timer);
        printf("Insufficient res");
        return EAGAIN;  //insufficient resources
    }

    //assigning the thread with the tid
    *thread = t->tid;

    //saving the context and changing the stack pointer, base pointer and program counter
    sigsetjmp(t->context, 1);
    t->context[0].__jmpbuf[6] = i64_ptr_mangle((long int) t->stack + THREAD_STACK_SIZE - sizeof(long int));
    t->context[0].__jmpbuf[1] = i64_ptr_mangle((long int) t->stack + THREAD_STACK_SIZE - sizeof(long int));
	t->context[0].__jmpbuf[7] = i64_ptr_mangle((long int) fn);
    // printf("%p", t->context);

    enqueue(threads, t);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl $0x100, %edi            # imm = 0x100
callq 0x10c0
movq %rax, -0x28(%rbp)
movq 0x3a5f(%rip), %rcx      # 0x50c8
movq %rcx, %rax
addq $0x1, %rax
movq %rax, 0x3a51(%rip)      # 0x50c8
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x28(%rbp), %rax
movl $0x1, 0xc(%rax)
movq -0x18(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x28(%rbp), %rax
movl $0xffffffff, 0xf8(%rax) # imm = 0xFFFFFFFF
movq -0x28(%rbp), %rax
movl $0x1, 0x8(%rax)
xorl %eax, %eax
movl %eax, %r9d
movl $0x10000, %esi          # imm = 0x10000
movl $0x3, %edx
movl $0x20022, %ecx          # imm = 0x20022
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movq %r9, %rdi
callq 0x1060
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rax
movq $-0x1, %rcx
cmpq %rcx, 0x10(%rax)
jne 0x171e
leaq 0x39d1(%rip), %rdi      # 0x50d0
callq 0x13d0
leaq 0x18f9(%rip), %rdi      # 0x3004
movb $0x0, %al
callq 0x1070
movl $0xb, -0x4(%rbp)
jmp 0x17b2
movq -0x28(%rbp), %rax
movq (%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rdi
addq $0x30, %rdi
movl $0x1, %esi
callq 0x1100
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rdi
addq $0x10000, %rdi          # imm = 0x10000
subq $0x8, %rdi
callq 0x17c0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rdi
addq $0x10000, %rdi          # imm = 0x10000
subq $0x8, %rdi
callq 0x17c0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x38(%rax)
leaq -0x35b(%rip), %rdi      # 0x1430
callq 0x17c0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x68(%rax)
movq 0x3916(%rip), %rdi      # 0x50b8
movq -0x28(%rbp), %rsi
callq 0x1a70
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax,%rax)