#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 EVP_MAX_MD_SIZE;
int /*<<< orphan*/ EVP_md5();
int MD5_DIGEST_LENGTH;
int /*<<< orphan*/ *SSL_get_peer_certificate(int /*<<< orphan*/ *const);
scalar_t__ X509_digest(int /*<<< orphan*/ *, int /*<<< orphan*/,
                       unsigned char *, unsigned int *);
int /*<<< orphan*/ X509_free(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(unsigned char *, int, unsigned int);
int sprintf(char *, char *, unsigned char);
char *get_fingerprint(SSL *const ssl_h) {
  char *string = NULL;
  unsigned char fp_digest[EVP_MAX_MD_SIZE];
  X509 *x509_data = SSL_get_peer_certificate(ssl_h);
  if (x509_data) {
    unsigned int fp_digest_size = sizeof fp_digest;
    memset(fp_digest, 0x00, fp_digest_size);
    if (X509_digest(x509_data, EVP_md5(), fp_digest, &fp_digest_size)) {
      string = (char *)malloc(MD5_DIGEST_LENGTH * 3 + 1);
      if (string) {
        int loop, pos = 0;
        for (loop = 0; loop < MD5_DIGEST_LENGTH; loop++) {
          if (loop)
            pos += sprintf(&string[pos], ":%02x", fp_digest[loop]);
          else
            pos = sprintf(&string[pos], "%02x", fp_digest[loop]);
        }
      }
    }
    X509_free(x509_data);
  }
  return string;
}


x86 assembly (clang, -O0):
<get_fingerprint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <SSL_get_peer_certificate@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12d2 <get_fingerprint+0x162>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    $0x0,%al
call   1030 <EVP_md5@plt>
mov    -0x50(%rbp),%rdi
mov    -0x40(%rbp),%rdx
mov    %eax,%esi
lea    -0x2c(%rbp),%rcx
call   1080 <X509_digest@plt>
cmp    $0x0,%rax
je     12c9 <get_fingerprint+0x159>
mov    0x2dc4(%rip),%rax
imul   $0x3,(%rax),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12c4 <get_fingerprint+0x154>
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    0x2d92(%rip),%rcx
cmp    (%rcx),%eax
jge    12bf <get_fingerprint+0x14f>
cmpl   $0x0,-0x30(%rbp)
je     1286 <get_fingerprint+0x116>
mov    -0x40(%rbp),%rax
mov    -0x10(%rbp),%rdi
movslq -0x34(%rbp),%rcx
add    %rcx,%rdi
movslq -0x30(%rbp),%rcx
lea    0xd8e(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1090 <sprintf@plt>
add    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    12ac <get_fingerprint+0x13c>
mov    -0x40(%rbp),%rax
mov    -0x10(%rbp),%rdi
movslq -0x34(%rbp),%rcx
add    %rcx,%rdi
movslq -0x30(%rbp),%rcx
lea    0xd61(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1090 <sprintf@plt>
mov    %eax,-0x34(%rbp)
jmp    12b1 <get_fingerprint+0x141>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    123c <get_fingerprint+0xcc>
jmp    12c4 <get_fingerprint+0x154>
jmp    12c9 <get_fingerprint+0x159>
mov    -0x28(%rbp),%rdi
call   1070 <X509_free@plt>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_fingerprint>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e60(%rip),%rax
mov    (%rax),%ebx
mov    %rsp,%r12
lea    0xf(%rbx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
call   1060 <SSL_get_peer_certificate@plt>
test   %rax,%rax
je     126f <get_fingerprint+0xff>
mov    %rax,%r14
mov    %ebx,-0x2c(%rbp)
xor    %r15d,%r15d
mov    %r12,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1040 <memset@plt>
xor    %eax,%eax
call   1030 <EVP_md5@plt>
lea    -0x2c(%rbp),%rcx
mov    %r14,%rdi
mov    %eax,%esi
mov    %r12,%rdx
call   1080 <X509_digest@plt>
test   %rax,%rax
je     1277 <get_fingerprint+0x107>
mov    0x2df2(%rip),%r13
mov    0x0(%r13),%eax
lea    (%rax,%rax,2),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1274 <get_fingerprint+0x104>
mov    %rax,%r15
cmpl   $0x0,0x0(%r13)
jle    1277 <get_fingerprint+0x107>
movzbl (%r12),%edx
lea    0xdf1(%rip),%rsi
mov    %r15,%rdi
call   1090 <sprintf@plt>
cmpl   $0x2,0x0(%r13)
jl     1277 <get_fingerprint+0x107>
mov    %eax,%ebx
mov    %r14,-0x38(%rbp)
mov    $0x1,%r14d
lea    0xdce(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movslq %ebx,%rbx
lea    (%r15,%rbx,1),%rdi
movzbl (%r12,%r14,1),%edx
mov    %r13,%rsi
call   1090 <sprintf@plt>
add    %eax,%ebx
mov    0x2d7b(%rip),%rax
movslq (%rax),%rax
add    $0x1,%r14
cmp    %rax,%r14
jl     1240 <get_fingerprint+0xd0>
mov    -0x38(%rbp),%r14
jmp    1277 <get_fingerprint+0x107>
xor    %r15d,%r15d
jmp    127f <get_fingerprint+0x10f>
xor    %r15d,%r15d
mov    %r14,%rdi
call   1070 <X509_free@plt>
mov    %r15,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EVP_MAX_MD_SIZE = global i32 0
@MD5_DIGEST_LENGTH = global i32 0
@.str = [6 x i8] c":%02x\00"
@.str.1 = [5 x i8] c"%02x\00"
define i8* @get_fingerprint(i32* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %2
store i8* null, i8** %3
%10 = load i32, i32* @EVP_MAX_MD_SIZE
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %4
%13 = alloca i8, i64 %11
store i64 %11, i64* %5
%14 = load i32*, i32** %2
%15 = call i32* @SSL_get_peer_certificate(i32* %14)
store i32* %15, i32** %6
%16 = load i32*, i32** %6
%17 = icmp ne i32* %16, null
br i1 %17, label %18, label %73
18: ; preds = %1
%19 = trunc i64 %11 to i32
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = call i32 @memset(i8* %13, i32 0, i32 %20)
%22 = load i32*, i32** %6
%23 = call i32 (...) @EVP_md5()
%24 = call i64 @X509_digest(i32* %22, i32 %23, i8* %13, i32* %7)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %70
26: ; preds = %18
%27 = load i32, i32* @MD5_DIGEST_LENGTH
%28 = mul nsw i32 %27, 3
%29 = add nsw i32 %28, 1
%30 = call i64 @malloc(i32 %29)
%31 = inttoptr i64 %30 to i8*
store i8* %31, i8** %3
%32 = load i8*, i8** %3
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %69
34: ; preds = %26
store i32 0, i32* %9
store i32 0, i32* %8
br label %35
35: ; preds = %65, %34
%36 = load i32, i32* %8
%37 = load i32, i32* @MD5_DIGEST_LENGTH
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %68
39: ; preds = %35
%40 = load i32, i32* %8
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %54
42: ; preds = %39
%43 = load i8*, i8** %3
%44 = load i32, i32* %9
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
%47 = load i32, i32* %8
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %13, i64 %48
%50 = load i8, i8* %49
%51 = call i32 @sprintf(i8* %46, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8 zeroext %50)
%52 = load i32, i32* %9
%53 = add nsw i32 %52, %51
store i32 %53, i32* %9
br label %64
54: ; preds = %39
%55 = load i8*, i8** %3
%56 = load i32, i32* %9
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
%59 = load i32, i32* %8
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %13, i64 %60
%62 = load i8, i8* %61
%63 = call i32 @sprintf(i8* %58, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %62)
store i32 %63, i32* %9
br label %64
64: ; preds = %54, %42
br label %65
65: ; preds = %64
%66 = load i32, i32* %8
%67 = add nsw i32 %66, 1
store i32 %67, i32* %8
br label %35
68: ; preds = %35
br label %69
69: ; preds = %68, %26
br label %70
70: ; preds = %69, %18
%71 = load i32*, i32** %6
%72 = call i32 @X509_free(i32* %71)
br label %73
73: ; preds = %70, %1
%74 = load i8*, i8** %3
%75 = load i8*, i8** %4
ret i8* %74
}

LLVM IR (-O2):
@EVP_MAX_MD_SIZE = global i32 0
@MD5_DIGEST_LENGTH = global i32 0
@.str = [6 x i8] c":%02x\00"
@.str.1 = [5 x i8] c"%02x\00"
define i8* @get_fingerprint(i32* %0) {
%2 = alloca i32
%3 = load i32, i32* @EVP_MAX_MD_SIZE
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = tail call i32* @SSL_get_peer_certificate(i32* %0)
%7 = icmp eq i32* %6, null
br i1 %7, label %45, label %8
8: ; preds = %1
%9 = bitcast i32* %2 to i8*
store i32 %3, i32* %2
%10 = call i32 @memset(i8* %5, i32 0, i32 %3)
%11 = call i32 (...) @EVP_md5()
%12 = call i64 @X509_digest(i32* %6, i32 %11, i8* %5, i32* %2)
%13 = icmp eq i64 %12, 0
br i1 %13, label %42, label %14
14: ; preds = %8
%15 = load i32, i32* @MD5_DIGEST_LENGTH
%16 = mul nsw i32 %15, 3
%17 = add nsw i32 %16, 1
%18 = call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i8*
%20 = icmp eq i64 %18, 0
br i1 %20, label %42, label %21
21: ; preds = %14
%22 = load i32, i32* @MD5_DIGEST_LENGTH
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %42
24: ; preds = %21
%25 = load i8, i8* %5
%26 = call i32 @sprintf(i8* %19, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %25)
%27 = load i32, i32* @MD5_DIGEST_LENGTH
%28 = icmp sgt i32 %27, 1
br i1 %28, label %29, label %42
29: ; preds = %24, %29
%30 = phi i64 [ %39, %29 ], [ 1, %24 ]
%31 = phi i32 [ %37, %29 ], [ %26, %24 ]
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %19, i64 %32
%34 = getelementptr inbounds i8, i8* %5, i64 %30
%35 = load i8, i8* %34
%36 = call i32 @sprintf(i8* %33, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8 zeroext %35)
%37 = add nsw i32 %36, %31
%38 = load i32, i32* @MD5_DIGEST_LENGTH
%39 = add nuw nsw i64 %30, 1
%40 = sext i32 %38 to i64
%41 = icmp slt i64 %39, %40
br i1 %41, label %29, label %42
42: ; preds = %29, %24, %21, %14, %8
%43 = phi i8* [ null, %14 ], [ null, %8 ], [ %19, %21 ], [ %19, %24 ], [ %19, %29 ]
%44 = call i32 @X509_free(i32* %6)
br label %45
45: ; preds = %42, %1
%46 = phi i8* [ %43, %42 ], [ null, %1 ]
ret i8* %46
}