// File: /workspace/github_c_makefile_star2_O1/OpenKMIP[P]libkmip/src/kmip_bio.c
// Function: kmip_bio_destroy_symmetric_key
int kmip_bio_destroy_symmetric_key(BIO *bio, char *uuid, int uuid_size)
{
    if(bio == NULL || uuid == NULL || uuid_size <= 0)
    {
        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 id = {0};
    id.value = uuid;
    id.size = uuid_size;
    
    DestroyRequestPayload drp = {0};
    drp.unique_identifier = &id;
    
    RequestBatchItem rbi = {0};
    kmip_init_request_batch_item(&rbi);
    rbi.operation = KMIP_OP_DESTROY;
    rbi.request_payload = &drp;
    
    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(extended == NULL)
    {
        kmip_free_buffer(&ctx, encoding, buffer_total_size);
        encoding = NULL;
        kmip_destroy(&ctx);
        return(KMIP_MEMORY_ALLOC_FAILED);
    }
    else
    {
        encoding = extended;
        extended = NULL;
    }
    
    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);
    }

    if(resp_m.batch_count != 1 || resp_m.batch_items == NULL)
    {
        kmip_free_response_message(&ctx, &resp_m);
        kmip_free_buffer(&ctx, encoding, buffer_total_size);
        encoding = NULL;
        kmip_destroy(&ctx);
        return(KMIP_MALFORMED_RESPONSE);
    }
    
    ResponseBatchItem resp_item = resp_m.batch_items[0];
    enum result_status result = resp_item.result_status;
    
    /* 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 $0x978, %rsp            # imm = 0x978
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
orb %al, %cl
testl %edx, %edx
setle %al
orb %cl, %al
movl $0xffffffef, %ebp       # imm = 0xFFFFFFEF
jne 0x1eaf2
movl %edx, %r13d
movq %rsi, %rbx
movq %rdi, %r12
leaq 0xe0(%rsp), %r15
movl $0x898, %edx            # imm = 0x898
movq %r15, %rdi
xorl %esi, %esi
callq 0x20e0
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x3877
movq 0x890(%r15), %rdi
movl $0x400, %r14d           # imm = 0x400
movl $0x1, %esi
movl $0x400, %edx            # imm = 0x400
callq *0x868(%r15)
testq %rax, %rax
je 0x1eaa7
movq %rax, %r15
movq %r12, 0x8(%rsp)
leaq 0xe0(%rsp), %r12
movl $0x400, %edx            # imm = 0x400
movq %r12, %rdi
movq %rax, %rsi
callq 0x3b42
leaq 0x30(%rsp), %rdi
movq $0x0, (%rdi)
movl 0x18(%r12), %esi
callq 0x3f58
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 0x3fee
leaq 0x30(%rsp), %rax
movq %rax, (%rbp)
movl 0x1c(%r12), %eax
movl %eax, 0x8(%rbp)
xorl %edi, %edi
callq 0x2130
movq %rax, 0x20(%rbp)
movl $0x1, 0x28(%rbp)
leaq 0x40(%rsp), %rax
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movq %rbx, (%rax)
movl %r13d, %ecx
movq %rcx, 0x8(%rax)
leaq 0x38(%rsp), %rbx
movq %rax, (%rbx)
leaq 0x70(%rsp), %r13
movaps %xmm0, 0x10(%r13)
movaps %xmm0, (%r13)
movq %r13, %rdi
callq 0x4048
movl $0x14, (%r13)
movq %rbx, 0x10(%r13)
leaq 0x50(%rsp), %rbx
xorps %xmm0, %xmm0
movaps %xmm0, (%rbx)
movq %rbp, (%rbx)
movq %r13, 0x8(%rbx)
movq $0x1, 0x10(%rbx)
movq %r12, %rdi
movq %rbx, %rsi
callq 0xbf1b
movl %eax, %ebp
movl $0x2, %r13d
cmpl $-0x2, %ebp
jne 0x1e9b1
movq %r12, %rdi
callq 0x3a89
movq 0x970(%rsp), %rdi
movq %r15, %rsi
callq *0x958(%rsp)
movq 0x970(%rsp), %rdi
movl $0x400, %edx            # imm = 0x400
movq %r13, %rsi
callq *0x948(%rsp)
testq %rax, %rax
je 0x1eaa7
movq %rax, %r15
addq $0x400, %r14            # imm = 0x400
movq %r12, %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0x3b42
movq %r12, %rdi
movq %rbx, %rsi
callq 0xbf1b
movl %eax, %ebp
incq %r13
jmp 0x1e948
leaq 0xe0(%rsp), %rbx
testl %ebp, %ebp
jne 0x1eabb
movq (%rbx), %rsi
movl 0x8(%rbx), %edx
subl %esi, %edx
movq 0x8(%rsp), %rdi
callq 0x2150
movslq %eax, %r12
movq 0x8(%rbx), %r13
subq (%rbx), %r13
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x4062
cmpq %r12, %r13
jne 0x1ead3
movq 0x970(%rsp), %rdi
movl $0x1, %esi
movl $0x8, %edx
callq *0x948(%rsp)
testq %rax, %rax
je 0x1eaa7
movq %rax, %rbx
movq 0x8(%rsp), %r12
movq %r12, %rdi
movq %rax, %rsi
movl $0x8, %edx
callq 0x2180
leaq 0xe0(%rsp), %r14
movl $0x8, %edx
movq %r14, %rdi
cmpl $0x8, %eax
jne 0x1eadd
movq %rbx, %rsi
callq 0x3b42
addq $0x4, 0x8(%r14)
leaq 0x4(%rsp), %r15
movl $0x0, (%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0xc2ab
movq %r14, %rdi
callq 0x3aef
movl (%r15), %eax
cmpl 0x1c(%r14), %eax
jle 0x1eb06
leaq 0xe0(%rsp), %r14
movl $0x8, %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4062
movq %r14, %rdi
callq 0x3b53
movl $0xfffffff2, %ebp       # imm = 0xFFFFFFF2
jmp 0x1eaf2
leaq 0xe0(%rsp), %rdi
callq 0x3b53
movl $0xfffffff4, %ebp       # imm = 0xFFFFFFF4
jmp 0x1eaf2
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x4062
movq %rbx, %rdi
callq 0x3b53
jmp 0x1eaf2
leaq 0xe0(%rsp), %rdi
jmp 0x1eae8
movq %rbx, %rsi
callq 0x4062
movq %r14, %rdi
callq 0x3b53
movl $0xfffffff3, %ebp       # imm = 0xFFFFFFF3
movl %ebp, %eax
addq $0x978, %rsp            # imm = 0x978
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xe0(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3b42
movq 0x890(%r14), %rdi
movslq 0x4(%rsp), %rdx
addq $0x8, %rdx
movq %rbx, %rsi
callq *0x870(%r14)
testq %rax, %rax
je 0x1ebd4
movq %rax, %r14
movq %rax, %r15
addq $0x8, %r15
movslq 0x4(%rsp), %rdx
movq %r15, %rdi
xorl %esi, %esi
callq *0x968(%rsp)
movslq 0x4(%rsp), %rdx
movq %rdx, %rbx
addq $0x8, %rbx
movq %r12, %rdi
movq %r15, %rsi
callq 0x2180
cmpl 0x4(%rsp), %eax
jne 0x1ebf4
leaq 0xe0(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x3b42
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rsi
movaps %xmm0, (%rsi)
movq $0x0, 0x10(%rsi)
movq %r15, %rdi
callq 0x18f35
testl %eax, %eax
je 0x1ec12
movl %eax, %ebp
leaq 0xe0(%rsp), %r15
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x57fe
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x4062
jmp 0x1ec89
leaq 0xe0(%rsp), %r14
movl $0x8, %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4062
movq %r14, %rdi
jmp 0x1eaaf
leaq 0xe0(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x4062
movq %r15, %rdi
jmp 0x1eae8
movq 0x18(%rsp), %rax
cmpq $0x1, 0x20(%rsp)
setne %cl
testq %rax, %rax
sete %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0x1ec57
leaq 0xe0(%rsp), %r15
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x57fe
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x4062
movl $0xfffffff1, %ebp       # imm = 0xFFFFFFF1
jmp 0x1ec89
movl 0x10(%rax), %ebp
leaq 0xe0(%rsp), %r15
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x57fe
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x4062
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3b42
leaq 0xe0(%rsp), %rdi
jmp 0x1eacc