// File: /workspace/github_c_makefile_star2_O0/OpenKMIP[P]libkmip/src/kmip_bio.c
// Function: kmip_bio_query_with_context
int kmip_bio_query_with_context(KMIP *ctx, BIO *bio, enum query_function queries[], size_t query_count, QueryResponse* query_result)
{
    if (ctx == NULL || bio == NULL || queries == NULL || query_count == 0 || query_result == NULL)
    {
        return(KMIP_ARG_INVALID);
    }

    size_t buffer_blocks = 1;
    size_t buffer_block_size = 1024;
    size_t buffer_total_size = buffer_blocks * buffer_block_size;

    uint8 *encoding = ctx->calloc_func(ctx->state, buffer_blocks, buffer_block_size);
    if(encoding == NULL)
    {
        return(KMIP_MEMORY_ALLOC_FAILED);
    }
    kmip_set_buffer(ctx, encoding, buffer_total_size);

    /* Build the request message. */


    ProtocolVersion pv = {0};
    kmip_init_protocol_version(&pv, ctx->version);

    RequestHeader rh = {0};
    kmip_init_request_header(&rh);

    rh.protocol_version = &pv;
    rh.maximum_response_size = ctx->max_message_size;
    rh.time_stamp = time(NULL);
    rh.batch_count = 1;

    LinkedList *funclist = ctx->calloc_func(ctx->state, 1, sizeof(LinkedList));
    for(size_t i = 0; i < query_count; i++)
    {
        LinkedListItem *item = ctx->calloc_func(ctx->state, 1, sizeof(LinkedListItem));
        item->data = &queries[i];
        kmip_linked_list_enqueue(funclist, item);
    }
    Functions functions = {0};
    functions.function_list = funclist;

    QueryRequestPayload qrp = {0};
    qrp.functions = &functions;

    RequestBatchItem rbi = {0};
    kmip_init_request_batch_item(&rbi);
    rbi.operation = KMIP_OP_QUERY;
    rbi.request_payload = &qrp;

    RequestMessage rm = {0};
    rm.request_header = &rh;
    rm.batch_items = &rbi;
    rm.batch_count = 1;

    /* Encode the request message. Dynamically resize the encoding buffer */
    /* if it's not big enough. Once encoding succeeds, send the request   */
    /* message.                                                           */
    int encode_result = kmip_encode_request_message(ctx, &rm);
    while(encode_result == KMIP_ERROR_BUFFER_FULL)
    {
        kmip_reset(ctx);
        ctx->free_func(ctx->state, encoding);

        buffer_blocks += 1;
        buffer_total_size = buffer_blocks * buffer_block_size;

        encoding = ctx->calloc_func(ctx->state, buffer_blocks, buffer_block_size);
        if(encoding == NULL)
        {
            return(KMIP_MEMORY_ALLOC_FAILED);
        }
        kmip_set_buffer(ctx, encoding, buffer_total_size);
        encode_result = kmip_encode_request_message(ctx, &rm);
    }

    if(encode_result != KMIP_OK)
    {
        kmip_free_buffer(ctx, encoding, buffer_total_size);
        encoding = NULL;
        kmip_set_buffer(ctx, NULL, 0);
        return(encode_result);
    }

    char *response = NULL;
    int response_size = 0;

    int result = kmip_bio_send_request_encoding(ctx, bio, (char *)encoding, ctx->index - ctx->buffer, &response, &response_size);
    if(result < 0)
    {
        kmip_free_buffer(ctx, encoding, buffer_total_size);
        kmip_free_buffer(ctx, response, response_size);
        encoding = NULL;
        response = NULL;
        kmip_set_buffer(ctx, NULL, 0);
        return(result);
    }

    kmip_free_query_request_payload(ctx, &qrp);

    if (response)
    {
        FILE* out = fopen( "/tmp/kmip_query.dat", "w" );
        if (out)
        {
            fwrite( response, response_size, 1, out );
            fclose(out);
        }
        // kmip_print_buffer(response, response_size);
    }

    kmip_free_buffer(ctx, encoding, buffer_total_size);
    encoding = NULL;
    kmip_set_buffer(ctx, response, response_size);

    /* Decode the response message and retrieve the operation results. */
    ResponseMessage resp_m = {0};
    int decode_result = kmip_decode_response_message(ctx, &resp_m);
    if(decode_result != KMIP_OK)
    {
        kmip_free_response_message(ctx, &resp_m);
        kmip_free_buffer(ctx, response, response_size);
        response = NULL;
        kmip_set_buffer(ctx, NULL, 0);
        return(decode_result);
    }

    if(resp_m.batch_count != 1 || resp_m.batch_items == NULL)
    {
        kmip_free_response_message(ctx, &resp_m);
        kmip_free_buffer(ctx, response, response_size);
        response = NULL;
        kmip_set_buffer(ctx, NULL, 0);
        return(KMIP_MALFORMED_RESPONSE);
    }

    ResponseBatchItem resp_item = resp_m.batch_items[0];
    enum result_status result_status = resp_item.result_status;

    kmip_set_last_result(&resp_item);

    if(result == KMIP_STATUS_SUCCESS)
    {
        kmip_copy_query_result(query_result, (QueryResponsePayload*) resp_item.response_payload);
    }

    /* Clean up the response message, the response buffer, and the KMIP */
    /* context.                                                         */
    kmip_free_response_message(ctx, &resp_m);
    kmip_free_buffer(ctx, response, response_size);
    response = NULL;

    return(result_status);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x29b22
cmpq $0x0, -0x18(%rbp)
je 0x29b22
cmpq $0x0, -0x20(%rbp)
je 0x29b22
cmpq $0x0, -0x28(%rbp)
je 0x29b22
cmpq $0x0, -0x30(%rbp)
jne 0x29b2e
movl $0xffffffef, -0x4(%rbp) # imm = 0xFFFFFFEF
jmp 0x2a119
movq $0x1, -0x38(%rbp)
movq $0x400, -0x40(%rbp)     # imm = 0x400
movq -0x38(%rbp), %rax
imulq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x868(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x890(%rcx), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq *%rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x29b82
movl $0xfffffff4, -0x4(%rbp) # imm = 0xFFFFFFF4
jmp 0x2a119
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x48f0
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x20e0
movq -0x10(%rbp), %rax
movl 0x18(%rax), %esi
leaq -0x58(%rbp), %rdi
callq 0x50f0
leaq -0xa8(%rbp), %rdi
xorl %esi, %esi
movl $0x50, %edx
callq 0x20e0
leaq -0xa8(%rbp), %rdi
callq 0x5350
leaq -0x58(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0xa0(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x2130
movq %rax, -0x88(%rbp)
movl $0x1, -0x80(%rbp)
movq -0x10(%rbp), %rax
movq 0x868(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x890(%rcx), %rdi
movl $0x1, %esi
movl $0x18, %edx
callq *%rax
movq %rax, -0xb0(%rbp)
movq $0x0, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0x29caf
movq -0x10(%rbp), %rax
movq 0x868(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x890(%rcx), %rdi
movl $0x1, %esi
movl $0x18, %edx
callq *%rax
movq %rax, -0xc0(%rbp)
movq -0x20(%rbp), %rcx
movq -0xb8(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rcx
movq -0xc0(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0xb0(%rbp), %rdi
movq -0xc0(%rbp), %rsi
callq 0x34f0
movq -0xb8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xb8(%rbp)
jmp 0x29c35
leaq -0xc8(%rbp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x20e0
movq -0xb0(%rbp), %rax
movq %rax, -0xc8(%rbp)
leaq -0xd0(%rbp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x20e0
leaq -0xc8(%rbp), %rax
movq %rax, -0xd0(%rbp)
leaq -0xf0(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x20e0
leaq -0xf0(%rbp), %rdi
callq 0x5480
movl $0x18, -0xf0(%rbp)
leaq -0xd0(%rbp), %rax
movq %rax, -0xe0(%rbp)
leaq -0x108(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x20e0
leaq -0xa8(%rbp), %rax
movq %rax, -0x108(%rbp)
leaq -0xf0(%rbp), %rax
movq %rax, -0x100(%rbp)
movq $0x1, -0xf8(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x12680
movl %eax, -0x10c(%rbp)
cmpl $-0x2, -0x10c(%rbp)
jne 0x29e26
movq -0x10(%rbp), %rdi
callq 0x4850
movq -0x10(%rbp), %rax
movq 0x878(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x890(%rcx), %rdi
movq -0x50(%rbp), %rsi
callq *%rax
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
imulq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x868(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x890(%rcx), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq *%rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x29dfa
movl $0xfffffff4, -0x4(%rbp) # imm = 0xFFFFFFF4
jmp 0x2a119
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x48f0
movq -0x10(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x12680
movl %eax, -0x10c(%rbp)
jmp 0x29d78
cmpl $0x0, -0x10c(%rbp)
je 0x29e66
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x54c0
movq $0x0, -0x50(%rbp)
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x48f0
movl -0x10c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2a119
movq $0x0, -0x118(%rbp)
movl $0x0, -0x11c(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
subq %rcx, %rax
movl %eax, %ecx
leaq -0x118(%rbp), %r8
leaq -0x11c(%rbp), %r9
callq 0x29840
movl %eax, -0x120(%rbp)
cmpl $0x0, -0x120(%rbp)
jge 0x29f16
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x54c0
movq -0x10(%rbp), %rdi
movq -0x118(%rbp), %rsi
movslq -0x11c(%rbp), %rdx
callq 0x54c0
movq $0x0, -0x50(%rbp)
movq $0x0, -0x118(%rbp)
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x48f0
movl -0x120(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2a119
movq -0x10(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x6a30
cmpq $0x0, -0x118(%rbp)
je 0x29f81
leaq 0x65e7(%rip), %rdi      # 0x3051e
leaq 0x4dc0(%rip), %rsi      # 0x2ecfe
callq 0x2180
movq %rax, -0x128(%rbp)
cmpq $0x0, -0x128(%rbp)
je 0x29f7f
movq -0x118(%rbp), %rdi
movslq -0x11c(%rbp), %rsi
movq -0x128(%rbp), %rcx
movl $0x1, %edx
callq 0x2200
movq -0x128(%rbp), %rdi
callq 0x2140
jmp 0x29f81
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x54c0
movq $0x0, -0x50(%rbp)
movq -0x10(%rbp), %rdi
movq -0x118(%rbp), %rsi
movslq -0x11c(%rbp), %rdx
callq 0x48f0
leaq -0x140(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x20e0
movq -0x10(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x1ed90
movl %eax, -0x144(%rbp)
cmpl $0x0, -0x144(%rbp)
je 0x2a033
movq -0x10(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x7820
movq -0x10(%rbp), %rdi
movq -0x118(%rbp), %rsi
movslq -0x11c(%rbp), %rdx
callq 0x54c0
movq $0x0, -0x118(%rbp)
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x48f0
movl -0x144(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2a119
cmpq $0x1, -0x130(%rbp)
jne 0x2a047
cmpq $0x0, -0x138(%rbp)
jne 0x2a095
movq -0x10(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x7820
movq -0x10(%rbp), %rdi
movq -0x118(%rbp), %rsi
movslq -0x11c(%rbp), %rdx
callq 0x54c0
movq $0x0, -0x118(%rbp)
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x48f0
movl $0xfffffff1, -0x4(%rbp) # imm = 0xFFFFFFF1
jmp 0x2a119
movq -0x138(%rbp), %rsi
leaq -0x178(%rbp), %rdi
movl $0x30, %edx
callq 0x2100
movl -0x168(%rbp), %eax
movl %eax, -0x17c(%rbp)
leaq -0x178(%rbp), %rdi
callq 0x27f00
cmpl $0x0, -0x120(%rbp)
jne 0x2a0de
movq -0x30(%rbp), %rdi
movq -0x150(%rbp), %rsi
callq 0x88a0
movq -0x10(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x7820
movq -0x10(%rbp), %rdi
movq -0x118(%rbp), %rsi
movslq -0x11c(%rbp), %rdx
callq 0x54c0
movq $0x0, -0x118(%rbp)
movl -0x17c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq