#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 hashinfo {
  int dummy;
};
typedef int /*<<< orphan*/ X509;
typedef int /*<<< orphan*/ EVP_MD;
typedef int /*<<< orphan*/ BIO;

/* Variables and functions */
int EVP_MAX_MD_SIZE;
int /*<<< orphan*/ *EVP_sha256();
int /*<<< orphan*/ *PEM_read_bio_X509(int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *);
int X509_digest(int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                unsigned char *, unsigned int *);
int /*<<< orphan*/ X509_free(int /*<<< orphan*/ *);
unsigned long X509_subject_name_hash(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
struct hashinfo *hashinfo(char const *, unsigned long, unsigned char *);
int /*<<< orphan*/ stderr;
struct hashinfo *certhash_cert(BIO *bio, const char *filename) {
  unsigned char fingerprint[EVP_MAX_MD_SIZE];
  struct hashinfo *hi = NULL;
  const EVP_MD *digest;
  X509 *cert = NULL;
  unsigned long hash;
  unsigned int len;
  if ((cert = PEM_read_bio_X509(bio, NULL, NULL, NULL)) == NULL)
    goto err;
  hash = X509_subject_name_hash(cert);
  digest = EVP_sha256();
  if (X509_digest(cert, digest, fingerprint, &len) != 1) {
    fprintf(stderr, "out of memory\n");
    goto err;
  }
  hi = hashinfo(filename, hash, fingerprint);
err:
  X509_free(cert);
  return (hi);
}


x86 assembly (clang, -O0):
<certhash_cert>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1080 <PEM_read_bio_X509@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    11e2 <certhash_cert+0x72>
jmp    1247 <certhash_cert+0xd7>
mov    -0x38(%rbp),%rdi
call   1030 <X509_subject_name_hash@plt>
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1090 <EVP_sha256@plt>
mov    -0x50(%rbp),%rdx
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
lea    -0x44(%rbp),%rcx
call   1070 <X509_digest@plt>
cmp    $0x1,%eax
je     1232 <certhash_cert+0xc2>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
lea    0xdd8(%rip),%rsi
call   1040 <fprintf@plt>
jmp    1247 <certhash_cert+0xd7>
mov    -0x50(%rbp),%rdx
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1050 <hashinfo@plt>
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rdi
call   1060 <X509_free@plt>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<certhash_cert>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
xor    %r12d,%r12d
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1080 <PEM_read_bio_X509@plt>
mov    %rax,%r15
test   %rax,%rax
je     1206 <certhash_cert+0x96>
mov    %r15,%rdi
call   1030 <X509_subject_name_hash@plt>
mov    %rax,%r12
xor    %eax,%eax
call   1090 <EVP_sha256@plt>
lea    -0x24(%rbp),%rcx
mov    %r15,%rdi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1070 <X509_digest@plt>
cmp    $0x1,%eax
jne    11ee <certhash_cert+0x7e>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1050 <hashinfo@plt>
mov    %rax,%r12
jmp    1206 <certhash_cert+0x96>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
lea    0xe02(%rip),%rsi
call   1040 <fprintf@plt>
xor    %r12d,%r12d
mov    %r15,%rdi
call   1060 <X509_free@plt>
mov    %r12,%rax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hashinfo = type { i32 }
@EVP_MAX_MD_SIZE = global i32 0
@stderr = global i32 0
@.str = [15 x i8] c"out of memory\0A\00"
define %struct.hashinfo* @certhash_cert(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca %struct.hashinfo*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i32
store i32* %0, i32** %3
store i8* %1, i8** %4
%12 = load i32, i32* @EVP_MAX_MD_SIZE
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %5
%15 = alloca i8, i64 %13
store i64 %13, i64* %6
store %struct.hashinfo* null, %struct.hashinfo** %7
store i32* null, i32** %9
%16 = load i32*, i32** %3
%17 = call i32* @PEM_read_bio_X509(i32* %16, i32* null, i32* null, i32* null)
store i32* %17, i32** %9
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %20
19: ; preds = %2
br label %35
20: ; preds = %2
%21 = load i32*, i32** %9
%22 = call i64 @X509_subject_name_hash(i32* %21)
store i64 %22, i64* %10
%23 = call i32* (...) @EVP_sha256()
store i32* %23, i32** %8
%24 = load i32*, i32** %9
%25 = load i32*, i32** %8
%26 = call i32 @X509_digest(i32* %24, i32* %25, i8* %15, i32* %11)
%27 = icmp ne i32 %26, 1
br i1 %27, label %28, label %31
28: ; preds = %20
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %35
31: ; preds = %20
%32 = load i8*, i8** %4
%33 = load i64, i64* %10
%34 = call %struct.hashinfo* @hashinfo(i8* %32, i64 %33, i8* %15)
store %struct.hashinfo* %34, %struct.hashinfo** %7
br label %35
35: ; preds = %31, %28, %19
%36 = load i32*, i32** %9
%37 = call i32 @X509_free(i32* %36)
%38 = load %struct.hashinfo*, %struct.hashinfo** %7
%39 = load i8*, i8** %5
ret %struct.hashinfo* %38
}

LLVM IR (-O2):
%struct.hashinfo = type { i32 }
@EVP_MAX_MD_SIZE = global i32 0
@stderr = global i32 0
@.str = [15 x i8] c"out of memory\0A\00"
define %struct.hashinfo* @certhash_cert(i32* %0, i8* %1) {
%3 = alloca i32
%4 = load i32, i32* @EVP_MAX_MD_SIZE
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast i32* %3 to i8*
%8 = tail call i32* @PEM_read_bio_X509(i32* %0, i32* null, i32* null, i32* null)
%9 = icmp eq i32* %8, null
br i1 %9, label %20, label %10
10: ; preds = %2
%11 = tail call i64 @X509_subject_name_hash(i32* %8)
%12 = tail call i32* (...) @EVP_sha256()
%13 = call i32 @X509_digest(i32* %8, i32* %12, i8* %6, i32* %3)
%14 = icmp eq i32 %13, 1
br i1 %14, label %18, label %15
15: ; preds = %10
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %20
18: ; preds = %10
%19 = call %struct.hashinfo* @hashinfo(i8* %1, i64 %11, i8* %6)
br label %20
20: ; preds = %2, %18, %15
%21 = phi %struct.hashinfo* [ null, %2 ], [ null, %15 ], [ %19, %18 ]
%22 = call i32 @X509_free(i32* %8)
ret %struct.hashinfo* %21
}