#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ SSL_CTX;
typedef int /*<<< orphan*/ SSL;
typedef int /*<<< orphan*/ BIO;

/* Variables and functions */
int BIO_do_connect(int /*<<< orphan*/ *);
int /*<<< orphan*/ BIO_get_ssl(int /*<<< orphan*/ *, int /*<<< orphan*/ **);
int /*<<< orphan*/ *BIO_new_ssl_connect(int /*<<< orphan*/ *);
int /*<<< orphan*/ BIO_set_conn_hostname(int /*<<< orphan*/ *, char *);
int SSL_CTX_load_verify_locations(int /*<<< orphan*/ *, char *,
                                  int /*<<< orphan*/ *);
int /*<<< orphan*/ *SSL_CTX_new(int /*<<< orphan*/);
int /*<<< orphan*/ SSL_MODE_AUTO_RETRY;
scalar_t__ SSL_get_verify_result(int /*<<< orphan*/ *);
int /*<<< orphan*/ SSL_set_mode(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ TLS_client_method();
scalar_t__ X509_V_OK;
int /*<<< orphan*/ print_ssl_error(char *, int /*<<< orphan*/);
int /*<<< orphan*/ print_ssl_error_2(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
BIO *connect_encrypted(char *host_and_port, char *store_path, SSL_CTX **ctx,
                       SSL **ssl) {
  BIO *bio = NULL;
  int r = 0;
  *ctx = SSL_CTX_new(TLS_client_method());
  *ssl = NULL;
  r = SSL_CTX_load_verify_locations(*ctx, store_path, NULL);
  if (r == 0) {
    print_ssl_error_2("Unable to load the trust store from %s.\n", store_path,
                      stdout);
    return NULL;
  }
  bio = BIO_new_ssl_connect(*ctx);
  BIO_get_ssl(bio, ssl);
  if (!(*ssl)) {
    print_ssl_error("Unable to allocate SSL pointer.\n", stdout);
    return NULL;
  }
  SSL_set_mode(*ssl, SSL_MODE_AUTO_RETRY);
  BIO_set_conn_hostname(bio, host_and_port);
  if (BIO_do_connect(bio) < 1) {
    print_ssl_error_2("Unable to connect BIO.%s\n", host_and_port, stdout);
    return NULL;
  }
  if (SSL_get_verify_result(*ssl) != X509_V_OK) {
    print_ssl_error("Unable to verify connection result.\n", stdout);
  }
  return bio;
}


x86 assembly (clang, -O0):
<connect_encrypted>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    $0x0,%al
call   1040 <TLS_client_method@plt>
mov    %eax,%edi
call   1080 <SSL_CTX_new@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <SSL_CTX_load_verify_locations@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    1241 <connect_encrypted+0x91>
mov    -0x18(%rbp),%rsi
mov    0x2daa(%rip),%rax
mov    (%rax),%edx
lea    0xdd1(%rip),%rdi
call   10b0 <print_ssl_error_2@plt>
movq   $0x0,-0x8(%rbp)
jmp    1321 <connect_encrypted+0x171>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   10d0 <BIO_new_ssl_connect@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <BIO_get_ssl@plt>
mov    -0x28(%rbp),%rax
cmpq   $0x0,(%rax)
jne    128e <connect_encrypted+0xde>
mov    0x2d5d(%rip),%rax
mov    (%rax),%esi
lea    0xdad(%rip),%rdi
call   10c0 <print_ssl_error@plt>
movq   $0x0,-0x8(%rbp)
jmp    1321 <connect_encrypted+0x171>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
mov    0x2d44(%rip),%rax
mov    (%rax),%esi
call   1060 <SSL_set_mode@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <BIO_set_conn_hostname@plt>
mov    -0x30(%rbp),%rdi
call   1030 <BIO_do_connect@plt>
cmp    $0x1,%eax
jge    12e8 <connect_encrypted+0x138>
mov    -0x10(%rbp),%rsi
mov    0x2d03(%rip),%rax
mov    (%rax),%edx
lea    0xd74(%rip),%rdi
call   10b0 <print_ssl_error_2@plt>
movq   $0x0,-0x8(%rbp)
jmp    1321 <connect_encrypted+0x171>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   10a0 <SSL_get_verify_result@plt>
mov    0x2cfd(%rip),%rcx
cmp    (%rcx),%rax
je     1319 <connect_encrypted+0x169>
mov    0x2cc5(%rip),%rax
mov    (%rax),%esi
lea    0xd50(%rip),%rdi
call   10c0 <print_ssl_error@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_encrypted>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%rbx
mov    %rsi,%r12
mov    %rdi,%r14
xor    %eax,%eax
call   1040 <TLS_client_method@plt>
mov    %eax,%edi
call   1080 <SSL_CTX_new@plt>
mov    %rax,(%rbx)
movq   $0x0,(%r15)
mov    (%rbx),%rdi
mov    %r12,%rsi
xor    %edx,%edx
call   1090 <SSL_CTX_load_verify_locations@plt>
test   %eax,%eax
je     125b <connect_encrypted+0xab>
mov    (%rbx),%rdi
call   10d0 <BIO_new_ssl_connect@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1050 <BIO_get_ssl@plt>
mov    (%r15),%rdi
test   %rdi,%rdi
je     1270 <connect_encrypted+0xc0>
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
call   1060 <SSL_set_mode@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <BIO_set_conn_hostname@plt>
mov    %rbx,%rdi
call   1030 <BIO_do_connect@plt>
test   %eax,%eax
jle    1287 <connect_encrypted+0xd7>
mov    (%r15),%rdi
call   10a0 <SSL_get_verify_result@plt>
mov    0x2db9(%rip),%rcx
cmp    (%rcx),%rax
je     12a1 <connect_encrypted+0xf1>
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
lea    0xe10(%rip),%rdi
call   10c0 <print_ssl_error@plt>
jmp    12a1 <connect_encrypted+0xf1>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edx
lea    0xd95(%rip),%rdi
mov    %r12,%rsi
jmp    129a <connect_encrypted+0xea>
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
lea    0xda9(%rip),%rdi
call   10c0 <print_ssl_error@plt>
jmp    129f <connect_encrypted+0xef>
mov    0x2d42(%rip),%rax
mov    (%rax),%edx
lea    0xdb3(%rip),%rdi
mov    %r14,%rsi
call   10b0 <print_ssl_error_2@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [41 x i8] c"Unable to load the trust store from %s.\0A\00"
@stdout = global i32 0
@.str.1 = [33 x i8] c"Unable to allocate SSL pointer.\0A\00"
@SSL_MODE_AUTO_RETRY = global i32 0
@.str.2 = [26 x i8] c"Unable to connect BIO.%s\0A\00"
@X509_V_OK = global i64 0
@.str.3 = [37 x i8] c"Unable to verify connection result.\0A\00"
define i32* @connect_encrypted(i8* %0, i8* %1, i32** %2, i32** %3) {
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32**
%9 = alloca i32**
%10 = alloca i32*
%11 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32** %2, i32*** %8
store i32** %3, i32*** %9
store i32* null, i32** %10
store i32 0, i32* %11
%12 = call i32 (...) @TLS_client_method()
%13 = call i32* @SSL_CTX_new(i32 %12)
%14 = load i32**, i32*** %8
store i32* %13, i32** %14
%15 = load i32**, i32*** %9
store i32* null, i32** %15
%16 = load i32**, i32*** %8
%17 = load i32*, i32** %16
%18 = load i8*, i8** %7
%19 = call i32 @SSL_CTX_load_verify_locations(i32* %17, i8* %18, i32* null)
store i32 %19, i32* %11
%20 = load i32, i32* %11
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %4
%23 = load i8*, i8** %7
%24 = load i32, i32* @stdout
%25 = call i32 @print_ssl_error_2(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %23, i32 %24)
store i32* null, i32** %5
br label %65
26: ; preds = %4
%27 = load i32**, i32*** %8
%28 = load i32*, i32** %27
%29 = call i32* @BIO_new_ssl_connect(i32* %28)
store i32* %29, i32** %10
%30 = load i32*, i32** %10
%31 = load i32**, i32*** %9
%32 = call i32 @BIO_get_ssl(i32* %30, i32** %31)
%33 = load i32**, i32*** %9
%34 = load i32*, i32** %33
%35 = icmp ne i32* %34, null
br i1 %35, label %39, label %36
36: ; preds = %26
%37 = load i32, i32* @stdout
%38 = call i32 @print_ssl_error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %37)
store i32* null, i32** %5
br label %65
39: ; preds = %26
%40 = load i32**, i32*** %9
%41 = load i32*, i32** %40
%42 = load i32, i32* @SSL_MODE_AUTO_RETRY
%43 = call i32 @SSL_set_mode(i32* %41, i32 %42)
%44 = load i32*, i32** %10
%45 = load i8*, i8** %6
%46 = call i32 @BIO_set_conn_hostname(i32* %44, i8* %45)
%47 = load i32*, i32** %10
%48 = call i32 @BIO_do_connect(i32* %47)
%49 = icmp slt i32 %48, 1
br i1 %49, label %50, label %54
50: ; preds = %39
%51 = load i8*, i8** %6
%52 = load i32, i32* @stdout
%53 = call i32 @print_ssl_error_2(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %51, i32 %52)
store i32* null, i32** %5
br label %65
54: ; preds = %39
%55 = load i32**, i32*** %9
%56 = load i32*, i32** %55
%57 = call i64 @SSL_get_verify_result(i32* %56)
%58 = load i64, i64* @X509_V_OK
%59 = icmp ne i64 %57, %58
br i1 %59, label %60, label %63
60: ; preds = %54
%61 = load i32, i32* @stdout
%62 = call i32 @print_ssl_error(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %61)
br label %63
63: ; preds = %60, %54
%64 = load i32*, i32** %10
store i32* %64, i32** %5
br label %65
65: ; preds = %63, %50, %36, %22
%66 = load i32*, i32** %5
ret i32* %66
}

LLVM IR (-O2):
@.str = [41 x i8] c"Unable to load the trust store from %s.\0A\00"
@stdout = global i32 0
@.str.1 = [33 x i8] c"Unable to allocate SSL pointer.\0A\00"
@SSL_MODE_AUTO_RETRY = global i32 0
@.str.2 = [26 x i8] c"Unable to connect BIO.%s\0A\00"
@X509_V_OK = global i64 0
@.str.3 = [37 x i8] c"Unable to verify connection result.\0A\00"
define i32* @connect_encrypted(i8* %0, i8* %1, i32** nocapture %2, i32** %3) {
%5 = tail call i32 (...) @TLS_client_method()
%6 = tail call i32* @SSL_CTX_new(i32 %5)
store i32* %6, i32** %2
store i32* null, i32** %3
%7 = load i32*, i32** %2
%8 = tail call i32 @SSL_CTX_load_verify_locations(i32* %7, i8* %1, i32* null)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %4
%11 = load i32, i32* @stdout
%12 = tail call i32 @print_ssl_error_2(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %1, i32 %11)
br label %39
13: ; preds = %4
%14 = load i32*, i32** %2
%15 = tail call i32* @BIO_new_ssl_connect(i32* %14)
%16 = tail call i32 @BIO_get_ssl(i32* %15, i32** %3)
%17 = load i32*, i32** %3
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = load i32, i32* @stdout
%21 = tail call i32 @print_ssl_error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %20)
br label %39
22: ; preds = %13
%23 = load i32, i32* @SSL_MODE_AUTO_RETRY
%24 = tail call i32 @SSL_set_mode(i32* %17, i32 %23)
%25 = tail call i32 @BIO_set_conn_hostname(i32* %15, i8* %0)
%26 = tail call i32 @BIO_do_connect(i32* %15)
%27 = icmp slt i32 %26, 1
br i1 %27, label %28, label %31
28: ; preds = %22
%29 = load i32, i32* @stdout
%30 = tail call i32 @print_ssl_error_2(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %29)
br label %39
31: ; preds = %22
%32 = load i32*, i32** %3
%33 = tail call i64 @SSL_get_verify_result(i32* %32)
%34 = load i64, i64* @X509_V_OK
%35 = icmp eq i64 %33, %34
br i1 %35, label %39, label %36
36: ; preds = %31
%37 = load i32, i32* @stdout
%38 = tail call i32 @print_ssl_error(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %37)
br label %39
39: ; preds = %31, %36, %28, %19, %10
%40 = phi i32* [ null, %10 ], [ null, %28 ], [ null, %19 ], [ %15, %36 ], [ %15, %31 ]
ret i32* %40
}