#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 uint;
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;
typedef int /*<<< orphan*/ SSL_CTX;
typedef int /*<<< orphan*/ SSL;

/* Variables and functions */
int /*<<< orphan*/ ERR_print_errors_fp(int /*<<< orphan*/);
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SSL_CTX_free(int /*<<< orphan*/ *);
scalar_t__ SSL_accept(int /*<<< orphan*/ *);
int /*<<< orphan*/ SSL_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *SSL_new(int /*<<< orphan*/ *);
int /*<<< orphan*/ SSL_set_fd(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ SSL_shutdown(int /*<<< orphan*/ *);
int /*<<< orphan*/ SSL_write(int /*<<< orphan*/ *, char const *,
                             int /*<<< orphan*/);
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ cleanup_openssl();
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ configure_context(int /*<<< orphan*/ *);
int /*<<< orphan*/ *create_context();
int create_socket(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ init_openssl();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char const *);
int main(int argc, char **argv) {
  int sock;
  SSL_CTX *ctx;
  init_openssl();
  ctx = create_context();
  configure_context(ctx);
  sock = create_socket(4433);
  while (1) {
    struct sockaddr_in addr;
    uint len = sizeof(addr);
    SSL *ssl;
    const char reply[] = "test\n";
    int client = accept(sock, (struct sockaddr *)&addr, &len);
    if (client < 0) {
      perror("Unable to accept");
      exit(EXIT_FAILURE);
    }
    ssl = SSL_new(ctx);
    SSL_set_fd(ssl, client);
    if (SSL_accept(ssl) <= 0) {
      ERR_print_errors_fp(stderr);
    } else {
      SSL_write(ssl, reply, strlen(reply));
    }
    SSL_shutdown(ssl);
    SSL_free(ssl);
    close(client);
  }
  close(sock);
  SSL_CTX_free(ctx);
  cleanup_openssl();
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1060 <init_openssl@plt>
mov    $0x0,%al
call   10a0 <create_context@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   10e0 <configure_context@plt>
mov    $0x1151,%edi
call   10d0 <create_socket@plt>
mov    %eax,-0x14(%rbp)
movl   $0x4,-0x2c(%rbp)
mov    0xdb5(%rip),%eax
mov    %eax,-0x3e(%rbp)
mov    0xdaf(%rip),%ax
mov    %ax,-0x3a(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1100 <accept@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    1290 <main+0x90>
lea    0xd89(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
call   1110 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1090 <SSL_new@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x44(%rbp),%esi
call   1050 <SSL_set_fd@plt>
mov    -0x38(%rbp),%rdi
call   10c0 <SSL_accept@plt>
cmp    $0x0,%rax
jg     12cf <main+0xcf>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
call   1120 <ERR_print_errors_fp@plt>
jmp    12f7 <main+0xf7>
mov    -0x38(%rbp),%rax
mov    %rax,-0x58(%rbp)
lea    -0x3e(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x3e(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
call   1030 <SSL_write@plt>
mov    -0x38(%rbp),%rdi
call   10b0 <SSL_shutdown@plt>
mov    -0x38(%rbp),%rdi
call   1040 <SSL_free@plt>
mov    -0x44(%rbp),%edi
call   1080 <close@plt>
jmp    123e <main+0x3e>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
xor    %eax,%eax
call   1060 <init_openssl@plt>
xor    %eax,%eax
call   10a0 <create_context@plt>
mov    %rax,%r13
mov    %rax,%rdi
call   10e0 <configure_context@plt>
mov    $0x1151,%edi
call   10d0 <create_socket@plt>
mov    %eax,%ebp
movl   $0x4,0x4(%rsp)
movw   $0xa,0xc(%rsp)
movl   $0x74736574,0x8(%rsp)
74
lea    0x10(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %eax,%edi
call   1100 <accept@plt>
test   %eax,%eax
jns    1279 <main+0x79>
lea    0xda0(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
call   1110 <exit@plt>
mov    %eax,%ebx
lea    0x4(%rsp),%r15
lea    0x8(%rsp),%r12
jmp    12ec <main+0xec>
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1070 <strlen@plt>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <SSL_write@plt>
mov    %r14,%rdi
call   10b0 <SSL_shutdown@plt>
mov    %r14,%rdi
call   1040 <SSL_free@plt>
mov    %ebx,%edi
call   1080 <close@plt>
movl   $0x4,0x4(%rsp)
movw   $0xa,0xc(%rsp)
movl   $0x74736574,0x8(%rsp)
74
mov    %ebp,%edi
lea    0x10(%rsp),%rsi
mov    %r15,%rdx
call   1100 <accept@plt>
mov    %eax,%ebx
test   %eax,%eax
js     125f <main+0x5f>
mov    %r13,%rdi
call   1090 <SSL_new@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %ebx,%esi
call   1050 <SSL_set_fd@plt>
mov    %r14,%rdi
call   10c0 <SSL_accept@plt>
test   %rax,%rax
jg     1290 <main+0x90>
mov    0x2ccb(%rip),%rax
mov    (%rax),%edi
call   1120 <ERR_print_errors_fp@plt>
jmp    12a5 <main+0xa5>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@__const.main.reply = [6 x i8] c"test\0A\00"
@.str = [17 x i8] c"Unable to accept\00"
@EXIT_FAILURE = global i32 0
@stderr = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32*
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca i32*
%11 = alloca [6 x i8]
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = call i32 (...) @init_openssl()
%14 = call i32* (...) @create_context()
store i32* %14, i32** %7
%15 = load i32*, i32** %7
%16 = call i32 @configure_context(i32* %15)
%17 = call i32 @create_socket(i32 4433)
store i32 %17, i32* %6
br label %18
18: ; preds = %2, %47
store i32 4, i32* %9
%19 = bitcast [6 x i8]* %11 to i8*
%20 = load i32, i32* %6
%21 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%22 = call i32 @accept(i32 %20, %struct.sockaddr* %21, i32* %9)
store i32 %22, i32* %12
%23 = load i32, i32* %12
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %18
%26 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %18
%30 = load i32*, i32** %7
%31 = call i32* @SSL_new(i32* %30)
store i32* %31, i32** %10
%32 = load i32*, i32** %10
%33 = load i32, i32* %12
%34 = call i32 @SSL_set_fd(i32* %32, i32 %33)
%35 = load i32*, i32** %10
%36 = call i64 @SSL_accept(i32* %35)
%37 = icmp sle i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %29
%39 = load i32, i32* @stderr
%40 = call i32 @ERR_print_errors_fp(i32 %39)
br label %47
41: ; preds = %29
%42 = load i32*, i32** %10
%43 = getelementptr inbounds [6 x i8], [6 x i8]* %11, i64 0, i64 0
%44 = getelementptr inbounds [6 x i8], [6 x i8]* %11, i64 0, i64 0
%45 = call i32 @strlen(i8* %44)
%46 = call i32 @SSL_write(i32* %42, i8* %43, i32 %45)
br label %47
47: ; preds = %41, %38
%48 = load i32*, i32** %10
%49 = call i32 @SSL_shutdown(i32* %48)
%50 = load i32*, i32** %10
%51 = call i32 @SSL_free(i32* %50)
%52 = load i32, i32* %12
%53 = call i32 @close(i32 %52)
br label %18
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@__const.main.reply = [6 x i8] c"test\0A\00"
@.str = [17 x i8] c"Unable to accept\00"
@EXIT_FAILURE = global i32 0
@stderr = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca [6 x i8]
%6 = tail call i32 (...) @init_openssl()
%7 = tail call i32* (...) @create_context()
%8 = tail call i32 @configure_context(i32* %7)
%9 = tail call i32 @create_socket(i32 4433)
%10 = bitcast %struct.sockaddr_in* %3 to i8*
%11 = bitcast i32* %4 to i8*
%12 = getelementptr inbounds [6 x i8], [6 x i8]* %5, i64 0, i64 0
%13 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
store i32 4, i32* %4
%14 = call i32 @accept(i32 %9, %struct.sockaddr* %13, i32* %4)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %32, %2
%17 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %2, %32
%21 = phi i32 [ %36, %32 ], [ %14, %2 ]
%22 = call i32* @SSL_new(i32* %7)
%23 = call i32 @SSL_set_fd(i32* %22, i32 %21)
%24 = call i64 @SSL_accept(i32* %22)
%25 = icmp slt i64 %24, 1
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = load i32, i32* @stderr
%28 = call i32 @ERR_print_errors_fp(i32 %27)
br label %32
29: ; preds = %20
%30 = call i32 @strlen(i8* %12)
%31 = call i32 @SSL_write(i32* %22, i8* %12, i32 %30)
br label %32
32: ; preds = %29, %26
%33 = call i32 @SSL_shutdown(i32* %22)
%34 = call i32 @SSL_free(i32* %22)
%35 = call i32 @close(i32 %21)
store i32 4, i32* %4
%36 = call i32 @accept(i32 %9, %struct.sockaddr* %13, i32* %4)
%37 = icmp slt i32 %36, 0
br i1 %37, label %16, label %20
}