// File: /workspace/github_c_makefile_star2_O0/coyove[P]chann/./src/general.cpp
// Function: unq_new_thread(unqlite*, char const*, char const*, char const*, char const*, char const*, bool)
int unq_new_thread(unqlite *pDb, 
    const char* content, 
    const char* title, 
    const char* ip, 
    const char* username, 
    const char* image, bool sega)
{
    struct Thread *t = new Thread();

    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 = MAIN_THREAD + NORMAL_DISPLAY;
    if (sega) t->state += SAGE_THREAD;

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

    struct Thread *r = unq_read_thread(pDb, 0); // get the root thread
    int oriNextThread = r->nextThread;

    //insert
    t->nextThread = r->nextThread;
    t->prevThread = 0; //point to the root
    t->childThread = 0; //no replies yet
    t->parentThread = 0;

    int newkey = ++r->childCount; //nextCounter(pDb);
    t->threadID = newkey;

    unq_write_thread(pDb, newkey, t, false);

    r->nextThread = newkey;
    unq_write_thread(pDb, 0, r, false);

    if (oriNextThread){
        struct Thread *rn = unq_read_thread(pDb, oriNextThread);
        rn->prevThread = newkey;
        unq_write_thread(pDb, oriNextThread, rn, false);
    }

    unqlite_commit(pDb);

    delete t;
    delete r;

    return 1;
}

// Assembly:
subq $0x98, %rsp
movb 0xa0(%rsp), %al
movq %rdi, 0x90(%rsp)
movq %rsi, 0x88(%rsp)
movq %rdx, 0x80(%rsp)
movq %rcx, 0x78(%rsp)
movq %r8, 0x70(%rsp)
movq %r9, 0x68(%rsp)
andb $0x1, %al
movb %al, 0x67(%rsp)
movl $0xd0, %edi
callq 0xd250
movq %rax, %rdi
movq %rdi, 0x18(%rsp)
xorl %esi, %esi
movl $0xd0, %edx
callq 0xd430
movq 0x18(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x90(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movl $0xf, %edx
callq 0x5a550
movb $0x0, 0x4f(%rsp)
movq 0x90(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movq 0x88(%rsp), %rdx
xorl %ecx, %ecx
callq 0xefe0
movq 0x58(%rsp), %rdi
addq $0x1, %rdi
leaq 0x40(%rsp), %rsi
movl $0x10, %edx
callq 0xd240
movq 0x58(%rsp), %rdi
addq $0x11, %rdi
movq 0x80(%rsp), %rsi
movl $0x40, %edx
callq 0xd240
movq 0x58(%rsp), %rdi
addq $0x51, %rdi
movq 0x78(%rsp), %rsi
movl $0x40, %edx
callq 0xd240
movq 0x58(%rsp), %rdi
addq $0x91, %rdi
movq 0x70(%rsp), %rsi
movl $0xa, %edx
callq 0xd240
movq 0x58(%rsp), %rdi
addq $0xa8, %rdi
movq 0x68(%rsp), %rsi
movl $0x10, %edx
callq 0xd240
movq 0x90(%rsp), %rdi
callq 0x5a060
movq 0x58(%rsp), %rax
movb $0x5, (%rax)
testb $0x1, 0x67(%rsp)
je 0xee65
movq 0x58(%rsp), %rax
movsbl (%rax), %ecx
addl $0x10, %ecx
movb %cl, (%rax)
leaq 0x38(%rsp), %rdi
callq 0xd980
movq 0x38(%rsp), %rcx
movq 0x58(%rsp), %rax
movq %rcx, 0xa0(%rax)
movq 0x90(%rsp), %rdi
xorl %esi, %esi
callq 0xea50
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rax
movl 0xbc(%rax), %eax
movl %eax, 0x2c(%rsp)
movq 0x30(%rsp), %rax
movl 0xbc(%rax), %ecx
movq 0x58(%rsp), %rax
movl %ecx, 0xbc(%rax)
movq 0x58(%rsp), %rax
movl $0x0, 0xc0(%rax)
movq 0x58(%rsp), %rax
movl $0x0, 0xc4(%rax)
movq 0x58(%rsp), %rax
movl $0x0, 0xc8(%rax)
movq 0x30(%rsp), %rcx
movl 0xcc(%rcx), %eax
addl $0x1, %eax
movl %eax, 0xcc(%rcx)
movl %eax, 0x28(%rsp)
movl 0x28(%rsp), %ecx
movq 0x58(%rsp), %rax
movl %ecx, 0xb8(%rax)
movq 0x90(%rsp), %rdi
movl 0x28(%rsp), %esi
movq 0x58(%rsp), %rdx
xorl %ecx, %ecx
callq 0xeca0
movl 0x28(%rsp), %ecx
movq 0x30(%rsp), %rax
movl %ecx, 0xbc(%rax)
movq 0x90(%rsp), %rdi
movq 0x30(%rsp), %rdx
xorl %ecx, %ecx
movl %ecx, %esi
callq 0xeca0
cmpl $0x0, 0x2c(%rsp)
je 0xef8e
movq 0x90(%rsp), %rdi
movl 0x2c(%rsp), %esi
callq 0xea50
movq %rax, 0x20(%rsp)
movl 0x28(%rsp), %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0xc0(%rax)
movq 0x90(%rsp), %rdi
movl 0x2c(%rsp), %esi
movq 0x20(%rsp), %rdx
xorl %ecx, %ecx
callq 0xeca0
movq 0x90(%rsp), %rdi
callq 0x5a060
movq 0x58(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0xefb5
movq 0x10(%rsp), %rdi
callq 0xd760
movq 0x30(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0xefcf
movq 0x8(%rsp), %rdi
callq 0xd760
movl $0x1, %eax
addq $0x98, %rsp
retq
nopl (%rax)