// File: /workspace/github_c_makefile_star2_O2/OpenKMIP[P]libkmip/demos/demo_query.c
// Function: use_low_level_api
int use_low_level_api(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);
    }

    printf("bio query start \n");

    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)
    {
        kmip_destroy(ctx);
        return(KMIP_MEMORY_ALLOC_FAILED);
    }
    printf("encoding = %p\n", encoding);
    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 *list_1 = 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(list_1, item);
    }
    Functions functions = {0};
    functions.function_list = list_1;

    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)
        {
            printf("Failure: Could not automatically enlarge the encoding ");
            printf("buffer for the Query request.\n");

            kmip_destroy(ctx);
            return(KMIP_MEMORY_ALLOC_FAILED);
        }
        printf("encoding = %p\n", encoding);

        kmip_set_buffer(ctx, encoding, buffer_total_size);
        encode_result = kmip_encode_request_message(ctx, &rm);
    }

    if(encode_result != KMIP_OK)
    {
        printf("An error occurred while encoding the Query request.\n");
        printf("Error Code: %d\n", encode_result);
        printf("Error Name: ");
        kmip_print_error_string(stdout, encode_result);
        printf("\n");
        printf("Context Error: %s\n", ctx->error_message);
        printf("Stack trace:\n");
        kmip_print_stack_trace(stdout, ctx);

        kmip_free_buffer(ctx, encoding, buffer_total_size);
        encoding = NULL;
        kmip_set_buffer(ctx, NULL, 0);
        kmip_destroy(ctx);
        return(encode_result);
    }

    kmip_print_request_message(stdout, &rm);
    printf("\n");

    char *response = NULL;
    int response_size = 0;

    printf("bio query send request\n");

    int result = kmip_bio_send_request_encoding(ctx, bio, (char *)encoding,
                                                ctx->index - ctx->buffer,
                                                &response, &response_size);

    printf("bio query response = %p\n", response);

    printf("\n");
    if(result < 0)
    {
        printf("An error occurred in query request.\n");
        printf("Error Code: %d\n", result);
        printf("Error Name: ");
        kmip_print_error_string(stderr, result);
        printf("\n");
        printf("Context Error: %s\n", ctx->error_message);
        printf("Stack trace:\n");
        kmip_print_stack_trace(stderr, ctx);

        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);
        kmip_destroy(ctx);
        return(result);
    }

    kmip_free_query_request_payload(ctx, &qrp);

    if (response)
    {
        FILE* out = fopen( "/tmp/kmip_query.dat", "w" );
        if (out)
        {
            if (fwrite( response, response_size, 1, out ) != 1 )
                fprintf(stderr, "failed writing dat file\n");
            fclose(out);
        }
        kmip_print_buffer(stdout, response, response_size);
    }

    printf("bio query free encoding =  %p\n", encoding);
    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)
    {
        printf("An error occurred while decoding the Query response.\n");
        printf("Error Code: %d\n", decode_result);
        printf("Error Name: ");
        kmip_print_error_string(stderr, decode_result);
        printf("\n");
        printf("Context Error: %s\n", ctx->error_message);
        printf("Stack trace:\n");
        kmip_print_stack_trace(stderr, ctx);

        kmip_free_response_message(ctx, &resp_m);
        kmip_free_buffer(ctx, response, response_size);
        response = NULL;
        kmip_set_buffer(ctx, NULL, 0);
        kmip_destroy(ctx);
        return(decode_result);
    }

    kmip_print_response_message(stdout, &resp_m);
    printf("\n");

    if(resp_m.batch_count != 1 || resp_m.batch_items == NULL)
    {
        printf("Expected to find one batch item in the Query response.\n");
        kmip_free_response_message(ctx, &resp_m);
        kmip_free_buffer(ctx, response, response_size);
        response = NULL;
        kmip_set_buffer(ctx, NULL, 0);
        kmip_destroy(ctx);
        return(KMIP_MALFORMED_RESPONSE);
    }

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

    printf("The KMIP operation was executed with no errors.\n");
    printf("Result: ");
    kmip_print_result_status_enum(stdout, result);
    printf(" (%d)\n\n", result);

    if(result == KMIP_STATUS_SUCCESS)
    {
        kmip_copy_query_result(query_result, (QueryResponsePayload*) req.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;

    kmip_set_buffer(ctx, NULL, 0);
    kmip_destroy(ctx);

    printf("bio query done \n");

    return(result_status);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rcx, %r12
movq %rsi, %xmm0
movq %rdi, %xmm1
punpcklqdq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0]
movq %r8, %xmm0
movq %rdx, %xmm2
punpcklqdq %xmm0, %xmm2    # xmm2 = xmm2[0],xmm0[0]
pxor %xmm0, %xmm0
testq %rcx, %rcx
sete %al
pcmpeqd %xmm0, %xmm2
pcmpeqd %xmm0, %xmm1
movdqa %xmm1, %xmm0
shufps $0xdd, %xmm2, %xmm0     # xmm0 = xmm0[1,3],xmm2[1,3]
shufps $0x88, %xmm2, %xmm1     # xmm1 = xmm1[0,2],xmm2[0,2]
andps %xmm0, %xmm1
movmskps %xmm1, %ecx
testl %ecx, %ecx
setne %cl
orb %al, %cl
je 0x259c
pushq $-0x11
jmp 0x2a34
movq %r8, %r14
movq %rdx, %rbp
movq %rsi, %r13
movq %rdi, %rbx
leaq 0x16e89(%rip), %rdi     # 0x19438
callq 0x2210
movq 0x890(%rbx), %rdi
pushq $0x1
popq %rsi
movl $0x400, %edx            # imm = 0x400
callq *0x868(%rbx)
testq %rax, %rax
je 0x2a2a
movq %rax, %r15
movq %r13, 0x20(%rsp)
movq %r14, 0x18(%rsp)
leaq 0x16b04(%rip), %rdi     # 0x190ea
movq %rax, %rsi
xorl %eax, %eax
callq 0x2040
movl $0x400, %edx            # imm = 0x400
movq %rbx, %rdi
movq %r15, 0x10(%rsp)
movq %r15, %rsi
callq 0x39c9
leaq 0x50(%rsp), %r14
andq $0x0, (%r14)
movl 0x18(%rbx), %esi
movq %r14, %rdi
callq 0x3d3a
xorps %xmm0, %xmm0
leaq 0x90(%rsp), %r15
movaps %xmm0, 0x40(%r15)
movaps %xmm0, 0x30(%r15)
movaps %xmm0, 0x20(%r15)
movaps %xmm0, 0x10(%r15)
movaps %xmm0, (%r15)
movq %r15, %rdi
callq 0x3dc2
movq %r14, (%r15)
movl 0x1c(%rbx), %eax
movl %eax, 0x8(%r15)
xorl %edi, %edi
callq 0x2130
movq %rax, 0x20(%r15)
movl $0x1, 0x28(%r15)
movq 0x890(%rbx), %rdi
pushq $0x1
popq %r14
pushq $0x18
popq %r13
movq %r14, %rsi
movq %r13, %rdx
callq *0x868(%rbx)
movq %rax, %r15
subq $0x1, %r12
jb 0x26ad
movq 0x890(%rbx), %rdi
movq %r14, %rsi
movq %r13, %rdx
callq *0x868(%rbx)
movq %rbp, 0x10(%rax)
movq %r15, %rdi
movq %rax, %rsi
callq 0x312f
addq $0x4, %rbp
jmp 0x267f
leaq 0x48(%rsp), %rax
movq %r15, (%rax)
leaq 0x28(%rsp), %r14
movq %rax, (%r14)
xorps %xmm0, %xmm0
leaq 0x70(%rsp), %r15
movaps %xmm0, 0x10(%r15)
movaps %xmm0, (%r15)
movq %r15, %rdi
callq 0x3e15
movl $0x18, (%r15)
movq %r14, 0x10(%r15)
leaq 0x58(%rsp), %r14
leaq 0x90(%rsp), %rax
movq %rax, (%r14)
movq %r15, 0x8(%r14)
movq $0x1, 0x10(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xabba
movl %eax, %ebp
movl $0x400, %r12d           # imm = 0x400
pushq $0x2
popq %r13
movq 0x10(%rsp), %r15
cmpl $-0x2, %ebp
jne 0x278f
movq %rbx, %rdi
callq 0x3986
movq 0x890(%rbx), %rdi
movq %r15, %rsi
callq *0x878(%rbx)
movq 0x890(%rbx), %rdi
movl $0x400, %edx            # imm = 0x400
movq %r13, %rsi
callq *0x868(%rbx)
testq %rax, %rax
je 0x2a10
movq %rax, %r15
addq $0x400, %r12            # imm = 0x400
leaq 0x16985(%rip), %rdi     # 0x190ea
movq %rax, %rsi
xorl %eax, %eax
callq 0x2040
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x39c9
movq %rbx, %rdi
movq %r14, %rsi
callq 0xabba
movl %eax, %ebp
incq %r13
jmp 0x2719
testl %ebp, %ebp
jne 0x2991
movq 0x24822(%rip), %rax     # 0x26fc0
movq (%rax), %rdi
leaq 0x58(%rsp), %rsi
callq 0x16064
pushq $0xa
popq %r14
movl %r14d, %edi
callq 0x21e0
movq %r15, %rbp
leaq 0x8(%rsp), %r15
andq $0x0, (%r15)
leaq 0x4(%rsp), %r13
andl $0x0, (%r13)
leaq 0x16c75(%rip), %rdi     # 0x19449
callq 0x2210
movl 0x8(%rbx), %ecx
subl (%rbx), %ecx
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
movq %rbp, 0x10(%rsp)
movq %rbp, %rdx
movq %r15, %r8
movq %r13, %r9
callq 0x18315
movl %eax, %ebp
movq (%r15), %rsi
leaq 0x1695b(%rip), %rdi     # 0x19160
xorl %eax, %eax
callq 0x2040
movl %r14d, %edi
callq 0x21e0
testl %ebp, %ebp
js 0x2a49
leaq 0x28(%rsp), %rsi
movq %rbx, %rdi
callq 0x49ba
cmpq $0x0, 0x8(%rsp)
movq 0x10(%rsp), %r15
movq 0x24785(%rip), %r13     # 0x26fc0
je 0x2891
leaq 0x16935(%rip), %rdi     # 0x19179
leaq 0x19db7(%rip), %rsi     # 0x1c602
callq 0x2190
testq %rax, %rax
je 0x287f
movq %rax, %r14
movq 0x8(%rsp), %rdi
movslq 0x4(%rsp), %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x2240
cmpq $0x1, %rax
jne 0x2c11
movq %r14, %rdi
callq 0x2140
movq (%r13), %rdi
movq 0x8(%rsp), %rsi
movl 0x4(%rsp), %edx
callq 0x1285c
leaq 0x1690e(%rip), %rdi     # 0x191a6
movq %r15, %rsi
xorl %eax, %eax
callq 0x2040
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x3e29
movq 0x8(%rsp), %rsi
movslq 0x4(%rsp), %rdx
movq %rbx, %rdi
callq 0x39c9
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rsi
movaps %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
movq %rbx, %rdi
callq 0x11d5c
testl %eax, %eax
je 0x2af6
movl %eax, %r15d
leaq 0x16bbb(%rip), %rdi     # 0x194a7
callq 0x2210
leaq 0x16838(%rip), %rdi     # 0x19130
movl %r15d, %esi
xorl %eax, %eax
callq 0x2040
leaq 0x16837(%rip), %rdi     # 0x19140
xorl %eax, %eax
callq 0x2040
movq 0x246c1(%rip), %r14     # 0x26fd8
movq (%r14), %rdi
movl %r15d, %esi
callq 0x12920
pushq $0xa
popq %rdi
callq 0x21e0
movq 0x28(%rbx), %rsi
leaq 0x16818(%rip), %rdi     # 0x1914d
xorl %eax, %eax
callq 0x2040
leaq 0x16c3f(%rip), %rdi     # 0x19582
callq 0x2210
movq (%r14), %rdi
movq %rbx, %rsi
callq 0x128ce
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
callq 0x517a
movq 0x8(%rsp), %rsi
movslq 0x4(%rsp), %rdx
movq %rbx, %rdi
callq 0x3e29
andq $0x0, 0x8(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x39c9
movq %rbx, %rdi
callq 0x39da
jmp 0x2c09
leaq 0x16b68(%rip), %rdi     # 0x19500
callq 0x2210
leaq 0x1678c(%rip), %rdi     # 0x19130
movl %ebp, %esi
xorl %eax, %eax
callq 0x2040
leaq 0x1678c(%rip), %rdi     # 0x19140
xorl %eax, %eax
callq 0x2040
movq 0x245fe(%rip), %r14     # 0x26fc0
movq (%r14), %rdi
movl %ebp, %esi
callq 0x12920
pushq $0xa
popq %rdi
callq 0x21e0
movq 0x28(%rbx), %rsi
leaq 0x1676e(%rip), %rdi     # 0x1914d
xorl %eax, %eax
callq 0x2040
leaq 0x16b95(%rip), %rdi     # 0x19582
callq 0x2210
movq (%r14), %rdi
movq %rbx, %rsi
callq 0x128ce
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x3e29
jmp 0x2add
leaq 0x166e2(%rip), %rdi     # 0x190f9
xorl %eax, %eax
callq 0x2040
leaq 0x16b0f(%rip), %rdi     # 0x19534
callq 0x2210
movq %rbx, %rdi
callq 0x39da
pushq $-0xc
popq %rbp
movl %ebp, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x16a8c(%rip), %rdi     # 0x194dc
callq 0x2210
leaq 0x166d4(%rip), %rdi     # 0x19130
movl %ebp, %esi
xorl %eax, %eax
callq 0x2040
leaq 0x166d4(%rip), %rdi     # 0x19140
xorl %eax, %eax
callq 0x2040
movq 0x2455e(%rip), %r14     # 0x26fd8
movq (%r14), %rdi
movl %ebp, %esi
callq 0x12920
pushq $0xa
popq %rdi
callq 0x21e0
movq 0x28(%rbx), %rsi
leaq 0x166b6(%rip), %rdi     # 0x1914d
xorl %eax, %eax
callq 0x2040
leaq 0x16add(%rip), %rdi     # 0x19582
callq 0x2210
movq (%r14), %rdi
movq %rbx, %rsi
callq 0x128ce
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
movq %r12, %rdx
callq 0x3e29
movq 0x8(%rsp), %rsi
movslq 0x4(%rsp), %rdx
movq %rbx, %rdi
callq 0x3e29
andq $0x0, 0x8(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x39c9
movq %rbx, %rdi
callq 0x39da
jmp 0x2a35
movq (%r13), %rdi
leaq 0x30(%rsp), %r14
movq %r14, %rsi
callq 0x160e8
pushq $0xa
popq %rdi
callq 0x21e0
movq 0x8(%r14), %rax
cmpq $0x1, 0x10(%r14)
setne %cl
testq %rax, %rax
sete %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0x2b76
leaq 0x16941(%rip), %rdi     # 0x19470
callq 0x2210
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
callq 0x517a
movq 0x8(%rsp), %rsi
movslq 0x4(%rsp), %rdx
movq %rbx, %rdi
callq 0x3e29
andq $0x0, 0x8(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x39c9
movq %rbx, %rdi
callq 0x39da
pushq $-0xf
popq %r15
jmp 0x2c09
movl 0x10(%rax), %r15d
movq 0x28(%rax), %r14
leaq 0x169cd(%rip), %rdi     # 0x19552
callq 0x2210
leaq 0x16634(%rip), %rdi     # 0x191c5
xorl %eax, %eax
callq 0x2040
movq (%r13), %rdi
movl %ebp, %esi
callq 0x12d6c
leaq 0x16624(%rip), %rdi     # 0x191ce
movl %ebp, %esi
xorl %eax, %eax
callq 0x2040
testl %ebp, %ebp
jne 0x2bc4
movq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x58bf
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
callq 0x517a
movq 0x8(%rsp), %rsi
movslq 0x4(%rsp), %rdx
movq %rbx, %rdi
callq 0x3e29
andq $0x0, 0x8(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x39c9
movq %rbx, %rdi
callq 0x39da
leaq 0x1685c(%rip), %rdi     # 0x19460
callq 0x2210
movl %r15d, %ebp
jmp 0x2a35
movq 0x243c0(%rip), %rax     # 0x26fd8
movq (%rax), %rcx
leaq 0x1656b(%rip), %rdi     # 0x1918d
pushq $0x18
popq %rsi
pushq $0x1
popq %rdx
callq 0x2240
jmp 0x2877