// File: /workspace/github_c_makefile_star2_O1/Cotix[P]cReddit/libreddit/token.c
// Function: redditvRunParser
TokenParserResult redditvRunParser(char *url, char *post, TokenIdent *idents, va_list args)
{
    /* Initalize various pieces that are needed to get and parse the JSON */
    TokenParser *parser = tokenParserNew();
    char *cookieStr = NULL;
    CURL *redditHandle = curl_easy_init();
    jsmnerr_t jsmnResult;
    char fullUseragent[1024];

    DEBUG_PRINT(L"Grabbing %s\n", url);
    if (post)
        DEBUG_PRINT(L"Post: %s\n", post);

    /* Set default response */
    TokenParserResult result = TOKEN_PARSER_SUCCESS;

    /* Sets up curl to get 'url' and use writeToParser as the callback writer */
    curl_easy_setopt(redditHandle, CURLOPT_URL, url);
    curl_easy_setopt(redditHandle, CURLOPT_FOLLOWLOCATION, 1L);
    curl_easy_setopt(redditHandle, CURLOPT_WRITEFUNCTION, writeToParser);
    curl_easy_setopt(redditHandle, CURLOPT_WRITEDATA, (void *)parser);

    /* This gets and sets any cookies, if any are currently in the global state */
    cookieStr = redditGetCookieString();
    if (cookieStr != NULL)
        curl_easy_setopt(redditHandle, CURLOPT_COOKIE, cookieStr);

    /* If we're doing a POST, then this sets curl to use POST and tells it what
     * text to use */
    if (post != NULL) {
        curl_easy_setopt(redditHandle, CURLOPT_POST, 1);
        curl_easy_setopt(redditHandle, CURLOPT_POSTFIELDS, (void *)post);
    }

    /* Set the user agent, A combo of libreddit's useragent and the program
     * using libreddit's useragent */
    strcpy(fullUseragent, LIBREDDIT_USERAGENT);
    strcat(fullUseragent, currentRedditState->userAgent);
    curl_easy_setopt(redditHandle, CURLOPT_USERAGENT, fullUseragent);

    /* Run curl, which will run the callback and store our text in the parser
     * then cleanup */
    curl_easy_perform(redditHandle);
    curl_easy_cleanup(redditHandle);

    /* If we didn't get any memory back for whatever reason, set our
     * result to an error and jump to cleanup code. */
    if (parser->block->size <= 0 || parser->block->memory == NULL) {
        result = TOKEN_PARSER_CURL_FAIL;
        goto cleanup;
    }

    /* Create our tokens */
    jsmnResult = tokenParserCreateTokens(parser);

    if (jsmnResult != JSMN_SUCCESS) {
        result = TOKEN_PARSER_JSON_FAIL;
        goto cleanup;
    }

    /* Run the parser over our tokens using the idents */
    vparseTokens(parser, idents, args);

    /* We're done, set the response and then goto cleanup code */
    result = TOKEN_PARSER_SUCCESS;



    /* Simply frees any allocated memory used in the function */
cleanup:;

    free(cookieStr);
    tokenParserFree(parser);

    return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x418, %rsp            # imm = 0x418
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %rbp
movq %rdi, %r12
callq 0x448e
movq %rax, %rbx
callq 0x2200
movq %rax, %r13
movq %rax, %rdi
movl $0x2712, %esi           # imm = 0x2712
movq %r12, %rdx
xorl %eax, %eax
callq 0x20c0
movl $0x1, %edx
movq %r13, %rdi
movl $0x34, %esi
xorl %eax, %eax
callq 0x20c0
leaq 0x1ea(%rip), %rdx       # 0x4d1c
movq %r13, %rdi
movl $0x4e2b, %esi           # imm = 0x4E2B
xorl %eax, %eax
callq 0x20c0
movq %r13, %rdi
movl $0x2711, %esi           # imm = 0x2711
movq %rbx, %rdx
xorl %eax, %eax
callq 0x20c0
xorl %eax, %eax
callq 0x2130
movq %rax, %r12
testq %rax, %rax
je 0x4b74
movq %r13, %rdi
movl $0x2726, %esi           # imm = 0x2726
movq %r12, %rdx
xorl %eax, %eax
callq 0x20c0
testq %rbp, %rbp
je 0x4b9f
movq %r13, %rdi
movl $0x2f, %esi
movl $0x1, %edx
xorl %eax, %eax
callq 0x20c0
movq %r13, %rdi
movl $0x271f, %esi           # imm = 0x271F
movq %rbp, %rdx
xorl %eax, %eax
callq 0x20c0
movups 0x16a6(%rip), %xmm0     # 0x624c
leaq 0x10(%rsp), %rbp
movaps %xmm0, 0x20(%rbp)
movups 0x1686(%rip), %xmm0     # 0x623c
movaps %xmm0, 0x10(%rbp)
movups 0x166b(%rip), %xmm0     # 0x622c
movaps %xmm0, (%rbp)
movl $0x202974, 0x2f(%rbp)   # imm = 0x202974
leaq 0x4565(%rip), %rax      # 0x9138
movq (%rax), %rax
movq 0x8(%rax), %rsi
movq %rbp, %rdi
callq 0x2220
movq %r13, %rdi
movl $0x2722, %esi           # imm = 0x2722
movq %rbp, %rdx
xorl %eax, %eax
callq 0x20c0
movq %r13, %rdi
callq 0x2100
movq %r13, %rdi
callq 0x20a0
movq (%rbx), %rax
movl $0x1, %ebp
cmpq $0x0, 0x8(%rax)
je 0x4cf8
cmpq $0x0, (%rax)
je 0x4cf8
cmpq $0x0, 0x8(%rbx)
jne 0x4c3d
movl $0xaf0, %edi            # imm = 0xAF0
callq 0x3538
movq %rax, 0x8(%rbx)
movl $0x64, 0x10(%rbx)
movq 0x8(%rbx), %rdi
movslq 0x10(%rbx), %rax
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rdx
addq %rax, %rdx
xorl %esi, %esi
callq 0x20e0
leaq 0x4(%rsp), %r13
movq %r13, %rdi
callq 0x3a62
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
movq (%rax), %rsi
movl 0x10(%rbx), %ecx
movq %r13, %rdi
callq 0x3578
cmpl $-0x1, %eax
jne 0x4cce
movslq 0x10(%rbx), %rax
leaq 0x64(%rax), %rcx
movl %ecx, 0x10(%rbx)
movq 0x8(%rbx), %rdi
leaq (%rcx,%rcx,8), %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx), %rsi
addq $0x64, %rsi
callq 0x354f
movq %rax, 0x8(%rbx)
movslq 0x10(%rbx), %rcx
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
leaq (%rax,%rdx), %rdi
addq $-0xaf0, %rdi           # imm = 0xF510
movl $0xaf0, %edx            # imm = 0xAF0
xorl %esi, %esi
callq 0x20e0
jmp 0x4c64
testl %eax, %eax
jne 0x4cda
movl 0x8(%rsp), %ecx
decl %ecx
jmp 0x4cdc
xorl %ecx, %ecx
movl %ecx, 0x10(%rbx)
movl $0x2, %ebp
testl %eax, %eax
jne 0x4cf8
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x498f
xorl %ebp, %ebp
movq %r12, %rdi
callq 0x2040
movq %rbx, %rdi
callq 0x44da
movl %ebp, %eax
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq