// File: /workspace/github_c_makefile_star2_O0/coyove[P]chann/./src/general.cpp
// Function: unq_delete_thread(unqlite*, int)
int unq_delete_thread(unqlite *pDb, int tid){
    // cchan will never truely delete a thread, it just hides it
    struct Thread* t = unq_read_thread(pDb, tid);
    struct Thread* tp = unq_read_thread(pDb, t->prevThread);
    struct Thread* tn = unq_read_thread(pDb, t->nextThread);

    if (!(t->state & NORMAL_DISPLAY)) return 0;

    if (t->parentThread == 0 && t->state & MAIN_THREAD){
        tn->prevThread = t->prevThread;
        tp->nextThread = t->nextThread;

        if (t->prevThread == t->nextThread){
            //this happens only if the thread has one child reply
            tn->nextThread = tn->threadID;
            unq_write_thread(pDb, tn->threadID, tn, false);
        }
        else{
            unq_write_thread(pDb, tn->threadID, tn, false);
            unq_write_thread(pDb, tp->threadID, tp, false);
        }

        //t->state = 'd';
        //t->state -= NORMAL_DISPLAY;
        changeState(t, NORMAL_DISPLAY, false);
        unq_write_thread(pDb, t->threadID, t, false);
    }

    if (!(t->state & MAIN_THREAD)){
        changeState(t, NORMAL_DISPLAY, false);
        unq_write_thread(pDb, t->threadID, t, false);
    }

    unqlite_commit(pDb);

    return 1;
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x28(%rsp)
movl %esi, 0x24(%rsp)
movq 0x28(%rsp), %rdi
movl 0x24(%rsp), %esi
callq 0xea50
movq %rax, 0x18(%rsp)
movq 0x28(%rsp), %rdi
movq 0x18(%rsp), %rax
movl 0xc0(%rax), %esi
callq 0xea50
movq %rax, 0x10(%rsp)
movq 0x28(%rsp), %rdi
movq 0x18(%rsp), %rax
movl 0xbc(%rax), %esi
callq 0xea50
movq %rax, 0x8(%rsp)
movq 0x18(%rsp), %rax
movsbl (%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0xeb41
movl $0x0, 0x34(%rsp)
jmp 0xec93
movq 0x18(%rsp), %rax
cmpl $0x0, 0xc8(%rax)
jne 0xec44
movq 0x18(%rsp), %rax
movsbl (%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0xec44
movq 0x18(%rsp), %rax
movl 0xc0(%rax), %ecx
movq 0x8(%rsp), %rax
movl %ecx, 0xc0(%rax)
movq 0x18(%rsp), %rax
movl 0xbc(%rax), %ecx
movq 0x10(%rsp), %rax
movl %ecx, 0xbc(%rax)
movq 0x18(%rsp), %rax
movl 0xc0(%rax), %eax
movq 0x18(%rsp), %rcx
cmpl 0xbc(%rcx), %eax
jne 0xebdf
movq 0x8(%rsp), %rax
movl 0xb8(%rax), %ecx
movq 0x8(%rsp), %rax
movl %ecx, 0xbc(%rax)
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rax
movl 0xb8(%rax), %esi
movq 0x8(%rsp), %rdx
xorl %ecx, %ecx
callq 0xeca0
jmp 0xec17
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rax
movl 0xb8(%rax), %esi
movq 0x8(%rsp), %rdx
xorl %ecx, %ecx
callq 0xeca0
movq 0x28(%rsp), %rdi
movq 0x10(%rsp), %rax
movl 0xb8(%rax), %esi
movq 0x10(%rsp), %rdx
xorl %ecx, %ecx
callq 0xeca0
movq 0x18(%rsp), %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0xe5c0
movq 0x28(%rsp), %rdi
movq 0x18(%rsp), %rax
movl 0xb8(%rax), %esi
movq 0x18(%rsp), %rdx
xorl %ecx, %ecx
callq 0xeca0
movq 0x18(%rsp), %rax
movsbl (%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
jne 0xec81
movq 0x18(%rsp), %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0xe5c0
movq 0x28(%rsp), %rdi
movq 0x18(%rsp), %rax
movl 0xb8(%rax), %esi
movq 0x18(%rsp), %rdx
xorl %ecx, %ecx
callq 0xeca0
movq 0x28(%rsp), %rdi
callq 0x5a060
movl $0x1, 0x34(%rsp)
movl 0x34(%rsp), %eax
addq $0x38, %rsp
retq
nopl (%rax)