#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 */
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 SSL_read(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ SSL_set_fd(int /*<<< orphan*/ *, int);
int accept(int, struct sockaddr *, int *);
int atoi(char *);
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*/ printf(char *, ...);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  if (argc != 2) {
    printf("usage: %s lport\n", argv[0]);
    printf("example: %s 4080\n", argv[0]);
    return -1;
  }
  int port = atoi(argv[1]);
  init_openssl();
  SSL_CTX *ctx = create_context();
  configure_context(ctx);
  SSL *ssl = SSL_new(ctx);
  int listen_sock = create_socket(port);
  printf("Opened the port %d, and begin to listen...\n", port);
  struct sockaddr_in addr;
  int len = sizeof(addr);
  int client_sock = accept(listen_sock, (struct sockaddr *)&addr, &len);
  if (client_sock < 0) {
    perror("Unable to accept");
    return -1;
  }
  printf("Accepted a new client\n");
  SSL_set_fd(ssl, client_sock);
  if (SSL_accept(ssl) <= 0) {
    ERR_print_errors_fp(stderr);
    exit(EXIT_FAILURE);
  }
  printf("Finished SSL handshake\n");
  printf("Begin to receive data from the client...\n");
  while (1) {
    char buf[256];
    int len = 256;
    int ret = SSL_read(ssl, buf, len - 1);
    if (ret <= 0) {
      ERR_print_errors_fp(stderr);
      exit(EXIT_FAILURE);
    }
    int i = 0;
    for (; i < ret; i++) {
      putchar(buf[i]);
    }
  }
  close(client_sock);
  SSL_free(ssl);
  close(listen_sock);
  SSL_CTX_free(ctx);
  cleanup_openssl();
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1249 <main+0x59>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d9 <main+0x1e9>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10f0 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1050 <init_openssl@plt>
mov    $0x0,%al
call   1090 <create_context@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   10c0 <configure_context@plt>
mov    -0x20(%rbp),%rdi
call   1080 <SSL_new@plt>
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   10b0 <create_socket@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%esi
lea    0xd8d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x4,-0x34(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   10e0 <accept@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    12d9 <main+0xe9>
lea    0xd87(%rip),%rdi
call   10d0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d9 <main+0x1e9>
lea    0xd80(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%esi
call   1040 <SSL_set_fd@plt>
mov    -0x28(%rbp),%rdi
call   10a0 <SSL_accept@plt>
cmp    $0x0,%rax
jg     1322 <main+0x132>
mov    0x2cd3(%rip),%rax
mov    (%rax),%edi
call   1110 <ERR_print_errors_fp@plt>
mov    0x2cbd(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
lea    0xd4e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xd58(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x100,-0x144(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x140(%rbp),%rsi
mov    -0x144(%rbp),%edx
sub    $0x1,%edx
call   1070 <SSL_read@plt>
mov    %eax,-0x148(%rbp)
cmpl   $0x0,-0x148(%rbp)
jg     1390 <main+0x1a0>
mov    0x2c65(%rip),%rax
mov    (%rax),%edi
call   1110 <ERR_print_errors_fp@plt>
mov    0x2c4f(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
movl   $0x0,-0x14c(%rbp)
mov    -0x14c(%rbp),%eax
cmp    -0x148(%rbp),%eax
jge    13d4 <main+0x1e4>
movslq -0x14c(%rbp),%rax
movsbl -0x140(%rbp,%rax,1),%edi
ff
call   1030 <putchar@plt>
mov    -0x14c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14c(%rbp)
jmp    139a <main+0x1aa>
jmp    133e <main+0x14e>
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x118,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
je     124a <main+0x4a>
mov    (%rbx),%rsi
lea    0xde1(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    (%rbx),%rsi
lea    0xde1(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xffffffff,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rbx),%rdi
call   1100 <atoi@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1060 <init_openssl@plt>
xor    %eax,%eax
call   10a0 <create_context@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   10d0 <configure_context@plt>
mov    %rbx,%rdi
call   1090 <SSL_new@plt>
mov    %rax,%r15
mov    %ebp,%edi
call   10c0 <create_socket@plt>
mov    %eax,%ebx
lea    0xd9a(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1070 <printf@plt>
movl   $0x4,0x4(%rsp)
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   10f0 <accept@plt>
test   %eax,%eax
jns    12c0 <main+0xc0>
lea    0xd99(%rip),%rdi
call   10e0 <perror@plt>
jmp    1237 <main+0x37>
mov    %eax,%ebp
lea    0xd97(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
mov    %ebp,%esi
call   1050 <SSL_set_fd@plt>
mov    %r15,%rdi
call   10b0 <SSL_accept@plt>
test   %rax,%rax
jle    1357 <main+0x157>
lea    0xd8a(%rip),%rdi
call   1040 <puts@plt>
lea    0xd95(%rip),%rdi
call   1040 <puts@plt>
lea    0x10(%rsp),%rsi
mov    %r15,%rdi
mov    $0xff,%edx
call   1080 <SSL_read@plt>
test   %eax,%eax
jle    1357 <main+0x157>
lea    0x10(%rsp),%r14
nopl   0x0(%rax,%rax,1)
mov    %eax,%ebx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsbl 0x10(%rsp,%rbp,1),%edi
call   1030 <putchar@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1330 <main+0x130>
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0xff,%edx
call   1080 <SSL_read@plt>
test   %eax,%eax
jg     1320 <main+0x120>
mov    0x2c82(%rip),%rax
mov    (%rax),%edi
call   1120 <ERR_print_errors_fp@plt>
mov    0x2c6c(%rip),%rax
mov    (%rax),%edi
call   1110 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [17 x i8] c"usage: %s lport\0A\00"
@.str.1 = [18 x i8] c"example: %s 4080\0A\00"
@.str.2 = [44 x i8] c"Opened the port %d, and begin to listen...\0A\00"
@.str.3 = [17 x i8] c"Unable to accept\00"
@.str.4 = [23 x i8] c"Accepted a new client\0A\00"
@stderr = global i32 0
@EXIT_FAILURE = global i32 0
@.str.5 = [24 x i8] c"Finished SSL handshake\0A\00"
@.str.6 = [42 x i8] c"Begin to receive data from the client...\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 i32
%10 = alloca %struct.sockaddr_in
%11 = alloca i32
%12 = alloca i32
%13 = alloca [256 x i8]
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%17 = load i32, i32* %4
%18 = icmp ne i32 %17, 2
br i1 %18, label %19, label %28
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 0
%22 = load i8*, i8** %21
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %22)
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 0
%26 = load i8*, i8** %25
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %26)
store i32 -1, i32* %3
br label %94
28: ; preds = %2
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 @atoi(i8* %31)
store i32 %32, i32* %6
%33 = call i32 (...) @init_openssl()
%34 = call i32* (...) @create_context()
store i32* %34, i32** %7
%35 = load i32*, i32** %7
%36 = call i32 @configure_context(i32* %35)
%37 = load i32*, i32** %7
%38 = call i32* @SSL_new(i32* %37)
store i32* %38, i32** %8
%39 = load i32, i32* %6
%40 = call i32 @create_socket(i32 %39)
store i32 %40, i32* %9
%41 = load i32, i32* %6
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %41)
store i32 4, i32* %11
%43 = load i32, i32* %9
%44 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%45 = call i32 @accept(i32 %43, %struct.sockaddr* %44, i32* %11)
store i32 %45, i32* %12
%46 = load i32, i32* %12
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %28
%49 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %94
50: ; preds = %28
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%52 = load i32*, i32** %8
%53 = load i32, i32* %12
%54 = call i32 @SSL_set_fd(i32* %52, i32 %53)
%55 = load i32*, i32** %8
%56 = call i64 @SSL_accept(i32* %55)
%57 = icmp sle i64 %56, 0
br i1 %57, label %58, label %63
58: ; preds = %50
%59 = load i32, i32* @stderr
%60 = call i32 @ERR_print_errors_fp(i32 %59)
%61 = load i32, i32* @EXIT_FAILURE
%62 = call i32 @exit(i32 %61)
unreachable
63: ; preds = %50
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.6, i64 0, i64 0))
br label %66
66: ; preds = %63, %93
store i32 256, i32* %14
%67 = load i32*, i32** %8
%68 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
%69 = load i32, i32* %14
%70 = sub nsw i32 %69, 1
%71 = call i32 @SSL_read(i32* %67, i8* %68, i32 %70)
store i32 %71, i32* %15
%72 = load i32, i32* %15
%73 = icmp sle i32 %72, 0
br i1 %73, label %74, label %79
74: ; preds = %66
%75 = load i32, i32* @stderr
%76 = call i32 @ERR_print_errors_fp(i32 %75)
%77 = load i32, i32* @EXIT_FAILURE
%78 = call i32 @exit(i32 %77)
unreachable
79: ; preds = %66
store i32 0, i32* %16
br label %80
80: ; preds = %90, %79
%81 = load i32, i32* %16
%82 = load i32, i32* %15
%83 = icmp slt i32 %81, %82
br i1 %83, label %84, label %93
84: ; preds = %80
%85 = load i32, i32* %16
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 %86
%88 = load i8, i8* %87
%89 = call i32 @putchar(i8 signext %88)
br label %90
90: ; preds = %84
%91 = load i32, i32* %16
%92 = add nsw i32 %91, 1
store i32 %92, i32* %16
br label %80
93: ; preds = %80
br label %66
94: ; preds = %48, %19
%95 = load i32, i32* %3
ret i32 %95
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [17 x i8] c"usage: %s lport\0A\00"
@.str.1 = [18 x i8] c"example: %s 4080\0A\00"
@.str.2 = [44 x i8] c"Opened the port %d, and begin to listen...\0A\00"
@.str.3 = [17 x i8] c"Unable to accept\00"
@stderr = global i32 0
@EXIT_FAILURE = global i32 0
@str = [22 x i8] c"Accepted a new client\00"
@str.7 = [23 x i8] c"Finished SSL handshake\00"
@str.8 = [41 x i8] c"Begin to receive data from the client...\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca [256 x i8]
%6 = icmp eq i32 %0, 2
br i1 %6, label %12, label %7
7: ; preds = %2
%8 = load i8*, i8** %1
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = load i8*, i8** %1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %10)
br label %63
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i32 @atoi(i8* %14)
%16 = tail call i32 (...) @init_openssl()
%17 = tail call i32* (...) @create_context()
%18 = tail call i32 @configure_context(i32* %17)
%19 = tail call i32* @SSL_new(i32* %17)
%20 = tail call i32 @create_socket(i32 %15)
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%22 = bitcast %struct.sockaddr_in* %3 to i8*
%23 = bitcast i32* %4 to i8*
store i32 4, i32* %4
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i32 @accept(i32 %20, %struct.sockaddr* %24, i32* %4)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %12
%28 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %63
29: ; preds = %12
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%31 = call i32 @SSL_set_fd(i32* %19, i32 %25)
%32 = call i64 @SSL_accept(i32* %19)
%33 = icmp slt i64 %32, 1
br i1 %33, label %34, label %39
34: ; preds = %29
%35 = load i32, i32* @stderr
%36 = call i32 @ERR_print_errors_fp(i32 %35)
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %29
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.7, i64 0, i64 0))
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str.8, i64 0, i64 0))
%42 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%43 = call i32 @SSL_read(i32* %19, i8* %42, i32 255)
%44 = icmp slt i32 %43, 1
br i1 %44, label %48, label %45
45: ; preds = %39, %60
%46 = phi i32 [ %61, %60 ], [ %43, %39 ]
%47 = zext i32 %46 to i64
br label %53
48: ; preds = %60, %39
%49 = load i32, i32* @stderr
%50 = call i32 @ERR_print_errors_fp(i32 %49)
%51 = load i32, i32* @EXIT_FAILURE
%52 = call i32 @exit(i32 %51)
unreachable
53: ; preds = %45, %53
%54 = phi i64 [ 0, %45 ], [ %58, %53 ]
%55 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %54
%56 = load i8, i8* %55
%57 = call i32 @putchar(i8 signext %56)
%58 = add nuw nsw i64 %54, 1
%59 = icmp eq i64 %58, %47
br i1 %59, label %60, label %53
60: ; preds = %53
%61 = call i32 @SSL_read(i32* %19, i8* %42, i32 255)
%62 = icmp slt i32 %61, 1
br i1 %62, label %48, label %45
63: ; preds = %27, %7
ret i32 -1
}