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

/* Variables and functions */
int /*<<< orphan*/ OPENSSL_free(char *);
char *SSL_get_cipher(int /*<<< orphan*/ *);
int /*<<< orphan*/ *SSL_get_peer_certificate(int /*<<< orphan*/ *);
char *SSL_get_version(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 /*<<< orphan*/ printf(char *, ...);
void dump_cert_info(SSL *ssl, bool server) {
  if (server) {
    printf("Ssl server version: %s", SSL_get_version(ssl));
  } else {
    printf("Client Version: %s", SSL_get_version(ssl));
  }
  printf("Using cipher %s", SSL_get_cipher(ssl));
  X509 *client_cert = SSL_get_peer_certificate(ssl);
  if (client_cert != NULL) {
    if (server) {
      printf("Client certificate:\n");
    } else {
      printf("Server certificate:\n");
    }
    char *str = X509_NAME_oneline(X509_get_subject_name(client_cert), 0, 0);
    if (str == NULL) {
      printf("warn X509 subject name is null");
    }
    printf("\t Subject: %s\n", str);
    OPENSSL_free(str);
    str = X509_NAME_oneline(X509_get_issuer_name(client_cert), 0, 0);
    if (str == NULL) {
      printf("warn X509 issuer name is null");
    }
    printf("\t Issuer: %s\n", str);
    OPENSSL_free(str);
    X509_free(client_cert);
  } else {
    printf("Client does not have certificate.\n");
  }
}


x86 assembly (clang, -O0):
<dump_cert_info>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11c8 <dump_cert_info+0x38>
mov    -0x8(%rbp),%rdi
call   1070 <SSL_get_version@plt>
mov    %rax,%rsi
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11e2 <dump_cert_info+0x52>
mov    -0x8(%rbp),%rdi
call   1070 <SSL_get_version@plt>
mov    %rax,%rsi
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1080 <SSL_get_cipher@plt>
mov    %rax,%rsi
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1090 <SSL_get_peer_certificate@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12e5 <dump_cert_info+0x155>
cmpl   $0x0,-0xc(%rbp)
je     1231 <dump_cert_info+0xa1>
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    123f <dump_cert_info+0xaf>
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <X509_get_subject_name@plt>
mov    %eax,%edi
xor    %edx,%edx
mov    %edx,%esi
call   1030 <X509_NAME_oneline@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1270 <dump_cert_info+0xe0>
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rsi
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   10b0 <OPENSSL_free@plt>
mov    -0x18(%rbp),%rdi
call   1060 <X509_get_issuer_name@plt>
mov    %eax,%edi
xor    %edx,%edx
mov    %edx,%esi
call   1030 <X509_NAME_oneline@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12bc <dump_cert_info+0x12c>
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rsi
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   10b0 <OPENSSL_free@plt>
mov    -0x18(%rbp),%rdi
call   10a0 <X509_free@plt>
jmp    12f3 <dump_cert_info+0x163>
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_cert_info>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
call   1080 <SSL_get_version@plt>
test   %ebp,%ebp
lea    0xe61(%rip),%rcx
lea    0xe43(%rip),%rdi
cmove  %rcx,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1090 <SSL_get_cipher@plt>
lea    0xe50(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   10a0 <SSL_get_peer_certificate@plt>
test   %rax,%rax
je     12a3 <dump_cert_info+0x103>
mov    %rax,%rbx
test   %ebp,%ebp
lea    0xeb5(%rip),%rax
lea    0xec2(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1060 <X509_get_subject_name@plt>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <X509_NAME_oneline@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    123a <dump_cert_info+0x9a>
lea    0xe07(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe18(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbp,%rdi
call   10c0 <OPENSSL_free@plt>
mov    %rbx,%rdi
call   1070 <X509_get_issuer_name@plt>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <X509_NAME_oneline@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    127c <dump_cert_info+0xdc>
lea    0xdf3(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe03(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbp,%rdi
call   10c0 <OPENSSL_free@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    10b0 <X509_free@plt>
lea    0xdea(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [23 x i8] c"Ssl server version: %s\00"
@.str.1 = [19 x i8] c"Client Version: %s\00"
@.str.2 = [16 x i8] c"Using cipher %s\00"
@.str.3 = [21 x i8] c"Client certificate:\0A\00"
@.str.4 = [21 x i8] c"Server certificate:\0A\00"
@.str.5 = [31 x i8] c"warn X509 subject name is null\00"
@.str.6 = [15 x i8] c"\09 Subject: %s\0A\00"
@.str.7 = [30 x i8] c"warn X509 issuer name is null\00"
@.str.8 = [14 x i8] c"\09 Issuer: %s\0A\00"
@.str.9 = [35 x i8] c"Client does not have certificate.\0A\00"
define void @dump_cert_info(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
store i32* %0, i32** %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i32*, i32** %3
%11 = call i8* @SSL_get_version(i32* %10)
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %17
13: ; preds = %2
%14 = load i32*, i32** %3
%15 = call i8* @SSL_get_version(i32* %14)
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %15)
br label %17
17: ; preds = %13, %9
%18 = load i32*, i32** %3
%19 = call i8* @SSL_get_cipher(i32* %18)
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %19)
%21 = load i32*, i32** %3
%22 = call i32* @SSL_get_peer_certificate(i32* %21)
store i32* %22, i32** %5
%23 = load i32*, i32** %5
%24 = icmp ne i32* %23, null
br i1 %24, label %25, label %59
25: ; preds = %17
%26 = load i32, i32* %4
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %32
30: ; preds = %25
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %32
32: ; preds = %30, %28
%33 = load i32*, i32** %5
%34 = call i32 @X509_get_subject_name(i32* %33)
%35 = call i8* @X509_NAME_oneline(i32 %34, i32 0, i32 0)
store i8* %35, i8** %6
%36 = load i8*, i8** %6
%37 = icmp eq i8* %36, null
br i1 %37, label %38, label %40
38: ; preds = %32
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
br label %40
40: ; preds = %38, %32
%41 = load i8*, i8** %6
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i8* %41)
%43 = load i8*, i8** %6
%44 = call i32 @OPENSSL_free(i8* %43)
%45 = load i32*, i32** %5
%46 = call i32 @X509_get_issuer_name(i32* %45)
%47 = call i8* @X509_NAME_oneline(i32 %46, i32 0, i32 0)
store i8* %47, i8** %6
%48 = load i8*, i8** %6
%49 = icmp eq i8* %48, null
br i1 %49, label %50, label %52
50: ; preds = %40
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0))
br label %52
52: ; preds = %50, %40
%53 = load i8*, i8** %6
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0), i8* %53)
%55 = load i8*, i8** %6
%56 = call i32 @OPENSSL_free(i8* %55)
%57 = load i32*, i32** %5
%58 = call i32 @X509_free(i32* %57)
br label %61
59: ; preds = %17
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.9, i64 0, i64 0))
br label %61
61: ; preds = %59, %52
ret void
}

LLVM IR (-O2):
@.str = [23 x i8] c"Ssl server version: %s\00"
@.str.1 = [19 x i8] c"Client Version: %s\00"
@.str.2 = [16 x i8] c"Using cipher %s\00"
@.str.5 = [31 x i8] c"warn X509 subject name is null\00"
@.str.6 = [15 x i8] c"\09 Subject: %s\0A\00"
@.str.7 = [30 x i8] c"warn X509 issuer name is null\00"
@.str.8 = [14 x i8] c"\09 Issuer: %s\0A\00"
@str = [34 x i8] c"Client does not have certificate.\00"
@str.10 = [20 x i8] c"Server certificate:\00"
@str.11 = [20 x i8] c"Client certificate:\00"
define void @dump_cert_info(i32* %0, i32 %1) {
%3 = icmp eq i32 %1, 0
%4 = tail call i8* @SSL_get_version(i32* %0)
%5 = select i1 %3, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0)
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %5, i8* %4)
%7 = tail call i8* @SSL_get_cipher(i32* %0)
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %7)
%9 = tail call i32* @SSL_get_peer_certificate(i32* %0)
%10 = icmp eq i32* %9, null
br i1 %10, label %31, label %11
11: ; preds = %2
%12 = select i1 %3, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @str.10, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @str.11, i64 0, i64 0)
%13 = tail call i32 @puts(i8* dereferenceable(1) %12)
%14 = tail call i32 @X509_get_subject_name(i32* %9)
%15 = tail call i8* @X509_NAME_oneline(i32 %14, i32 0, i32 0)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
br label %19
19: ; preds = %17, %11
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i8* %15)
%21 = tail call i32 @OPENSSL_free(i8* %15)
%22 = tail call i32 @X509_get_issuer_name(i32* %9)
%23 = tail call i8* @X509_NAME_oneline(i32 %22, i32 0, i32 0)
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0))
br label %27
27: ; preds = %25, %19
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0), i8* %23)
%29 = tail call i32 @OPENSSL_free(i8* %23)
%30 = tail call i32 @X509_free(i32* %9)
br label %33
31: ; preds = %2
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
br label %33
33: ; preds = %31, %27
ret void
}