// File: /workspace/github_c_makefile_star2_O1/OpenKMIP[P]libkmip/demos/demo_get.c
// Function: use_mid_level_api
int
use_mid_level_api(char *server_address,
                  char *server_port,
                  char *client_certificate,
                  char *client_key,
                  char *ca_certificate,
                  char *username,
                  char *password,
                  char *id)
{
    /* Set up the TLS connection to the KMIP server. */
    SSL_CTX *ctx = NULL;
    SSL *ssl = NULL;
    OPENSSL_init_ssl(0, NULL);
    ctx = SSL_CTX_new(TLS_client_method());
    
    printf("\n");
    printf("Loading the client certificate: %s\n", client_certificate);
    if(SSL_CTX_use_certificate_file(ctx, client_certificate, SSL_FILETYPE_PEM) != 1)
    {
        fprintf(stderr, "Loading the client certificate failed\n");
        ERR_print_errors_fp(stderr);
        SSL_CTX_free(ctx);
        return(-1);
    }
    
    printf("Loading the client key: %s\n", client_key);
    if(SSL_CTX_use_PrivateKey_file(ctx, client_key, SSL_FILETYPE_PEM) != 1)
    {
        fprintf(stderr, "Loading the client key failed\n");
        ERR_print_errors_fp(stderr);
        SSL_CTX_free(ctx);
        return(-1);
    }
    
    printf("Loading the CA certificate: %s\n", ca_certificate);
    if(SSL_CTX_load_verify_locations(ctx, ca_certificate, NULL) != 1)
    {
        fprintf(stderr, "Loading the CA file failed\n");
        ERR_print_errors_fp(stderr);
        SSL_CTX_free(ctx);
        return(-1);
    }
    
    BIO *bio = NULL;
    bio = BIO_new_ssl_connect(ctx);
    if(bio == NULL)
    {
        printf("BIO_new_ssl_connect failed\n");
        SSL_CTX_free(ctx);
        return(-1);
    }
    
    BIO_get_ssl(bio, &ssl);
    SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
    BIO_set_conn_hostname(bio, server_address);
    BIO_set_conn_port(bio, server_port);

    if(BIO_do_connect(bio) != 1)
    {
        fprintf(stderr, "BIO_do_connect failed\n");
        ERR_print_errors_fp(stderr);
        BIO_free_all(bio);
        SSL_CTX_free(ctx);
        return(-1);
    }
    
    printf("\n");
    
    char *key = NULL;
    int key_size = 0;
    size_t id_size = kmip_strnlen_s(id, 50);
    
    /* Set up the KMIP context and send the request message. */
    KMIP kmip_context = {0};
    
    kmip_context.calloc_func = &demo_calloc;
    kmip_context.realloc_func = &demo_realloc;
    kmip_context.free_func = &demo_free;
    
    kmip_init(&kmip_context, NULL, 0, KMIP_1_0);
    
    TextString u = {0};
    u.value = username;
    u.size = kmip_strnlen_s(username, 50);
    
    TextString p = {0};
    p.value = password;
    p.size = kmip_strnlen_s(password, 50);
    
    UsernamePasswordCredential upc = {0};
    upc.username = &u;
    upc.password = &p;
    
    Credential credential = {0};
    credential.credential_type = KMIP_CRED_USERNAME_AND_PASSWORD;
    credential.credential_value = &upc;
    
    int result = kmip_add_credential(&kmip_context, &credential);
    
    if(result != KMIP_OK)
    {
        printf("Failed to add credential to the KMIP context.\n");
        BIO_free_all(bio);
        SSL_CTX_free(ctx);
        kmip_destroy(&kmip_context);
        return(result);
    }
    
    result = kmip_bio_get_symmetric_key_with_context(&kmip_context, bio, id, id_size, &key, &key_size);
    
    BIO_free_all(bio);
    SSL_CTX_free(ctx);
    
    /* Handle the response results. */
    printf("\n");
    if(result < 0)
    {
        printf("An error occurred while creating the symmetric key.");
        printf("Error Code: %d\n", result);
        printf("Error Name: ");
        kmip_print_error_string(stderr, result);
        printf("\n");
        printf("Context Error: %s\n", kmip_context.error_message);
        printf("Stack trace:\n");
        kmip_print_stack_trace(stderr, &kmip_context);
    }
    else if(result >= 0)
    {
        printf("The KMIP operation was executed with no errors.\n");
        printf("Result: ");
        kmip_print_result_status_enum(stdout, result);
        printf(" (%d)\n", result);
        
        if(result == KMIP_STATUS_SUCCESS)
        {
            printf("Symmetric Key ID: %s\n", id);
            printf("Symmetric Key Size: %d bits\n", key_size * 8);
            printf("Symmetric Key:");
            kmip_print_buffer(stdout, key, key_size);
            printf("\n");
        }
    }
    
    printf("\n");
    
    if(key != NULL)
    {
        kmip_memset(key, 0, key_size);
        kmip_free(NULL, key);
    }
    
    /* Clean up the KMIP context and return the results. */
    kmip_destroy(&kmip_context);
    return(result);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x908, %rsp            # imm = 0x908
movq %r9, 0x20(%rsp)
movq %r8, %r15
movq %rcx, %rbp
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %r13
movq $0x0, 0x28(%rsp)
xorl %edi, %edi
xorl %esi, %esi
callq 0x2120
callq 0x20b0
movq %rax, %rdi
callq 0x21f0
movq %rax, %rbx
movl $0xa, %edi
callq 0x21e0
leaq 0x1e937(%rip), %rdi     # 0x2110b
movq %r14, %rsi
xorl %eax, %eax
callq 0x2040
movq %rbx, %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0x21d0
cmpl $0x1, %eax
jne 0x2ba8
leaq 0x1e958(%rip), %rdi     # 0x21156
movq %rbp, %rsi
xorl %eax, %eax
callq 0x2040
movq %rbx, %rdi
movq %rbp, %rsi
movl $0x1, %edx
callq 0x2030
cmpl $0x1, %eax
jne 0x2bb5
leaq 0x1e969(%rip), %rdi     # 0x21191
movq %r15, %rsi
xorl %eax, %eax
callq 0x2040
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x21c0
cmpl $0x1, %eax
jne 0x2bc2
movq %rbx, %rdi
callq 0x2160
testq %rax, %rax
je 0x29ec
movq %rax, %r15
leaq 0x28(%rsp), %r14
movq %rax, %rdi
movl $0x6e, %esi
xorl %edx, %edx
movq %r14, %rcx
callq 0x2060
movq (%r14), %rdi
movl $0x4, %edx
movl $0x21, %esi
xorl %ecx, %ecx
callq 0x2070
movq %r15, %rdi
movl $0x64, %esi
xorl %edx, %edx
movq %r13, %rcx
callq 0x2060
movl $0x1, %edx
movq %r15, %rdi
movl $0x64, %esi
movq %r12, %rcx
callq 0x2060
movq %r15, %rdi
movl $0x65, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2060
cmpq $0x1, %rax
jne 0x2bcf
movq 0x948(%rsp), %r14
movq 0x940(%rsp), %rbp
movl $0xa, %edi
callq 0x21e0
movq $0x0, 0x10(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x32, %esi
movq %r14, %rdi
callq 0x2ca8
movq %rax, 0x18(%rsp)
leaq 0x70(%rsp), %r12
movl $0x898, %edx            # imm = 0x898
movq %r12, %rdi
xorl %esi, %esi
callq 0x20e0
leaq -0x22c(%rip), %rax      # 0x26f5
movq %rax, 0x868(%r12)
leaq -0x208(%rip), %rax      # 0x2728
movq %rax, 0x870(%r12)
leaq -0x1e4(%rip), %rax      # 0x275b
movq %rax, 0x878(%r12)
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x35f7
xorps %xmm0, %xmm0
leaq 0x60(%rsp), %r14
movaps %xmm0, (%r14)
movq 0x20(%rsp), %rdi
movq %rdi, (%r14)
movl $0x32, %esi
callq 0x2ca8
movq %rax, 0x8(%r14)
leaq 0x50(%rsp), %r13
xorps %xmm0, %xmm0
movaps %xmm0, (%r13)
movq %rbp, (%r13)
movl $0x32, %esi
movq %rbp, %rdi
callq 0x2ca8
movq %rax, 0x8(%r13)
leaq 0x40(%rsp), %rax
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movq %r14, (%rax)
movq %r13, 0x8(%rax)
leaq 0x30(%rsp), %rsi
movaps %xmm0, (%rsi)
movl $0x1, (%rsi)
movq %rax, 0x8(%rsi)
movq %r12, %rdi
callq 0x3736
testl %eax, %eax
je 0x2a0a
movl %eax, %ebp
leaq 0x1ea94(%rip), %rdi     # 0x21466
callq 0x2210
movq %r15, %rdi
callq 0x2080
movq %rbx, %rdi
callq 0x2050
jmp 0x2b8a
leaq 0x1eaa1(%rip), %rdi     # 0x21494
callq 0x2210
movq %rbx, %rdi
callq 0x2050
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x2b94
leaq 0x70(%rsp), %rdi
leaq 0x10(%rsp), %r8
leaq 0xc(%rsp), %r9
movq %r15, %rsi
movq 0x948(%rsp), %r14
movq %r14, %rdx
movq 0x18(%rsp), %rcx
callq 0x1f51f
movl %eax, %ebp
movq %r15, %rdi
callq 0x2080
movq %rbx, %rdi
callq 0x2050
movl $0xa, %edi
callq 0x21e0
testl %ebp, %ebp
js 0x2ae9
leaq 0x1e9cd(%rip), %rdi     # 0x21429
callq 0x2210
leaq 0x1e7e0(%rip), %rdi     # 0x21248
xorl %eax, %eax
callq 0x2040
movq 0x2b54a(%rip), %rbx     # 0x2dfc0
movq (%rbx), %rdi
movl %ebp, %esi
callq 0x1a4ef
leaq 0x1e7ca(%rip), %rdi     # 0x21251
movl %ebp, %esi
xorl %eax, %eax
callq 0x2040
testl %ebp, %ebp
jne 0x2b5e
leaq 0x1e7b9(%rip), %rdi     # 0x21258
movq %r14, %rsi
xorl %eax, %eax
callq 0x2040
movl 0xc(%rsp), %esi
shll $0x3, %esi
leaq 0x1e7b7(%rip), %rdi     # 0x2126e
xorl %eax, %eax
callq 0x2040
leaq 0x1e7c6(%rip), %rdi     # 0x2128b
xorl %eax, %eax
callq 0x2040
movq (%rbx), %rdi
movq 0x10(%rsp), %rsi
movl 0xc(%rsp), %edx
callq 0x19f5c
movl $0xa, %edi
callq 0x21e0
jmp 0x2b5e
leaq 0x1e6f4(%rip), %rdi     # 0x211e4
xorl %eax, %eax
callq 0x2040
leaq 0x1e71a(%rip), %rdi     # 0x21218
movl %ebp, %esi
xorl %eax, %eax
callq 0x2040
leaq 0x1e71a(%rip), %rdi     # 0x21228
xorl %eax, %eax
callq 0x2040
movq 0x2b4bc(%rip), %r14     # 0x2dfd8
movq (%r14), %rdi
movl %ebp, %esi
callq 0x1a024
movl $0xa, %edi
callq 0x21e0
leaq 0x70(%rsp), %rbx
movq 0x28(%rbx), %rsi
leaq 0x1e6f5(%rip), %rdi     # 0x21235
xorl %eax, %eax
callq 0x2040
leaq 0x1e90b(%rip), %rdi     # 0x21459
callq 0x2210
movq (%r14), %rdi
movq %rbx, %rsi
callq 0x19fd2
movl $0xa, %edi
callq 0x21e0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x2b8a
movslq 0xc(%rsp), %rdx
xorl %esi, %esi
callq 0x19f40
movq 0x10(%rsp), %rsi
xorl %edi, %edi
callq 0x2dbd
leaq 0x70(%rsp), %rdi
callq 0x38d3
movl %ebp, %eax
addq $0x908, %rsp            # imm = 0x908
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x2260
jmp 0x2a00
movq %rbx, %rdi
callq 0x229a
jmp 0x2a00
movq %rbx, %rdi
callq 0x22d4
jmp 0x2a00
movq %r15, %rdi
movq %rbx, %rsi
callq 0x230e
jmp 0x2a00