// File: /workspace/github_c_makefile_star2_O3/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 0x4396
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 0x1f7(%rip), %rdx       # 0x4c30
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 0x4a7b
movq %r13, %rdi
movl $0x2726, %esi           # imm = 0x2726
movq %r12, %rdx
xorl %eax, %eax
callq 0x20c0
testq %rbp, %rbp
je 0x4aa6
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 0x1799(%rip), %xmm0     # 0x6246
leaq 0x10(%rsp), %rbp
movaps %xmm0, 0x20(%rbp)
movups 0x1779(%rip), %xmm0     # 0x6236
movaps %xmm0, 0x10(%rbp)
movups 0x175e(%rip), %xmm0     # 0x6226
movaps %xmm0, (%rbp)
movl $0x202974, 0x2f(%rbp)   # imm = 0x202974
leaq 0x465e(%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 0x4c0c
cmpq $0x0, (%rax)
je 0x4c0c
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x4b42
movslq 0x10(%rbx), %rax
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %r13
addq %rax, %r13
jmp 0x4b60
movl $0xaf0, %r13d           # imm = 0xAF0
movl $0xaf0, %edi            # imm = 0xAF0
callq 0x3520
movq %rax, %rdi
movq %rax, 0x8(%rbx)
movl $0x64, 0x10(%rbx)
xorl %esi, %esi
movq %r13, %rdx
callq 0x20e0
leaq 0x4(%rsp), %r13
movq %r13, %rdi
callq 0x3973
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
movq (%rax), %rsi
movl 0x10(%rbx), %ecx
movq %r13, %rdi
callq 0x3560
cmpl $-0x1, %eax
jne 0x4be1
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 0x3537
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 0x4b77
testl %eax, %eax
jne 0x4c00
movl 0x8(%rsp), %eax
decl %eax
movl %eax, 0x10(%rbx)
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x489f
xorl %ebp, %ebp
jmp 0x4c0c
movl $0x0, 0x10(%rbx)
movl $0x2, %ebp
movq %r12, %rdi
callq 0x2040
movq %rbx, %rdi
callq 0x43e2
movl %ebp, %eax
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq