// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rcx, %r13
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
movl $0xffffffef, %ebp       # imm = 0xFFFFFFEF
jne 0x1f7ea
movq %r8, %r12
movq %rdx, %r14
movq %rdi, %rbx
movq %rsi, 0x10(%rsp)
movq 0x890(%rdi), %rdi
movl $0x1, %esi
movl $0x400, %edx            # imm = 0x400
callq *0x868(%rbx)
testq %rax, %rax
je 0x1f6b8
movq %rax, %r15
movq %r12, 0x38(%rsp)
movl $0x400, %edx            # imm = 0x400
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3a80
leaq 0x50(%rsp), %r12
movq $0x0, (%r12)
movl 0x18(%rbx), %esi
movq %r12, %rdi
callq 0x3e96
xorps %xmm0, %xmm0
leaq 0x90(%rsp), %rbp
movaps %xmm0, 0x40(%rbp)
movaps %xmm0, 0x30(%rbp)
movaps %xmm0, 0x20(%rbp)
movaps %xmm0, 0x10(%rbp)
movaps %xmm0, (%rbp)
movq %rbp, %rdi
callq 0x3f2c
movq %r12, (%rbp)
movl 0x1c(%rbx), %eax
movl %eax, 0x8(%rbp)
xorl %edi, %edi
callq 0x2130
movq %rax, 0x20(%rbp)
movl $0x1, 0x28(%rbp)
movq 0x890(%rbx), %rdi
movl $0x1, %esi
movl $0x18, %edx
callq *0x868(%rbx)
movq %rax, %r12
movq 0x890(%rbx), %rdi
movl $0x1, %esi
movl $0x18, %edx
callq *0x868(%rbx)
movq %r14, 0x10(%rax)
movq %r12, %rdi
movq %rax, %rsi
callq 0x2f28
addq $0x4, %r14
decq %r13
jne 0x1f5c3
leaq 0x48(%rsp), %rax
movq %r12, (%rax)
leaq 0x40(%rsp), %r14
movq %rax, (%r14)
xorps %xmm0, %xmm0
leaq 0x70(%rsp), %r12
movaps %xmm0, 0x10(%r12)
movaps %xmm0, (%r12)
movq %r12, %rdi
callq 0x3f86
movl $0x18, (%r12)
movq %r14, 0x10(%r12)
leaq 0x58(%rsp), %r14
movq %rbp, (%r14)
movq %r12, 0x8(%r14)
movq $0x1, 0x10(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xba66
movl %eax, %ebp
movl $0x400, %r13d           # imm = 0x400
movl $0x2, %r12d
cmpl $-0x2, %ebp
jne 0x1f6c2
movq %rbx, %rdi
callq 0x39c3
movq 0x890(%rbx), %rdi
movq %r15, %rsi
callq *0x878(%rbx)
movq 0x890(%rbx), %rdi
movl $0x400, %edx            # imm = 0x400
movq %r12, %rsi
callq *0x868(%rbx)
testq %rax, %rax
je 0x1f6b8
movq %rax, %r15
addq $0x400, %r13            # imm = 0x400
movq %rbx, %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0x3a80
movq %rbx, %rdi
movq %r14, %rsi
callq 0xba66
movl %eax, %ebp
incq %r12
jmp 0x1f657
movl $0xfffffff4, %ebp       # imm = 0xFFFFFFF4
jmp 0x1f7ea
testl %ebp, %ebp
jne 0x1f7a3
leaq 0x8(%rsp), %r8
movq $0x0, (%r8)
leaq 0x4(%rsp), %r9
movl $0x0, (%r9)
movl 0x8(%rbx), %ecx
subl (%rbx), %ecx
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
movq %r15, %rdx
callq 0x1f2c4
testl %eax, %eax
js 0x1f7b3
leaq 0x40(%rsp), %rsi
movq %rbx, %rdi
callq 0x4d68
cmpq $0x0, 0x8(%rsp)
je 0x1f74e
leaq 0x6c47(%rip), %rdi      # 0x26362
leaq 0x5420(%rip), %rsi      # 0x24b42
callq 0x2190
testq %rax, %rax
je 0x1f74e
movq %rax, %r14
movq 0x8(%rsp), %rdi
movslq 0x4(%rsp), %rsi
movl $0x1, %edx
movq %rax, %rcx
callq 0x2240
movq %r14, %rdi
callq 0x2140
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x3fa0
movq 0x8(%rsp), %rsi
movslq 0x4(%rsp), %rdx
movq %rbx, %rdi
callq 0x3a80
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rsi
movaps %xmm0, (%rsi)
movq $0x0, 0x10(%rsi)
movq %rbx, %rdi
callq 0x182ec
testl %eax, %eax
movq 0x38(%rsp), %r15
je 0x1f7fe
movl %eax, %ebp
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x5757
jmp 0x1f7c3
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x3fa0
jmp 0x1f7de
movl %eax, %ebp
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x3fa0
movq 0x8(%rsp), %rsi
movslq 0x4(%rsp), %rdx
movq %rbx, %rdi
callq 0x3fa0
movq $0x0, 0x8(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3a80
movl %ebp, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x28(%rsp), %rax
cmpq $0x1, 0x30(%rsp)
setne %cl
testq %rax, %rax
sete %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0x1f854
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x5757
movq 0x8(%rsp), %rsi
movslq 0x4(%rsp), %rdx
movq %rbx, %rdi
callq 0x3fa0
movq $0x0, 0x8(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3a80
movl $0xfffffff1, %ebp       # imm = 0xFFFFFFF1
jmp 0x1f7ea
movl (%rax), %ecx
movq 0x18(%rax), %rsi
movq 0x28(%rax), %r14
movl %ecx, 0xd8e0(%rip)      # 0x2d144
movq 0x10(%rax), %rax
movq %rax, %xmm0
movdqa %xmm0, 0x10(%rsp)
movq %rax, 0xd8ce(%rip)      # 0x2d148
testq %rsi, %rsi
je 0x1f892
leaq 0xd8ca(%rip), %rdi      # 0x2d150
movl $0x80, %edx
callq 0x5e82
jmp 0x1f899
movb $0x0, 0xd8b7(%rip)      # 0x2d150
movq %r15, %rdi
movq %r14, %rsi
callq 0x5f68
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x5757
movq 0x8(%rsp), %rsi
movslq 0x4(%rsp), %rdx
movq %rbx, %rdi
callq 0x3fa0
movdqa 0x10(%rsp), %xmm0
movd %xmm0, %ebp
jmp 0x1f7ea