// File: /workspace/github_c_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, 0x10(%rsp)
movq %r8, 0x8(%rsp)
movq %rcx, (%rsp)
movq %rdx, %rbp
movq %rsi, %r15
movq %rdi, %rbx
movb 0x70(%rsp), %r13b
movl $0xd0, %edi
callq 0xc1f0
movq %rax, %r14
movl $0xd0, %edx
movq %rax, %rdi
xorl %esi, %esi
callq 0xc370
leaq 0x20(%rsp), %r12
pushq $0xf
popq %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2bc94
movb $0x0, 0xf(%r12)
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0xd86c
leaq 0x1(%r14), %rdi
pushq $0x10
popq %r15
movq %r12, %rsi
movq %r15, %rdx
callq 0xc1e0
leaq 0x11(%r14), %rdi
pushq $0x40
popq %r12
movq %rbp, %rsi
movq %r12, %rdx
callq 0xc1e0
leaq 0x51(%r14), %rdi
movq (%rsp), %rsi
movq %r12, %rdx
callq 0xc1e0
leaq 0x91(%r14), %rdi
pushq $0xa
popq %rdx
movq 0x8(%rsp), %rsi
callq 0xc1e0
leaq 0xa8(%r14), %rdi
movq 0x10(%rsp), %rsi
movq %r15, %rdx
callq 0xc1e0
movq %rbx, %rdi
callq 0x2b666
shlb $0x4, %r13b
orb $0x5, %r13b
movb %r13b, (%r14)
leaq 0x18(%rsp), %r15
movq %r15, %rdi
callq 0xc850
movq (%r15), %rax
movq %rax, 0xa0(%r14)
movq %rbx, %rdi
xorl %esi, %esi
callq 0xd515
movq %rax, %r15
movl 0xbc(%rax), %ebp
movl %ebp, 0xbc(%r14)
andq $0x0, 0xc0(%r14)
andl $0x0, 0xc8(%r14)
movl 0xcc(%rax), %r12d
incl %r12d
movl %r12d, 0xcc(%rax)
movl %r12d, 0xb8(%r14)
movq %rbx, %rdi
movl %r12d, %esi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0xd674
movl %r12d, 0xbc(%r15)
movq %rbx, %rdi
xorl %esi, %esi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0xd674
testl %ebp, %ebp
je 0xd842
movq %rbx, %rdi
movl %ebp, %esi
callq 0xd515
movl %r12d, 0xc0(%rax)
movq %rbx, %rdi
movl %ebp, %esi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0xd674
movq %rbx, %rdi
callq 0x2b666
movq %r14, %rdi
callq 0xc660
movq %r15, %rdi
callq 0xc660
pushq $0x1
popq %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq