#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;

/* Variables and functions */
int /*<<< orphan*/ EVP_sha1();
int SHA_DIGEST_LENGTH;
int /*<<< orphan*/ X509_pubkey_digest(int /*<<< orphan*/ *, int /*<<< orphan*/,
                                      unsigned char *, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
char *SSL_keyid(int ptr) {
  int i;
  unsigned char sha1_hash[SHA_DIGEST_LENGTH];
  X509 *x = (X509 *)ptr;
  X509_pubkey_digest(x, EVP_sha1(), sha1_hash, NULL);
  char *hash = malloc(SHA_DIGEST_LENGTH * 2 + 1);
  for (i = 0; i < SHA_DIGEST_LENGTH; ++i)
    sprintf(&hash[2 * i], "%02x", sha1_hash[i]);
  return hash;
}


x86 assembly (clang, -O0):
<SSL_keyid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e96(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
movslq -0x4(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1030 <EVP_sha1@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    %eax,%esi
xor    %eax,%eax
mov    %eax,%ecx
call   1060 <X509_pubkey_digest@plt>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
shl    $0x1,%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2e1e(%rip),%rcx
cmp    (%rcx),%eax
jge    1208 <SSL_keyid+0xc8>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rdi
movslq -0x8(%rbp),%rcx
lea    0xe0f(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1050 <sprintf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11c0 <SSL_keyid+0x80>
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SSL_keyid>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e93(%rip),%r13
mov    0x0(%r13),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
movslq %edi,%rbx
xor    %eax,%eax
call   1030 <EVP_sha1@plt>
mov    %rbx,%rdi
mov    %eax,%esi
mov    %r12,%rdx
xor    %ecx,%ecx
call   1060 <X509_pubkey_digest@plt>
mov    0x0(%r13),%ebx
lea    (%rbx,%rbx,1),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x30(%rbp)
test   %ebx,%ebx
jle    11d1 <SSL_keyid+0x91>
xor    %r14d,%r14d
lea    0xe5c(%rip),%r15
mov    -0x30(%rbp),%rbx
nopl   0x0(%rax,%rax,1)
movzbl (%r12,%r14,1),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <sprintf@plt>
add    $0x1,%r14
movslq 0x0(%r13),%rax
add    $0x2,%rbx
cmp    %rax,%r14
jl     11b0 <SSL_keyid+0x70>
mov    -0x30(%rbp),%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SHA_DIGEST_LENGTH = global i32 0
@.str = [5 x i8] c"%02x\00"
define i8* @SSL_keyid(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i8*
store i32 %0, i32* %2
%8 = load i32, i32* @SHA_DIGEST_LENGTH
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %4
%11 = alloca i8, i64 %9
store i64 %9, i64* %5
%12 = load i32, i32* %2
%13 = sext i32 %12 to i64
%14 = inttoptr i64 %13 to i32*
store i32* %14, i32** %6
%15 = load i32*, i32** %6
%16 = call i32 (...) @EVP_sha1()
%17 = call i32 @X509_pubkey_digest(i32* %15, i32 %16, i8* %11, i32* null)
%18 = load i32, i32* @SHA_DIGEST_LENGTH
%19 = mul nsw i32 %18, 2
%20 = add nsw i32 %19, 1
%21 = call i8* @malloc(i32 %20)
store i8* %21, i8** %7
store i32 0, i32* %3
br label %22
22: ; preds = %37, %1
%23 = load i32, i32* %3
%24 = load i32, i32* @SHA_DIGEST_LENGTH
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %40
26: ; preds = %22
%27 = load i8*, i8** %7
%28 = load i32, i32* %3
%29 = mul nsw i32 2, %28
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %27, i64 %30
%32 = load i32, i32* %3
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %11, i64 %33
%35 = load i8, i8* %34
%36 = call i32 @sprintf(i8* %31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %35)
br label %37
37: ; preds = %26
%38 = load i32, i32* %3
%39 = add nsw i32 %38, 1
store i32 %39, i32* %3
br label %22
40: ; preds = %22
%41 = load i8*, i8** %7
%42 = load i8*, i8** %4
ret i8* %41
}

LLVM IR (-O2):
@SHA_DIGEST_LENGTH = global i32 0
@.str = [5 x i8] c"%02x\00"
define noalias i8* @SSL_keyid(i32 %0) {
%2 = load i32, i32* @SHA_DIGEST_LENGTH
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = sext i32 %0 to i64
%6 = inttoptr i64 %5 to i32*
%7 = tail call i32 (...) @EVP_sha1()
%8 = call i32 @X509_pubkey_digest(i32* %6, i32 %7, i8* %4, i32* null)
%9 = load i32, i32* @SHA_DIGEST_LENGTH
%10 = shl nsw i32 %9, 1
%11 = or i32 %10, 1
%12 = call i8* @malloc(i32 %11)
%13 = icmp sgt i32 %9, 0
br i1 %13, label %14, label %25
14: ; preds = %1, %14
%15 = phi i64 [ %21, %14 ], [ 0, %1 ]
%16 = shl nuw nsw i64 %15, 1
%17 = getelementptr inbounds i8, i8* %12, i64 %16
%18 = getelementptr inbounds i8, i8* %4, i64 %15
%19 = load i8, i8* %18
%20 = call i32 @sprintf(i8* dereferenceable(1) %17, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %19)
%21 = add nuw nsw i64 %15, 1
%22 = load i32, i32* @SHA_DIGEST_LENGTH
%23 = sext i32 %22 to i64
%24 = icmp slt i64 %21, %23
br i1 %24, label %14, label %25
25: ; preds = %14, %1
ret i8* %12
}