// File: /workspace/github_c_makefile_star2_O1/OpenKMIP[P]libkmip/src/kmip_bio.c
// Function: kmip_bio_get_symmetric_key_with_context
int kmip_bio_get_symmetric_key_with_context(KMIP *ctx, BIO *bio,
                                            char *uuid, int uuid_size,
                                            char **key, int *key_size)
{
    if(ctx == NULL || bio == NULL || uuid == NULL || uuid_size <= 0 || key == NULL || key_size == NULL)
    {
        return(KMIP_ARG_INVALID);
    }
    
    /* Set up the initial encoding buffer. */
    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;
    
    TextString id = {0};
    id.value = uuid;
    id.size = uuid_size;
    
    GetRequestPayload grp = {0};
    grp.unique_identifier = &id;
    
    RequestBatchItem rbi = {0};
    kmip_init_request_batch_item(&rbi);
    rbi.operation = KMIP_OP_GET;
    rbi.request_payload = &grp;
    
    RequestMessage rm = {0};
    rm.request_header = &rh;
    rm.batch_items = &rbi;
    rm.batch_count = 1;
    
    /* Add the context credential to the request message if it exists. */
    /* TODO (ph) Update this to add multiple credentials. */
    Authentication auth = {0};
    if(ctx->credential_list != NULL)
    {
        LinkedListItem *item = ctx->credential_list->head;
        if(item != NULL)
        {
            auth.credential = (Credential *)item->data;
            rh.authentication = &auth;
        }
    }
    
    /* 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;
        return(encode_result);
    }
    
    int sent = BIO_write(bio, ctx->buffer, ctx->index - ctx->buffer);
    if(sent != ctx->index - ctx->buffer)
    {
        kmip_free_buffer(ctx, encoding, buffer_total_size);
        encoding = NULL;
        return(KMIP_IO_FAILURE);
    }
    
    kmip_free_buffer(ctx, encoding, buffer_total_size);
    encoding = NULL;
    
    /* Read the response message. Dynamically resize the encoding buffer  */
    /* to align with the message size advertised by the message encoding. */
    /* Reject the message if the message size is too large.               */
    buffer_blocks = 1;
    buffer_block_size = 8;
    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);
    }
    
    int recv = BIO_read(bio, encoding, buffer_total_size);
    if((size_t)recv != buffer_total_size)
    {
        kmip_free_buffer(ctx, encoding, buffer_total_size);
        encoding = NULL;
        return(KMIP_IO_FAILURE);
    }
    
    kmip_set_buffer(ctx, encoding, buffer_total_size);
    ctx->index += 4;
    int length = 0;
    
    kmip_decode_int32_be(ctx, &length);
    kmip_rewind(ctx);
    if(length > ctx->max_message_size)
    {
        kmip_free_buffer(ctx, encoding, buffer_total_size);
        encoding = NULL;
        return(KMIP_EXCEED_MAX_MESSAGE_SIZE);
    }
    
    kmip_set_buffer(ctx, NULL, 0);
    uint8 *extended = ctx->realloc_func(
        ctx->state,
        encoding,
        buffer_total_size + length);
    if(encoding != extended)
    {
        encoding = extended;
    }
    ctx->memset_func(encoding + buffer_total_size, 0, length);
    
    buffer_block_size += length;
    buffer_total_size = buffer_blocks * buffer_block_size;
    
    recv = BIO_read(bio, encoding + 8, length);
    if(recv != length)
    {
        kmip_free_buffer(ctx, encoding, buffer_total_size);
        encoding = NULL;
        return(KMIP_IO_FAILURE);
    }
    
    kmip_set_buffer(ctx, encoding, buffer_block_size);
    
    /* Decode the response message and retrieve the operation result status. */
    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, encoding, buffer_total_size);
        encoding = NULL;
        return(decode_result);
    }
    
    kmip_free_buffer(ctx, encoding, buffer_total_size);
    encoding = NULL;

    if(resp_m.batch_count != 1 || resp_m.batch_items == NULL)
    {
        kmip_free_response_message(ctx, &resp_m);
        kmip_set_buffer(ctx, NULL, 0);
        return(KMIP_MALFORMED_RESPONSE);
    }
    
    ResponseBatchItem resp_item = resp_m.batch_items[0];
    enum result_status result = resp_item.result_status;
    
    kmip_set_last_result(&resp_item);
    
    if(result != KMIP_STATUS_SUCCESS)
    {
        kmip_free_response_message(ctx, &resp_m);
        kmip_set_buffer(ctx, NULL, 0);
        return(result);
    }
    
    GetResponsePayload *pld = (GetResponsePayload *)resp_item.response_payload;
    
    if(pld->object_type != KMIP_OBJTYPE_SYMMETRIC_KEY)
    {
        kmip_free_response_message(ctx, &resp_m);
        kmip_set_buffer(ctx, NULL, 0);
        return(KMIP_OBJECT_MISMATCH);
    }
    
    SymmetricKey *symmetric_key = (SymmetricKey *)pld->object;
    KeyBlock *block = symmetric_key->key_block;
    if((block->key_format_type != KMIP_KEYFORMAT_RAW) || 
       (block->key_wrapping_data != NULL))
    {
        kmip_free_response_message(ctx, &resp_m);
        kmip_set_buffer(ctx, NULL, 0);
        return(KMIP_OBJECT_MISMATCH);
    }
    
    KeyValue *block_value = block->key_value;
    ByteString *material = (ByteString *)block_value->key_material;
    
    char *result_key = ctx->calloc_func(ctx->state, 1, material->size);
    *key_size = material->size;
    for(int i = 0; i < *key_size; i++)
    {
        result_key[i] = material->value[i];
    }
    *key = result_key;
    
    /* Clean up the response message, the encoding buffer, and the KMIP */
    /* context. */
    kmip_free_response_message(ctx, &resp_m);
    kmip_free_buffer(ctx, encoding, buffer_total_size);
    encoding = NULL;
    kmip_set_buffer(ctx, NULL, 0);
    
    return(result);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movl %ecx, %r15d
movq %rdx, %r12
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
orb %al, %cl
testq %rdx, %rdx
sete %al
testl %r15d, %r15d
setle %dl
orb %al, %dl
orb %cl, %dl
testq %r8, %r8
sete %al
testq %r9, %r9
sete %cl
orb %al, %cl
orb %dl, %cl
movl $0xffffffef, %r13d      # imm = 0xFFFFFFEF
jne 0x1fa97
movq %r8, %rbp
movq %rdi, %rbx
movq %rsi, 0x8(%rsp)
movq %r9, 0x38(%rsp)
movq 0x890(%rdi), %rdi
movl $0x1, %esi
movl $0x400, %edx            # imm = 0x400
callq *0x868(%rbx)
testq %rax, %rax
je 0x1fa81
movq %rax, %r14
movq %rbp, 0x30(%rsp)
movl $0x400, %ebp            # imm = 0x400
movl $0x400, %edx            # imm = 0x400
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3b42
xorl %eax, %eax
leaq 0x48(%rsp), %rdi
movq %rax, (%rdi)
movl 0x18(%rbx), %esi
callq 0x3f58
xorps %xmm0, %xmm0
leaq 0xc0(%rsp), %r13
movaps %xmm0, 0x40(%r13)
movaps %xmm0, 0x30(%r13)
movaps %xmm0, 0x20(%r13)
movaps %xmm0, 0x10(%r13)
movaps %xmm0, (%r13)
movq %r13, %rdi
callq 0x3fee
leaq 0x48(%rsp), %rax
movq %rax, (%r13)
movl 0x1c(%rbx), %eax
movl %eax, 0x8(%r13)
xorl %edi, %edi
callq 0x2130
movq %rax, 0x20(%r13)
movl $0x1, 0x28(%r13)
leaq 0x70(%rsp), %rax
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movq %r12, (%rax)
movl %r15d, %ecx
movq %rcx, 0x8(%rax)
leaq 0xa0(%rsp), %r12
movaps %xmm0, (%r12)
movaps %xmm0, 0x10(%r12)
movq %rax, (%r12)
leaq 0x80(%rsp), %r15
movaps %xmm0, 0x10(%r15)
movaps %xmm0, (%r15)
movq %r15, %rdi
callq 0x4048
movl $0xa, (%r15)
movq %r12, 0x10(%r15)
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movq %r13, 0x50(%rsp)
movq %r15, 0x58(%rsp)
movq $0x1, 0x60(%rsp)
xorl %eax, %eax
movq %rax, 0x40(%rsp)
movq 0x20(%rbx), %rax
testq %rax, %rax
je 0x1f935
movq (%rax), %rax
testq %rax, %rax
je 0x1f935
movq 0x10(%rax), %rax
leaq 0x40(%rsp), %rcx
movq %rax, (%rcx)
movq %rcx, 0xd0(%rsp)
leaq 0x50(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0xbf1b
movl %eax, %r13d
movl $0x2, %r12d
cmpl $-0x2, %r13d
jne 0x1f9b5
movq %rbx, %rdi
callq 0x3a89
movq 0x890(%rbx), %rdi
movq %r14, %rsi
callq *0x878(%rbx)
movq 0x890(%rbx), %rdi
movl $0x400, %edx            # imm = 0x400
movq %r12, %rsi
callq *0x868(%rbx)
testq %rax, %rax
je 0x1fa81
movq %rax, %r14
addq $0x400, %rbp            # imm = 0x400
movq %rbx, %rdi
movq %rax, %rsi
movq %rbp, %rdx
callq 0x3b42
movq %rbx, %rdi
movq %r15, %rsi
callq 0xbf1b
movl %eax, %r13d
incq %r12
jmp 0x1f94e
testl %r13d, %r13d
jne 0x1fa89
movq (%rbx), %rsi
movl 0x8(%rbx), %edx
subl %esi, %edx
movq 0x8(%rsp), %rdi
callq 0x2150
movslq %eax, %r15
movq 0x8(%rbx), %r12
subq (%rbx), %r12
movq %rbx, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x4062
movl $0xfffffff3, %r13d      # imm = 0xFFFFFFF3
cmpq %r15, %r12
jne 0x1fa97
movq 0x890(%rbx), %rdi
movl $0x1, %esi
movl $0x8, %edx
callq *0x868(%rbx)
testq %rax, %rax
je 0x1fa81
movq %rax, %r14
movq 0x8(%rsp), %rdi
movq %rax, %rsi
movl $0x8, %edx
callq 0x2180
movl $0x8, %edx
cmpl $0x8, %eax
jne 0x1faac
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3b42
addq $0x4, 0x8(%rbx)
leaq 0x4(%rsp), %r15
movl $0x0, (%r15)
movq %rbx, %rdi
movq %r15, %rsi
callq 0xc2ab
movq %rbx, %rdi
callq 0x3aef
movl (%r15), %eax
cmpl 0x1c(%rbx), %eax
jle 0x1fab4
movl $0x8, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4062
movl $0xfffffff2, %r13d      # imm = 0xFFFFFFF2
jmp 0x1fa97
movl $0xfffffff4, %r13d      # imm = 0xFFFFFFF4
jmp 0x1fa97
movq %rbx, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x4062
movl %r13d, %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
movq %r14, %rsi
jmp 0x1fa92
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3b42
movq 0x890(%rbx), %rdi
movslq 0x4(%rsp), %rdx
addq $0x8, %rdx
movq %r14, %rsi
callq *0x870(%rbx)
movq %rax, %r15
leaq 0x8(%rax), %r12
movslq 0x4(%rsp), %rdx
movq %r12, %rdi
xorl %esi, %esi
callq *0x888(%rbx)
movslq 0x4(%rsp), %rdx
movq %rdx, %r14
addq $0x8, %r14
movq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x2180
cmpl 0x4(%rsp), %eax
jne 0x1fb5a
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x3b42
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rsi
movaps %xmm0, (%rsi)
movq $0x0, 0x10(%rsi)
movq %rbx, %rdi
callq 0x18f35
testl %eax, %eax
je 0x1fb73
movl %eax, %r13d
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x57fe
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
jmp 0x1fa92
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x4062
movl $0xfffffff3, %r13d      # imm = 0xFFFFFFF3
jmp 0x1fa97
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x4062
movq 0x18(%rsp), %rax
cmpq $0x1, 0x20(%rsp)
setne %cl
testq %rax, %rax
sete %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0x1fbc0
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x57fe
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3b42
movl $0xfffffff1, %r13d      # imm = 0xFFFFFFF1
jmp 0x1fa97
movl (%rax), %ecx
movl 0x10(%rax), %r13d
movl 0x14(%rax), %edx
movq 0x18(%rax), %rsi
movq 0x28(%rax), %r15
movl %ecx, 0xe56d(%rip)      # 0x2e144
movl %r13d, 0xe56a(%rip)     # 0x2e148
movl %edx, 0xe568(%rip)      # 0x2e14c
testq %rsi, %rsi
je 0x1fbfc
leaq 0xe560(%rip), %rdi      # 0x2e150
movl $0x80, %edx
callq 0x5f7c
jmp 0x1fc03
movb $0x0, 0xe54d(%rip)      # 0x2e150
testl %r13d, %r13d
je 0x1fc26
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x57fe
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3b42
jmp 0x1fa97
cmpl $0x2, (%r15)
jne 0x1fc3f
movq 0x10(%r15), %rax
movq (%rax), %rax
cmpl $0x1, (%rax)
jne 0x1fc3f
cmpq $0x0, 0x20(%rax)
je 0x1fc63
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x57fe
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3b42
movl $0xfffffff0, %r13d      # imm = 0xFFFFFFF0
jmp 0x1fa97
movq 0x8(%rax), %rax
movq (%rax), %r15
movq 0x890(%rbx), %rdi
movq 0x8(%r15), %rdx
movl $0x1, %esi
callq *0x868(%rbx)
movl 0x8(%r15), %ecx
movq 0x38(%rsp), %rsi
movl %ecx, (%rsi)
testl %ecx, %ecx
jle 0x1fca5
xorl %ecx, %ecx
movq (%r15), %rdx
movb (%rdx,%rcx), %dl
movb %dl, (%rax,%rcx)
incq %rcx
movslq (%rsi), %rdx
cmpq %rdx, %rcx
jl 0x1fc91
movq 0x30(%rsp), %rcx
movq %rax, (%rcx)
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x57fe
xorl %r13d, %r13d
movq %rbx, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x4062
jmp 0x1fc15