// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movq %r9, 0x20(%rsp)
movq %r8, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
movq %rdx, %rbp
movq %rsi, %r15
movq %rdi, %rbx
movb 0x80(%rsp), %r13b
movl $0xd0, %edi
callq 0xc1d0
movq %rax, %r14
movl $0xd0, %edx
movq %rax, %rdi
xorl %esi, %esi
callq 0xc360
leaq 0x30(%rsp), %r12
movq %rbx, %rdi
movq %r12, %rsi
movl $0xf, %edx
callq 0x33572
movb $0x0, 0xf(%r12)
movq %rbx, %rdi
movq %r12, %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %r15, %rcx
xorl %eax, %eax
callq 0x31b64
leaq 0x1(%r14), %rdi
movl $0x10, %edx
movq %r12, %rsi
callq 0xc1c0
leaq 0x11(%r14), %rdi
movl $0x40, %edx
movq %rbp, %rsi
callq 0xc1c0
leaq 0x51(%r14), %rdi
movl $0x40, %edx
movq 0x10(%rsp), %rsi
callq 0xc1c0
leaq 0x91(%r14), %rdi
movl $0xa, %edx
movq 0x18(%rsp), %rsi
callq 0xc1c0
leaq 0xa8(%r14), %rdi
movl $0x10, %edx
movq 0x20(%rsp), %rsi
callq 0xc1c0
movq %rbx, %rdi
callq 0x32eb6
shlb $0x4, %r13b
orb $0x5, %r13b
movb %r13b, (%r14)
leaq 0x28(%rsp), %r15
movq %r15, %rdi
callq 0xc820
movq (%r15), %rax
movq %rax, 0xa0(%r14)
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe181
movq %rax, %r15
movl 0xbc(%rax), %ebp
movl %ebp, 0xbc(%r14)
movq $0x0, 0xc0(%r14)
xorl %eax, %eax
movl %eax, 0xc8(%r14)
movl 0xcc(%r15), %r13d
incl %r13d
movl %r13d, 0xcc(%r15)
movl %r13d, 0xb8(%r14)
leaq 0xc(%rsp), %r12
movl %r13d, (%r12)
movl $0xd0, %r8d
movq %rbx, %rdi
movq %r12, %rsi
movl $0x4, %edx
movq %r14, %rcx
callq 0x319b2
movl %r13d, 0xbc(%r15)
xorl %eax, %eax
movl %eax, (%r12)
leaq 0xc(%rsp), %rsi
movl $0xd0, %r8d
movq %rbx, %rdi
movl $0x4, %edx
movq %r15, %rcx
callq 0x319b2
testl %ebp, %ebp
je 0xe56b
movq %rbx, %rdi
movl %ebp, %esi
callq 0xe181
movl %r13d, 0xc0(%rax)
leaq 0xc(%rsp), %rsi
movl %ebp, (%rsi)
movl $0xd0, %r8d
movq %rbx, %rdi
movl $0x4, %edx
movq %rax, %rcx
callq 0x319b2
movq %rbx, %rdi
callq 0x32eb6
movq %r14, %rdi
callq 0xc640
movq %r15, %rdi
callq 0xc640
movl $0x1, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq