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

/* Variables and functions */
int ENOMEM;
int EVP_MAX_MD_SIZE;
int /*<<< orphan*/ X509_digest(int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                               unsigned char *, unsigned int *);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
int get_cert_fingerprint(struct openconnect_info *vpninfo, X509 *cert,
                         const EVP_MD *type, char *buf) {
  unsigned char md[EVP_MAX_MD_SIZE];
  unsigned int i, n;
  if (!X509_digest(cert, type, md, &n))
    return -ENOMEM;
  for (i = 0; i < n; i++)
    sprintf(&buf[i * 2], "%02X", md[i]);
  return 0;
}


x86 assembly (clang, -O0):
<get_cert_fingerprint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    0x2ea9(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdx
sub    %rcx,%rdx
mov    %rdx,-0x50(%rbp)
mov    %rdx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x40(%rbp),%rcx
call   1030 <X509_digest@plt>
cmp    $0x0,%eax
jne    1198 <get_cert_fingerprint+0x78>
xor    %eax,%eax
mov    0x2e51(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    11ed <get_cert_fingerprint+0xcd>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x40(%rbp),%eax
jae    11df <get_cert_fingerprint+0xbf>
mov    -0x50(%rbp),%rax
mov    -0x28(%rbp),%rdi
mov    -0x3c(%rbp),%ecx
shl    $0x1,%ecx
mov    %ecx,%ecx
add    %rcx,%rdi
mov    -0x3c(%rbp),%ecx
lea    0xe38(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1040 <sprintf@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    119f <get_cert_fingerprint+0x7f>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_cert_fingerprint>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rsi,%rdi
mov    0x2ead(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r13
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
lea    -0x2c(%rbp),%rcx
mov    %rdx,%rsi
mov    %r13,%rdx
call   1030 <X509_digest@plt>
test   %eax,%eax
je     11aa <get_cert_fingerprint+0x8a>
cmpl   $0x0,-0x2c(%rbp)
je     11a6 <get_cert_fingerprint+0x86>
lea    0xe92(%rip),%r12
xor    %ebx,%ebx
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%edi
and    $0xfffffffe,%edi
add    %r14,%rdi
movzbl 0x0(%r13,%r15,1),%edx
mov    %r12,%rsi
call   1040 <sprintf@plt>
add    $0x1,%r15
mov    -0x2c(%rbp),%eax
add    $0x2,%rbx
cmp    %rax,%r15
jb     1180 <get_cert_fingerprint+0x60>
xor    %eax,%eax
jmp    11b5 <get_cert_fingerprint+0x95>
mov    0x2e27(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.openconnect_info = type { i32 }
@EVP_MAX_MD_SIZE = global i32 0
@ENOMEM = global i32 0
@.str = [5 x i8] c"%02X\00"
define i32 @get_cert_fingerprint(%struct.openconnect_info* %0, i32* %1, i32* %2, i8* %3) {
%5 = alloca i32
%6 = alloca %struct.openconnect_info*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store %struct.openconnect_info* %0, %struct.openconnect_info** %6
store i32* %1, i32** %7
store i32* %2, i32** %8
store i8* %3, i8** %9
%15 = load i32, i32* @EVP_MAX_MD_SIZE
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %10
%18 = alloca i8, i64 %16
store i64 %16, i64* %11
%19 = load i32*, i32** %7
%20 = load i32*, i32** %8
%21 = call i32 @X509_digest(i32* %19, i32* %20, i8* %18, i32* %13)
%22 = icmp ne i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %4
%24 = load i32, i32* @ENOMEM
%25 = sub nsw i32 0, %24
store i32 %25, i32* %5
store i32 1, i32* %14
br label %46
26: ; preds = %4
store i32 0, i32* %12
br label %27
27: ; preds = %42, %26
%28 = load i32, i32* %12
%29 = load i32, i32* %13
%30 = icmp ult i32 %28, %29
br i1 %30, label %31, label %45
31: ; preds = %27
%32 = load i8*, i8** %9
%33 = load i32, i32* %12
%34 = mul i32 %33, 2
%35 = zext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %32, i64 %35
%37 = load i32, i32* %12
%38 = zext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %18, i64 %38
%40 = load i8, i8* %39
%41 = call i32 @sprintf(i8* %36, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %40)
br label %42
42: ; preds = %31
%43 = load i32, i32* %12
%44 = add i32 %43, 1
store i32 %44, i32* %12
br label %27
45: ; preds = %27
store i32 0, i32* %5
store i32 1, i32* %14
br label %46
46: ; preds = %45, %23
%47 = load i8*, i8** %10
%48 = load i32, i32* %5
ret i32 %48
}

LLVM IR (-O2):
%struct.openconnect_info = type { i32 }
@EVP_MAX_MD_SIZE = global i32 0
@ENOMEM = global i32 0
@.str = [5 x i8] c"%02X\00"
define i32 @get_cert_fingerprint(%struct.openconnect_info* nocapture readnone %0, i32* %1, i32* %2, i8* nocapture writeonly %3) {
%5 = alloca i32
%6 = load i32, i32* @EVP_MAX_MD_SIZE
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = bitcast i32* %5 to i8*
%10 = call i32 @X509_digest(i32* %1, i32* %2, i8* %8, i32* %5)
%11 = icmp eq i32 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %4
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, 0
br i1 %14, label %30, label %18
15: ; preds = %4
%16 = load i32, i32* @ENOMEM
%17 = sub nsw i32 0, %16
br label %30
18: ; preds = %12, %18
%19 = phi i64 [ %26, %18 ], [ 0, %12 ]
%20 = shl nuw i64 %19, 1
%21 = and i64 %20, 4294967294
%22 = getelementptr inbounds i8, i8* %3, i64 %21
%23 = getelementptr inbounds i8, i8* %8, i64 %19
%24 = load i8, i8* %23
%25 = call i32 @sprintf(i8* dereferenceable(1) %22, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %24)
%26 = add nuw nsw i64 %19, 1
%27 = load i32, i32* %5
%28 = zext i32 %27 to i64
%29 = icmp ult i64 %26, %28
br i1 %29, label %18, label %30
30: ; preds = %18, %12, %15
%31 = phi i32 [ %17, %15 ], [ 0, %12 ], [ 0, %18 ]
ret i32 %31
}