#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;
};
typedef int /*<<< orphan*/ X509;
typedef int /*<<< orphan*/ SSL_METHOD;
typedef int /*<<< orphan*/ SSL_CTX;
typedef int /*<<< orphan*/ SSL;

/* Variables and functions */
int /*<<< orphan*/ ERR_print_errors_fp(int /*<<< orphan*/);
int SSL_accept(int /*<<< orphan*/ *);
int /*<<< orphan*/ *SSL_get_peer_certificate(int /*<<< orphan*/ *);
int /*<<< orphan*/ *SSL_new(int /*<<< orphan*/ *);
int /*<<< orphan*/ SSL_set_fd(int /*<<< orphan*/ *, int);
int SSL_shutdown(int /*<<< orphan*/ *);
char *X509_NAME_oneline(int /*<<< orphan*/, int /*<<< orphan*/,
                        int /*<<< orphan*/);
int /*<<< orphan*/ X509_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ X509_get_issuer_name(int /*<<< orphan*/ *);
int /*<<< orphan*/ X509_get_subject_name(int /*<<< orphan*/ *);
int close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ run_php_prog_ssl(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *ssl_sock_read(int sock, SSL_CTX *ctx) {
  int err;
  int verify_client = 0;
  int pid;
  struct sockaddr_in sa_serv;
  struct sockaddr_in sa_cli;
  size_t client_len;
  char *str;
  char buf[4096];
  SSL *ssl;
  SSL_METHOD *meth;
  X509 *client_cert = NULL;
  ssl = SSL_new(ctx);
  if ((ssl) == NULL)
    exit(1);
  SSL_set_fd(ssl, sock);
  err = SSL_accept(ssl);
  if ((err) == -1) {
    ERR_print_errors_fp(stderr);
    exit(1);
  };
  if (verify_client == 1) {
    client_cert = SSL_get_peer_certificate(ssl);
    if (client_cert != NULL) {
      str = X509_NAME_oneline(X509_get_subject_name(client_cert), 0, 0);
      if ((str) == NULL)
        exit(1);
      free(str);
      str = X509_NAME_oneline(X509_get_issuer_name(client_cert), 0, 0);
      if ((str) == NULL)
        exit(1);
      free(str);
      X509_free(client_cert);
    } else {
      printf("The SSL client does not have certificate.\n");
    }
  }
  run_php_prog_ssl(ssl, 0);
  err = SSL_shutdown(ssl);
  if ((err) == -1) {
    ERR_print_errors_fp(stderr);
    exit(1);
  };
  err = close(sock);
  if ((err) == -1) {
    perror("close");
    exit(1);
  };
  exit(0);
}


x86 assembly (clang, -O0):
<ssl_sock_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1060,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movq   $0x0,-0x1058(%rbp)
mov    -0x10(%rbp),%rdi
call   10a0 <SSL_new@plt>
mov    %rax,-0x1048(%rbp)
cmpq   $0x0,-0x1048(%rbp)
jne    124c <ssl_sock_read+0x4c>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    -0x1048(%rbp),%rdi
mov    -0x4(%rbp),%esi
call   1040 <SSL_set_fd@plt>
mov    -0x1048(%rbp),%rdi
call   10c0 <SSL_accept@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    128c <ssl_sock_read+0x8c>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
call   1120 <ERR_print_errors_fp@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
cmpl   $0x1,-0x18(%rbp)
jne    134b <ssl_sock_read+0x14b>
mov    -0x1048(%rbp),%rdi
call   10d0 <SSL_get_peer_certificate@plt>
mov    %rax,-0x1058(%rbp)
cmpq   $0x0,-0x1058(%rbp)
je     133a <ssl_sock_read+0x13a>
mov    -0x1058(%rbp),%rdi
call   1070 <X509_get_subject_name@plt>
mov    %eax,%edi
xor    %edx,%edx
mov    %edx,%esi
call   1050 <X509_NAME_oneline@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12e7 <ssl_sock_read+0xe7>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1058(%rbp),%rdi
call   1080 <X509_get_issuer_name@plt>
mov    %eax,%edi
xor    %edx,%edx
mov    %edx,%esi
call   1050 <X509_NAME_oneline@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1320 <ssl_sock_read+0x120>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1058(%rbp),%rdi
call   10f0 <X509_free@plt>
jmp    1346 <ssl_sock_read+0x146>
lea    0xcbf(%rip),%rdi
call   1060 <printf@plt>
jmp    134b <ssl_sock_read+0x14b>
mov    -0x1048(%rbp),%rdi
xor    %esi,%esi
call   10e0 <run_php_prog_ssl@plt>
mov    -0x1048(%rbp),%rdi
call   10b0 <SSL_shutdown@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    138a <ssl_sock_read+0x18a>
mov    0x2c67(%rip),%rax
mov    (%rax),%edi
call   1120 <ERR_print_errors_fp@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    -0x4(%rbp),%edi
call   1090 <close@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    13b5 <ssl_sock_read+0x1b5>
lea    0xc85(%rip),%rdi
call   1100 <perror@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
xor    %edi,%edi
call   1110 <exit@plt>

x86 assembly (clang, -O2):
<ssl_sock_read>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    %rsi,%rdi
call   1050 <SSL_new@plt>
test   %rax,%rax
jne    11ac <ssl_sock_read+0x1c>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %ebp,%esi
call   1030 <SSL_set_fd@plt>
mov    %rbx,%rdi
call   1070 <SSL_accept@plt>
cmp    $0xffffffff,%eax
je     11dd <ssl_sock_read+0x4d>
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <run_php_prog_ssl@plt>
mov    %rbx,%rdi
call   1060 <SSL_shutdown@plt>
cmp    $0xffffffff,%eax
jne    11f5 <ssl_sock_read+0x65>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   10b0 <ERR_print_errors_fp@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    %ebp,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    1217 <ssl_sock_read+0x87>
lea    0xdf8(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
@stderr = global i32 0
@.str = [43 x i8] c"The SSL client does not have certificate.\0A\00"
@.str.1 = [6 x i8] c"close\00"
define i8* @ssl_sock_read(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.sockaddr_in
%10 = alloca i64
%11 = alloca i8*
%12 = alloca [4096 x i8]
%13 = alloca i32*
%14 = alloca i32*
%15 = alloca i32*
store i32 %0, i32* %3
store i32* %1, i32** %4
store i32 0, i32* %6
store i32* null, i32** %15
%16 = load i32*, i32** %4
%17 = call i32* @SSL_new(i32* %16)
store i32* %17, i32** %13
%18 = load i32*, i32** %13
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %22
20: ; preds = %2
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %2
%23 = load i32*, i32** %13
%24 = load i32, i32* %3
%25 = call i32 @SSL_set_fd(i32* %23, i32 %24)
%26 = load i32*, i32** %13
%27 = call i32 @SSL_accept(i32* %26)
store i32 %27, i32* %5
%28 = load i32, i32* %5
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %34
30: ; preds = %22
%31 = load i32, i32* @stderr
%32 = call i32 @ERR_print_errors_fp(i32 %31)
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %22
%35 = load i32, i32* %6
%36 = icmp eq i32 %35, 1
br i1 %36, label %37, label %68
37: ; preds = %34
%38 = load i32*, i32** %13
%39 = call i32* @SSL_get_peer_certificate(i32* %38)
store i32* %39, i32** %15
%40 = load i32*, i32** %15
%41 = icmp ne i32* %40, null
br i1 %41, label %42, label %65
42: ; preds = %37
%43 = load i32*, i32** %15
%44 = call i32 @X509_get_subject_name(i32* %43)
%45 = call i8* @X509_NAME_oneline(i32 %44, i32 0, i32 0)
store i8* %45, i8** %11
%46 = load i8*, i8** %11
%47 = icmp eq i8* %46, null
br i1 %47, label %48, label %50
48: ; preds = %42
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %42
%51 = load i8*, i8** %11
%52 = call i32 @free(i8* %51)
%53 = load i32*, i32** %15
%54 = call i32 @X509_get_issuer_name(i32* %53)
%55 = call i8* @X509_NAME_oneline(i32 %54, i32 0, i32 0)
store i8* %55, i8** %11
%56 = load i8*, i8** %11
%57 = icmp eq i8* %56, null
br i1 %57, label %58, label %60
58: ; preds = %50
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %50
%61 = load i8*, i8** %11
%62 = call i32 @free(i8* %61)
%63 = load i32*, i32** %15
%64 = call i32 @X509_free(i32* %63)
br label %67
65: ; preds = %37
%66 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
br label %67
67: ; preds = %65, %60
br label %68
68: ; preds = %67, %34
%69 = load i32*, i32** %13
%70 = call i32 @run_php_prog_ssl(i32* %69, i32 0)
%71 = load i32*, i32** %13
%72 = call i32 @SSL_shutdown(i32* %71)
store i32 %72, i32* %5
%73 = load i32, i32* %5
%74 = icmp eq i32 %73, -1
br i1 %74, label %75, label %79
75: ; preds = %68
%76 = load i32, i32* @stderr
%77 = call i32 @ERR_print_errors_fp(i32 %76)
%78 = call i32 @exit(i32 1)
unreachable
79: ; preds = %68
%80 = load i32, i32* %3
%81 = call i32 @close(i32 %80)
store i32 %81, i32* %5
%82 = load i32, i32* %5
%83 = icmp eq i32 %82, -1
br i1 %83, label %84, label %87
84: ; preds = %79
%85 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%86 = call i32 @exit(i32 1)
unreachable
87: ; preds = %79
%88 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str.1 = [6 x i8] c"close\00"
define noalias i8* @ssl_sock_read(i32 %0, i32* %1) {
%3 = tail call i32* @SSL_new(i32* %1)
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %2
%8 = tail call i32 @SSL_set_fd(i32* %3, i32 %0)
%9 = tail call i32 @SSL_accept(i32* %3)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %7
%12 = load i32, i32* @stderr
%13 = tail call i32 @ERR_print_errors_fp(i32 %12)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %7
%16 = tail call i32 @run_php_prog_ssl(i32* %3, i32 0)
%17 = tail call i32 @SSL_shutdown(i32* %3)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = tail call i32 @ERR_print_errors_fp(i32 %20)
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %15
%24 = tail call i32 @close(i32 %0)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %23
%30 = tail call i32 @exit(i32 0)
unreachable
}