// File: /workspace/github_c_makefile_star2_O1/OpenKMIP[P]libkmip/src/kmip_bio.c
// Function: kmip_bio_send_request_encoding
int kmip_bio_send_request_encoding(KMIP *ctx, BIO *bio,
                                   char *request, int request_size,
                                   char **response, int *response_size)
{
    if(ctx == NULL || bio == NULL || request == NULL || request_size <= 0 || response == NULL || response_size == NULL)
    {
        return(KMIP_ARG_INVALID);
    }
    
    /* Send the request message. */
    int sent = BIO_write(bio, request, request_size);
    if(sent != request_size)
    {
        return(KMIP_IO_FAILURE);
    }
    
    /* Read the response message. Dynamically resize the receiving buffer */
    /* to align with the message size advertised by the message encoding. */
    /* Reject the message if the message size is too large.               */
    size_t buffer_blocks = 1;
    size_t buffer_block_size = 8;
    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);
    }
    
    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;
        kmip_set_buffer(ctx, NULL, 0);
        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_set_buffer(ctx, NULL, 0);
        return(KMIP_IO_FAILURE);
    }
    
    *response_size = buffer_total_size;
    *response = (char *)encoding;
    
    kmip_set_buffer(ctx, NULL, 0);
    
    return(KMIP_OK);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %ecx, %ebp
movq %rsi, %r12
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
orb %al, %cl
testq %rdx, %rdx
sete %al
testl %ebp, %ebp
setle %sil
orb %al, %sil
orb %cl, %sil
testq %r8, %r8
sete %al
testq %r9, %r9
sete %cl
orb %al, %cl
orb %sil, %cl
movl $0xffffffef, %r13d      # imm = 0xFFFFFFEF
jne 0x202c4
movq %r9, %r15
movq %r8, %r14
movq %rdi, %rbx
movq %r12, %rdi
movq %rdx, %rsi
movl %ebp, %edx
callq 0x2150
movl $0xfffffff3, %r13d      # imm = 0xFFFFFFF3
cmpl %ebp, %eax
jne 0x202c4
movq 0x890(%rbx), %rdi
movl $0x1, %esi
movl $0x8, %edx
callq *0x868(%rbx)
testq %rax, %rax
je 0x20218
movq %rax, %rbp
movq %r12, %rdi
movq %rax, %rsi
movl $0x8, %edx
callq 0x2180
movl $0x8, %edx
cmpl $0x8, %eax
jne 0x20223
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x3b42
addq $0x4, 0x8(%rbx)
leaq 0x4(%rsp), %r13
movl $0x0, (%r13)
movq %rbx, %rdi
movq %r13, %rsi
callq 0xc2ab
movq %rbx, %rdi
callq 0x3aef
movl (%r13), %eax
cmpl 0x1c(%rbx), %eax
jle 0x20233
movl $0x8, %edx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x4062
movl $0xfffffff2, %r13d      # imm = 0xFFFFFFF2
jmp 0x202b8
movl $0xfffffff4, %r13d      # imm = 0xFFFFFFF4
jmp 0x202c4
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x4062
jmp 0x202c4
xorl %r13d, %r13d
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3b42
movq 0x890(%rbx), %rdi
movslq 0x4(%rsp), %rdx
addq $0x8, %rdx
movq %rbp, %rsi
callq *0x870(%rbx)
movq %rax, 0x8(%rsp)
leaq 0x8(%rax), %rdi
movq %rdi, 0x10(%rsp)
movslq 0x4(%rsp), %rdx
xorl %esi, %esi
callq *0x888(%rbx)
movslq 0x4(%rsp), %rdx
movq %rdx, %rbp
addq $0x8, %rbp
movq %r12, %rdi
movq 0x10(%rsp), %rsi
callq 0x2180
cmpl 0x4(%rsp), %eax
jne 0x202a2
movl %ebp, (%r15)
movq 0x8(%rsp), %rax
movq %rax, (%r14)
jmp 0x202b8
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movq %rbp, %rdx
callq 0x4062
movl $0xfffffff3, %r13d      # imm = 0xFFFFFFF3
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3b42
movl %r13d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq