#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*/ buf;
typedef int /*<<< orphan*/ SSL_CTX;
typedef int /*<<< orphan*/ SSL;

/* Variables and functions */
int /*<<< orphan*/ ERR_print_errors_fp(int /*<<< orphan*/);
int /*<<< orphan*/ *InitCTX();
int OpenConnection(char *, int /*<<< orphan*/);
int /*<<< orphan*/ SSL_CTX_free(int /*<<< orphan*/ *);
int SSL_connect(int /*<<< orphan*/ *);
int /*<<< orphan*/ SSL_free(int /*<<< orphan*/ *);
char *SSL_get_cipher(int /*<<< orphan*/ *);
int /*<<< orphan*/ SSL_library_init();
int /*<<< orphan*/ *SSL_new(int /*<<< orphan*/ *);
int SSL_read(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ SSL_set_fd(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ SSL_write(int /*<<< orphan*/ *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ ShowCerts(int /*<<< orphan*/ *);
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int main(int count, char *strings[]) {
  SSL_CTX *ctx;
  int server;
  SSL *ssl;
  char buf[1024];
  int bytes;
  char *hostname, *portnum;
  if (count != 3) {
    printf("usage: %s <hostname> <portnum>\n", strings[0]);
    exit(0);
  }
  SSL_library_init();
  hostname = strings[1];
  portnum = strings[2];
  ctx = InitCTX();
  server = OpenConnection(hostname, atoi(portnum));
  ssl = SSL_new(ctx);
  SSL_set_fd(ssl, server);
  if (SSL_connect(ssl) == -1)
    ERR_print_errors_fp(stderr);
  else {
    char *msg = "Hello???";
    printf("Connected with %s encryption\n", SSL_get_cipher(ssl));
    ShowCerts(ssl);
    SSL_write(ssl, msg, strlen(msg));
    bytes = SSL_read(ssl, buf, sizeof(buf));
    buf[bytes] = 0;
    printf("Received: \"%s\"\n", buf);
    SSL_free(ssl);
  }
  close(server);
  SSL_CTX_free(ctx);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     125d <main+0x3d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdaf(%rip),%rdi
call   1090 <printf@plt>
xor    %edi,%edi
call   1130 <exit@plt>
mov    $0x0,%al
call   10c0 <SSL_library_init@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x440(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x448(%rbp)
mov    $0x0,%al
call   1050 <InitCTX@plt>
mov    %rax,-0x18(%rbp)
mov    -0x440(%rbp),%rax
mov    %rax,-0x458(%rbp)
mov    -0x448(%rbp),%rdi
call   1120 <atoi@plt>
mov    -0x458(%rbp),%rdi
mov    %eax,%esi
call   1110 <OpenConnection@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   10e0 <SSL_new@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1070 <SSL_set_fd@plt>
mov    -0x28(%rbp),%rdi
call   10f0 <SSL_connect@plt>
cmp    $0xffffffff,%eax
jne    12f6 <main+0xd6>
mov    0x2cf6(%rip),%rax
mov    (%rax),%edi
call   1140 <ERR_print_errors_fp@plt>
jmp    13a5 <main+0x185>
lea    0xd23(%rip),%rax
mov    %rax,-0x450(%rbp)
mov    -0x28(%rbp),%rdi
call   10b0 <SSL_get_cipher@plt>
mov    %rax,%rsi
lea    0xd12(%rip),%rdi
call   1090 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <ShowCerts@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x468(%rbp)
mov    -0x450(%rbp),%rax
mov    %rax,-0x460(%rbp)
mov    -0x450(%rbp),%rdi
call   1080 <strlen@plt>
mov    -0x468(%rbp),%rdi
mov    -0x460(%rbp),%rsi
mov    %eax,%edx
call   1030 <SSL_write@plt>
mov    -0x28(%rbp),%rdi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   10d0 <SSL_read@plt>
mov    %eax,-0x434(%rbp)
movslq -0x434(%rbp),%rax
movb   $0x0,-0x430(%rbp,%rax,1)
lea    -0x430(%rbp),%rsi
lea    0xcb0(%rip),%rdi
call   1090 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1060 <SSL_free@plt>
mov    -0x1c(%rbp),%edi
call   10a0 <close@plt>
mov    -0x18(%rbp),%rdi
call   1100 <SSL_CTX_free@plt>
xor    %eax,%eax
add    $0x470,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    1325 <main+0x105>
xor    %eax,%eax
call   10c0 <SSL_library_init@plt>
mov    0x8(%rbx),%rbp
mov    0x10(%rbx),%rbx
xor    %eax,%eax
call   1050 <InitCTX@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1120 <atoi@plt>
mov    %rbp,%rdi
mov    %eax,%esi
call   1110 <OpenConnection@plt>
mov    %eax,%r15d
mov    %r14,%rdi
call   10e0 <SSL_new@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15d,%esi
call   1070 <SSL_set_fd@plt>
mov    %rbx,%rdi
call   10f0 <SSL_connect@plt>
cmp    $0xffffffff,%eax
je     12f7 <main+0xd7>
mov    %rbx,%rdi
call   10b0 <SSL_get_cipher@plt>
lea    0xd90(%rip),%rdi
mov    %rax,%rsi
call   1090 <printf@plt>
mov    %rbx,%rdi
call   1040 <ShowCerts@plt>
lea    0xd70(%rip),%rbp
mov    %rbp,%rdi
call   1080 <strlen@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <SSL_write@plt>
mov    %rsp,%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x400,%edx
call   10d0 <SSL_read@plt>
cltq
movb   $0x0,(%rsp,%rax,1)
lea    0xd62(%rip),%rdi
mov    %rbp,%rsi
call   1090 <printf@plt>
mov    %rbx,%rdi
call   1060 <SSL_free@plt>
jmp    1305 <main+0xe5>
mov    0x2ce2(%rip),%rax
mov    (%rax),%edi
call   1140 <ERR_print_errors_fp@plt>
mov    %r15d,%edi
call   10a0 <close@plt>
mov    %r14,%rdi
call   1100 <SSL_CTX_free@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rbx),%rsi
lea    0xcd1(%rip),%rdi
call   1090 <printf@plt>
xor    %edi,%edi
call   1130 <exit@plt>

LLVM IR (-O0):
@.str = [32 x i8] c"usage: %s <hostname> <portnum>\0A\00"
@stderr = global i32 0
@.str.1 = [9 x i8] c"Hello???\00"
@.str.2 = [30 x i8] c"Connected with %s encryption\0A\00"
@.str.3 = [16 x i8] c"Received: \22%s\22\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca [1024 x i8]
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 3
br i1 %15, label %16, label %22
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = call i32 @exit(i32 0)
unreachable
22: ; preds = %2
%23 = call i32 (...) @SSL_library_init()
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
store i8* %26, i8** %11
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
store i8* %29, i8** %12
%30 = call i32* (...) @InitCTX()
store i32* %30, i32** %6
%31 = load i8*, i8** %11
%32 = load i8*, i8** %12
%33 = call i32 @atoi(i8* %32)
%34 = call i32 @OpenConnection(i8* %31, i32 %33)
store i32 %34, i32* %7
%35 = load i32*, i32** %6
%36 = call i32* @SSL_new(i32* %35)
store i32* %36, i32** %8
%37 = load i32*, i32** %8
%38 = load i32, i32* %7
%39 = call i32 @SSL_set_fd(i32* %37, i32 %38)
%40 = load i32*, i32** %8
%41 = call i32 @SSL_connect(i32* %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %46
43: ; preds = %22
%44 = load i32, i32* @stderr
%45 = call i32 @ERR_print_errors_fp(i32 %44)
br label %67
46: ; preds = %22
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8** %13
%47 = load i32*, i32** %8
%48 = call i8* @SSL_get_cipher(i32* %47)
%49 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %48)
%50 = load i32*, i32** %8
%51 = call i32 @ShowCerts(i32* %50)
%52 = load i32*, i32** %8
%53 = load i8*, i8** %13
%54 = load i8*, i8** %13
%55 = call i32 @strlen(i8* %54)
%56 = call i32 @SSL_write(i32* %52, i8* %53, i32 %55)
%57 = load i32*, i32** %8
%58 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%59 = call i32 @SSL_read(i32* %57, i8* %58, i32 1024)
store i32 %59, i32* %10
%60 = load i32, i32* %10
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 %61
store i8 0, i8* %62
%63 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%64 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %63)
%65 = load i32*, i32** %8
%66 = call i32 @SSL_free(i32* %65)
br label %67
67: ; preds = %46, %43
%68 = load i32, i32* %7
%69 = call i32 @close(i32 %68)
%70 = load i32*, i32** %6
%71 = call i32 @SSL_CTX_free(i32* %70)
ret i32 0
}

LLVM IR (-O2):
@.str = [32 x i8] c"usage: %s <hostname> <portnum>\0A\00"
@stderr = global i32 0
@.str.1 = [9 x i8] c"Hello???\00"
@.str.2 = [30 x i8] c"Connected with %s encryption\0A\00"
@.str.3 = [16 x i8] c"Received: \22%s\22\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 3
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %2
%11 = tail call i32 (...) @SSL_library_init()
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = tail call i32* (...) @InitCTX()
%17 = tail call i32 @atoi(i8* %15)
%18 = tail call i32 @OpenConnection(i8* %13, i32 %17)
%19 = tail call i32* @SSL_new(i32* %16)
%20 = tail call i32 @SSL_set_fd(i32* %19, i32 %18)
%21 = tail call i32 @SSL_connect(i32* %19)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %10
%24 = load i32, i32* @stderr
%25 = tail call i32 @ERR_print_errors_fp(i32 %24)
br label %37
26: ; preds = %10
%27 = tail call i8* @SSL_get_cipher(i32* %19)
%28 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %27)
%29 = tail call i32 @ShowCerts(i32* %19)
%30 = tail call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%31 = tail call i32 @SSL_write(i32* %19, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %30)
%32 = call i32 @SSL_read(i32* %19, i8* %4, i32 1024)
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %33
store i8 0, i8* %34
%35 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%36 = call i32 @SSL_free(i32* %19)
br label %37
37: ; preds = %26, %23
%38 = call i32 @close(i32 %18)
%39 = call i32 @SSL_CTX_free(i32* %16)
ret i32 0
}