// File: /workspace/github_c_makefile_star2_O0/coyove[P]chann/./src/general.cpp
// Function: unq_new_reply(unqlite*, int, char const*, char const*, char const*, char const*, char const*, bool)
int unq_new_reply(unqlite *pDb, 
    int id, 
    const char* content, 
    const char* title, 
    const char* ip, 
    const char* username, 
    const char* image, bool sega)
{
    struct Thread *r = unq_read_thread(pDb, id);
    struct Thread *root = unq_read_thread(pDb, 0);
    struct Thread *self = r;
    struct Thread *t = new Thread();
    bool reply2reply = false;

    if (r->state & THREAD_REPLY) reply2reply = true;

    char contentkey[16];
    unqlite_util_random_string(pDb, contentkey, 15);
    contentkey[15] = 0;
    unq_write_string(pDb, contentkey, content, false);

    strncpy(t->content, contentkey, 16);
    strncpy(t->author, title, 64);
    strncpy(t->email, ip, 64);
    strncpy(t->ssid, username, 10);
    strncpy(t->imgSrc, image, 16);

    unqlite_commit(pDb);
    t->state = THREAD_REPLY + NORMAL_DISPLAY;
    if (sega) t->state += SAGE_THREAD;

    time_t rawtime;
    time(&rawtime);
    t->date = rawtime;

    if (r->childThread == 0){ //no reply yet
        int newkey = ++root->childCount;// nextCounter(pDb);
        t->nextThread = newkey;
        t->prevThread = newkey;
        t->childThread = 0;
        t->parentThread = r->threadID;
        t->threadID = newkey;
        //it's strange and wired but we update the count of children here
        t->childCount = 1;
        unq_write_thread(pDb, newkey, t, false);
        r->childThread = newkey;
        unq_write_thread(pDb, r->threadID, r, false);
    }
    else{ // construct a circled linked list of threads
        r = unq_read_thread(pDb, r->childThread);
        r->childCount++;
        
        struct Thread *rp;
        if (r->prevThread == r->threadID && r->nextThread == r->threadID) // only one child
            rp = r;
        else
            rp = unq_read_thread(pDb, r->prevThread);

        int newkey = ++root->childCount; //nextCounter(pDb);

        t->prevThread = r->prevThread;
        t->nextThread = r->threadID;
        t->childThread = t->parentThread = 0;
        t->threadID = newkey;

        r->prevThread = newkey;
        rp->nextThread = newkey;

        unq_write_thread(pDb, newkey, t, false);
        unq_write_thread(pDb, r->threadID, r, false);
        unq_write_thread(pDb, rp->threadID, rp, false);

    }

    
    if (self->prevThread == 0 || self->state & SAGE_THREAD || t->state & SAGE_THREAD || reply2reply){
        //under certain circumstance the thread won't be pushed to the top
        //1. already at top
        //2. is a sega thread
        //3. has a sega reply
        //4. what it replies to is a comment itself
    }
    else{//push the replied thread to the top
        if (self->prevThread) { 
            struct Thread* sp = unq_read_thread(pDb, self->prevThread);
            sp->nextThread = self->nextThread;
            unq_write_thread(pDb, sp->threadID, sp, false);

            if (self->nextThread){
                struct Thread* sn = unq_read_thread(pDb, self->nextThread);
                sn->prevThread = sp->threadID;
                unq_write_thread(pDb, sn->threadID, sn, false);
            }
        }

        self->nextThread = root->nextThread;
        self->prevThread = 0;
        root->nextThread = self->threadID;

        if (self->nextThread){
            struct Thread* sn = unq_read_thread(pDb, self->nextThread);
            sn->prevThread = self->threadID;
            unq_write_thread(pDb, sn->threadID, sn, false);
        }

        unq_write_thread(pDb, self->threadID, self, false);

    }

    unq_write_thread(pDb, 0, root, false);
    unqlite_commit(pDb);

    
    delete root;
    if (self != r) delete self;
    delete r;
    delete t;

    return 1;
}

// Assembly:
subq $0xd8, %rsp
movb 0xe8(%rsp), %al
movq 0xe0(%rsp), %r10
movq %rdi, 0xd0(%rsp)
movl %esi, 0xcc(%rsp)
movq %rdx, 0xc0(%rsp)
movq %rcx, 0xb8(%rsp)
movq %r8, 0xb0(%rsp)
movq %r9, 0xa8(%rsp)
andb $0x1, %al
movb %al, 0xa7(%rsp)
movq 0xd0(%rsp), %rdi
movl 0xcc(%rsp), %esi
callq 0xea50
movq %rax, 0x98(%rsp)
movq 0xd0(%rsp), %rdi
xorl %esi, %esi
callq 0xea50
movq %rax, 0x90(%rsp)
movq 0x98(%rsp), %rax
movq %rax, 0x88(%rsp)
movl $0xd0, %edi
callq 0xd250
movq %rax, %rdi
movq %rdi, 0x20(%rsp)
xorl %esi, %esi
movl $0xd0, %edx
callq 0xd430
movq 0x20(%rsp), %rax
movq %rax, 0x80(%rsp)
movb $0x0, 0x7f(%rsp)
movq 0x98(%rsp), %rax
movsbl (%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0xf129
movb $0x1, 0x7f(%rsp)
movq 0xd0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
movl $0xf, %edx
callq 0x5a550
movb $0x0, 0x6f(%rsp)
movq 0xd0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
movq 0xc0(%rsp), %rdx
xorl %ecx, %ecx
callq 0xefe0
movq 0x80(%rsp), %rdi
addq $0x1, %rdi
leaq 0x60(%rsp), %rsi
movl $0x10, %edx
callq 0xd240
movq 0x80(%rsp), %rdi
addq $0x11, %rdi
movq 0xb8(%rsp), %rsi
movl $0x40, %edx
callq 0xd240
movq 0x80(%rsp), %rdi
addq $0x51, %rdi
movq 0xb0(%rsp), %rsi
movl $0x40, %edx
callq 0xd240
movq 0x80(%rsp), %rdi
addq $0x91, %rdi
movq 0xa8(%rsp), %rsi
movl $0xa, %edx
callq 0xd240
movq 0x80(%rsp), %rdi
addq $0xa8, %rdi
movq 0xe0(%rsp), %rsi
movl $0x10, %edx
callq 0xd240
movq 0xd0(%rsp), %rdi
callq 0x5a060
movq 0x80(%rsp), %rax
movb $0x3, (%rax)
testb $0x1, 0xa7(%rsp)
je 0xf22c
movq 0x80(%rsp), %rax
movsbl (%rax), %ecx
addl $0x10, %ecx
movb %cl, (%rax)
leaq 0x58(%rsp), %rdi
callq 0xd980
movq 0x58(%rsp), %rcx
movq 0x80(%rsp), %rax
movq %rcx, 0xa0(%rax)
movq 0x98(%rsp), %rax
cmpl $0x0, 0xc4(%rax)
jne 0xf347
movq 0x90(%rsp), %rcx
movl 0xcc(%rcx), %eax
addl $0x1, %eax
movl %eax, 0xcc(%rcx)
movl %eax, 0x54(%rsp)
movl 0x54(%rsp), %ecx
movq 0x80(%rsp), %rax
movl %ecx, 0xbc(%rax)
movl 0x54(%rsp), %ecx
movq 0x80(%rsp), %rax
movl %ecx, 0xc0(%rax)
movq 0x80(%rsp), %rax
movl $0x0, 0xc4(%rax)
movq 0x98(%rsp), %rax
movl 0xb8(%rax), %ecx
movq 0x80(%rsp), %rax
movl %ecx, 0xc8(%rax)
movl 0x54(%rsp), %ecx
movq 0x80(%rsp), %rax
movl %ecx, 0xb8(%rax)
movq 0x80(%rsp), %rax
movl $0x1, 0xcc(%rax)
movq 0xd0(%rsp), %rdi
movl 0x54(%rsp), %esi
movq 0x80(%rsp), %rdx
xorl %ecx, %ecx
callq 0xeca0
movl 0x54(%rsp), %ecx
movq 0x98(%rsp), %rax
movl %ecx, 0xc4(%rax)
movq 0xd0(%rsp), %rdi
movq 0x98(%rsp), %rax
movl 0xb8(%rax), %esi
movq 0x98(%rsp), %rdx
xorl %ecx, %ecx
callq 0xeca0
jmp 0xf4f5
movq 0xd0(%rsp), %rdi
movq 0x98(%rsp), %rax
movl 0xc4(%rax), %esi
callq 0xea50
movq %rax, 0x98(%rsp)
movq 0x98(%rsp), %rax
movl 0xcc(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0xcc(%rax)
movq 0x98(%rsp), %rax
movl 0xc0(%rax), %eax
movq 0x98(%rsp), %rcx
cmpl 0xb8(%rcx), %eax
jne 0xf3cc
movq 0x98(%rsp), %rax
movl 0xbc(%rax), %eax
movq 0x98(%rsp), %rcx
cmpl 0xb8(%rcx), %eax
jne 0xf3cc
movq 0x98(%rsp), %rax
movq %rax, 0x48(%rsp)
jmp 0xf3ec
movq 0xd0(%rsp), %rdi
movq 0x98(%rsp), %rax
movl 0xc0(%rax), %esi
callq 0xea50
movq %rax, 0x48(%rsp)
movq 0x90(%rsp), %rcx
movl 0xcc(%rcx), %eax
addl $0x1, %eax
movl %eax, 0xcc(%rcx)
movl %eax, 0x44(%rsp)
movq 0x98(%rsp), %rax
movl 0xc0(%rax), %ecx
movq 0x80(%rsp), %rax
movl %ecx, 0xc0(%rax)
movq 0x98(%rsp), %rax
movl 0xb8(%rax), %ecx
movq 0x80(%rsp), %rax
movl %ecx, 0xbc(%rax)
movq 0x80(%rsp), %rax
movl $0x0, 0xc8(%rax)
movq 0x80(%rsp), %rax
movl $0x0, 0xc4(%rax)
movl 0x44(%rsp), %ecx
movq 0x80(%rsp), %rax
movl %ecx, 0xb8(%rax)
movl 0x44(%rsp), %ecx
movq 0x98(%rsp), %rax
movl %ecx, 0xc0(%rax)
movl 0x44(%rsp), %ecx
movq 0x48(%rsp), %rax
movl %ecx, 0xbc(%rax)
movq 0xd0(%rsp), %rdi
movl 0x44(%rsp), %esi
movq 0x80(%rsp), %rdx
xorl %ecx, %ecx
callq 0xeca0
movq 0xd0(%rsp), %rdi
movq 0x98(%rsp), %rax
movl 0xb8(%rax), %esi
movq 0x98(%rsp), %rdx
xorl %ecx, %ecx
callq 0xeca0
movq 0xd0(%rsp), %rdi
movq 0x48(%rsp), %rax
movl 0xb8(%rax), %esi
movq 0x48(%rsp), %rdx
xorl %ecx, %ecx
callq 0xeca0
movq 0x88(%rsp), %rax
cmpl $0x0, 0xc0(%rax)
je 0xf533
movq 0x88(%rsp), %rax
movsbl (%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
jne 0xf533
movq 0x80(%rsp), %rax
movsbl (%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
jne 0xf533
testb $0x1, 0x7f(%rsp)
je 0xf538
jmp 0xf6e5
movq 0x88(%rsp), %rax
cmpl $0x0, 0xc0(%rax)
je 0xf60d
movq 0xd0(%rsp), %rdi
movq 0x88(%rsp), %rax
movl 0xc0(%rax), %esi
callq 0xea50
movq %rax, 0x38(%rsp)
movq 0x88(%rsp), %rax
movl 0xbc(%rax), %ecx
movq 0x38(%rsp), %rax
movl %ecx, 0xbc(%rax)
movq 0xd0(%rsp), %rdi
movq 0x38(%rsp), %rax
movl 0xb8(%rax), %esi
movq 0x38(%rsp), %rdx
xorl %ecx, %ecx
callq 0xeca0
movq 0x88(%rsp), %rax
cmpl $0x0, 0xbc(%rax)
je 0xf60b
movq 0xd0(%rsp), %rdi
movq 0x88(%rsp), %rax
movl 0xbc(%rax), %esi
callq 0xea50
movq %rax, 0x30(%rsp)
movq 0x38(%rsp), %rax
movl 0xb8(%rax), %ecx
movq 0x30(%rsp), %rax
movl %ecx, 0xc0(%rax)
movq 0xd0(%rsp), %rdi
movq 0x30(%rsp), %rax
movl 0xb8(%rax), %esi
movq 0x30(%rsp), %rdx
xorl %ecx, %ecx
callq 0xeca0
jmp 0xf60d
movq 0x90(%rsp), %rax
movl 0xbc(%rax), %ecx
movq 0x88(%rsp), %rax
movl %ecx, 0xbc(%rax)
movq 0x88(%rsp), %rax
movl $0x0, 0xc0(%rax)
movq 0x88(%rsp), %rax
movl 0xb8(%rax), %ecx
movq 0x90(%rsp), %rax
movl %ecx, 0xbc(%rax)
movq 0x88(%rsp), %rax
cmpl $0x0, 0xbc(%rax)
je 0xf6c0
movq 0xd0(%rsp), %rdi
movq 0x88(%rsp), %rax
movl 0xbc(%rax), %esi
callq 0xea50
movq %rax, 0x28(%rsp)
movq 0x88(%rsp), %rax
movl 0xb8(%rax), %ecx
movq 0x28(%rsp), %rax
movl %ecx, 0xc0(%rax)
movq 0xd0(%rsp), %rdi
movq 0x28(%rsp), %rax
movl 0xb8(%rax), %esi
movq 0x28(%rsp), %rdx
xorl %ecx, %ecx
callq 0xeca0
movq 0xd0(%rsp), %rdi
movq 0x88(%rsp), %rax
movl 0xb8(%rax), %esi
movq 0x88(%rsp), %rdx
xorl %ecx, %ecx
callq 0xeca0
movq 0xd0(%rsp), %rdi
movq 0x90(%rsp), %rdx
xorl %ecx, %ecx
movl %ecx, %esi
callq 0xeca0
movq 0xd0(%rsp), %rdi
callq 0x5a060
movq 0x90(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0xf728
movq 0x18(%rsp), %rdi
callq 0xd760
movq 0x88(%rsp), %rax
cmpq 0x98(%rsp), %rax
je 0xf759
movq 0x88(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0xf757
movq 0x10(%rsp), %rdi
callq 0xd760
jmp 0xf759
movq 0x98(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0xf776
movq 0x8(%rsp), %rdi
callq 0xd760
movq 0x80(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0xf791
movq (%rsp), %rdi
callq 0xd760
movl $0x1, %eax
addq $0xd8, %rsp
retq
nop