#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sock_addr;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ SSL_CTX;
typedef int /*<<< orphan*/ SSL;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SSL_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *SSL_CTX_new(int /*<<< orphan*/);
int /*<<< orphan*/ SSL_connect(int /*<<< orphan*/ *);
int /*<<< orphan*/ SSL_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ SSL_library_init();
int /*<<< orphan*/ *SSL_new(int /*<<< orphan*/ *);
int /*<<< orphan*/ SSL_read(int /*<<< orphan*/ *, char *, scalar_t__);
int /*<<< orphan*/ SSL_set_fd(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ SSL_shutdown(int /*<<< orphan*/ *);
int /*<<< orphan*/ SSL_write(int /*<<< orphan*/ *, char *, scalar_t__);
int /*<<< orphan*/ SSLv23_client_method();
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ printf(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
scalar_t__ strlen(char *);
int main() {
  char buf[256];
  struct sockaddr_in sock_addr;
  sock_addr.sin_family = AF_INET;
  sock_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  sock_addr.sin_port = htons(31337);
  int connect_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (connect(connect_sock, (struct sockaddr *)&sock_addr, sizeof(sock_addr)) <
      0)
    exit(1);
  SSL_library_init();
  SSL_CTX *ctx = SSL_CTX_new(SSLv23_client_method());
  SSL *ssl = SSL_new(ctx);
  SSL_set_fd(ssl, connect_sock);
  SSL_connect(ssl);
  while (1) {
    fgets(buf, sizeof(buf), stdin);
    SSL_write(ssl, buf, strlen(buf) + 1);
    SSL_read(ssl, buf, strlen(buf) + 1);
    printf("%s\n", buf);
  }
  SSL_shutdown(ssl);
  SSL_free(ssl);
  SSL_CTX_free(ctx);
  close(connect_sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x170,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x118(%rbp)
lea    0xdd8(%rip),%rdi
call   10d0 <inet_addr@plt>
mov    %eax,-0x11c(%rbp)
mov    $0x7a69,%edi
call   1070 <htons@plt>
mov    %eax,-0x120(%rbp)
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
mov    0x2d75(%rip),%rax
mov    (%rax),%esi
mov    0x2d7c(%rip),%rax
mov    (%rax),%edx
call   1120 <socket@plt>
mov    %eax,-0x124(%rbp)
mov    -0x124(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0xc,%edx
call   1110 <connect@plt>
cmp    $0x0,%rax
jge    1294 <main+0x94>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    $0x0,%al
call   1090 <SSL_library_init@plt>
mov    $0x0,%al
call   1040 <SSLv23_client_method@plt>
mov    %eax,%edi
call   10e0 <SSL_CTX_new@plt>
mov    %rax,-0x130(%rbp)
mov    -0x130(%rbp),%rdi
call   10c0 <SSL_new@plt>
mov    %rax,-0x138(%rbp)
mov    -0x138(%rbp),%rdi
mov    -0x124(%rbp),%esi
call   1050 <SSL_set_fd@plt>
mov    -0x138(%rbp),%rdi
call   10f0 <SSL_connect@plt>
lea    -0x110(%rbp),%rdi
mov    0x2cf1(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   10b0 <fgets@plt>
mov    -0x138(%rbp),%rax
mov    %rax,-0x168(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x160(%rbp)
lea    -0x110(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x168(%rbp),%rdi
mov    -0x160(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x158(%rbp)
call   1030 <SSL_write@plt>
mov    -0x138(%rbp),%rax
mov    %rax,-0x150(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x148(%rbp)
lea    -0x110(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x150(%rbp),%rdi
mov    -0x148(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x140(%rbp)
call   10a0 <SSL_read@plt>
lea    -0x110(%rbp),%rsi
lea    0xc6f(%rip),%rdi
call   1080 <printf@plt>
jmp    12e1 <main+0xe1>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    0x2dd3(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
lea    0xdde(%rip),%rdi
call   10d0 <inet_addr@plt>
mov    %eax,0x4(%rsp)
mov    $0x7a69,%edi
call   1070 <htons@plt>
mov    %eax,(%rsp)
mov    (%rbx),%edi
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
mov    0x2d8e(%rip),%rax
mov    (%rax),%edx
call   1120 <socket@plt>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1110 <connect@plt>
test   %rax,%rax
js     12fc <main+0xfc>
xor    %eax,%eax
call   1090 <SSL_library_init@plt>
xor    %eax,%eax
call   1040 <SSLv23_client_method@plt>
mov    %eax,%edi
call   10e0 <SSL_CTX_new@plt>
mov    %rax,%rdi
call   10c0 <SSL_new@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %ebx,%esi
call   1050 <SSL_set_fd@plt>
mov    %r14,%rdi
call   10f0 <SSL_connect@plt>
mov    0x2d3c(%rip),%r12
lea    0x10(%rsp),%rbx
lea    0xd5a(%rip),%r15
mov    (%r12),%edx
mov    %rbx,%rdi
mov    $0x100,%esi
call   10b0 <fgets@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <SSL_write@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %r14,%rdi
mov    %rbx,%rsi
call   10a0 <SSL_read@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1080 <printf@plt>
jmp    12b0 <main+0xb0>
mov    $0x1,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@stdin = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
store i32 0, i32* %1
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %7, i32* %8
%9 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%11 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %10, i32 0, i32 0
store i32 %9, i32* %11
%12 = call i32 @htons(i32 31337)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = load i32, i32* @IPPROTO_TCP
%17 = call i32 @socket(i32 %14, i32 %15, i32 %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%20 = call i64 @connect(i32 %18, %struct.sockaddr* %19, i32 12)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %0
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %0
%25 = call i32 (...) @SSL_library_init()
%26 = call i32 (...) @SSLv23_client_method()
%27 = call i32* @SSL_CTX_new(i32 %26)
store i32* %27, i32** %5
%28 = load i32*, i32** %5
%29 = call i32* @SSL_new(i32* %28)
store i32* %29, i32** %6
%30 = load i32*, i32** %6
%31 = load i32, i32* %4
%32 = call i32 @SSL_set_fd(i32* %30, i32 %31)
%33 = load i32*, i32** %6
%34 = call i32 @SSL_connect(i32* %33)
br label %35
35: ; preds = %24, %35
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%37 = load i32, i32* @stdin
%38 = call i32 @fgets(i8* %36, i32 256, i32 %37)
%39 = load i32*, i32** %6
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%41 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%42 = call i64 @strlen(i8* %41)
%43 = add nsw i64 %42, 1
%44 = call i32 @SSL_write(i32* %39, i8* %40, i64 %43)
%45 = load i32*, i32** %6
%46 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%48 = call i64 @strlen(i8* %47)
%49 = add nsw i64 %48, 1
%50 = call i32 @SSL_read(i32* %45, i8* %46, i64 %49)
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%52 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %51)
br label %35
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@stdin = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = alloca %struct.sockaddr_in
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = load i32, i32* @AF_INET
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %5, i32* %6
%7 = tail call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %7, i32* %8
%9 = tail call i32 @htons(i32 31337)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %9, i32* %10
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = load i32, i32* @IPPROTO_TCP
%14 = tail call i32 @socket(i32 %11, i32 %12, i32 %13)
%15 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%16 = call i64 @connect(i32 %14, %struct.sockaddr* %15, i32 12)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %0
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %0
%21 = call i32 (...) @SSL_library_init()
%22 = call i32 (...) @SSLv23_client_method()
%23 = call i32* @SSL_CTX_new(i32 %22)
%24 = call i32* @SSL_new(i32* %23)
%25 = call i32 @SSL_set_fd(i32* %24, i32 %14)
%26 = call i32 @SSL_connect(i32* %24)
br label %27
27: ; preds = %27, %20
%28 = load i32, i32* @stdin
%29 = call i32 @fgets(i8* %3, i32 256, i32 %28)
%30 = call i64 @strlen(i8* %3)
%31 = add nsw i64 %30, 1
%32 = call i32 @SSL_write(i32* %24, i8* %3, i64 %31)
%33 = call i64 @strlen(i8* %3)
%34 = add nsw i64 %33, 1
%35 = call i32 @SSL_read(i32* %24, i8* %3, i64 %34)
%36 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %3)
br label %27
}