// File: /workspace/github_c_makefile_star2_O3/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 0x1d9f0(%rip), %rdi     # 0x200d4
movq %r14, %rsi
xorl %eax, %eax
callq 0x2040
movq %rbx, %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0x21d0
cmpl $0x1, %eax
jne 0x2aa3
leaq 0x1da11(%rip), %rdi     # 0x2011f
movq %rbp, %rsi
xorl %eax, %eax
callq 0x2040
movq %rbx, %rdi
movq %rbp, %rsi
movl $0x1, %edx
callq 0x2030
cmpl $0x1, %eax
jne 0x2ab0
leaq 0x1da22(%rip), %rdi     # 0x2015a
movq %r15, %rsi
xorl %eax, %eax
callq 0x2040
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x21c0
cmpl $0x1, %eax
jne 0x2abd
movq %rbx, %rdi
callq 0x2160
testq %rax, %rax
je 0x28e7
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 0x2aca
movq 0x948(%rsp), %r14
movq 0x940(%rsp), %rbp
movl $0xa, %edi
callq 0x21e0
xorl %eax, %eax
movq %rax, 0x10(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x32, %esi
movq %r14, %rdi
callq 0x2ba0
movq %rax, 0x18(%rsp)
leaq 0x70(%rsp), %r12
movl $0x898, %edx            # imm = 0x898
movq %r12, %rdi
xorl %esi, %esi
callq 0x20e0
leaq -0x22a(%rip), %rax      # 0x2605
movq %rax, 0x868(%r12)
leaq -0x206(%rip), %rax      # 0x2638
movq %rax, 0x870(%r12)
leaq -0x1e2(%rip), %rax      # 0x266b
movq %rax, 0x878(%r12)
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x34ef
leaq 0x60(%rsp), %r13
movq 0x20(%rsp), %rdi
movq %rdi, (%r13)
movl $0x32, %esi
callq 0x2ba0
movq %rax, 0x8(%r13)
leaq 0x50(%rsp), %r14
movq %rbp, (%r14)
movl $0x32, %esi
movq %rbp, %rdi
callq 0x2ba0
movq %rax, 0x8(%r14)
leaq 0x40(%rsp), %rax
movq %r13, (%rax)
movq %r14, 0x8(%rax)
leaq 0x30(%rsp), %rsi
xorl %ecx, %ecx
movq %rcx, (%rsi)
movl $0x1, (%rsi)
movq %rax, 0x8(%rsi)
movq %r12, %rdi
callq 0x362c
testl %eax, %eax
je 0x2905
movl %eax, %ebp
leaq 0x1db62(%rip), %rdi     # 0x2042f
callq 0x2210
movq %r15, %rdi
callq 0x2080
movq %rbx, %rdi
callq 0x2050
jmp 0x2a85
leaq 0x1db6f(%rip), %rdi     # 0x2045d
callq 0x2210
movq %rbx, %rdi
callq 0x2050
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x2a8f
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 0x1e685
movl %eax, %ebp
movq %r15, %rdi
callq 0x2080
movq %rbx, %rdi
callq 0x2050
movl $0xa, %edi
callq 0x21e0
testl %ebp, %ebp
js 0x29e4
leaq 0x1da9b(%rip), %rdi     # 0x203f2
callq 0x2210
leaq 0x1d8ae(%rip), %rdi     # 0x20211
xorl %eax, %eax
callq 0x2040
movq 0x2a64f(%rip), %rbx     # 0x2cfc0
movq (%rbx), %rdi
movl %ebp, %esi
callq 0x195d3
leaq 0x1d898(%rip), %rdi     # 0x2021a
movl %ebp, %esi
xorl %eax, %eax
callq 0x2040
testl %ebp, %ebp
jne 0x2a59
leaq 0x1d887(%rip), %rdi     # 0x20221
movq %r14, %rsi
xorl %eax, %eax
callq 0x2040
movl 0xc(%rsp), %esi
shll $0x3, %esi
leaq 0x1d885(%rip), %rdi     # 0x20237
xorl %eax, %eax
callq 0x2040
leaq 0x1d894(%rip), %rdi     # 0x20254
xorl %eax, %eax
callq 0x2040
movq (%rbx), %rdi
movq 0x10(%rsp), %rsi
movl 0xc(%rsp), %edx
callq 0x19040
movl $0xa, %edi
callq 0x21e0
jmp 0x2a59
leaq 0x1d7c2(%rip), %rdi     # 0x201ad
xorl %eax, %eax
callq 0x2040
leaq 0x1d7e8(%rip), %rdi     # 0x201e1
movl %ebp, %esi
xorl %eax, %eax
callq 0x2040
leaq 0x1d7e8(%rip), %rdi     # 0x201f1
xorl %eax, %eax
callq 0x2040
movq 0x2a5c1(%rip), %r14     # 0x2cfd8
movq (%r14), %rdi
movl %ebp, %esi
callq 0x19108
movl $0xa, %edi
callq 0x21e0
leaq 0x70(%rsp), %rbx
movq 0x28(%rbx), %rsi
leaq 0x1d7c3(%rip), %rdi     # 0x201fe
xorl %eax, %eax
callq 0x2040
leaq 0x1d9d9(%rip), %rdi     # 0x20422
callq 0x2210
movq (%r14), %rdi
movq %rbx, %rsi
callq 0x190b6
movl $0xa, %edi
callq 0x21e0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x2a85
movslq 0xc(%rsp), %rdx
xorl %esi, %esi
callq 0x19024
movq 0x10(%rsp), %rsi
xorl %edi, %edi
callq 0x2cb4
leaq 0x70(%rsp), %rdi
callq 0x37cd
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 0x28fb
movq %rbx, %rdi
callq 0x229a
jmp 0x28fb
movq %rbx, %rdi
callq 0x22d4
jmp 0x28fb
movq %r15, %rdi
movq %rbx, %rsi
callq 0x230e
jmp 0x28fb