// File: /workspace/github_c_makefile_star2_O2/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 $0x428, %rsp            # imm = 0x428
movq %rcx, 0x8(%rsp)
movq %rdx, %r15
movq %rsi, %rbp
movq %rdi, %r12
callq 0x427f
movq %rax, %rbx
callq 0x2240
movq %rax, %r13
movq %rax, %rdi
movl $0x2712, %esi           # imm = 0x2712
movq %r12, %rdx
xorl %eax, %eax
callq 0x20e0
pushq $0x34
popq %rsi
pushq $0x1
popq %rdx
movq %r13, %rdi
xorl %eax, %eax
callq 0x20e0
leaq 0x1d7(%rip), %rdx       # 0x4a62
movq %r13, %rdi
movl $0x4e2b, %esi           # imm = 0x4E2B
xorl %eax, %eax
callq 0x20e0
movq %r13, %rdi
movl $0x2711, %esi           # imm = 0x2711
movq %rbx, %rdx
xorl %eax, %eax
callq 0x20e0
xorl %eax, %eax
callq 0x2170
movq %rax, %r12
testq %rax, %rax
je 0x48cd
movq %r13, %rdi
movl $0x2726, %esi           # imm = 0x2726
movq %r12, %rdx
xorl %eax, %eax
callq 0x20e0
testq %rbp, %rbp
je 0x48f4
pushq $0x2f
popq %rsi
pushq $0x1
popq %rdx
movq %r13, %rdi
xorl %eax, %eax
callq 0x20e0
movq %r13, %rdi
movl $0x271f, %esi           # imm = 0x271F
movq %rbp, %rdx
xorl %eax, %eax
callq 0x20e0
pushq $0x1
popq %rbp
movups 0x1948(%rip), %xmm0     # 0x6246
leaq 0x20(%rsp), %r14
movaps %xmm0, 0x20(%r14)
movups 0x1927(%rip), %xmm0     # 0x6236
movaps %xmm0, 0x10(%r14)
movups 0x190b(%rip), %xmm0     # 0x6226
movaps %xmm0, (%r14)
movl $0x202974, 0x2f(%r14)   # imm = 0x202974
leaq 0x483a(%rip), %rax      # 0x9168
movq (%rax), %rax
movq 0x8(%rax), %rsi
movq %r14, %rdi
callq 0x2260
movq %r13, %rdi
movl $0x2722, %esi           # imm = 0x2722
movq %r14, %rdx
xorl %eax, %eax
callq 0x20e0
movq %r13, %rdi
callq 0x2130
movq %r13, %rdi
callq 0x20c0
movq (%rbx), %rax
cmpq $0x0, 0x8(%rax)
je 0x4a3e
cmpq $0x0, (%rax)
je 0x4a3e
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x498a
movslq 0x10(%rbx), %rax
imulq $0x1c, %rax, %r13
jmp 0x49a8
movl $0xaf0, %r13d           # imm = 0xAF0
movl $0xaf0, %edi            # imm = 0xAF0
callq 0x34e0
movq %rax, %rdi
movq %rax, 0x8(%rbx)
movl $0x64, 0x10(%rbx)
xorl %esi, %esi
movq %r13, %rdx
callq 0x2100
leaq 0x14(%rsp), %r13
movq %r13, %rdi
callq 0x38b9
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
movq (%rax), %rsi
movl 0x10(%rbx), %ecx
movq %r13, %rdi
callq 0x3518
cmpl $-0x1, %eax
jne 0x4a16
movslq 0x10(%rbx), %rax
addq $0x64, %rax
movl %eax, 0x10(%rbx)
movq 0x8(%rbx), %rdi
imulq $0x1c, %rax, %rsi
callq 0x34f5
movq %rax, 0x8(%rbx)
movslq 0x10(%rbx), %rcx
imulq $0x1c, %rcx, %rcx
leaq (%rax,%rcx), %rdi
addq $-0xaf0, %rdi           # imm = 0xF510
movl $0xaf0, %edx            # imm = 0xAF0
xorl %esi, %esi
callq 0x2100
jmp 0x49bf
testl %eax, %eax
jne 0x4a37
movl 0x18(%rsp), %eax
decl %eax
movl %eax, 0x10(%rbx)
movq %rbx, %rdi
movq %r15, %rsi
movq 0x8(%rsp), %rdx
callq 0x4703
xorl %ebp, %ebp
jmp 0x4a3e
andl $0x0, 0x10(%rbx)
pushq $0x2
popq %rbp
movq %r12, %rdi
callq 0x2050
movq %rbx, %rdi
callq 0x42a3
movl %ebp, %eax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq