#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 socklen_t;
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*/ EVP_cleanup();
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 accept(int, struct sockaddr *, int *);
int atoi(char *);
int /*<<< orphan*/ client_do(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ configure_context(int /*<<< orphan*/ *);
int /*<<< orphan*/ create_context(int /*<<< orphan*/ **);
int create_socket(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ init_openssl();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  int sock, port;
  SSL_CTX *ctx;
  if (argc < 2) {
    printf("Usage: %s [port] [sock name]\n", *argv);
    return 0;
  }
  port = atoi(argv[1]);
  init_openssl();
  create_context(&ctx);
  configure_context(ctx);
  sock = create_socket(port);
  while (1) {
    struct sockaddr_in addr;
    socklen_t len = sizeof(addr);
    SSL *ssl;
    int client = accept(sock, (struct sockaddr *)&addr, &len);
    if (client < 0) {
      perror("could not accept client\n");
      exit(EXIT_FAILURE);
    }
    ssl = SSL_new(ctx);
    SSL_set_fd(ssl, client);
    if (SSL_accept(ssl) <= 0)
      ERR_print_errors_fp(stderr);
    else
      client_do(ssl, argv[2]);
    SSL_free(ssl);
    close(client);
  }
  close(sock);
  SSL_CTX_free(ctx);
  EVP_cleanup();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    123b <main+0x3b>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdd2(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1100 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1050 <init_openssl@plt>
lea    -0x20(%rbp),%rdi
call   1090 <create_context@plt>
mov    -0x20(%rbp),%rdi
call   10d0 <configure_context@plt>
mov    -0x18(%rbp),%edi
call   10c0 <create_socket@plt>
mov    %eax,-0x14(%rbp)
movl   $0x4,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   10f0 <accept@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    12ad <main+0xad>
lea    0xd84(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
call   1110 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1080 <SSL_new@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x3c(%rbp),%esi
call   1040 <SSL_set_fd@plt>
mov    -0x38(%rbp),%rdi
call   10b0 <SSL_accept@plt>
cmp    $0x0,%rax
jg     12ec <main+0xec>
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
call   1120 <ERR_print_errors_fp@plt>
jmp    12fd <main+0xfd>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
call   10a0 <client_do@plt>
mov    -0x38(%rbp),%rdi
call   1030 <SSL_free@plt>
mov    -0x3c(%rbp),%edi
call   1070 <close@plt>
jmp    126f <main+0x6f>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r13
cmp    $0x1,%edi
jg     1237 <main+0x37>
mov    0x0(%r13),%rsi
lea    0xddf(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%r13),%rdi
call   1100 <atoi@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <init_openssl@plt>
lea    0x10(%rsp),%rdi
call   1090 <create_context@plt>
mov    0x10(%rsp),%rdi
call   10d0 <configure_context@plt>
mov    %ebx,%edi
call   10c0 <create_socket@plt>
mov    %eax,%r12d
movl   $0x4,0x4(%rsp)
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %eax,%edi
call   10f0 <accept@plt>
test   %eax,%eax
jns    129e <main+0x9e>
lea    0xd93(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
call   1110 <exit@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%r14
lea    0x4(%rsp),%r15
jmp    12e7 <main+0xe7>
nopl   0x0(%rax)
mov    0x10(%r13),%rsi
mov    %rbx,%rdi
call   10a0 <client_do@plt>
mov    %rbx,%rdi
call   1030 <SSL_free@plt>
mov    %ebp,%edi
call   1070 <close@plt>
movl   $0x4,0x4(%rsp)
mov    %r12d,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10f0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1284 <main+0x84>
mov    0x10(%rsp),%rdi
call   1080 <SSL_new@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %ebp,%esi
call   1040 <SSL_set_fd@plt>
mov    %rbx,%rdi
call   10b0 <SSL_accept@plt>
test   %rax,%rax
jg     12b0 <main+0xb0>
mov    0x2cce(%rip),%rax
mov    (%rax),%edi
call   1120 <ERR_print_errors_fp@plt>
jmp    12bc <main+0xbc>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [30 x i8] c"Usage: %s [port] [sock name]\0A\00"
@.str.1 = [25 x i8] c"could not accept client\0A\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 i32*
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 2
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = load i8*, i8** %16
%18 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %17)
ret i32 0
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %7
%24 = call i32 (...) @init_openssl()
%25 = call i32 @create_context(i32** %8)
%26 = load i32*, i32** %8
%27 = call i32 @configure_context(i32* %26)
%28 = load i32, i32* %7
%29 = call i32 @create_socket(i32 %28)
store i32 %29, i32* %6
br label %30
30: ; preds = %19, %58
store i32 4, i32* %10
%31 = load i32, i32* %6
%32 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%33 = call i32 @accept(i32 %31, %struct.sockaddr* %32, i32* %10)
store i32 %33, i32* %12
%34 = load i32, i32* %12
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %30
%37 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %30
%41 = load i32*, i32** %8
%42 = call i32* @SSL_new(i32* %41)
store i32* %42, i32** %11
%43 = load i32*, i32** %11
%44 = load i32, i32* %12
%45 = call i32 @SSL_set_fd(i32* %43, i32 %44)
%46 = load i32*, i32** %11
%47 = call i64 @SSL_accept(i32* %46)
%48 = icmp sle i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %40
%50 = load i32, i32* @stderr
%51 = call i32 @ERR_print_errors_fp(i32 %50)
br label %58
52: ; preds = %40
%53 = load i32*, i32** %11
%54 = load i8**, i8*** %5
%55 = getelementptr inbounds i8*, i8** %54, i64 2
%56 = load i8*, i8** %55
%57 = call i32 @client_do(i32* %53, i8* %56)
br label %58
58: ; preds = %52, %49
%59 = load i32*, i32** %11
%60 = call i32 @SSL_free(i32* %59)
%61 = load i32, i32* %12
%62 = call i32 @close(i32 %61)
br label %30
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [30 x i8] c"Usage: %s [port] [sock name]\0A\00"
@.str.1 = [25 x i8] c"could not accept client\0A\00"
@EXIT_FAILURE = global i32 0
@stderr = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32*
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = bitcast i32** %3 to i8*
%7 = icmp slt i32 %0, 2
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %9)
ret i32 0
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = tail call i32 (...) @init_openssl()
%16 = call i32 @create_context(i32** %3)
%17 = load i32*, i32** %3
%18 = call i32 @configure_context(i32* %17)
%19 = call i32 @create_socket(i32 %14)
%20 = bitcast %struct.sockaddr_in* %4 to i8*
%21 = bitcast i32* %5 to i8*
%22 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
store i32 4, i32* %5
%23 = call i32 @accept(i32 %19, %struct.sockaddr* %22, i32* %5)
%24 = icmp slt i32 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %11
%26 = getelementptr inbounds i8*, i8** %1, i64 2
br label %31
27: ; preds = %44, %11
%28 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %25, %44
%32 = phi i32 [ %23, %25 ], [ %47, %44 ]
%33 = load i32*, i32** %3
%34 = call i32* @SSL_new(i32* %33)
%35 = call i32 @SSL_set_fd(i32* %34, i32 %32)
%36 = call i64 @SSL_accept(i32* %34)
%37 = icmp slt i64 %36, 1
br i1 %37, label %38, label %41
38: ; preds = %31
%39 = load i32, i32* @stderr
%40 = call i32 @ERR_print_errors_fp(i32 %39)
br label %44
41: ; preds = %31
%42 = load i8*, i8** %26
%43 = call i32 @client_do(i32* %34, i8* %42)
br label %44
44: ; preds = %41, %38
%45 = call i32 @SSL_free(i32* %34)
%46 = call i32 @close(i32 %32)
store i32 4, i32* %5
%47 = call i32 @accept(i32 %19, %struct.sockaddr* %22, i32* %5)
%48 = icmp slt i32 %47, 0
br i1 %48, label %27, label %31
}