#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(char *);
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*/ TLSv1_2_client_method();
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
char *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);
  TLSv1_2_client_method();
  if (SSL_connect(ssl) == -1) {
    printf("Eroor: %s\n", stderr);
    ERR_print_errors_fp(stderr);
  } else {
    char *msg = "HelloWorld";
    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     126d <main+0x3d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xd9f(%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    $0x0,%al
call   1140 <TLSv1_2_client_method@plt>
mov    -0x28(%rbp),%rdi
call   10f0 <SSL_connect@plt>
cmp    $0xffffffff,%eax
jne    1324 <main+0xf4>
mov    0x2cdf(%rip),%rax
mov    (%rax),%rsi
lea    0xd15(%rip),%rdi
call   1090 <printf@plt>
mov    0x2cc9(%rip),%rax
mov    (%rax),%rdi
call   1150 <ERR_print_errors_fp@plt>
jmp    13d3 <main+0x1a3>
lea    0xd00(%rip),%rax
mov    %rax,-0x450(%rbp)
mov    -0x28(%rbp),%rdi
call   10b0 <SSL_get_cipher@plt>
mov    %rax,%rsi
lea    0xcf1(%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    0xc8f(%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    134c <main+0x11c>
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>
xor    %eax,%eax
call   1140 <TLSv1_2_client_method@plt>
mov    %rbx,%rdi
call   10f0 <SSL_connect@plt>
cmp    $0xffffffff,%eax
je     130e <main+0xde>
mov    %rbx,%rdi
call   10b0 <SSL_get_cipher@plt>
lea    0xd86(%rip),%rdi
mov    %rax,%rsi
call   1090 <printf@plt>
mov    %rbx,%rdi
call   1040 <ShowCerts@plt>
lea    0xd64(%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    0xd58(%rip),%rdi
mov    %rbp,%rsi
call   1090 <printf@plt>
mov    %rbx,%rdi
call   1060 <SSL_free@plt>
jmp    132c <main+0xfc>
mov    0x2ccb(%rip),%rbx
mov    (%rbx),%rsi
lea    0xd01(%rip),%rdi
call   1090 <printf@plt>
mov    (%rbx),%rdi
call   1150 <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    0xcaa(%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"
@.str.1 = [11 x i8] c"Eroor: %s\0A\00"
@stderr = global i8* null
@.str.2 = [11 x i8] c"HelloWorld\00"
@.str.3 = [30 x i8] c"Connected with %s encryption\0A\00"
@.str.4 = [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 = call i32 (...) @TLSv1_2_client_method()
%41 = load i32*, i32** %8
%42 = call i32 @SSL_connect(i32* %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %49
44: ; preds = %22
%45 = load i8*, i8** @stderr
%46 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %45)
%47 = load i8*, i8** @stderr
%48 = call i32 @ERR_print_errors_fp(i8* %47)
br label %70
49: ; preds = %22
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8** %13
%50 = load i32*, i32** %8
%51 = call i8* @SSL_get_cipher(i32* %50)
%52 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %51)
%53 = load i32*, i32** %8
%54 = call i32 @ShowCerts(i32* %53)
%55 = load i32*, i32** %8
%56 = load i8*, i8** %13
%57 = load i8*, i8** %13
%58 = call i32 @strlen(i8* %57)
%59 = call i32 @SSL_write(i32* %55, i8* %56, i32 %58)
%60 = load i32*, i32** %8
%61 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%62 = call i32 @SSL_read(i32* %60, i8* %61, i32 1024)
store i32 %62, i32* %10
%63 = load i32, i32* %10
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 %64
store i8 0, i8* %65
%66 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%67 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %66)
%68 = load i32*, i32** %8
%69 = call i32 @SSL_free(i32* %68)
br label %70
70: ; preds = %49, %44
%71 = load i32, i32* %7
%72 = call i32 @close(i32 %71)
%73 = load i32*, i32** %6
%74 = call i32 @SSL_CTX_free(i32* %73)
ret i32 0
}

LLVM IR (-O2):
@.str = [32 x i8] c"usage: %s <hostname> <portnum>\0A\00"
@.str.1 = [11 x i8] c"Eroor: %s\0A\00"
@stderr = global i8* null
@.str.2 = [11 x i8] c"HelloWorld\00"
@.str.3 = [30 x i8] c"Connected with %s encryption\0A\00"
@.str.4 = [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 (...) @TLSv1_2_client_method()
%22 = tail call i32 @SSL_connect(i32* %19)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %29
24: ; preds = %10
%25 = load i8*, i8** @stderr
%26 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = load i8*, i8** @stderr
%28 = tail call i32 @ERR_print_errors_fp(i8* %27)
br label %40
29: ; preds = %10
%30 = tail call i8* @SSL_get_cipher(i32* %19)
%31 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %30)
%32 = tail call i32 @ShowCerts(i32* %19)
%33 = tail call i32 @strlen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%34 = tail call i32 @SSL_write(i32* %19, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %33)
%35 = call i32 @SSL_read(i32* %19, i8* %4, i32 1024)
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %36
store i8 0, i8* %37
%38 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %4)
%39 = call i32 @SSL_free(i32* %19)
br label %40
40: ; preds = %29, %24
%41 = call i32 @close(i32 %18)
%42 = call i32 @SSL_CTX_free(i32* %16)
ret i32 0
}