// File: /workspace/github_c_makefile_star2_O2/OpenKMIP[P]libkmip/demos/demo_query.c
// Function: main
int
main(int argc, char **argv)
{
    char *server_address = NULL;
    char *server_port = NULL;
    char *client_certificate = NULL;
    char *client_key = NULL;
    char *ca_certificate = NULL;
    int help = 0;
    int result = 1;
    
    int error = parse_arguments(argc, argv, &server_address, &server_port,
                                &client_certificate, &client_key,
                                &ca_certificate, &help);
    if(error)
    {
        return(error);
    }
    if(help)
    {
        print_help(argv[0]);
        return(0);
    }

    /* 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)
    {
        fprintf(stderr, "BIO_new_ssl_connect failed\n");
        ERR_print_errors_fp(stderr);
        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);
    }

    QueryResponse query_result = {0};
    result = use_mid_level_api(bio, &query_result);
    if(result == KMIP_STATUS_SUCCESS)
    {
        printf("Query results: ");
        printf("vendor: %s\n", query_result.vendor_identification);
        printf("num ops: %zu\n", query_result.operations_size);
        printf("num objs: %zu\n", query_result.objects_size);
        printf("server info: %d\n", query_result.server_information_valid );
        printf("\n");
    }

    BIO_free_all(bio);
    SSL_CTX_free(ctx);

    return(result);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x658, %rsp            # imm = 0x658
movq %rsi, %rbx
leaq 0x38(%rsp), %rdx
andq $0x0, (%rdx)
leaq 0x30(%rsp), %rcx
andq $0x0, (%rcx)
leaq 0x28(%rsp), %r8
andq $0x0, (%r8)
leaq 0x20(%rsp), %r9
andq $0x0, (%r9)
leaq 0x18(%rsp), %rax
andq $0x0, (%rax)
leaq 0xc(%rsp), %r10
andl $0x0, (%r10)
pushq %r10
pushq %rax
callq 0x23b3
addq $0x10, %rsp
testl %eax, %eax
je 0x2e06
pushq $-0x1
popq %rbp
jmp 0x2fe4
cmpl $0x0, 0xc(%rsp)
je 0x2e1c
movq (%rbx), %rdi
callq 0x234c
xorl %ebp, %ebp
jmp 0x2fe4
andq $0x0, 0x10(%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
movq 0x28(%rsp), %r14
leaq 0x163d5(%rip), %rdi     # 0x19224
movq %r14, %rsi
xorl %eax, %eax
callq 0x2040
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x21d0
cmpl $0x1, %eax
jne 0x2ff4
movq 0x20(%rsp), %r14
leaq 0x163f3(%rip), %rdi     # 0x1926f
movq %r14, %rsi
xorl %eax, %eax
callq 0x2040
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2030
cmpl $0x1, %eax
jne 0x3009
movq 0x18(%rsp), %r14
leaq 0x16401(%rip), %rdi     # 0x192aa
movq %r14, %rsi
xorl %eax, %eax
callq 0x2040
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x21c0
cmpl $0x1, %eax
jne 0x301e
movq %rbx, %rdi
callq 0x2160
testq %rax, %rax
je 0x3031
movq %rax, %r14
pushq $0x6e
popq %rsi
leaq 0x10(%rsp), %r15
movq %rax, %rdi
xorl %edx, %edx
movq %r15, %rcx
callq 0x2060
movq (%r15), %rdi
pushq $0x21
popq %rsi
pushq $0x4
popq %rdx
xorl %ecx, %ecx
callq 0x2070
movq 0x38(%rsp), %rcx
pushq $0x64
popq %rbp
movq %r14, %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0x2060
movq 0x30(%rsp), %rcx
pushq $0x1
popq %rdx
movq %r14, %rdi
movl %ebp, %esi
callq 0x2060
pushq $0x65
popq %rsi
movq %r14, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2060
cmpq $0x1, %rax
jne 0x3062
leaq 0x40(%rsp), %r15
movl $0x618, %edx            # imm = 0x618
movq %r15, %rdi
xorl %esi, %esi
callq 0x20e0
movq %r14, %rdi
movq %r15, %rsi
callq 0x2c32
movl %eax, %ebp
testl %eax, %eax
jne 0x2fd4
leaq 0x1629b(%rip), %rdi     # 0x19208
xorl %eax, %eax
callq 0x2040
leaq 0x1d0(%rsp), %r15
leaq 0x16295(%rip), %rdi     # 0x19218
movq %r15, %rsi
xorl %eax, %eax
callq 0x2040
movq -0x190(%r15), %rsi
leaq 0x1637e(%rip), %rdi     # 0x19319
xorl %eax, %eax
callq 0x2040
movq -0x88(%r15), %rsi
leaq 0x16377(%rip), %rdi     # 0x19327
xorl %eax, %eax
callq 0x2040
movl 0x80(%r15), %esi
leaq 0x16371(%rip), %rdi     # 0x19336
xorl %eax, %eax
callq 0x2040
pushq $0xa
popq %rdi
callq 0x21e0
movq %r14, %rdi
callq 0x2080
movq %rbx, %rdi
callq 0x2050
movl %ebp, %eax
addq $0x658, %rsp            # imm = 0x658
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq 0x23fdd(%rip), %r14     # 0x26fd8
movq (%r14), %rcx
leaq 0x16243(%rip), %rdi     # 0x19248
pushq $0x26
jmp 0x3044
movq 0x23fc8(%rip), %r14     # 0x26fd8
movq (%r14), %rcx
leaq 0x16271(%rip), %rdi     # 0x1928b
pushq $0x1e
jmp 0x3044
movq 0x23fb3(%rip), %r14     # 0x26fd8
movq (%r14), %rcx
leaq 0x1629b(%rip), %rdi     # 0x192ca
jmp 0x3042
movq 0x23fa0(%rip), %r14     # 0x26fd8
movq (%r14), %rcx
leaq 0x162a4(%rip), %rdi     # 0x192e6
pushq $0x1b
popq %rsi
pushq $0x1
popq %rdx
callq 0x2240
movq (%r14), %rdi
callq 0x20d0
movq %rbx, %rdi
callq 0x2050
jmp 0x2dfe
movq 0x23f6f(%rip), %r15     # 0x26fd8
movq (%r15), %rcx
leaq 0x1628f(%rip), %rdi     # 0x19302
pushq $0x16
popq %rsi
pushq $0x1
popq %rdx
callq 0x2240
movq (%r15), %rdi
callq 0x20d0
movq %r14, %rdi
callq 0x2080
jmp 0x3055