// File: /workspace/github_c_makefile_star2_O2/OpenKMIP[P]libkmip/src/kmip_bio.c
// Function: kmip_bio_get_symmetric_key
int kmip_bio_get_symmetric_key(BIO *bio,
                               char *id, int id_size,
                               char **key, int *key_size)
{
    if(bio == NULL || id == NULL || id_size <= 0 || key == NULL || key_size == NULL)
    {
        return(KMIP_ARG_INVALID);
    }
    
    /* Set up the KMIP context and the initial encoding buffer. */
    KMIP ctx = {0};
    kmip_init(&ctx, NULL, 0, KMIP_1_0);
    
    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);
    }
    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 uuid = {0};
    uuid.value = id;
    uuid.size = id_size;
    
    GetRequestPayload grp = {0};
    grp.unique_identifier = &uuid;
    
    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;
    
    /* 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)
        {
            kmip_destroy(&ctx);
            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_destroy(&ctx);
        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;
        kmip_destroy(&ctx);
        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)
    {
        kmip_destroy(&ctx);
        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;
        kmip_destroy(&ctx);
        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;
        kmip_destroy(&ctx);
        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;
        kmip_destroy(&ctx);
        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;
        kmip_destroy(&ctx);
        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);
        kmip_destroy(&ctx);
        return(KMIP_MALFORMED_RESPONSE);
    }
    
    ResponseBatchItem resp_item = resp_m.batch_items[0];
    enum result_status result = resp_item.result_status;
    
    if(result != KMIP_STATUS_SUCCESS)
    {
        kmip_free_response_message(&ctx, &resp_m);
        kmip_set_buffer(&ctx, NULL, 0);
        kmip_destroy(&ctx);
        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);
        kmip_destroy(&ctx);
        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);
        kmip_destroy(&ctx);
        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);
    kmip_destroy(&ctx);
    
    return(result);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x9a8, %rsp            # imm = 0x9A8
movq %rcx, %r15
movq %rsi, %xmm0
movq %rdi, %xmm1
punpcklqdq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0]
movq %r8, %xmm0
movq %rcx, %xmm2
punpcklqdq %xmm0, %xmm2    # xmm2 = xmm2[0],xmm0[0]
pxor %xmm0, %xmm0
testl %edx, %edx
setle %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 0x16c37
pushq $-0x11
jmp 0x16ed2
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, 0x28(%rsp)
movq %r8, 0x38(%rsp)
leaq 0xb8(%rsp), %rbx
movl $0x898, %edx            # imm = 0x898
movq %rbx, %rdi
xorl %esi, %esi
callq 0x20e0
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x34a1
movq 0x890(%rbx), %rdi
pushq $0x1
popq %rsi
movl $0x400, %r12d           # imm = 0x400
movl $0x400, %edx            # imm = 0x400
callq *0x868(%rbx)
testq %rax, %rax
je 0x16ec3
movq %rax, %r13
movq %r15, 0x30(%rsp)
leaq 0xb8(%rsp), %rbx
movl $0x400, %edx            # imm = 0x400
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3681
leaq 0x40(%rsp), %rdi
andq $0x0, (%rdi)
movl 0x18(%rbx), %esi
callq 0x39f2
xorps %xmm0, %xmm0
leaq 0x950(%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 0x3a7a
leaq 0x40(%rsp), %rax
movq %rax, (%r15)
movl 0x1c(%rbx), %eax
movl %eax, 0x8(%r15)
xorl %edi, %edi
callq 0x2130
movq %rax, 0x20(%r15)
movl $0x1, 0x28(%r15)
leaq 0x48(%rsp), %rax
movq %r14, (%rax)
movl %ebp, %ecx
movq %rcx, 0x8(%rax)
leaq 0x98(%rsp), %r14
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%r14)
andq $0x0, 0x18(%r14)
movq %rax, (%r14)
leaq 0x70(%rsp), %rbp
movaps %xmm0, 0x10(%rbp)
movaps %xmm0, (%rbp)
movq %rbp, %rdi
callq 0x3acd
movl $0xa, (%rbp)
movq %r14, 0x10(%rbp)
leaq 0x58(%rsp), %r14
movq %r15, (%r14)
movq %rbp, 0x8(%r14)
movq $0x1, 0x10(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xa872
movl %eax, %ebp
pushq $0x2
popq %r15
cmpl $-0x2, %ebp
jne 0x16de1
movq %rbx, %rdi
callq 0x363e
movq 0x948(%rsp), %rdi
movq %r13, %rsi
callq *0x930(%rsp)
movq 0x948(%rsp), %rdi
movl $0x400, %edx            # imm = 0x400
movq %r15, %rsi
callq *0x920(%rsp)
testq %rax, %rax
je 0x16ec3
movq %rax, %r13
addq $0x400, %r12            # imm = 0x400
movq %rbx, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x3681
movq %rbx, %rdi
movq %r14, %rsi
callq 0xa872
movl %eax, %ebp
incq %r15
jmp 0x16d78
leaq 0xb8(%rsp), %rbx
testl %ebp, %ebp
jne 0x16ee7
movq (%rbx), %rsi
movl 0x8(%rbx), %edx
subl %esi, %edx
movq 0x28(%rsp), %rdi
callq 0x2150
movslq %eax, %r14
movq 0x8(%rbx), %r15
subq (%rbx), %r15
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x3ae1
cmpq %r14, %r15
jne 0x16eff
movq 0x948(%rsp), %rdi
pushq $0x1
popq %rsi
pushq $0x8
popq %rdx
callq *0x920(%rsp)
testq %rax, %rax
je 0x16ec3
movq %rax, %rbx
pushq $0x8
popq %rdx
movq 0x28(%rsp), %r13
movq %r13, %rdi
movq %rax, %rsi
callq 0x2180
leaq 0xb8(%rsp), %r14
pushq $0x8
popq %rdx
movq %r14, %rdi
cmpl $0x8, %eax
jne 0x16f09
movq %rbx, %rsi
callq 0x3681
addq $0x4, 0x8(%r14)
leaq 0xc(%rsp), %r15
andl $0x0, (%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0xaaec
movq %r14, %rdi
callq 0x366f
movl (%r15), %eax
cmpl 0x1c(%r14), %eax
jle 0x16f1d
leaq 0xb8(%rsp), %r14
pushq $0x8
popq %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3ae1
movq %r14, %rdi
callq 0x3692
pushq $-0xe
jmp 0x16ed2
leaq 0xb8(%rsp), %rdi
callq 0x3692
pushq $-0xc
popq %rbp
movl %ebp, %eax
addq $0x9a8, %rsp            # imm = 0x9A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x3ae1
movq %rbx, %rdi
callq 0x3692
jmp 0x16ed3
leaq 0xb8(%rsp), %rdi
jmp 0x16f14
movq %rbx, %rsi
callq 0x3ae1
movq %r14, %rdi
callq 0x3692
pushq $-0xd
jmp 0x16ed2
leaq 0xb8(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3681
movq 0x890(%r14), %rdi
movslq 0xc(%rsp), %rdx
addq $0x8, %rdx
movq %rbx, %rsi
callq *0x870(%r14)
movq %rax, %rbx
leaq 0x8(%rax), %r15
movslq 0xc(%rsp), %rdx
movq %r15, %rdi
xorl %esi, %esi
callq *0x888(%r14)
movslq 0xc(%rsp), %rdx
movq %rdx, %r12
addq $0x8, %r12
movq %r13, %rdi
movq %r15, %rsi
callq 0x2180
cmpl 0xc(%rsp), %eax
jne 0x16fdc
leaq 0xb8(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x3681
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rsi
movaps %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
movq %r14, %rdi
callq 0x11a14
testl %eax, %eax
je 0x16ff2
movl %eax, %ebp
leaq 0xb8(%rsp), %r14
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x4e32
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x3ae1
jmp 0x170af
leaq 0xb8(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
jmp 0x16f0c
leaq 0xb8(%rsp), %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x3ae1
movq 0x18(%rsp), %rax
cmpq $0x1, 0x20(%rsp)
setne %cl
testq %rax, %rax
sete %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0x17045
leaq 0xb8(%rsp), %rbx
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x4e32
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3681
pushq $-0xf
jmp 0x170ae
movl 0x10(%rax), %ebp
testl %ebp, %ebp
je 0x1706f
leaq 0xb8(%rsp), %rbx
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x4e32
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3681
jmp 0x170af
movq 0x28(%rax), %rax
cmpl $0x2, (%rax)
jne 0x1708b
movq 0x10(%rax), %rax
movq (%rax), %rax
cmpl $0x1, (%rax)
jne 0x1708b
cmpq $0x0, 0x20(%rax)
je 0x170bc
leaq 0xb8(%rsp), %rbx
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x4e32
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3681
pushq $-0x10
popq %rbp
leaq 0xb8(%rsp), %rdi
jmp 0x16ef8
movq 0x8(%rax), %rax
movq (%rax), %rbx
movq 0x948(%rsp), %rdi
movq 0x8(%rbx), %rdx
pushq $0x1
popq %rsi
callq *0x920(%rsp)
movl 0x8(%rbx), %edx
movq 0x38(%rsp), %rsi
movl %edx, (%rsi)
xorl %ecx, %ecx
movslq %edx, %rdx
cmpq %rdx, %rcx
jge 0x170fd
movq (%rbx), %rdx
movb (%rdx,%rcx), %dl
movb %dl, (%rax,%rcx)
incq %rcx
movl (%rsi), %edx
jmp 0x170e5
movq 0x30(%rsp), %rcx
movq %rax, (%rcx)
leaq 0xb8(%rsp), %rbx
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x4e32
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x3ae1
jmp 0x17061