// File: /workspace/github_c_makefile_star2_O2/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
andq $0x0, 0x28(%rsp)
xorl %edi, %edi
xorl %esi, %esi
callq 0x2120
callq 0x20b0
movq %rax, %rdi
callq 0x21f0
movq %rax, %rbx
pushq $0xa
popq %rdi
callq 0x21e0
leaq 0x16aff(%rip), %rdi     # 0x190d4
movq %r14, %rsi
xorl %eax, %eax
callq 0x2040
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x21d0
cmpl $0x1, %eax
jne 0x2950
leaq 0x16b22(%rip), %rdi     # 0x1911f
movq %rbp, %rsi
xorl %eax, %eax
callq 0x2040
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x2030
cmpl $0x1, %eax
jne 0x2965
leaq 0x16b35(%rip), %rdi     # 0x1915a
movq %r15, %rsi
xorl %eax, %eax
callq 0x2040
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x21c0
cmpl $0x1, %eax
jne 0x297a
movq %rbx, %rdi
callq 0x2160
testq %rax, %rax
je 0x27bb
movq %rax, %r15
pushq $0x6e
popq %rsi
leaq 0x28(%rsp), %r14
movq %rax, %rdi
xorl %edx, %edx
movq %r14, %rcx
callq 0x2060
movq (%r14), %rdi
pushq $0x21
popq %rsi
pushq $0x4
popq %rdx
xorl %ecx, %ecx
callq 0x2070
pushq $0x64
popq %rbp
movq %r15, %rdi
movl %ebp, %esi
xorl %edx, %edx
movq %r13, %rcx
callq 0x2060
pushq $0x1
popq %rdx
movq %r15, %rdi
movl %ebp, %esi
movq %r12, %rcx
callq 0x2060
pushq $0x65
popq %rsi
movq %r15, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2060
cmpq $0x1, %rax
jne 0x29bd
movq 0x948(%rsp), %r14
pushq $0xa
popq %rdi
callq 0x21e0
andq $0x0, 0x10(%rsp)
andl $0x0, 0xc(%rsp)
pushq $0x32
popq %r13
movq %r14, %rdi
movq %r13, %rsi
callq 0x2ab4
movq %rax, 0x18(%rsp)
leaq 0x70(%rsp), %r12
movl $0x898, %edx            # imm = 0x898
movq %r12, %rdi
xorl %esi, %esi
callq 0x20e0
leaq -0x208(%rip), %rax      # 0x24fb
movq %rax, 0x868(%r12)
leaq -0x1e4(%rip), %rax      # 0x252e
movq %rax, 0x870(%r12)
leaq -0x1c0(%rip), %rax      # 0x2561
movq %rax, 0x878(%r12)
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x320d
leaq 0x60(%rsp), %r14
movq 0x20(%rsp), %rdi
movq %rdi, (%r14)
movq %r13, %rsi
callq 0x2ab4
movq %rax, 0x8(%r14)
leaq 0x50(%rsp), %rbp
movq 0x940(%rsp), %rdi
movq %rdi, (%rbp)
movq %r13, %rsi
callq 0x2ab4
movq %rax, 0x8(%rbp)
leaq 0x40(%rsp), %rax
leaq 0x30(%rsp), %rsi
andq $0x0, (%rsi)
movq %r14, (%rax)
movq %rbp, 0x8(%rax)
movl $0x1, (%rsi)
movq %rax, 0x8(%rsi)
movq %r12, %rdi
callq 0x3308
testl %eax, %eax
je 0x27cc
movl %eax, %ebp
leaq 0x16c8e(%rip), %rdi     # 0x1942f
callq 0x2210
movq %r15, %rdi
callq 0x2080
movq %rbx, %rdi
callq 0x2050
jmp 0x2944
leaq 0x16c9b(%rip), %rdi     # 0x1945d
callq 0x2210
jmp 0x299e
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 0x1740e
movl %eax, %ebp
movq %r15, %rdi
callq 0x2080
movq %rbx, %rdi
callq 0x2050
pushq $0xa
popq %rdi
callq 0x21e0
testl %ebp, %ebp
js 0x28a7
leaq 0x16bd6(%rip), %rdi     # 0x193f2
callq 0x2210
leaq 0x169e9(%rip), %rdi     # 0x19211
xorl %eax, %eax
callq 0x2040
movq 0x2478a(%rip), %rbx     # 0x26fc0
movq (%rbx), %rdi
movl %ebp, %esi
callq 0x12790
leaq 0x169d3(%rip), %rdi     # 0x1921a
movl %ebp, %esi
xorl %eax, %eax
callq 0x2040
testl %ebp, %ebp
jne 0x291a
leaq 0x169c2(%rip), %rdi     # 0x19221
movq %r14, %rsi
xorl %eax, %eax
callq 0x2040
movl 0xc(%rsp), %esi
shll $0x3, %esi
leaq 0x169c0(%rip), %rdi     # 0x19237
xorl %eax, %eax
callq 0x2040
leaq 0x169cf(%rip), %rdi     # 0x19254
xorl %eax, %eax
callq 0x2040
movq (%rbx), %rdi
movq 0x10(%rsp), %rsi
movl 0xc(%rsp), %edx
callq 0x12280
pushq $0xa
popq %rdi
callq 0x21e0
jmp 0x291a
leaq 0x168ff(%rip), %rdi     # 0x191ad
xorl %eax, %eax
callq 0x2040
leaq 0x16925(%rip), %rdi     # 0x191e1
movl %ebp, %esi
xorl %eax, %eax
callq 0x2040
leaq 0x16925(%rip), %rdi     # 0x191f1
xorl %eax, %eax
callq 0x2040
movq 0x246fe(%rip), %r14     # 0x26fd8
movq (%r14), %rdi
movl %ebp, %esi
callq 0x12344
pushq $0xa
popq %rdi
callq 0x21e0
leaq 0x70(%rsp), %rbx
movq 0x28(%rbx), %rsi
leaq 0x16902(%rip), %rdi     # 0x191fe
xorl %eax, %eax
callq 0x2040
leaq 0x16b18(%rip), %rdi     # 0x19422
callq 0x2210
movq (%r14), %rdi
movq %rbx, %rsi
callq 0x122f2
pushq $0xa
popq %rdi
callq 0x21e0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x2944
movslq 0xc(%rsp), %rdx
xorl %esi, %esi
callq 0x12265
movq 0x10(%rsp), %rsi
xorl %edi, %edi
callq 0x2ba0
leaq 0x70(%rsp), %rdi
callq 0x33fe
jmp 0x29a9
movq 0x24681(%rip), %r14     # 0x26fd8
movq (%r14), %rcx
leaq 0x16797(%rip), %rdi     # 0x190f8
pushq $0x26
jmp 0x298d
movq 0x2466c(%rip), %r14     # 0x26fd8
movq (%r14), %rcx
leaq 0x167c5(%rip), %rdi     # 0x1913b
pushq $0x1e
jmp 0x298d
movq 0x24657(%rip), %r14     # 0x26fd8
movq (%r14), %rcx
leaq 0x167ef(%rip), %rdi     # 0x1917a
pushq $0x1b
popq %rsi
pushq $0x1
popq %rdx
callq 0x2240
movq (%r14), %rdi
callq 0x20d0
movq %rbx, %rdi
callq 0x2050
pushq $-0x1
popq %rbp
movl %ebp, %eax
addq $0x908, %rsp            # imm = 0x908
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x24614(%rip), %r14     # 0x26fd8
movq (%r14), %rcx
leaq 0x167c8(%rip), %rdi     # 0x19196
pushq $0x16
popq %rsi
pushq $0x1
popq %rdx
callq 0x2240
movq (%r14), %rdi
callq 0x20d0
movq %r15, %rdi
callq 0x2080
jmp 0x299e